2022-04-06 02:33:16 +00:00
|
|
|
use crate::ast::*;
|
|
|
|
|
|
|
|
grammar;
|
|
|
|
|
|
|
|
pub Program: Vec<Decl> = Decl* => <>;
|
|
|
|
|
|
|
|
Decl: Decl = {
|
|
|
|
Func => Decl::Func(<>),
|
|
|
|
};
|
|
|
|
|
|
|
|
Func: Func = {
|
2022-04-06 03:07:34 +00:00
|
|
|
"fn" <name:Ident> "(" ")" "->" <return_ty:Type> "{" <stmts:Stmt*> "}" =>
|
|
|
|
Func { name, return_ty, stmts },
|
2022-04-06 02:33:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Stmt: Stmt = {
|
|
|
|
"return" <expr:Expr?> ";" => Stmt::Return(expr),
|
|
|
|
};
|
|
|
|
|
|
|
|
Expr: Expr = {
|
|
|
|
Expr1 => <>,
|
|
|
|
};
|
|
|
|
|
|
|
|
Expr1: Expr = {
|
|
|
|
r"[0-9]+" => Expr::Int(<>.parse::<i64>().unwrap()),
|
|
|
|
"(" <expr:Expr> ")" => expr,
|
|
|
|
};
|
|
|
|
|
|
|
|
Type: Type = {
|
|
|
|
"int" => Type::Int,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ident: String = r"([A-Za-z][A-Za-z0-9_]*)|(_[A-Za-z0-9_]+)" => <>.to_string();
|