Skip to content

Commit

Permalink
fix: More expression handling
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Jul 28, 2024
1 parent a30c2a4 commit cc21440
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 44 deletions.
14 changes: 8 additions & 6 deletions lykiadb-lang/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ pub trait VisitorMut<O, E> {

pub trait SqlVisitor<T, Q> {
fn visit_sql_select(&self, e: &SqlSelect) -> Result<T, Q>;
fn visit_sql_select_core(&self, core: &SqlSelectCore) -> Result<T, Q>;
fn visit_sql_subquery(&self, subquery: &SqlCollectionSubquery) -> Result<T, Q>;
fn visit_sql_expr(&self, sql_expr: &SqlExpr) -> Result<T, Q>;
fn visit_sql_insert(&self, sql_insert: &SqlInsert) -> Result<T, Q>;
fn visit_sql_update(&self, sql_update: &SqlUpdate) -> Result<T, Q>;
fn visit_sql_delete(&self, sql_delete: &SqlDelete) -> Result<T, Q>;
//
fn visit_sql_select_core(&self, core: &SqlSelectCore) -> Result<T, Q>;
fn visit_sql_subquery(&self, subquery: &SqlCollectionSubquery) -> Result<T, Q>;
fn visit_sql_expr(&self, sql_expr: &SqlExpr) -> Result<T, Q>;
}

pub trait SqlVisitorMut<T, Q> {
fn visit_sql_select(&mut self, e: &SqlSelect) -> Result<T, Q>;
fn visit_sql_select_core(&mut self, core: &SqlSelectCore) -> Result<T, Q>;
fn visit_sql_subquery(&mut self, subquery: &SqlCollectionSubquery) -> Result<T, Q>;
fn visit_sql_expr(&mut self, sql_expr: &SqlExpr) -> Result<T, Q>;
fn visit_sql_insert(&mut self, sql_insert: &SqlInsert) -> Result<T, Q>;
fn visit_sql_update(&mut self, sql_update: &SqlUpdate) -> Result<T, Q>;
fn visit_sql_delete(&mut self, sql_delete: &SqlDelete) -> Result<T, Q>;
//
fn visit_sql_select_core(&mut self, core: &SqlSelectCore) -> Result<T, Q>;
fn visit_sql_subquery(&mut self, subquery: &SqlCollectionSubquery) -> Result<T, Q>;
fn visit_sql_expr(&mut self, sql_expr: &SqlExpr) -> Result<T, Q>;
}
23 changes: 19 additions & 4 deletions lykiadb-playground/app/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@ await init();

const EditorView = () => {
const [code, setCode] = React.useState(
`function sum($a, $b) {
return $a + $b;
};`
);
`var $calc = {
add: function ($a, $b) {
return $a + $b;
},
sub: function ($a, $b) {
return $a - $b;
},
mul: function ($a, $b) {
return $a * $b;
},
div: function ($a, $b) {
return $a / $b;
},
};
print($calc.add(4, 5));
print($calc.sub(4, 5));
print($calc.mul(4, 5));
print($calc.div(4, 5));
`);

const [ast, setAst] = React.useState({});

Expand Down
21 changes: 16 additions & 5 deletions lykiadb-playground/app/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Number: t.number,
Boolean: t.bool,
identifier: t.variableName,
function: t.keyword
function: t.keyword,
} as Record<string, any>

