Skip to content

Commit

Permalink
feat: Object literals
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 10, 2023
1 parent a41411a commit 76ce193
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions server/src/lang/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::rc::Rc;

use rustc_hash::FxHashMap;

use self::{
expr::{Expr, ExprId},
stmt::{Stmt, StmtId},
Expand All @@ -14,6 +16,7 @@ pub enum Literal {
Num(f64),
Bool(bool),
Undefined,
Object(FxHashMap<String, ExprId>),
NaN,
Null,
}
Expand Down
19 changes: 19 additions & 0 deletions server/src/lang/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rustc_hash::FxHashMap;
use super::ast::expr::{Expr, ExprId};
use super::ast::sql::{
SelectCore, SqlCompoundOperator, SqlDistinct, SqlExpr, SqlFrom, SqlOrdering, SqlProjection,
Expand Down Expand Up @@ -669,6 +670,24 @@ impl<'a> Parser<'a> {
let tok = self.peek_bw(0);
self.current += 1;
match &tok.tok_type {
TokenType::Symbol(LeftBrace) => {
let mut obj_literal: FxHashMap<String, ExprId> = FxHashMap::default();
while !self.cmp_tok(&sym!(RightBrace)) {
let key = self.expected(Identifier { dollar: false })?.clone();
self.expected(sym!(Colon))?;
let value = self.expression()?;
obj_literal.insert(key.lexeme.unwrap(), value);
if !self.match_next(sym!(Comma)) {
break;
}
}
self.expected(sym!(RightBrace))?;
Ok(self.arena.expression(Expr::Literal {
value: Literal::Object(obj_literal),
raw: "".to_string(),
span: self.get_merged_span(&tok.span, &self.peek_bw(0).span),
}))
}
True => Ok(self.arena.expression(Expr::Literal {
value: Literal::Bool(true),
raw: "true".to_string(),
Expand Down
2 changes: 2 additions & 0 deletions server/src/lang/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::ast::Literal;
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Symbol {
Comma,
Colon,
Semicolon,
LeftParen,
RightParen,
Expand Down Expand Up @@ -168,6 +169,7 @@ pub static SYMBOLS: phf::Map<char, TokenType> = phf_map! {
'{' => sym!(Symbol::LeftBrace),
'}' => sym!(Symbol::RightBrace),
',' => sym!(Symbol::Comma),
':' => sym!(Symbol::Colon),
'.' => sym!(Symbol::Dot),
'-' => sym!(Symbol::Minus),
'+' => sym!(Symbol::Plus),
Expand Down
11 changes: 10 additions & 1 deletion server/src/runtime/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rustc_hash::FxHashMap;

use super::eval::{coerce2number, eval_binary, is_value_truthy};
use super::resolver::Resolver;
use crate::lang::ast::expr::{Expr, ExprId};
Expand Down Expand Up @@ -214,14 +216,21 @@ impl Interpreter {
ret
}

pub fn literal_to_rv(&self, literal: &Literal) -> RV {
pub fn literal_to_rv(&mut self, literal: &Literal) -> RV {
match literal {
Literal::Str(s) => RV::Str(Rc::clone(s)),
Literal::Num(n) => RV::Num(*n),
Literal::Bool(b) => RV::Bool(*b),
Literal::Undefined => RV::Undefined,
Literal::NaN => RV::NaN,
Literal::Null => RV::Null,
Literal::Object(map) => {
let mut new_map = FxHashMap::default();
for (k, v) in map.iter() {
new_map.insert(k.clone(), self.visit_expr(*v).unwrap());
}
RV::Object(new_map)
}
}
}
}
Expand Down

0 comments on commit 76ce193

Please sign in to comment.