33 lines
584 B
Text
33 lines
584 B
Text
|
use crate::ast::*;
|
||
|
|
||
|
grammar;
|
||
|
|
||
|
pub Program: Vec<Decl> = Decl* => <>;
|
||
|
|
||
|
Decl: Decl = {
|
||
|
Func => Decl::Func(<>),
|
||
|
};
|
||
|
|
||
|
Func: Func = {
|
||
|
"fn" <name:Ident> "(" ")" "->" <return_ty:Type> "{" <stmts:Stmt*> "}" => Func { name, return_ty, stmts },
|
||
|
};
|
||
|
|
||
|
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();
|