Make loops typecheck and type infere

This commit is contained in:
Sofia 2025-07-23 20:53:11 +03:00
parent 15ff9c5906
commit a251be2715
7 changed files with 28 additions and 20 deletions

View File

@ -1,9 +1,12 @@
// Arithmetic, function calls and imports! // Arithmetic, function calls and imports!
import std::print;
import std::from_str;
fn main() -> u32 { fn main() -> u32 {
for i in 0 to 10 { let text = from_str("hello");
print("hello") for i in 0 .. 10 {
print(&text)
} }
let mut num = 0; let mut num = 0;

View File

@ -440,7 +440,6 @@ impl Builder {
} }
Instr::GetStructElemPtr(ptr_val, idx) => { Instr::GetStructElemPtr(ptr_val, idx) => {
let ptr_ty = ptr_val.get_type(&self)?; let ptr_ty = ptr_val.get_type(&self)?;
dbg!(&ptr_ty);
if let Type::Ptr(ty) = ptr_ty { if let Type::Ptr(ty) = ptr_ty {
if let Type::CustomType(val) = *ty { if let Type::CustomType(val) = *ty {
match self.type_data(&val).kind { match self.type_data(&val).kind {

View File

@ -155,11 +155,12 @@ impl Parse for PrimaryExpression {
} }
Token::DecimalValue(v) => { Token::DecimalValue(v) => {
stream.next(); // Consume decimal stream.next(); // Consume decimal
if let Some(Token::Dot) = stream.peek() { if let (Some(Token::Dot), Some(Token::DecimalValue(fractional))) =
(stream.peek(), stream.peek2())
{
stream.next(); // Consume dot stream.next(); // Consume dot
let Some(Token::DecimalValue(fractional)) = stream.next() else { stream.next(); // Consume fractional
return Err(stream.expected_err("fractional part")?);
};
Expression( Expression(
Kind::Literal(Literal::Decimal( Kind::Literal(Literal::Decimal(
format!("{}.{}", v, fractional) format!("{}.{}", v, fractional)
@ -706,7 +707,8 @@ impl Parse for ForStatement {
let start_range = stream.get_range().unwrap(); let start_range = stream.get_range().unwrap();
stream.expect(Token::In)?; stream.expect(Token::In)?;
let start = stream.parse()?; let start = stream.parse()?;
stream.expect(Token::To)?; stream.expect(Token::Dot)?;
stream.expect(Token::Dot)?;
let end = stream.parse()?; let end = stream.parse()?;
Ok(ForStatement(idx, start_range, start, end, stream.parse()?)) Ok(ForStatement(idx, start_range, start, end, stream.parse()?))

View File

@ -1001,7 +1001,6 @@ impl mir::Expression {
let TypeKind::CodegenPtr(inner) = &struct_val.1 else { let TypeKind::CodegenPtr(inner) = &struct_val.1 else {
panic!("tried accessing non-pointer"); panic!("tried accessing non-pointer");
}; };
dbg!(&inner);
let TypeKind::CustomType(key) = *inner.clone() else { let TypeKind::CustomType(key) = *inner.clone() else {
panic!("tried accessing non-custom-type"); panic!("tried accessing non-custom-type");
}; };

View File

@ -46,11 +46,7 @@ pub enum Token {
While, While,
/// `for` /// `for`
For, For,
/// `from` /// `In`
From,
/// `to`
To,
/// `to`
In, In,
// Symbols // Symbols
@ -147,9 +143,7 @@ impl ToString for Token {
Token::Struct => String::from("struct"), Token::Struct => String::from("struct"),
Token::AsKeyword => String::from("as"), Token::AsKeyword => String::from("as"),
Token::For => String::from("for"), Token::For => String::from("for"),
Token::From => String::from("from"),
Token::In => String::from("in"), Token::In => String::from("in"),
Token::To => String::from("to"),
Token::While => String::from("while"), Token::While => String::from("while"),
Token::Semi => String::from(';'), Token::Semi => String::from(';'),
Token::Equals => String::from('='), Token::Equals => String::from('='),
@ -340,8 +334,6 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
"for" => Token::For, "for" => Token::For,
"while" => Token::While, "while" => Token::While,
"in" => Token::In, "in" => Token::In,
"from" => Token::From,
"to" => Token::To,
_ => Token::Identifier(value), _ => Token::Identifier(value),
}; };
variant variant

View File

@ -335,7 +335,14 @@ impl Block {
None None
} }
} }
StmtKind::While(while_statement) => todo!(), StmtKind::While(WhileStatement {
condition, block, ..
}) => {
condition.typecheck(&mut state, typerefs, Some(&TypeKind::Bool))?;
block.typecheck(&mut state, typerefs, None)?;
None
}
}; };
if let Some((ReturnKind::Hard, _)) = ret { if let Some((ReturnKind::Hard, _)) = ret {

View File

@ -16,6 +16,7 @@ use super::{
IfExpression, Module, ReturnKind, StmtKind, IfExpression, Module, ReturnKind, StmtKind,
TypeKind::*, TypeKind::*,
VagueType::*, VagueType::*,
WhileStatement,
}; };
/// Struct used to implement Type Inference, where an intermediary /// Struct used to implement Type Inference, where an intermediary
@ -126,7 +127,12 @@ impl Block {
let expr_res = expr.infer_types(&mut state, &inner_refs); let expr_res = expr.infer_types(&mut state, &inner_refs);
state.ok(expr_res, expr.1); state.ok(expr_res, expr.1);
} }
StmtKind::While(while_statement) => todo!(), StmtKind::While(WhileStatement {
condition, block, ..
}) => {
condition.infer_types(&mut state, &inner_refs)?;
block.infer_types(&mut state, &inner_refs)?;
}
}; };
} }