Skip to content

Commit

Permalink
Merge pull request #12 from sigmasoldi3r/feat/code-emision-abstraction
Browse files Browse the repository at this point in the history
wip: code-emission abstraction
  • Loading branch information
sigmasoldi3r authored Nov 4, 2023
2 parents 68ed578 + dc7555a commit 790b520
Show file tree
Hide file tree
Showing 11 changed files with 923 additions and 1,009 deletions.
93 changes: 93 additions & 0 deletions src/code/ast_visitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::error::Error;

use ast::*;

use crate::parser::{ast, Script};

use super::builder::Builder;

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

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

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_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_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 {
match expression {
Expression::Lambda(e) => self.visit_lambda(ctx, e),
Expression::Reference(e) => self.visit_reference(ctx, e),
Expression::Call(e) => self.visit_call(ctx, e),
Expression::Tuple(e) => self.visit_tuple(ctx, e),
Expression::Number(e) => self.visit_number(ctx, e),
Expression::String(e) => self.visit_string(ctx, e),
Expression::Unit => self.visit_unit(ctx),
Expression::Binary(e) => self.visit_binary(ctx, e),
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_wrapped_expression(ctx, e),
Expression::Identifier(e) => self.visit_identifier(ctx, e),
Expression::Do(e) => self.visit_block_expression(ctx, e),
Expression::Use(e) => self.visit_use_expression(ctx, e),
}
}
fn visit_block(&self, ctx: Builder, script: &Script) -> Result {
script
.statements
.iter()
.fold(Ok(ctx), |ctx, stmt| match stmt {
ast::Statement::If(e) => self.visit_if(ctx?, e),
ast::Statement::For(e) => self.visit_for(ctx?, e),
ast::Statement::Loop(e) => self.visit_loop(ctx?, e),
ast::Statement::While(e) => self.visit_while(ctx?, e),
ast::Statement::Return(e) => self.visit_return(ctx?, e),
ast::Statement::Class(e) => self.visit_class(ctx?, e),
ast::Statement::Function(e) => self.visit_fn(ctx?, e),
ast::Statement::Assignment(e) => self.visit_assignment(ctx?, e),
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),
})
}
}
6 changes: 0 additions & 6 deletions src/code/generator.rs

This file was deleted.

11 changes: 2 additions & 9 deletions src/code/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
mod builder;
mod generator;
pub mod ast_visitor;
pub mod builder;
pub mod macros;
mod visitor;

pub use builder::Builder;
pub use generator::Generator;
pub use visitor::{VisitError, Visitor};

pub trait BuilderVisitor: Visitor<Builder> {}
85 changes: 0 additions & 85 deletions src/code/visitor.rs

This file was deleted.

Empty file added src/lua/helpers.rs
Empty file.
Loading

0 comments on commit 790b520

Please sign in to comment.