From cc21440fc6c20c83a4a3445580727236c4f0046b Mon Sep 17 00:00:00 2001 From: Vedat Can Keklik Date: Sun, 28 Jul 2024 16:58:13 +0300 Subject: [PATCH] fix: More expression handling --- lykiadb-lang/src/ast/visitor.rs | 14 +-- lykiadb-playground/app/editor.tsx | 23 ++++- lykiadb-playground/app/parser.ts | 21 ++++- lykiadb-playground/src/regularizer.rs | 128 ++++++++++++++++++++------ 4 files changed, 142 insertions(+), 44 deletions(-) diff --git a/lykiadb-lang/src/ast/visitor.rs b/lykiadb-lang/src/ast/visitor.rs index a6f73eb..81fbed4 100644 --- a/lykiadb-lang/src/ast/visitor.rs +++ b/lykiadb-lang/src/ast/visitor.rs @@ -17,20 +17,22 @@ pub trait VisitorMut { pub trait SqlVisitor { fn visit_sql_select(&self, e: &SqlSelect) -> Result; - fn visit_sql_select_core(&self, core: &SqlSelectCore) -> Result; - fn visit_sql_subquery(&self, subquery: &SqlCollectionSubquery) -> Result; - fn visit_sql_expr(&self, sql_expr: &SqlExpr) -> Result; fn visit_sql_insert(&self, sql_insert: &SqlInsert) -> Result; fn visit_sql_update(&self, sql_update: &SqlUpdate) -> Result; fn visit_sql_delete(&self, sql_delete: &SqlDelete) -> Result; + // + fn visit_sql_select_core(&self, core: &SqlSelectCore) -> Result; + fn visit_sql_subquery(&self, subquery: &SqlCollectionSubquery) -> Result; + fn visit_sql_expr(&self, sql_expr: &SqlExpr) -> Result; } pub trait SqlVisitorMut { fn visit_sql_select(&mut self, e: &SqlSelect) -> Result; - fn visit_sql_select_core(&mut self, core: &SqlSelectCore) -> Result; - fn visit_sql_subquery(&mut self, subquery: &SqlCollectionSubquery) -> Result; - fn visit_sql_expr(&mut self, sql_expr: &SqlExpr) -> Result; fn visit_sql_insert(&mut self, sql_insert: &SqlInsert) -> Result; fn visit_sql_update(&mut self, sql_update: &SqlUpdate) -> Result; fn visit_sql_delete(&mut self, sql_delete: &SqlDelete) -> Result; + // + fn visit_sql_select_core(&mut self, core: &SqlSelectCore) -> Result; + fn visit_sql_subquery(&mut self, subquery: &SqlCollectionSubquery) -> Result; + fn visit_sql_expr(&mut self, sql_expr: &SqlExpr) -> Result; } diff --git a/lykiadb-playground/app/editor.tsx b/lykiadb-playground/app/editor.tsx index ccf0e12..2aea128 100644 --- a/lykiadb-playground/app/editor.tsx +++ b/lykiadb-playground/app/editor.tsx @@ -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({}); diff --git a/lykiadb-playground/app/parser.ts b/lykiadb-playground/app/parser.ts index 771bc0e..e68c06f 100644 --- a/lykiadb-playground/app/parser.ts +++ b/lykiadb-playground/app/parser.ts @@ -13,7 +13,7 @@ import { Number: t.number, Boolean: t.bool, identifier: t.variableName, - function: t.keyword + function: t.keyword, } as Record export const jsHighlight = styleTags(tgs) @@ -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: () => { diff --git a/lykiadb-playground/src/regularizer.rs b/lykiadb-playground/src/regularizer.rs index f1c4463..5bcc159 100644 --- a/lykiadb-playground/src/regularizer.rs +++ b/lykiadb-playground/src/regularizer.rs @@ -46,28 +46,56 @@ impl<'a> VisitorMut for TreeBuilder { fn visit_expr(&mut self, e: &Expr) -> Result { 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 = 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, @@ -75,11 +103,19 @@ impl<'a> VisitorMut for TreeBuilder { 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, } } @@ -93,9 +129,13 @@ impl<'a> VisitorMut 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, } } @@ -110,9 +150,13 @@ impl<'a> VisitorMut 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, } } @@ -122,11 +166,20 @@ impl<'a> VisitorMut 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, } } @@ -137,21 +190,34 @@ impl<'a> VisitorMut 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, } } @@ -217,9 +283,13 @@ impl<'a> VisitorMut 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, } }