-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.rs
165 lines (141 loc) · 3.42 KB
/
ast.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::fmt;
pub type OffsetSpan = (usize, usize);
#[derive(Debug, Clone, PartialEq)]
pub struct Spanned<T> {
pub pos: OffsetSpan,
pub data: T,
}
#[derive(Debug, Clone, PartialEq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Mod,
Lt,
Lte,
Gt,
Gte,
Eq,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LogicalBinOp {
And,
Or,
}
#[derive(Debug, Clone, PartialEq)]
pub enum UnOp {
Neg,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LogicalUnOp {
Not,
}
impl fmt::Display for BinOp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BinOp::Add => write!(f, "+"),
BinOp::Sub => write!(f, "-"),
BinOp::Mul => write!(f, "*"),
BinOp::Div => write!(f, "/"),
BinOp::Mod => write!(f, "%"),
BinOp::Lt => write!(f, "<"),
BinOp::Lte => write!(f, "<="),
BinOp::Gt => write!(f, ">"),
BinOp::Gte => write!(f, ">="),
BinOp::Eq => write!(f, "=="),
}
}
}
impl fmt::Display for LogicalBinOp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LogicalBinOp::And => write!(f, "and"),
LogicalBinOp::Or => write!(f, "or"),
}
}
}
impl fmt::Display for UnOp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
UnOp::Neg => write!(f, "-"),
}
}
}
impl fmt::Display for LogicalUnOp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LogicalUnOp::Not => write!(f, "not"),
}
}
}
#[derive(Debug, Clone)]
pub enum Literal {
Integer(i64),
Float(f64),
Bool(bool),
String(String),
}
pub type LiteralNode = Spanned<Literal>;
#[derive(Debug, Clone)]
pub enum LhsExpr {
Identifier(String),
}
pub type LhsExprNode = Spanned<LhsExpr>;
#[derive(Debug, Clone)]
pub enum Variable {
Identifier(BindingType, String),
}
#[derive(Debug, Clone)]
pub enum BindingType {
Mutable,
}
#[derive(Debug, Clone)]
pub struct FnDefExpr {
pub maybe_id: Option<String>,
pub params: Vec<String>,
pub body: Box<StmtNode>,
}
#[derive(Debug, Clone)]
pub enum Expr {
Literal(LiteralNode),
Identifier(String),
Binary(Box<ExprNode>, BinOp, Box<ExprNode>),
BinaryLogical(Box<ExprNode>, LogicalBinOp, Box<ExprNode>),
Unary(UnOp, Box<ExprNode>),
UnaryLogical(LogicalUnOp, Box<ExprNode>),
FnDef(FnDefExpr),
FnCall(Box<ExprNode>, Vec<ExprNode>),
Tuple(Vec<ExprNode>),
MemberByIdx(Box<ExprNode>, Box<ExprNode>),
}
// Only for parser convenience
pub enum ExprSuffix {
ListInParens(Vec<ExprNode>),
InSquareBrackets(ExprNode),
}
pub type ExprNode = Spanned<Expr>;
#[derive(Debug, Clone)]
pub struct IfThenStmt {
pub cond: ExprNode,
pub then_block: Box<StmtNode>,
pub maybe_else_block: Option<Box<StmtNode>>,
}
#[derive(Debug, Clone)]
pub enum Stmt {
Assign(LhsExprNode, ExprNode),
AssignOp(LhsExprNode, BinOp, ExprNode),
VarDecl(Variable, ExprNode),
Expr(ExprNode),
Block(Vec<StmtNode>),
IfThen(IfThenStmt),
Loop(Box<StmtNode>),
Return(Option<ExprNode>),
Break,
Continue,
Empty,
}
pub type StmtNode = Spanned<Stmt>;