mirror of https://git.sr.ht/~garritfra/sabre
Garrit Franke
3 years ago
4 changed files with 44 additions and 4 deletions
@ -1,5 +1,5 @@ |
|||||||
fn main() { |
fn main() { |
||||||
|
|
||||||
let x = (1 + (2 * 3)) |
let x = 2 |
||||||
println(x) |
println(x) |
||||||
} |
} |
@ -0,0 +1,34 @@ |
|||||||
|
use super::node_type::*; |
||||||
|
|
||||||
|
/// Try to infer types of variables
|
||||||
|
pub(super) fn infer(program: &mut Program) -> Result<(), String> { |
||||||
|
// TODO: Fix aweful nesting
|
||||||
|
for func in &mut program.func { |
||||||
|
if let Statement::Block(statements) = &mut func.body { |
||||||
|
for statement in statements { |
||||||
|
match statement { |
||||||
|
Statement::Declare(var, expr) => { |
||||||
|
if let None = &var.ty { |
||||||
|
if let Some(e) = expr { |
||||||
|
dbg!(&var); |
||||||
|
var.ty = infer_expression(&e); |
||||||
|
dbg!(&var); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
_ => {} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
Ok(()) |
||||||
|
} |
||||||
|
|
||||||
|
fn infer_expression(expr: &Expression) -> Option<Type> { |
||||||
|
match expr { |
||||||
|
Expression::Int(_) => Some(Type::Int), |
||||||
|
Expression::Bool(_) => Some(Type::Bool), |
||||||
|
Expression::Str(_) => Some(Type::Str), |
||||||
|
_ => None, |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue