-
Notifications
You must be signed in to change notification settings - Fork 0
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 #13 from lykia-rs/feature/ast-refactor
Refactor: AST, Testing, Error handling
- Loading branch information
Showing
24 changed files
with
1,109 additions
and
671 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
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
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 |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
fun fib($n) { | ||
if ($n < 2) return $n; | ||
return fib($n - 2) + fib($n - 1); | ||
} | ||
}; | ||
|
||
117E |
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,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); |
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,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] | ||
} | ||
} |
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
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,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); |
Oops, something went wrong.