Skip to content

Commit

Permalink
formatted visitor simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmasoldi3r committed Nov 4, 2023
1 parent de16294 commit dc7555a
Show file tree
Hide file tree
Showing 9 changed files with 847 additions and 1,082 deletions.
54 changes: 34 additions & 20 deletions src/code/walker.rs → src/code/ast_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,49 @@ use crate::parser::{ast, Script};
use super::builder::Builder;

#[derive(Debug)]
pub struct VisitError(Box<dyn Error>);
pub struct VisitError(pub Box<dyn Error>);

pub type Result = std::result::Result<Builder, VisitError>;

pub trait AstWalker {
// Those need to be implemented explicitly by the user:
fn visit_return(&self, ctx: Builder, stmt: &Return) -> Result;
fn visit_class(&self, ctx: Builder, stmt: &Class) -> Result;
fn visit_fn(&self, ctx: Builder, stmt: &Function) -> Result;
fn visit_assignment(&self, ctx: Builder, stmt: &Assignment) -> Result;
fn visit_declaration(&self, ctx: Builder, stmt: &Let) -> Result;
fn visit_expression_statement(&self, ctx: Builder, stmt: &Expression) -> Result;
fn visit_lambda(&self, ctx: Builder, expr: &Lambda) -> Result;
pub trait Visitor {
// Expressions
fn visit_reference(&self, ctx: Builder, expr: &MemberExpression) -> Result;
fn visit_call(&self, ctx: Builder, expr: &CallExpression) -> Result;
fn visit_binary(&self, ctx: Builder, expr: &BinaryExpression) -> Result;
fn visit_unary(&self, ctx: Builder, expr: &UnaryExpression) -> Result;
fn visit_wrapped_expression(&self, ctx: Builder, expr: &Expression) -> Result;
fn visit_identifier(&self, ctx: Builder, expr: &Identifier) -> Result;
fn visit_use_expression(&self, ctx: Builder, expr: &Identifier) -> Result;

// Literals
fn visit_lambda(&self, ctx: Builder, expr: &Lambda) -> Result;
fn visit_tuple(&self, ctx: Builder, expr: &Tuple) -> Result;
fn visit_unit(&self, ctx: Builder) -> Result;
fn visit_number(&self, ctx: Builder, expr: &Number) -> Result;
fn visit_string(&self, ctx: Builder, expr: &StringLiteral) -> Result;
fn visit_unit(&self, ctx: Builder) -> Result;
fn visit_binary(&self, ctx: Builder, expr: &BinaryExpression) -> Result;
fn visit_unary(&self, ctx: Builder, expr: &UnaryExpression) -> Result;
fn visit_if(&self, ctx: Builder, expr: &If) -> Result;
fn visit_table(&self, ctx: Builder, expr: &Table) -> Result;
fn visit_vector(&self, ctx: Builder, expr: &Vector) -> Result;

// Statements
fn visit_return(&self, ctx: Builder, stmt: &Return) -> Result;
fn visit_class(&self, ctx: Builder, stmt: &Class) -> Result;
fn visit_fn(&self, ctx: Builder, stmt: &Function) -> Result;
fn visit_assignment(&self, ctx: Builder, stmt: &Assignment) -> Result;
fn visit_declaration(&self, ctx: Builder, stmt: &Let) -> Result;
fn visit_expression_statement(&self, ctx: Builder, stmt: &Expression) -> Result;
fn visit_use_statement(&self, ctx: Builder, stmt: &Identifier) -> Result;

// Looping
fn visit_for(&self, ctx: Builder, expr: &For) -> Result;
fn visit_while(&self, ctx: Builder, expr: &While) -> Result;
fn visit_loop(&self, ctx: Builder, expr: &Loop) -> Result;

// Conditionals
fn visit_if(&self, ctx: Builder, expr: &If) -> Result;
fn visit_match(&self, ctx: Builder, expr: &Match) -> Result;
fn visit_1tuple(&self, ctx: Builder, expr: &Expression) -> Result;
fn visit_identifier(&self, ctx: Builder, expr: &Identifier) -> Result;
fn visit_do(&self, ctx: Builder, expr: &Do) -> Result;

fn visit_block_expression(&self, ctx: Builder, expr: &Do) -> Result;
fn visit_script(&self, ctx: Builder, script: &Script) -> Result;

// Generically implementable matching patterns:
fn visit_expression(&self, ctx: Builder, expression: &Expression) -> Result {
Expand All @@ -53,12 +65,13 @@ pub trait AstWalker {
Expression::Unary(e) => self.visit_unary(ctx, e),
Expression::Table(e) => self.visit_table(ctx, e),
Expression::Vector(e) => self.visit_vector(ctx, e),
Expression::Tuple1(e) => self.visit_1tuple(ctx, e),
Expression::Tuple1(e) => self.visit_wrapped_expression(ctx, e),
Expression::Identifier(e) => self.visit_identifier(ctx, e),
Expression::Do(e) => self.visit_do(ctx, e),
Expression::Do(e) => self.visit_block_expression(ctx, e),
Expression::Use(e) => self.visit_use_expression(ctx, e),
}
}
fn visit_script(&self, ctx: Builder, script: &Script) -> Result {
fn visit_block(&self, ctx: Builder, script: &Script) -> Result {
script
.statements
.iter()
Expand All @@ -74,6 +87,7 @@ pub trait AstWalker {
ast::Statement::Let(e) => self.visit_declaration(ctx?, e),
ast::Statement::Match(e) => self.visit_match(ctx?, e),
ast::Statement::Expression(e) => self.visit_expression_statement(ctx?, e),
ast::Statement::Use(e) => self.visit_use_statement(ctx?, e),
})
}
}
158 changes: 0 additions & 158 deletions src/code/generation.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/code/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod ast_visitor;
pub mod builder;
pub mod generation;
pub mod macros;
pub mod walker;
Empty file added src/lua/helpers.rs
Empty file.
Loading

0 comments on commit dc7555a

Please sign in to comment.