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

move to new syntax, wip1

parent 2f1cb57d
No related branches found
No related tags found
No related merge requests found
fn main() {
//let a: i32 = if true { 1 } else { 2 };
let a = 5;
}
...@@ -94,6 +94,8 @@ pub enum Expr { ...@@ -94,6 +94,8 @@ pub enum Expr {
RefMut(Box<Expr>), RefMut(Box<Expr>),
DeRef(Box<Expr>), DeRef(Box<Expr>),
Call(Id, Exprs), Call(Id, Exprs),
Block(Stmts),
Stmt(Stmt),
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
...@@ -319,6 +321,8 @@ impl Display for Expr { ...@@ -319,6 +321,8 @@ impl Display for Expr {
RefMut(ref e) => write!(fmt, "&mut {}", e), RefMut(ref e) => write!(fmt, "&mut {}", e),
DeRef(ref e) => write!(fmt, "*{}", e), DeRef(ref e) => write!(fmt, "*{}", e),
Call(ref s, ref exprs) => write!(fmt, "{}{}", s, exprs), Call(ref s, ref exprs) => write!(fmt, "{}{}", s, exprs),
Block(ref s) => write!(fmt, "{}", s),
Stmt(ref s) => write!(fmt, "{}", s),
} }
} }
} }
......
...@@ -187,6 +187,10 @@ fn expr_type( ...@@ -187,6 +187,10 @@ fn expr_type(
_ => Err(format!("cannot deref {} of type {}", e, t)), _ => Err(format!("cannot deref {} of type {}", e, t)),
} }
} }
Block(b) => panic!(),
Stmt(s) => panic!(),
} }
} }
......
...@@ -98,7 +98,7 @@ Variant : Variant = { ...@@ -98,7 +98,7 @@ Variant : Variant = {
} }
pub FnDecl : FnDecl = { pub FnDecl : FnDecl = {
"fn" <id: Id> <params: Params> <result: ("->" <Type>)?> "{" <body: Stmts> "}" "fn" <id: Id> <params: Params> <result: ("->" <Type>)?> <body: Block>
=> FnDecl { => FnDecl {
id, id,
params, params,
...@@ -126,29 +126,6 @@ Param : Param = { ...@@ -126,29 +126,6 @@ Param : Param = {
}, },
} }
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 }
},
}
}
Stmt : Stmt = {
"let" <m: "mut"?> <id: Id> <t: (":" <Type>)?> <e: ("=" <Expr>)?> => Stmt::Let(id, m.is_some(), t, e),
<Expr> "=" <Expr> => Stmt::Assign(<>),
"while" <Expr> "{" <Stmts> "}" => Stmt::While(<>),
"if" <Expr> "{" <Stmts> "}" < ("else" "{" <Stmts> "}")?> => Stmt::If(<>),
<Expr> => Stmt::Expr(<>),
"{" <Stmts> "}" => Stmt::Block(<>),
}
Type : Type = { Type : Type = {
"bool" => Type::Bool, "bool" => Type::Bool,
"()" => Type::Unit, "()" => Type::Unit,
...@@ -158,16 +135,79 @@ Type : Type = { ...@@ -158,16 +135,79 @@ Type : Type = {
"&" "mut" <Type> => Type::Ref(Box::new(Type::Mut(Box::new(<>)))), "&" "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 = {
"{" <Stmt*> "}" => Stmts { stmts: <>, ret: false },
}
// pub Stmt: () = {
// ";" => (),
// "let" "mut"? Id "=" Expr ";" => (),
// ExprNoBlock "=" Expr => (),
// "while" Expr Block => (),
// "if" Expr Block ("else" Block)? => (),
// ExprNoBlock => (),
// Block => (),
// }
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(<>),
<ExprNoBlock> => Stmt::Expr(<>),
// <Block> => Stmt::Block(<>),
}
pub Exprs : Exprs = { pub Exprs : Exprs = {
ParCNT<Expr> => Exprs( <> ), ParCNT<ExprNoBlock> => Exprs( <> ),
}; }
pub Expr = {
ExprBlock,
ExprNoBlock,
}
pub ExprBlock: Box<Expr> = {
"if" <e: Expr> <tb: Block> "else" <eb: Block> => {
let s = Stmt::If(e, tb, Some(eb));
Box::new(Expr::Stmt(s))
},
Block => Box::new(Expr::Block(<>)),
}
pub ExprNoBlock: Box<Expr> = {
// ExprNoBlock Cmp Expr1 => (),
Expr1,
}
// Not sure about the precedence // Not sure about the precedence
pub Expr: Box<Expr> = { pub Expr1: Box<Expr> = {
Expr BoolOp Factor => Box::new(Expr::Infix(<>)), Expr1 BoolOp Expr2 => Box::new(Expr::Infix(<>)),
Expr AddSubOp Factor => Box::new(Expr::Infix(<>)), Expr1 AddSubOp Expr2 => Box::new(Expr::Infix(<>)),
<Expr> "as" <Type> => Box::new(Expr::As(<>)), <Expr1> "as" <Type> => Box::new(Expr::As(<>)),
Factor, Expr2,
}; };
AddSubOp: Op = { AddSubOp: Op = {
...@@ -186,8 +226,8 @@ BoolOp: Op = { ...@@ -186,8 +226,8 @@ BoolOp: Op = {
"&&" => Op::And, "&&" => Op::And,
}; };
Factor: Box<Expr> = { Expr2: Box<Expr> = {
Factor MulDivOp Term => Box::new(Expr::Infix(<>)), Expr2 MulDivOp Term => Box::new(Expr::Infix(<>)),
Term, Term,
}; };
...@@ -198,17 +238,16 @@ MulDivOp: Op = { ...@@ -198,17 +238,16 @@ MulDivOp: Op = {
Term: Box<Expr> = { Term: Box<Expr> = {
Num => Box::new(Expr::Num(<>)), Num => Box::new(Expr::Num(<>)),
"true" => Box::new(Expr::Bool(true)), // "true" => Box::new(Expr::Bool(true)),
"false" => Box::new(Expr::Bool(false)), // "false" => Box::new(Expr::Bool(false)),
<Id> <Exprs> => Box::new(Expr::Call(<>)), // <Id> <Exprs> => Box::new(Expr::Call(<>)),
Id => Box::new(Expr::Id(<>)), // Id => Box::new(Expr::Id(<>)),
AddSubOp Term => Box::new(Expr::Prefix(<>)), // AddSubOp Term => Box::new(Expr::Prefix(<>)),
"!" <Term> => Box::new(Expr::Prefix(Op::Not, <>)), // "!" <Term> => Box::new(Expr::Prefix(Op::Not, <>)),
"&" <Term> => Box::new(Expr::Ref(<>)), // "&" <Term> => Box::new(Expr::Ref(<>)),
"&" "mut" <Term> => Box::new(Expr::RefMut(<>)), // "&" "mut" <Term> => Box::new(Expr::RefMut(<>)),
"*" <Term> => Box::new(Expr::DeRef(<>)), // "*" <Term> => Box::new(Expr::DeRef(<>)),
"(" <Expr> ")" // "(" <Expr> ")"
// "{{" <Stmt> "}} "=> Box::new(Expr::Stmt(<>))
}; };
Num: i32 = { Num: i32 = {
......
...@@ -169,3 +169,8 @@ fn check_scopes() { ...@@ -169,3 +169,8 @@ fn check_scopes() {
fn check_scopes_err() { fn check_scopes_err() {
assert!(check(&read_file::parse("examples/scopes_err.rs")).is_err()); assert!(check(&read_file::parse("examples/scopes_err.rs")).is_err());
} }
#[test]
fn check_let_if() {
assert!(check(&read_file::parse("examples/let_if.rs")).is_err());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment