-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from sigmasoldi3r/feat/code-emision-abstraction
wip: code-emission abstraction
- Loading branch information
Showing
11 changed files
with
923 additions
and
1,009 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> {} |
This file was deleted.
Oops, something went wrong.
Empty file.
Oops, something went wrong.