Skip to content

Commit 113610b

Browse files
authored
refactor: cleanup AST code (#32)
**Description** This PR refactors the code in `src/grammar/rules_actions.rs` to use `let ast = &mut compiler_context.ast;` as it reduced some noise added by having to use `compiler_context.ast` for every interaction with the AST It removes some functions for `push|pop` out of the expression and term stacks as those fields are now public to be consistent with the way we use the other AST stacks It also wraps the repeated code for `log_error_and_exist` into `log_ast_error` function
1 parent 8b59a74 commit 113610b

File tree

2 files changed

+374
-385
lines changed

2 files changed

+374
-385
lines changed

src/compiler/ast.rs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use std::{
1111

1212
pub struct Ast {
1313
tree: [Rc<Node>; mem::variant_count::<AstPtr>()],
14-
pub stack_t: Vec<Rc<Node>>,
15-
pub stack_e: Vec<Rc<Node>>,
14+
pub term_stack: Vec<Rc<Node>>,
15+
pub expression_stack: Vec<Rc<Node>>,
1616
pub comparision_op_stack: Vec<ComparisonOp>,
1717
pub comparision_expressions_stack: Vec<Rc<Node>>,
1818
pub boolean_expression_stack: Vec<Rc<Node>>,
@@ -23,8 +23,8 @@ pub struct Ast {
2323

2424
impl Debug for Ast {
2525
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26-
writeln!(f, "{:?}", self.stack_t)?;
27-
writeln!(f, "{:?}", self.stack_e)?;
26+
writeln!(f, "{:?}", self.term_stack)?;
27+
writeln!(f, "{:?}", self.expression_stack)?;
2828
writeln!(f, "{:?}", self.comparision_op_stack)?;
2929
writeln!(f, "{:?}", self.comparision_expressions_stack)
3030
}
@@ -132,6 +132,7 @@ pub enum AstAction {
132132
Read,
133133
Write,
134134
S,
135+
Negative,
135136
Noop,
136137
}
137138

@@ -159,6 +160,7 @@ impl Display for AstAction {
159160
Self::Read => write!(f, "READ"),
160161
Self::Write => write!(f, "WRITE"),
161162
Self::S => write!(f, "S"),
163+
Self::Negative => write!(f, "NEG"),
162164
Self::Noop => write!(f, "NOOP"),
163165
}
164166
}
@@ -181,8 +183,8 @@ impl Default for Ast {
181183
fn default() -> Self {
182184
Self {
183185
tree: array::from_fn(|_| Rc::new(Node::new_leaf(NodeValue::Value("".to_string())))),
184-
stack_e: Vec::new(),
185-
stack_t: Vec::new(),
186+
expression_stack: Vec::new(),
187+
term_stack: Vec::new(),
186188
comparision_op_stack: Vec::new(),
187189
comparision_expressions_stack: Vec::new(),
188190
boolean_expression_stack: Vec::new(),
@@ -286,32 +288,6 @@ impl Ast {
286288
Ok(node_count)
287289
}
288290

289-
pub fn push_t_stack(&mut self, node: AstNodeRef) {
290-
let node = match node {
291-
AstNodeRef::Node(node) => node,
292-
AstNodeRef::Ptr(ptr) => self.tree[ptr as usize].clone(),
293-
};
294-
295-
self.stack_t.push(node);
296-
}
297-
298-
pub fn pop_t_stack(&mut self) -> Option<Rc<Node>> {
299-
self.stack_t.pop()
300-
}
301-
302-
pub fn push_e_stack(&mut self, node: AstNodeRef) {
303-
let node = match node {
304-
AstNodeRef::Node(node) => node,
305-
AstNodeRef::Ptr(ptr) => self.tree[ptr as usize].clone(),
306-
};
307-
308-
self.stack_e.push(node);
309-
}
310-
311-
pub fn pop_e_stack(&mut self) -> Option<Rc<Node>> {
312-
self.stack_e.pop()
313-
}
314-
315291
pub fn get_node_from_ptr(&self, from: AstPtr) -> Rc<Node> {
316292
self.tree[from as usize].clone()
317293
}

0 commit comments

Comments
 (0)