Skip to content

Commit

Permalink
Merge pull request #13 from lykia-rs/feature/ast-refactor
Browse files Browse the repository at this point in the history
Refactor: AST, Testing, Error handling
  • Loading branch information
can-keklik authored Dec 3, 2023
2 parents a806f61 + 89f774d commit ae29fab
Show file tree
Hide file tree
Showing 24 changed files with 1,109 additions and 671 deletions.
2 changes: 1 addition & 1 deletion examples/fib.ly
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
fun fib($n) {
if ($n < 2) return $n;
return fib($n - 2) + fib($n - 1);
}
};

var $start_ly = clock();
print(fib(35) == 9227465);
Expand Down
2 changes: 1 addition & 1 deletion examples/fn.ly
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fun helloWorld ($message) {
print("outer");
}
}
}
};

for (var $i = 0; $i < 10; $i = $i + 1) {
print(helloWorld("My name is Lykia."));
Expand Down
2 changes: 1 addition & 1 deletion examples/scan_err.ly
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
fun fib($n) {
if ($n < 2) return $n;
return fib($n - 2) + fib($n - 1);
}
};

117E
45 changes: 45 additions & 0 deletions src/lang/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::rc::Rc;

use crate::{lang::token::Token, runtime::types::RV};

use super::{sql::SqlSelect, stmt::StmtId};

#[derive(Debug, Eq, PartialEq)]
pub enum Expr {
Select(SqlSelect),
Variable(Token),
Grouping(ExprId),
Literal(RV),
Function {
name: Option<Token>,
parameters: Vec<Token>,
body: Rc<Vec<StmtId>>,
},
Binary {
left: ExprId,
token: Token,
right: ExprId,
},
Unary {
token: Token,
expr: ExprId,
},
Assignment {
var_tok: Token,
expr: ExprId,
},
Logical {
left: ExprId,
token: Token,
right: ExprId,
},
Call {
callee: ExprId,
paren: Token,
args: Vec<ExprId>,
},
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ExprId(pub usize);
45 changes: 45 additions & 0 deletions src/lang/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use self::{
expr::{Expr, ExprId},
stmt::{Stmt, StmtId},
};

pub mod expr;
pub mod sql;
pub mod stmt;

pub trait Visitor<T, Q> {
fn visit_expr(&mut self, e: ExprId) -> Result<T, Q>;
fn visit_stmt(&mut self, e: StmtId) -> Result<T, Q>;
}

pub struct ParserArena {
expressions: Vec<Expr>,
statements: Vec<Stmt>,
}

impl ParserArena {
pub fn new() -> ParserArena {
ParserArena {
expressions: Vec::new(),
statements: Vec::new(),
}
}

pub fn expression(&mut self, expr: Expr) -> ExprId {
self.expressions.push(expr);
ExprId(self.expressions.len() - 1)
}

pub fn statement(&mut self, stmt: Stmt) -> StmtId {
self.statements.push(stmt);
StmtId(self.statements.len() - 1)
}

pub fn get_expression(&self, idx: ExprId) -> &Expr {
&self.expressions[idx.0]
}

pub fn get_statement(&self, idx: StmtId) -> &Stmt {
&self.statements[idx.0]
}
}
68 changes: 1 addition & 67 deletions src/lang/ast.rs → src/lang/ast/sql.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use crate::lang::token::Token;
use crate::runtime::types::RV;
use std::rc::Rc;

pub trait Visitor<T, Q> {
fn visit_expr(&mut self, e: ExprId) -> Result<T, Q>;
fn visit_stmt(&mut self, e: StmtId) -> Result<T, Q>;
}
use super::expr::ExprId;

#[derive(Debug, Eq, PartialEq)]
pub enum SqlDistinct {
Expand Down Expand Up @@ -95,64 +90,3 @@ pub struct SqlSelect {
pub enum SqlExpr {
Default(ExprId),
}

#[derive(Debug, Eq, PartialEq)]
pub enum Stmt {
Expression(ExprId),
Function(Token, Vec<Token>, Rc<Vec<StmtId>>),
Declaration(Token, ExprId),
Block(Vec<StmtId>),
If(ExprId, StmtId, Option<StmtId>),
Loop(Option<ExprId>, StmtId, Option<StmtId>),
Break(Token),
Continue(Token),
Return(Token, Option<ExprId>),
}

#[derive(Debug, Eq, PartialEq)]
pub enum Expr {
Select(SqlSelect),
Binary(Token, ExprId, ExprId),
Grouping(ExprId),
Literal(RV),
Unary(Token, ExprId),
Variable(Token),
Assignment(Token, ExprId),
Logical(ExprId, Token, ExprId),
Call(ExprId, Token, Vec<ExprId>),
}

pub type ExprId = usize;
pub type StmtId = usize;

pub struct ParserArena {
expressions: Vec<Expr>,
statements: Vec<Stmt>,
}

impl ParserArena {
pub fn new() -> ParserArena {
ParserArena {
expressions: Vec::new(),
statements: Vec::new(),
}
}

pub fn expression(&mut self, expr: Expr) -> ExprId {
self.expressions.push(expr);
self.expressions.len() - 1
}

pub fn statement(&mut self, stmt: Stmt) -> StmtId {
self.statements.push(stmt);
self.statements.len() - 1
}

pub fn get_expression(&self, idx: ExprId) -> &Expr {
&self.expressions[idx]
}

pub fn get_statement(&self, idx: StmtId) -> &Stmt {
&self.statements[idx]
}
}
33 changes: 33 additions & 0 deletions src/lang/ast/stmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::lang::token::Token;

use super::expr::ExprId;

#[derive(Debug, Eq, PartialEq)]
pub enum Stmt {
Expression(ExprId),
Break(Token),
Continue(Token),
Block(Vec<StmtId>),
Declaration {
token: Token,
expr: ExprId,
},
If {
condition: ExprId,
body: StmtId,
r#else: Option<StmtId>,
},
Loop {
condition: Option<ExprId>,
body: StmtId,
post: Option<StmtId>,
},
Return {
token: Token,
expr: Option<ExprId>,
},
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct StmtId(pub usize);
Loading

0 comments on commit ae29fab

Please sign in to comment.