e0/src/parser.lalrpop

37 lines
689 B
Text
Raw Normal View History

2022-04-06 02:33:16 +00:00
use crate::ast::*;
grammar;
pub Program: Vec<Decl> = Decl* => <>;
Decl: Decl = {
2022-04-06 04:32:37 +00:00
Func => Decl::Func(<>),
2022-04-06 02:33:16 +00:00
};
Func: Func = {
2022-04-06 04:32:37 +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 = {
2022-04-06 04:32:37 +00:00
"let" <name:Ident> ":" <ty:Type> "=" <expr:Expr> ";" =>
Stmt::Let(name, ty, expr),
"return" <expr:Expr?> ";" => Stmt::Return(expr),
2022-04-06 02:33:16 +00:00
};
Expr: Expr = {
2022-04-06 04:32:37 +00:00
Expr1 => <>,
2022-04-06 02:33:16 +00:00
};
Expr1: Expr = {
2022-04-06 04:32:37 +00:00
r"[0-9]+" => Expr::Int(<>.parse::<i64>().unwrap()),
Ident => Expr::Var(<>),
"(" <expr:Expr> ")" => expr,
2022-04-06 02:33:16 +00:00
};
Type: Type = {
2022-04-06 04:32:37 +00:00
"int" => Type::Int,
2022-04-06 02:33:16 +00:00
};
Ident: String = r"([A-Za-z][A-Za-z0-9_]*)|(_[A-Za-z0-9_]+)" => <>.to_string();