Skip to content

Commit

Permalink
fix: Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Jan 8, 2024
1 parent 5b8c63b commit 1bce7b6
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 70 deletions.
5 changes: 4 additions & 1 deletion server/src/lang/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use serde::Serialize;
use std::rc::Rc;

use crate::lang::{tokens::token::{Span, Spanned}, Identifier};
use crate::lang::{
tokens::token::{Span, Spanned},
Identifier,
};

use super::{
sql::{SqlDelete, SqlInsert, SqlSelect, SqlUpdate},
Expand Down
41 changes: 22 additions & 19 deletions server/src/lang/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ impl<'a> Parser<'a> {

Ok(self.arena.alloc_expression(Expr::Function {
name: token.map(|t| t.extract_identifier().unwrap()),
parameters: parameters.iter().map(|t| t.extract_identifier().unwrap()).collect(),
parameters: parameters
.iter()
.map(|t| t.extract_identifier().unwrap())
.collect(),
body: Rc::new(body),
span: self.get_merged_span(
&fun_tok.span,
Expand All @@ -383,25 +386,17 @@ impl<'a> Parser<'a> {
return Ok(self.arena.alloc_expression(Expr::Assignment {
dst: name.clone(),
expr: value,
span: self.get_merged_span(
span,
&self.arena.get_expression(value).get_span(),
),
span: self
.get_merged_span(span, &self.arena.get_expression(value).get_span()),
}));
}
Expr::Get {
object,
name,
span,
} => {
Expr::Get { object, name, span } => {
return Ok(self.arena.alloc_expression(Expr::Set {
object: *object,
name: name.clone(),
value,
span: self.get_merged_span(
span,
&self.arena.get_expression(value).get_span(),
),
span: self
.get_merged_span(span, &self.arena.get_expression(value).get_span()),
}));
}
_ => {
Expand Down Expand Up @@ -513,13 +508,18 @@ impl<'a> Parser<'a> {
return Ok(Some(SqlCollectionIdentifier {
namespace: Some(self.peek_bw(3).extract_identifier().unwrap()),
name: self.peek_bw(1).extract_identifier().unwrap(),
alias: optional_with_expected!(self, skw!(As), Identifier { dollar: false }).map(|t| t.extract_identifier().unwrap()),
alias: optional_with_expected!(self, skw!(As), Identifier { dollar: false })
.map(|t| t.extract_identifier().unwrap()),
}));
}
return Ok(Some(SqlCollectionIdentifier {
namespace: None,
name: self.expected(Identifier { dollar: false })?.extract_identifier().unwrap(),
alias: optional_with_expected!(self, skw!(As), Identifier { dollar: false }).map(|t| t.extract_identifier().unwrap()),
name: self
.expected(Identifier { dollar: false })?
.extract_identifier()
.unwrap(),
alias: optional_with_expected!(self, skw!(As), Identifier { dollar: false })
.map(|t| t.extract_identifier().unwrap()),
}));
}
Ok(None)
Expand Down Expand Up @@ -804,7 +804,7 @@ impl<'a> Parser<'a> {
optional_with_expected!(self, skw!(As), Identifier { dollar: false });
projections.push(SqlProjection::Expr {
expr: SqlExpr::Default(expr),
alias: alias.map(|t| t.extract_identifier().unwrap())
alias: alias.map(|t| t.extract_identifier().unwrap()),
});
}
if !self.match_next(sym!(Comma)) {
Expand Down Expand Up @@ -886,7 +886,10 @@ impl<'a> Parser<'a> {
self.expected(sym!(RightParen))?; // closing paren
let alias: Option<Token> =
optional_with_expected!(self, skw!(As), Identifier { dollar: false });
return Ok(SqlCollectionSubquery::Select { expr, alias: alias.map(|t| t.extract_identifier().unwrap() ) });
return Ok(SqlCollectionSubquery::Select {
expr,
alias: alias.map(|t| t.extract_identifier().unwrap()),
});
}
// If the next token is a left paren, then it must be either a select statement or a recursive subquery
let parsed = self.sql_select_subquery_join()?; // TODO(vck): Check if using _collection variant makes sense.
Expand Down
10 changes: 8 additions & 2 deletions server/src/lang/ast/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ pub enum SqlProjection {
#[serde(rename = "SqlProjection::All")]
All { collection: Option<Identifier> },
#[serde(rename = "SqlProjection::Expr")]
Expr { expr: SqlExpr, alias: Option<Identifier> },
Expr {
expr: SqlExpr,
alias: Option<Identifier>,
},
}

#[derive(Debug, Eq, PartialEq, Serialize)]
Expand Down Expand Up @@ -118,7 +121,10 @@ pub enum SqlCollectionSubquery {
#[serde(rename = "SqlCollectionSubquery::Collection")]
Collection(SqlCollectionIdentifier),
#[serde(rename = "SqlCollectionSubquery::Select")]
Select { expr: ExprId, alias: Option<Identifier> },
Select {
expr: ExprId,
alias: Option<Identifier>,
},
}

#[derive(Debug, Eq, PartialEq, Serialize)]
Expand Down
5 changes: 4 additions & 1 deletion server/src/lang/ast/stmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use serde::Serialize;

use crate::lang::{tokens::token::{Span, Spanned}, Identifier};
use crate::lang::{
tokens::token::{Span, Spanned},
Identifier,
};

use super::expr::ExprId;

Expand Down
2 changes: 1 addition & 1 deletion server/src/lang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ impl Eq for Literal {}
pub struct Identifier {
pub name: String,
pub dollar: bool,
}
}
2 changes: 1 addition & 1 deletion server/src/lang/tokens/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<'a> Scanner<'a> {
Ok(Token {
tok_type: Identifier {
dollar: literal.starts_with('$'),
},
},
literal: Some(Str(literal)),
lexeme: Some(raw_str),
span,
Expand Down
2 changes: 1 addition & 1 deletion server/src/lang/tokens/token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::lang::{Literal, Identifier};
use crate::lang::{Identifier, Literal};
use phf::phf_map;
use serde::Serialize;

Expand Down
52 changes: 18 additions & 34 deletions server/src/runtime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,23 +273,18 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
right,
span: _,
} => self.eval_binary(*left, *right, *operation),
Expr::Variable { name, span: _ } => {
self.look_up_variable(&name.name, eidx)
}
Expr::Variable { name, span: _ } => self.look_up_variable(&name.name, eidx),
Expr::Assignment { dst, expr, span: _ } => {
let distance = self.resolver.get_distance(eidx);
let evaluated = self.visit_expr(*expr)?;
let result = if let Some(distance_unv) = distance {
self.env.borrow_mut().assign_at(
distance_unv,
&dst.name,
evaluated.clone(),
)
self.env
.borrow_mut()
.assign_at(distance_unv, &dst.name, evaluated.clone())
} else {
self.root_env.borrow_mut().assign(
dst.name.clone(),
evaluated.clone(),
)
self.root_env
.borrow_mut()
.assign(dst.name.clone(), evaluated.clone())
};
if result.is_err() {
return Err(result.err().unwrap());
Expand Down Expand Up @@ -352,32 +347,24 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
span: _,
} => {
let fn_name = if name.is_some() {
&name.as_ref()
.unwrap()
.name
&name.as_ref().unwrap().name
} else {
"<anonymous>"
};
let fun = Function::UserDefined {
name: fn_name.to_string(),
body: Rc::clone(body),
parameters: parameters
.iter()
.map(|x| x.name.to_string())
.collect(),
parameters: parameters.iter().map(|x| x.name.to_string()).collect(),
closure: self.env.clone(),
};

let callable = Callable(Some(parameters.len()), fun.into());

if name.is_some() {
// TODO(vck): Callable shouldn't be cloned here
self.env.borrow_mut().declare(
name.as_ref()
.unwrap()
.name.to_string(),
callable.clone(),
);
self.env
.borrow_mut()
.declare(name.as_ref().unwrap().name.to_string(), callable.clone());
}

Ok(callable)
Expand All @@ -392,7 +379,7 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
}
Err(HaltReason::Error(InterpretError::PropertyNotFound {
span: *span,
property: name.name.to_string()
property: name.name.to_string(),
}))
} else {
Err(HaltReason::Error(InterpretError::Other {
Expand All @@ -412,10 +399,8 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
let object_eval = self.visit_expr(*object)?;
if let RV::Object(map) = object_eval {
let evaluated = self.visit_expr(*value)?;
map.borrow_mut().insert(
name.name.to_string(),
evaluated.clone(),
);
map.borrow_mut()
.insert(name.name.to_string(), evaluated.clone());
Ok(evaluated)
} else {
Err(HaltReason::Error(InterpretError::Other {
Expand Down Expand Up @@ -450,10 +435,9 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
}
Stmt::Declaration { dst, expr, span: _ } => {
let evaluated = self.visit_expr(*expr)?;
self.env.borrow_mut().declare(
dst.name.to_string(),
evaluated.clone(),
);
self.env
.borrow_mut()
.declare(dst.name.to_string(), evaluated.clone());
}
Stmt::Block {
body: stmts,
Expand Down
5 changes: 4 additions & 1 deletion server/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ impl Runtime {
pub fn print_ast(&mut self, source: &str) -> Result<(), ParseError> {
let tokens = Scanner::scan(source).unwrap();
let program = Parser::parse(&tokens)?;
println!("{}", serde_json::to_string_pretty(&program.to_json()).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&program.to_json()).unwrap()
);
Ok(())
}

Expand Down
12 changes: 3 additions & 9 deletions server/src/runtime/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::lang::ast::program::AstArena;
use crate::lang::ast::stmt::{Stmt, StmtId};
use crate::lang::ast::visitor::VisitorMut;
use crate::lang::tokens::token::Span;
use crate::lang::{Literal, Identifier};
use crate::lang::{Identifier, Literal};
use crate::runtime::types::RV;
use rustc_hash::FxHashMap;
use std::rc::Rc;
Expand Down Expand Up @@ -68,21 +68,15 @@ impl Resolver {
return;
}
let last = self.scopes.last_mut();
last.unwrap().insert(
name.name.to_string(),
false,
);
last.unwrap().insert(name.name.to_string(), false);
}

pub fn define(&mut self, name: &Identifier) {
if self.scopes.is_empty() {
return;
}
let last = self.scopes.last_mut();
last.unwrap().insert(
name.name.to_string(),
true,
);
last.unwrap().insert(name.name.to_string(), true);
}
}

Expand Down

0 comments on commit 1bce7b6

Please sign in to comment.