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

move to new syntax, wip5

parent 3f81d391
Branches
No related tags found
No related merge requests found
fn main() {
{}
a;
// {}
let mut a = 5;
a = 5;
// a = { 7 }
}
fn b(x: i32) -> i32 {
x;
}
......@@ -41,7 +41,7 @@ fn expr_type(
e: &Expr,
fn_env: &FnEnv,
type_env: &TypeEnv,
var_env: &VarEnv,
var_env: &mut VarEnv,
) -> Result<Type, Error> {
use Expr::*;
trace!("expr_type {}", e);
......@@ -188,7 +188,7 @@ fn expr_type(
}
}
Block(b) => panic!(),
Block(b) => check_stmts(b, fn_env, type_env, var_env),
Stmt(s) => panic!(),
}
......@@ -478,24 +478,24 @@ fn test_expr_type() {
// type of number
assert_eq!(
expr_type(&Expr::Num(1), &fn_env, &type_env, &var_env),
expr_type(&Expr::Num(1), &fn_env, &type_env, &mut var_env),
Ok(I32)
);
// type of variables
// not found
assert!(expr_type(&Expr::Id("a".to_string()), &fn_env, &type_env, &var_env).is_err());
assert!(expr_type(&Expr::Id("a".to_string()), &fn_env, &type_env, &mut var_env).is_err());
// let i: i32 ...
assert_eq!(
expr_type(&Expr::Id("i".to_string()), &fn_env, &type_env, &var_env),
expr_type(&Expr::Id("i".to_string()), &fn_env, &type_env, &mut var_env),
Ok(I32)
);
// let j ... (has no type yet)
assert_eq!(
expr_type(&Expr::Id("j".to_string()), &fn_env, &type_env, &var_env),
expr_type(&Expr::Id("j".to_string()), &fn_env, &type_env, &mut var_env),
Ok(Unknown)
);
......@@ -511,7 +511,7 @@ fn test_expr_type() {
&*ExprParser::new().parse("1 + 2 - 5").unwrap(),
&fn_env,
&type_env,
&var_env
&mut var_env
),
Ok(I32)
);
......@@ -522,7 +522,7 @@ fn test_expr_type() {
&*ExprParser::new().parse("- 5").unwrap(),
&fn_env,
&type_env,
&var_env
&mut var_env
),
Ok(I32)
);
......@@ -533,7 +533,7 @@ fn test_expr_type() {
&*ExprParser::new().parse("b(1)").unwrap(),
&fn_env,
&type_env,
&var_env
&mut var_env
),
Ok(I32)
);
......@@ -544,7 +544,7 @@ fn test_expr_type() {
&*ExprParser::new().parse("b(i)").unwrap(),
&fn_env,
&type_env,
&var_env
&mut var_env
),
Ok(I32)
);
......@@ -554,7 +554,7 @@ fn test_expr_type() {
&*ExprParser::new().parse("b(1, 2)").unwrap(),
&fn_env,
&type_env,
&var_env
&mut var_env
)
.is_err());
......@@ -563,7 +563,7 @@ fn test_expr_type() {
&*ExprParser::new().parse("b(true)").unwrap(),
&fn_env,
&type_env,
&var_env
&mut var_env
)
.is_err());
......
......@@ -27,11 +27,7 @@ Comma<T>: Vec<T> = {
// A comma separated sequence without trailing comma
CommaNoTrail<T>: Vec<T> = {
<v:(<T> ",")*> <e:T> => {
let mut v = v;
v.push(e);
v
}
<mut v:(<T> ",")*> <e:T> => { v.push(e); v }
}
// Parenthesized, comma delimeted
......@@ -135,22 +131,20 @@ Type : Type = {
"&" "mut" <Type> => Type::Ref(Box::new(Type::Mut(Box::new(<>)))),
}
// pub Stmts: Stmts = {
// <s: (<Stmt> ";")*> <e: Stmt?> => match e {
// // stmts return ()
// None => Stmts { stmts: s, ret: true },
// // last statement is the return value
// Some(e) => {
// let mut s = s;
// s.push(e);
// Stmts { stmts: s, ret: false }
// },
// }
// }
pub Block: Stmts = {
"{" <sts: StmtSeq*> <st: Stmt?> "}"
=> Stmts { stmts: sts, ret: false },
"{" <mut stmts: StmtSeq*> <st: Stmt?> "}" => {
match &st {
Some(st) => stmts.push(st.clone()),
_ =>(),
};
Stmts {
stmts,
ret: match st {
Some(Stmt::Semi) => true,
_ => false
}
}
}
}
StmtSeq: Stmt = {
......@@ -166,18 +160,11 @@ StmtBlock: Stmts = {
Stmt: Stmt = {
";" => Stmt::Semi,
//"let" <m: "mut"?> <id: Id> <t: (":" <Type>)?> <e: ("=" <Expr>)?> => Stmt::Let(id, m.is_some(), t, e),
// <Expr> "=" <Expr> => Stmt::Assign(<>),
// "while" <Expr> "{" <Block> "}" => Stmt::While(<>),
// "if" <Expr> <Block> < ("else" <Block>)?> => Stmt::If(<>),
"let" <m: "mut"?> <id: Id> <t: (":" <Type>)?> <e: ("=" <Expr>)?> => Stmt::Let(id, m.is_some(), t, e),
<Expr> "=" <Expr> => Stmt::Assign(<>),
<ExprNoBlock> => Stmt::Expr(<>),
// <Block> => Stmt::Block(<>),
}
pub Exprs : Exprs = {
ParCNT<ExprNoBlock> => Exprs( <> ),
}
......@@ -195,8 +182,7 @@ pub ExprBlock: Box<Expr> = {
Block => Box::new(Expr::Block(<>)),
}
pub ExprNoBlock: Box<Expr> = {
// ExprNoBlock Cmp Expr1 => (),
ExprNoBlock = {
Expr1,
}
......
......@@ -177,5 +177,5 @@ fn check_let_if() {
#[test]
fn wip() {
assert!(check(&read_file::parse("examples/wip.rs")).is_err());
let _ = check(&read_file::parse("examples/wip.rs")).is_ok();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment