-
Notifications
You must be signed in to change notification settings - Fork 61
/
frontend.rs
87 lines (76 loc) · 2.96 KB
/
frontend.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/// The AST node for expressions.
pub enum Expr {
Literal(String),
Identifier(String),
Assign(String, Box<Expr>),
Eq(Box<Expr>, Box<Expr>),
Ne(Box<Expr>, Box<Expr>),
Lt(Box<Expr>, Box<Expr>),
Le(Box<Expr>, Box<Expr>),
Gt(Box<Expr>, Box<Expr>),
Ge(Box<Expr>, Box<Expr>),
Add(Box<Expr>, Box<Expr>),
Sub(Box<Expr>, Box<Expr>),
Mul(Box<Expr>, Box<Expr>),
Div(Box<Expr>, Box<Expr>),
IfElse(Box<Expr>, Vec<Expr>, Vec<Expr>),
WhileLoop(Box<Expr>, Vec<Expr>),
Call(String, Vec<Expr>),
GlobalDataAddr(String),
}
peg::parser!(pub grammar parser() for str {
pub rule function() -> (String, Vec<String>, String, Vec<Expr>)
= [' ' | '\t' | '\n']* "fn" _ name:identifier() _
"(" params:((_ i:identifier() _ {i}) ** ",") ")" _
"->" _
"(" returns:(_ i:identifier() _ {i}) ")" _
"{" _ "\n"
stmts:statements()
_ "}" _ "\n" _
{ (name, params, returns, stmts) }
rule statements() -> Vec<Expr>
= s:(statement()*) { s }
rule statement() -> Expr
= _ e:expression() _ "\n" { e }
rule expression() -> Expr
= if_else()
/ while_loop()
/ assignment()
/ binary_op()
rule if_else() -> Expr
= "if" _ e:expression() _ "{" _ "\n"
then_body:statements() _ "}" _ "else" _ "{" _ "\n"
else_body:statements() _ "}"
{ Expr::IfElse(Box::new(e), then_body, else_body) }
rule while_loop() -> Expr
= "while" _ e:expression() _ "{" _ "\n"
loop_body:statements() _ "}"
{ Expr::WhileLoop(Box::new(e), loop_body) }
rule assignment() -> Expr
= i:identifier() _ "=" _ e:expression() {Expr::Assign(i, Box::new(e))}
rule binary_op() -> Expr = precedence!{
a:@ _ "==" _ b:(@) { Expr::Eq(Box::new(a), Box::new(b)) }
a:@ _ "!=" _ b:(@) { Expr::Ne(Box::new(a), Box::new(b)) }
a:@ _ "<" _ b:(@) { Expr::Lt(Box::new(a), Box::new(b)) }
a:@ _ "<=" _ b:(@) { Expr::Le(Box::new(a), Box::new(b)) }
a:@ _ ">" _ b:(@) { Expr::Gt(Box::new(a), Box::new(b)) }
a:@ _ ">=" _ b:(@) { Expr::Ge(Box::new(a), Box::new(b)) }
--
a:@ _ "+" _ b:(@) { Expr::Add(Box::new(a), Box::new(b)) }
a:@ _ "-" _ b:(@) { Expr::Sub(Box::new(a), Box::new(b)) }
--
a:@ _ "*" _ b:(@) { Expr::Mul(Box::new(a), Box::new(b)) }
a:@ _ "/" _ b:(@) { Expr::Div(Box::new(a), Box::new(b)) }
--
i:identifier() _ "(" args:((_ e:expression() _ {e}) ** ",") ")" { Expr::Call(i, args) }
i:identifier() { Expr::Identifier(i) }
l:literal() { l }
}
rule identifier() -> String
= quiet!{ n:$(['a'..='z' | 'A'..='Z' | '_']['a'..='z' | 'A'..='Z' | '0'..='9' | '_']*) { n.to_owned() } }
/ expected!("identifier")
rule literal() -> Expr
= n:$(['0'..='9']+) { Expr::Literal(n.to_owned()) }
/ "&" i:identifier() { Expr::GlobalDataAddr(i) }
rule _() = quiet!{[' ' | '\t']*}
});