Skip to content
Snippets Groups Projects
Commit a382754b authored by Per Lindgren's avatar Per Lindgren
Browse files

passes w1_2

parent 767f2612
No related branches found
No related tags found
No related merge requests found
......@@ -16,12 +16,11 @@ fn c(x: i32, y: i32) -> i32 {
-a - (-b) * y // here we have prefix operator "-"
}
// More advanced statements
// a function taking two bool arguments returning the bool type
// with some let statements and function calls
fn a(x: bool, y: bool) -> bool {
fn a1(x: bool, y: bool) -> bool {
if x && y {
let a: bool = true;
y || a
......@@ -32,8 +31,8 @@ fn a(x: bool, y: bool) -> bool {
// a function taking two bool arguments returning the i32 type
// with some let statements and function calls
fn b(x: bool, y: bool) -> i32 {
let a: bool = a(x, y || false);
fn b1(x: bool, y: bool) -> i32 {
let a: bool = a1(x, y || false);
let mut b: i32 = 0;
if a && y {
let a: bool = true; // shadowing
......@@ -50,7 +49,7 @@ fn b(x: bool, y: bool) -> i32 {
// a function taking two bool arguments returning the i32 type
// while
fn c(x: bool, y: bool) -> i32 {
fn c1(x: bool, y: bool) -> i32 {
let mut b: i32 = 0;
let mut c: i32 = 1;
while (b < 10) {
......
......@@ -45,6 +45,8 @@ fn expr_type(
) -> Result<Type, Error> {
use Expr::*;
trace!("expr_type {}", e);
trace!("var_env {:?}", var_env);
match e {
Num(_) => Ok(Type::I32),
Bool(_) => Ok(Type::Bool),
......@@ -57,15 +59,16 @@ fn expr_type(
use Op::*;
match op {
// Arithmetic and Boolean
// Arithmetic and Boolen
Add | Mul | Div | Sub | And | Or => {
// check if op and args are of i32
if lt == Type::I32 && rt == Type::I32 {
Ok(Type::I32)
// check if op and args are compliant
let opt = op.get_type();
if lt == opt && rt == opt {
Ok(opt)
} else {
Err(format!(
"Num operation requires i32, left {} right {}",
lt, rt
"Expected type {}, found, left {}: {}, {:?} right {}: {}, {:?}",
opt, l, lt, lt, r, rt, rt
))
}
}
......@@ -373,7 +376,7 @@ pub fn check(p: &Program) -> Result<(), Error> {
trace!("Input program \n{}", &p);
for fd in p.fn_decls.iter() {
trace!("function id {}", fd.id);
trace!("check function\n{}", fd);
let mut var_env = VarEnv::new();
// build a scope for the arguments
......@@ -390,9 +393,9 @@ pub fn check(p: &Program) -> Result<(), Error> {
var_env.push_param_scope(arg_ty);
let stmt_type = check_stmts(&fd.body, &fn_env, &type_env, &mut var_env);
trace!("function id {}: {:?}", fd.id, &stmt_type);
trace!("result {}: {:?}", fd.id, &stmt_type);
let stmt_type = stmt_type?;
let stmt_type = strip_mut(stmt_type?);
if stmt_type != fd.result {
Err(format!(
......
......@@ -150,9 +150,10 @@ Stmt : Stmt = {
Type : Type = {
Id => Type::Named(<>),
"bool" => Type::Bool,
"()" => Type::Unit,
"i32" => Type::I32, // this should likely be a paratrized Num type later
Id => Type::Named(<>),
"&" <Type> => Type::Ref(Box::new(<>)),
"&" "mut" <Type> => Type::Ref(Box::new(Type::Mut(Box::new(<>)))),
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment