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

syntax tests pass, polish

parent 262e02bf
No related branches found
No related tags found
No related merge requests found
fn main() {
fn main() {}
fn a(mut a: i32) {
let a = 5;
let b = { 5 };
}
fn b(x: bool) -> i32 {
if x {
1
} else {
2
}
}
fn c() {
b(false);
let b = if true {
let mut a = 1;
while a > 0 {
a = a - 1
}
a
} else {
{
5
}
};
}
......@@ -8,7 +8,6 @@ lalrpop_mod!(pub parser, "/ast/parser.rs");
use parser::*;
pub mod ast;
use ast::*;
fn main() {}
......@@ -21,7 +20,7 @@ pub fn read(file_name: &str) -> std::io::Result<String> {
pub fn parse(file_name: &str) {
let p = read(file_name).expect("File not found");
FunctionParser::new().parse(&p).unwrap()
ProgramParser::new().parse(&p).unwrap()
}
#[test]
......
......@@ -16,113 +16,118 @@ match {
// 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 }
}
Tier<Op,NextTier>: Box<Expr> = {
Tier<Op,NextTier> Op NextTier => Box::new(Expr::Op(<>)),
NextTier
};
pub Program: () = {
Function*
}
pub Function: () = {
"fn" Id Params ("->" Type)? Block => (),
"fn" Id Params ("->" Type)? Block,
}
pub Params: () = {
"()" => (), // seems like a haxx
"(" ((Param ",")* Param)? ")" => (),
"()", // seems like a haxx
"(" (Param ",")* Param? ")",
}
pub Param:() = {
Id ":" Type,
"mut"? Id ":" Type,
}
pub Type:() = {
"i32" => (),
"bool" => (),
"()" => (),
"i32",
"bool",
"()",
}
pub Block: () = {
"{" StmtSeq* "}" => (),
"{" StmtSeq* Stmt "}" => (),
"{" StmtSeq* "}",
"{" StmtSeq* Stmt "}",
}
pub StmtSeq: () = {
Stmt ";" => (),
StmtBlock => (),
Stmt ";",
StmtBlock,
}
pub StmtBlock: () = {
"while" Expr Block => (),
"if" Expr Block ("else" Block)? => (),
Block => (),
"while" Expr Block,
"if" Expr Block ("else" Block)?,
Block,
}
pub Stmt: () = {
";" => (),
"let" "mut"? Id "=" Expr => (),
ExprNoBlock "=" Expr => (),
ExprNoBlock => (),
";",
"let" "mut"? Id "=" Expr,
ExprNoBlock "=" Expr,
ExprNoBlock,
}
pub Expr: () = {
ExprBlock => (),
ExprNoBlock => (),
ExprBlock,
ExprNoBlock,
}
pub ExprBlock: () = {
"if" ExprNoBlock Block "else" Block => (),
Block => (),
"if" ExprNoBlock Block "else" Block,
Block,
}
// Here is our ordinary expressions
pub ExprNoBlock: () = {
ExprNoBlock "||" Expr0 => (),
ExprNoBlock "&&" Expr0 => (),
Expr0 => (),
ExprNoBlock "||" Expr0,
ExprNoBlock "&&" Expr0,
Expr0,
}
// Comparison
pub Expr0: () = {
Expr0 "==" Expr1 => (),
Expr0 "!=" Expr1 => (),
Expr0 ">" Expr1 => (),
Expr0 "<" Expr1 => (),
Expr1 => (),
Expr0 "==" Expr1,
Expr0 "!=" Expr1,
Expr0 ">" Expr1,
Expr0 "<" Expr1,
Expr1,
}
// AddSub
pub Expr1: () = {
Expr1 "+" Expr2 => (),
Expr1 "-" Expr2 => (),
Expr2 => (),
Expr1 "+" Expr2,
Expr1 "-" Expr2,
Expr2,
}
// MulDiv
pub Expr2: () = {
Expr2 "/" Expr3 => (),
Expr2 "*" Expr3 => (),
Expr3 => (),
Expr2 "/" Expr3,
Expr2 "*" Expr3,
Expr3,
}
// Unary
pub Expr3: () = {
"*" Term => (),
"&" Term => (),
"&" "mut" Term => (),
"!" Term => (),
Term => (),
"*" Term,
"&" Term,
"&" "mut" Term,
"!" Term,
Term,
}
pub Term: () = {
Id => (),
Num => (),
Id "(" CommaNoTrail<Expr> ")" => (),
Id,
Num,
Id "(" CommaNoTrail<Expr> ")",
"(" Expr ")" => (),
"(" Expr ")",
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment