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

syntax tests pass, polish

parent 262e02bf
Branches
No related tags found
No related merge requests found
fn main() { fn main() {}
fn a(mut a: i32) {
let a = 5; let a = 5;
let b = { 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"); ...@@ -8,7 +8,6 @@ lalrpop_mod!(pub parser, "/ast/parser.rs");
use parser::*; use parser::*;
pub mod ast; pub mod ast;
use ast::*;
fn main() {} fn main() {}
...@@ -21,7 +20,7 @@ pub fn read(file_name: &str) -> std::io::Result<String> { ...@@ -21,7 +20,7 @@ pub fn read(file_name: &str) -> std::io::Result<String> {
pub fn parse(file_name: &str) { pub fn parse(file_name: &str) {
let p = read(file_name).expect("File not found"); let p = read(file_name).expect("File not found");
FunctionParser::new().parse(&p).unwrap() ProgramParser::new().parse(&p).unwrap()
} }
#[test] #[test]
......
...@@ -16,113 +16,118 @@ match { ...@@ -16,113 +16,118 @@ match {
// A comma separated sequence without trailing comma // A comma separated sequence without trailing comma
CommaNoTrail<T>: Vec<T> = { CommaNoTrail<T>: Vec<T> = {
<v:(<T> ",")*> <e:T> => { <mut v:(<T> ",")*> <e:T> => { v.push(e); v }
let mut v = v;
v.push(e);
v
} }
Tier<Op,NextTier>: Box<Expr> = {
Tier<Op,NextTier> Op NextTier => Box::new(Expr::Op(<>)),
NextTier
};
pub Program: () = {
Function*
} }
pub Function: () = { pub Function: () = {
"fn" Id Params ("->" Type)? Block => (), "fn" Id Params ("->" Type)? Block,
} }
pub Params: () = { pub Params: () = {
"()" => (), // seems like a haxx "()", // seems like a haxx
"(" ((Param ",")* Param)? ")" => (), "(" (Param ",")* Param? ")",
} }
pub Param:() = { pub Param:() = {
Id ":" Type, "mut"? Id ":" Type,
} }
pub Type:() = { pub Type:() = {
"i32" => (), "i32",
"bool" => (), "bool",
"()" => (), "()",
} }
pub Block: () = { pub Block: () = {
"{" StmtSeq* "}" => (), "{" StmtSeq* "}",
"{" StmtSeq* Stmt "}" => (), "{" StmtSeq* Stmt "}",
} }
pub StmtSeq: () = { pub StmtSeq: () = {
Stmt ";" => (), Stmt ";",
StmtBlock => (), StmtBlock,
} }
pub StmtBlock: () = { pub StmtBlock: () = {
"while" Expr Block => (), "while" Expr Block,
"if" Expr Block ("else" Block)? => (), "if" Expr Block ("else" Block)?,
Block => (), Block,
} }
pub Stmt: () = { pub Stmt: () = {
";" => (), ";",
"let" "mut"? Id "=" Expr => (), "let" "mut"? Id "=" Expr,
ExprNoBlock "=" Expr => (), ExprNoBlock "=" Expr,
ExprNoBlock => (), ExprNoBlock,
} }
pub Expr: () = { pub Expr: () = {
ExprBlock => (), ExprBlock,
ExprNoBlock => (), ExprNoBlock,
} }
pub ExprBlock: () = { pub ExprBlock: () = {
"if" ExprNoBlock Block "else" Block => (), "if" ExprNoBlock Block "else" Block,
Block => (), Block,
} }
// Here is our ordinary expressions // Here is our ordinary expressions
pub ExprNoBlock: () = { pub ExprNoBlock: () = {
ExprNoBlock "||" Expr0 => (), ExprNoBlock "||" Expr0,
ExprNoBlock "&&" Expr0 => (), ExprNoBlock "&&" Expr0,
Expr0 => (), Expr0,
} }
// Comparison // Comparison
pub Expr0: () = { pub Expr0: () = {
Expr0 "==" Expr1 => (), Expr0 "==" Expr1,
Expr0 "!=" Expr1 => (), Expr0 "!=" Expr1,
Expr0 ">" Expr1 => (), Expr0 ">" Expr1,
Expr0 "<" Expr1 => (), Expr0 "<" Expr1,
Expr1 => (), Expr1,
} }
// AddSub // AddSub
pub Expr1: () = { pub Expr1: () = {
Expr1 "+" Expr2 => (), Expr1 "+" Expr2,
Expr1 "-" Expr2 => (), Expr1 "-" Expr2,
Expr2 => (), Expr2,
} }
// MulDiv // MulDiv
pub Expr2: () = { pub Expr2: () = {
Expr2 "/" Expr3 => (), Expr2 "/" Expr3,
Expr2 "*" Expr3 => (), Expr2 "*" Expr3,
Expr3 => (), Expr3,
} }
// Unary // Unary
pub Expr3: () = { pub Expr3: () = {
"*" Term => (), "*" Term,
"&" Term => (), "&" Term,
"&" "mut" Term => (), "&" "mut" Term,
"!" Term => (), "!" Term,
Term => (), Term,
} }
pub Term: () = { pub Term: () = {
Id => (), Id,
Num => (), Num,
Id "(" CommaNoTrail<Expr> ")" => (), 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