This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlang.lalrpop
134 lines (105 loc) · 3.75 KB
/
tlang.lalrpop
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::tree::Expr;
use crate::tree::Literal;
use crate::tree::Op;
grammar;
pub Exprs : Expr = MultiLine<Expr> => Expr::Block {
body: <>
};
Identifier : Expr = <i:r"[a-zA-Z_][a-zA-Z0-9_]*"> => Expr::Ident{name: i.to_string()};
Block: Expr = "{" <e:Exprs> "}" => e;
Num : Expr = <n:r"[0-9]+"> => Expr::Literal{value: Literal::Number(n.parse::<f64>().unwrap())} ;
Str : Expr = <s:r#"'(\\.|[^"])*'"#> => Expr::Literal{value: Literal::String(s[1..s.len()-1].to_string())} ;
IfExpr : Expr = "if" <e:Expr> "{" <e1:Exprs> "}" => Expr::IfThen{cond: Box::new(e), then: Box::new(e1)};
IfElseExpr : Expr = <e1:IfExpr> "else" <e2:Block> => {
match e1 {
Expr::IfThen{cond, then} => Expr::IfThenElse{cond, then, else_: Box::new(e2)},
_ => panic!("Invalid if-else expression")
}
};
ForExpr : Expr = "for" <e1:Expr> "in" <e2:Expr> "{" <e3:Exprs> "}" => Expr::For{
name: Box::new(e1),
iter: Box::new(e2),
body: Box::new(e3)
};
WhileExpr : Expr = "while" <e1:Expr> "{" <e2:Exprs> "}" => Expr::While{
cond: Box::new(e1),
body: Box::new(e2)
};
LetExpr : Expr = "let" <e1:Identifier> "=" <e2:Expr> => Expr::Assign{name: match e1 {
Expr::Ident{name} => name,
_ => panic!("Invalid let expression")
}, value: Box::new(e2)};
Op: Expr = {
<e1:Term> "+" <e2:Fact> => Expr::BinOp{left: Box::new(e1), op: Op::Add, right: Box::new(e2)},
<e1:Term> "-" <e2:Fact> => Expr::BinOp{left: Box::new(e1), op: Op::Sub, right: Box::new(e2)},
Fact
};
Fact: Expr = {
<e1:Fact> "*" <e2:Term> => Expr::BinOp{left: Box::new(e1), op: Op::Mul, right: Box::new(e2)},
<e1:Fact> "/" <e2:Term> => Expr::BinOp{left: Box::new(e1), op: Op::Div, right: Box::new(e2)},
Term
};
CmpOp: Expr = {
<e1:Term> "==" <e2:Term> => Expr::BinOp{left: Box::new(e1), op: Op::Eq, right: Box::new(e2)},
<e1:Term> "!=" <e2:Term> => Expr::BinOp{left: Box::new(e1), op: Op::Neq, right: Box::new(e2)},
<e1:Term> "<" <e2:Term> => Expr::BinOp{left: Box::new(e1), op: Op::Lt, right: Box::new(e2)},
<e1:Term> "<=" <e2:Term> => Expr::BinOp{left: Box::new(e1), op: Op::Le, right: Box::new(e2)},
<e1:Term> ">" <e2:Term> => Expr::BinOp{left: Box::new(e1), op: Op::Gt, right: Box::new(e2)},
<e1:Term> ">=" <e2:Term> => Expr::BinOp{left: Box::new(e1), op: Op::Ge, right: Box::new(e2)}
};
List: Expr = {
"[" <s:Expr *> <e:("," Expr)*> "]" => {
let i = s.into_iter();
let n = e.into_iter().map(|x| x.1);
Expr::List {
elems: i.chain(n).collect::<Vec<Expr>>()
}
}
};
ArgDef: Vec<Expr> = {
"(" <s:Identifier*> <e:("," Identifier)*> ")" => {
let i = s.into_iter();
let n = e.into_iter().map(|x| x.1);
i.chain(n).collect::<Vec<Expr>>()
}
};
ArgCall: Vec<Expr> = {
"(" <s:Expr*> <e:("," Expr)*> ")" => {
let i = s.into_iter();
let n = e.into_iter().map(|x| x.1);
i.chain(n).collect::<Vec<Expr>>()
}
};
FunDef : Expr = "def" <e1:Identifier> <e2:ArgDef> "{" <e3:Exprs> "}" => {
match e1 {
Expr::Ident{name} => Expr::FunDef{name, args: e2, body: Box::new(e3) },
_ => panic!("Invalid function definition")
}
};
True : Expr = "true" => Expr::Literal{value: Literal::Bool(true)};
False : Expr = "false" => Expr::Literal{value: Literal::Bool(false)};
Call : Expr = "@" <e1:Identifier> <e2:ArgCall> => Expr::Call{name: match e1 {
Expr::Ident{name} => name,
_ => panic!("Invalid function call")
}, args: e2};
Value = { Num, Str, Identifier , True, False, Call, List};
Term = {
Value,
"(" <e:Expr> ")" => e,
};
Expr = {
Block,
IfExpr,
IfElseExpr,
ForExpr,
WhileExpr,
LetExpr,
Op,
CmpOp,
FunDef
};
MultiLine<T> : Vec<T> = {
<v:(<T> "\n") *> <e:T*> => {
v.into_iter().chain(e).collect()
}
};