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
166
167
168
use crate::debug::DebugSymbol;
use crate::parser::operator::{BinaryOp, UnaryOp};
use crate::parser::primary::{Atom, Primary};
use crate::parser::lvalue::{Assignment, Declaration};
use crate::parser::fundefs::FunctionDef;
use crate::parser::stmt::{StmtMeta, Stmt, Label, StmtList};
#[derive(Debug, Clone)]
pub enum Expr {
Atom(Atom),
Primary(Primary),
UnaryOp(UnaryOp, Box<Expr>),
BinaryOp(BinaryOp, Box<(Expr, Expr)>),
Assignment(Box<Assignment>),
Declaration(Box<Declaration>),
Tuple(Box<[ExprMeta]>),
IfExpr {
branches: Box<[ConditionalBranch]>,
else_clause: Option<Box<ExprBlock>>,
},
Block {
label: Option<Label>,
suite: Box<ExprBlock>,
},
FunctionDef(FunctionDef),
Echo(Box<Expr>),
}
#[derive(Debug, Clone)]
pub struct ExprBlock {
stmt_list: StmtList,
result: Option<ExprMeta>,
}
impl From<StmtList> for ExprBlock {
fn from(stmt_list: StmtList) -> Self {
let (mut suite, control) = stmt_list.take();
let mut result = None;
if control.is_none() && !suite.is_empty() {
match ExprMeta::try_from(suite.pop().unwrap()) {
Err(stmt) => suite.push(stmt),
Ok(expr) => { result.replace(expr); },
}
}
let stmt_list = StmtList::new(suite, control);
Self { stmt_list, result }
}
}
impl ExprBlock {
pub fn stmt_list(&self) -> &StmtList { &self.stmt_list }
pub fn result(&self) -> Option<&ExprMeta> { self.result.as_ref() }
}
#[derive(Debug, Clone)]
pub struct ConditionalBranch {
condition: Expr,
suite: ExprBlock,
}
impl ConditionalBranch {
pub fn new(condition: Expr, suite: ExprBlock) -> Self {
Self { condition, suite }
}
pub fn condition(&self) -> &Expr { &self.condition }
pub fn suite(&self) -> &ExprBlock { &self.suite }
}
#[derive(Debug, Clone)]
pub struct ExprMeta {
variant: Expr,
symbol: DebugSymbol,
}
impl ExprMeta {
pub fn new(variant: Expr, symbol: DebugSymbol) -> Self {
ExprMeta { variant, symbol }
}
pub fn variant(&self) -> &Expr { &self.variant }
pub fn take_variant(self) -> Expr { self.variant }
pub fn debug_symbol(&self) -> &DebugSymbol { &self.symbol }
pub fn take_symbol(self) -> DebugSymbol { self.symbol }
pub fn take(self) -> (Expr, DebugSymbol) { (self.variant, self.symbol) }
}
impl From<ExprMeta> for (Expr, DebugSymbol) {
fn from(expr: ExprMeta) -> Self { (expr.variant, expr.symbol) }
}
impl From<Expr> for Stmt {
#[inline]
fn from(expr: Expr) -> Self { Stmt::Expression(expr) }
}
impl TryFrom<Stmt> for Expr {
type Error = Stmt;
#[inline]
fn try_from(stmt: Stmt) -> Result<Self, Stmt> {
if let Stmt::Expression(expr) = stmt { Ok(expr) }
else { Err(stmt) }
}
}
impl TryFrom<StmtMeta> for Expr {
type Error = StmtMeta;
#[inline]
fn try_from(stmt: StmtMeta) -> Result<Self, StmtMeta> {
let (stmt, symbol) = stmt.take();
if let Stmt::Expression(expr) = stmt { Ok(expr) }
else { Err(StmtMeta::new(stmt, symbol)) }
}
}
impl From<ExprMeta> for StmtMeta {
#[inline]
fn from(expr: ExprMeta) -> Self {
let (expr, symbol) = expr.take();
StmtMeta::new(expr.into(), symbol)
}
}
impl TryFrom<StmtMeta> for ExprMeta {
type Error = StmtMeta;
#[inline]
fn try_from(stmt: StmtMeta) -> Result<Self, StmtMeta> {
let (stmt, symbol) = stmt.take();
match Expr::try_from(stmt) {
Ok(expr) => Ok(ExprMeta::new(expr, symbol)),
Err(stmt) => Err(StmtMeta::new(stmt, symbol)),
}
}
}