export const jsHighlight = styleTags(tgs)
Expand Down Expand Up @@ -51,15 +51,26 @@ import {
let parsed = null;
try {
parsed = this.parseFn(doc);
if (!parsed?.span) {
return Tree.empty;
}
const tr = new Tree(
NodeType.define({
id: 0,
name: "_root",
top: false,
props: [jsHighlight],
}),
[convertParsedToLezer(parsed)],
[parsed.span.start],
parsed.span.end
);
return tr
}
catch (e) {
console.error(e);
return Tree.empty;
}

const tr = convertParsedToLezer(parsed);
console.log(tr)
return tr
},
parsedPos: input.length,
stopAt: () => {
Expand Down
128 changes: 99 additions & 29 deletions lykiadb-playground/src/regularizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,76 @@ impl<'a> VisitorMut<Tree, ()> for TreeBuilder {
fn visit_expr(&mut self, e: &Expr) -> Result<Tree, ()> {
let tree = match e {
Expr::Literal {
raw,
raw: _,
span,
value,
id,
} => Tree {
name: match value {
Literal::Str(_) => "String".to_string(),
Literal::Num(_) => "Number".to_string(),
Literal::Bool(_) => "Boolean".to_string(),
Literal::Array(_) => "Array".to_string(),
Literal::Object(_) => "Object".to_string(),
Literal::Null => "Null".to_string(),
Literal::Undefined => "Undefined".to_string(),
Literal::NaN => "NaN".to_string(),
},
children: self.get_children(*id),
span: *span,
} => {
let mut children: Vec<Tree> = vec![];
self.get_children(*id).map(|c| children.extend(c));
Tree {
name: match value {
Literal::Str(_) => "String".to_string(),
Literal::Num(_) => "Number".to_string(),
Literal::Bool(_) => "Boolean".to_string(),
Literal::Array(exprs) => {
for expr in exprs {
children.push(self.visit_expr(expr)?);
}
"Array".to_string()
},
Literal::Object(exprs) => {
for (key, value) in exprs {
// children.push(self.visit_expr(key)?);
children.push(self.visit_expr(value)?);
}
"Object".to_string()
},
Literal::Null => "Null".to_string(),
Literal::Undefined => "Undefined".to_string(),
Literal::NaN => "NaN".to_string(),
},
children: Some(children),
span: *span,
}
},
Expr::Variable { name, span, id } => Tree {
name: name.name.clone(),
children: self.get_children(*id),
span: *span,
Expr::Variable { name, span, id } => {
let mut children = vec![
Tree {
name: "identifier".to_string(),
children: None,
span: name.span,
},
];
let token_children = self.get_children(*id);
if let Some(c) = token_children {
children.extend(c);
}
Tree {
name: "Variable".to_owned(),
children: Some(children),
span: *span,
}
},
Expr::Assignment {
dst,
expr,
span,
id,
} => {
let mut children = vec![];
let mut children = vec![Tree {
name: "identifier".to_string(),
children: None,
span: dst.span,
}];
children.push(self.visit_expr(expr)?);
let token_children = self.get_children(*id);
if let Some(c) = token_children {
children.extend(c);
}
Tree {
name: dst.name.clone(),
children: self.get_children(*id),
name: "Assignment".to_string(),
children: Some(children),
span: *span,
}
}
Expand All @@ -93,9 +129,13 @@ impl<'a> VisitorMut<Tree, ()> for TreeBuilder {
let mut children = vec![];
children.push(self.visit_expr(left)?);
children.push(self.visit_expr(right)?);
let token_children = self.get_children(*id);
if let Some(c) = token_children {
children.extend(c);
}
Tree {
name: format!("{:?}", operation),
children: self.get_children(*id),
children: Some(children),
span: *span,
}
}
Expand All @@ -110,9 +150,13 @@ impl<'a> VisitorMut<Tree, ()> for TreeBuilder {
for arg in args {
children.push(self.visit_expr(arg)?);
}
let token_children = self.get_children(*id);
if let Some(c) = token_children {
children.extend(c);
}
Tree {
name: "Call".to_string(),
children: self.get_children(*id),
children: Some(children),
span: *span,
}
}
Expand All @@ -122,11 +166,20 @@ impl<'a> VisitorMut<Tree, ()> for TreeBuilder {
span,
id,
} => {
let mut children = vec![];
let mut children = vec![
Tree {
name: "identifier".to_string(),
children: None,
span: name.span,
}];
children.push(self.visit_expr(object)?);
let token_children = self.get_children(*id);
if let Some(c) = token_children {
children.extend(c);
}
Tree {
name: name.name.clone(),
children: self.get_children(*id),
children: Some(children),
span: *span,
}
}
Expand All @@ -137,21 +190,34 @@ impl<'a> VisitorMut<Tree, ()> for TreeBuilder {
span,
id,
} => {
let mut children = vec![];
let mut children = vec![
Tree {
name: "identifier".to_string(),
children: None,
span: name.span,
}];
children.push(self.visit_expr(object)?);
children.push(self.visit_expr(value)?);
let token_children = self.get_children(*id);
if let Some(c) = token_children {
children.extend(c);
}
Tree {
name: name.name.clone(),
children: self.get_children(*id),
children: Some(children),
span: *span,
}
}
Expr::Grouping { expr, span, id } => {
let mut children = vec![];
children.push(self.visit_expr(expr)?);
let token_children = self.get_children(*id);
if let Some(c) = token_children {
children.extend(c);
}
Tree {
name: "Grouping".to_string(),
children: self.get_children(*id),
children: Some(children),
span: *span,
}
}
Expand Down Expand Up @@ -217,9 +283,13 @@ impl<'a> VisitorMut<Tree, ()> for TreeBuilder {
} => {
let mut children = vec![];
children.push(self.visit_expr(expr)?);
let token_children = self.get_children(*id);
if let Some(c) = token_children {
children.extend(c);
}
Tree {
name: format!("{:?}", operation),
children: self.get_children(*id),
children: Some(children),
span: *span,
}
}
Expand Down

0 comments on commit cc21440

Please sign in to comment.