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!
import std::print;
import std::from_str;
fn main() -> u32 {
for i in 0 to 10 {
print("hello")
let text = from_str("hello");
for i in 0 .. 10 {
print(&text)
}
let mut num = 0;

View File

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

View File

@ -155,11 +155,12 @@ impl Parse for PrimaryExpression {
}
Token::DecimalValue(v) => {
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
let Some(Token::DecimalValue(fractional)) = stream.next() else {
return Err(stream.expected_err("fractional part")?);
};
stream.next(); // Consume fractional
Expression(
Kind::Literal(Literal::Decimal(
format!("{}.{}", v, fractional)
@ -706,7 +707,8 @@ impl Parse for ForStatement {
let start_range = stream.get_range().unwrap();
stream.expect(Token::In)?;
let start = stream.parse()?;
stream.expect(Token::To)?;
stream.expect(Token::Dot)?;
stream.expect(Token::Dot)?;
let 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 {
panic!("tried accessing non-pointer");
};
dbg!(&inner);
let TypeKind::CustomType(key) = *inner.clone() else {
panic!("tried accessing non-custom-type");
};

View File

@ -46,11 +46,7 @@ pub enum Token {
While,
/// `for`
For,
/// `from`
From,
/// `to`
To,
/// `to`
/// `In`
In,
// Symbols
@ -147,9 +143,7 @@ impl ToString for Token {
Token::Struct => String::from("struct"),
Token::AsKeyword => String::from("as"),
Token::For => String::from("for"),
Token::From => String::from("from"),
Token::In => String::from("in"),
Token::To => String::from("to"),
Token::While => String::from("while"),
Token::Semi => 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,
"while" => Token::While,
"in" => Token::In,
"from" => Token::From,
"to" => Token::To,
_ => Token::Identifier(value),
};
variant

View File

@ -335,7 +335,14 @@ impl Block {
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 {

View File

@ -16,6 +16,7 @@ use super::{
IfExpression, Module, ReturnKind, StmtKind,
TypeKind::*,
VagueType::*,
WhileStatement,
};
/// 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);
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)?;
}
};
}