64 lines
1.6 KiB
Plaintext
64 lines
1.6 KiB
Plaintext
|
|
use std::str::FromStr;
|
||
|
|
use crate::ast;
|
||
|
|
|
||
|
|
|
||
|
|
grammar;
|
||
|
|
|
||
|
|
|
||
|
|
pub LiteralInt: ast::LiteralInt = {
|
||
|
|
r"[0-9]+" => ast::LiteralInt{value: i32::from_str(<>).unwrap()}
|
||
|
|
};
|
||
|
|
|
||
|
|
pub Identifier: ast::Identifier = {
|
||
|
|
r"[A-Za-z][A-Za-z0-9_]*" => ast::Identifier{name: <>.to_string()}
|
||
|
|
};
|
||
|
|
|
||
|
|
pub Expression: Box<ast::Expression> = {
|
||
|
|
<l:Expression> "+" <r:Factor> => Box::new(ast::Expression::Op(l, ast::Operator::Plus, r)),
|
||
|
|
<l:Expression> "-" <r:Factor> => Box::new(ast::Expression::Op(l, ast::Operator::Minus, r)),
|
||
|
|
Factor,
|
||
|
|
}
|
||
|
|
|
||
|
|
pub Factor: Box<ast::Expression> = {
|
||
|
|
<l:Factor> "*" <r:Term> => Box::new(ast::Expression::Op(l, ast::Operator::Mul, r)),
|
||
|
|
<l:Factor> "/" <r:Term> => Box::new(ast::Expression::Op(l, ast::Operator::Div, r)),
|
||
|
|
Term,
|
||
|
|
}
|
||
|
|
|
||
|
|
pub Term: Box<ast::Expression> = {
|
||
|
|
LiteralInt => Box::new(ast::Expression::LiteralInt(<>)),
|
||
|
|
Identifier => Box::new(ast::Expression::Identifier(<>)),
|
||
|
|
"(" <Expression> ")",
|
||
|
|
}
|
||
|
|
|
||
|
|
pub Block: ast::Block = {
|
||
|
|
"{" <e:Expression> "}" => ast::Block{expression: e}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub VariableDeclaration: ast::VariableDeclaration = {
|
||
|
|
Identifier => ast::VariableDeclaration{name: <>}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub Function: ast::Function = {
|
||
|
|
"fn" <n:Identifier> "(" <args:Comma<VariableDeclaration>> ")" <b:Block> => ast::Function{name: n, arguments: args, block: b}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
pub Program: ast::Program = {
|
||
|
|
<fs:Function*> => ast::Program{functions: fs}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// From https://lalrpop.github.io/lalrpop/tutorial/006_macros.html
|
||
|
|
// Comma seperated list of T with optional trailing comma
|
||
|
|
Comma<T>: Vec<T> = {
|
||
|
|
<v:(<T> ",")*> <e:T?> => match e {
|
||
|
|
None => v,
|
||
|
|
Some(e) => {
|
||
|
|
let mut v = v;
|
||
|
|
v.push(e);
|
||
|
|
v
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|