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

syntax tests pass, somehow broken

parent 58eac48f
Branches
No related tags found
No related merge requests found
......@@ -16,7 +16,7 @@ fn b(x: bool) -> i32 {
fn c() {
b(false);
let b = if true {
let mut a = 1;
let mut a: i32 = 1;
while a > 0 {
a = a - 1
}
......
use std::fmt;
// use std::fmt;
// ast
......
......@@ -19,8 +19,8 @@ CommaNoTrail<T>: Vec<T> = {
<mut v:(<T> ",")*> <e:T> => { v.push(e); v }
}
Tier<Op,NextTier>: Box<Expr> = {
Tier<Op,NextTier> Op NextTier => Box::new(Expr::Op(<>)),
Tier<Op, NextTier>: () = {
Tier<Op,NextTier> Op NextTier,
NextTier
};
......@@ -28,114 +28,108 @@ pub Program: () = {
Function*
}
pub Function: () = {
Function: () = {
"fn" Id Params ("->" Type)? Block,
}
pub Params: () = {
Params: () = {
"()", // seems like a haxx
"(" (Param ",")* Param? ")",
}
pub Param:() = {
Param:() = {
"mut"? Id ":" Type,
}
pub Type:() = {
Type:() = {
"i32",
"bool",
"()",
}
pub Block: () = {
Block: () = {
"{" StmtSeq* "}",
"{" StmtSeq* Stmt "}",
}
pub StmtSeq: () = {
StmtSeq: () = {
Stmt ";",
StmtBlock,
}
pub StmtBlock: () = {
StmtBlock: () = {
"while" Expr Block,
"if" Expr Block ("else" Block)?,
Block,
}
pub Stmt: () = {
Stmt: () = {
";",
"let" "mut"? Id "=" Expr,
"let" "mut"? Id (":" Type)? "=" Expr,
ExprNoBlock "=" Expr,
ExprNoBlock,
}
pub Expr: () = {
Expr: () = {
ExprBlock,
ExprNoBlock,
}
pub ExprBlock: () = {
ExprBlock: () = {
"if" ExprNoBlock Block "else" Block,
Block,
}
// Here is our ordinary expressions
pub ExprNoBlock: () = {
ExprNoBlock "||" Expr0,
ExprNoBlock "&&" Expr0,
Expr0,
}
ExprNoBlock = Tier<AndOr, Expr0>;
Expr0 = Tier<Comparison, Expr1>;
Expr1 = Tier<AddSub, Expr2>;
Expr2 = Tier<MulDiv, Expr3>;
Expr3 = Tier<Unary, Term>;
// Comparison
pub Expr0: () = {
Expr0 "==" Expr1,
Expr0 "!=" Expr1,
Expr0 ">" Expr1,
Expr0 "<" Expr1,
Expr1,
AndOr: () = {
"||",
"&&",
}
Comparison: () = {
"==",
"!=",
">",
"<",
}
// AddSub
pub Expr1: () = {
Expr1 "+" Expr2,
Expr1 "-" Expr2,
Expr2,
AddSub: () = {
"+",
"-",
}
// MulDiv
pub Expr2: () = {
Expr2 "/" Expr3,
Expr2 "*" Expr3,
Expr3,
MulDiv: () = {
"/",
"*",
}
// Unary
pub Expr3: () = {
"*" Term,
"&" Term,
"&" "mut" Term,
"!" Term,
Term,
Unary: () = {
"!",
"*",
"&",
"&" "mut",
}
pub Term: () = {
Term: () = {
Id,
Num,
Id "(" CommaNoTrail<Expr> ")",
"(" Expr ")",
}
pub Num: i32 = {
Num: i32 = {
r"[0-9]+" => i32::from_str(<>).unwrap(),
};
pub Id: String = {
Id: String = {
r"(_|[a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*" => String::from_str(<>).unwrap(),
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment