Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion core/expression/src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'arena, 'bytecode_ref> CompilerInner<'arena, 'bytecode_ref> {
match node {
Node::Root => Some(vec![FetchFastTarget::Root]),
Node::Identifier(v) => Some(vec![
FetchFastTarget::Root,
FetchFastTarget::Begin,
FetchFastTarget::String(Arc::from(*v)),
]),
Node::Member { node, property } => {
Expand Down Expand Up @@ -171,6 +171,24 @@ impl<'arena, 'bytecode_ref> CompilerInner<'arena, 'bytecode_ref> {
self.emit(Opcode::PushNumber(Decimal::from(v.len())));
Ok(self.emit(Opcode::Object))
}
Node::Assignments { list, output } => {
self.emit(Opcode::AssignedObjectBegin);
list.iter().try_for_each(|&(key, value)| {
self.compile_node(key).map(|_| ())?;
self.compile_node(value).map(|_| ())?;
self.emit(Opcode::AssignedObjectStep);

Ok(())
})?;

if let Some(output) = output {
self.compile_node(output).map(|_| ())?;
}

Ok(self.emit(Opcode::AssignedObjectEnd {
with_return: output.is_some(),
}))
}
Node::Identifier(v) => Ok(self.emit(Opcode::FetchEnv(Arc::from(*v)))),
Node::Closure(v) => self.compile_node(v),
Node::Parenthesized(v) => self.compile_node(v),
Expand Down
3 changes: 3 additions & 0 deletions core/expression/src/compiler/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum CompilerError {

#[error("Invalid method call `{name}`: {message}")]
InvalidMethodCall { name: String, message: String },

#[error("Unexpected assigned object")]
UnexpectedAssignedObject,
}

pub(crate) type CompilerResult<T> = Result<T, CompilerError>;
6 changes: 6 additions & 0 deletions core/expression/src/compiler/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use strum_macros::Display;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FetchFastTarget {
Root,
Begin,
String(Arc<str>),
Number(u32),
}
Expand Down Expand Up @@ -40,6 +41,11 @@ pub enum Opcode {
Slice,
Array,
Object,
AssignedObjectBegin,
AssignedObjectStep,
AssignedObjectEnd {
with_return: bool,
},
Len,
IncrementIt,
IncrementCount,
Expand Down
6 changes: 3 additions & 3 deletions core/expression/src/functions/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::rc::Rc;
pub trait FunctionDefinition {
fn required_parameters(&self) -> usize;
fn optional_parameters(&self) -> usize;
fn check_types(&self, args: &[Rc<VariableType>]) -> FunctionTypecheck;
fn check_types(&self, args: &[VariableType]) -> FunctionTypecheck;
fn call(&self, args: Arguments) -> anyhow::Result<Variable>;
fn param_type(&self, index: usize) -> Option<VariableType>;
fn param_type_str(&self, index: usize) -> String;
Expand Down Expand Up @@ -52,7 +52,7 @@ impl FunctionDefinition for StaticFunction {
0
}

fn check_types(&self, args: &[Rc<VariableType>]) -> FunctionTypecheck {
fn check_types(&self, args: &[VariableType]) -> FunctionTypecheck {
let mut typecheck = FunctionTypecheck::default();
typecheck.return_type = self.signature.return_type.clone();

Expand Down Expand Up @@ -135,7 +135,7 @@ impl FunctionDefinition for CompositeFunction {
max - required_params
}

fn check_types(&self, args: &[Rc<VariableType>]) -> FunctionTypecheck {
fn check_types(&self, args: &[VariableType]) -> FunctionTypecheck {
let mut typecheck = FunctionTypecheck::default();
if self.signatures.is_empty() {
typecheck.general = Some("No implementation".to_string());
Expand Down
19 changes: 9 additions & 10 deletions core/expression/src/intellisense/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::parser::{Node, Parser};
use crate::variable::VariableType;
use serde::Serialize;
use std::cell::RefCell;
use std::rc::Rc;

mod scope;
mod types;
Expand All @@ -15,7 +14,7 @@ mod types;
#[serde(rename_all = "camelCase")]
pub struct IntelliSenseToken {
pub span: (u32, u32),
pub kind: Rc<VariableType>,
pub kind: VariableType,
pub node_kind: &'static str,
pub error: Option<String>,
}
Expand Down Expand Up @@ -50,9 +49,9 @@ impl<'arena> IntelliSense<'arena> {
let type_data = TypesProvider::generate(
ast,
IntelliSenseScope {
pointer_data: data,
root_data: data,
current_data: data,
pointer_data: data.shallow_clone(),
root_data: data.shallow_clone(),
current_data: data.shallow_clone(),
},
);

Expand All @@ -71,7 +70,7 @@ impl<'arena> IntelliSense<'arena> {
error: typ.map(|t| t.error.clone()).flatten(),
kind: typ
.map(|t| t.kind.clone())
.unwrap_or_else(|| Rc::new(VariableType::Any)),
.unwrap_or_else(|| VariableType::Any),
});
});

Expand All @@ -96,9 +95,9 @@ impl<'arena> IntelliSense<'arena> {
let type_data = TypesProvider::generate(
ast,
IntelliSenseScope {
pointer_data: data,
root_data: data,
current_data: data,
pointer_data: data.shallow_clone(),
root_data: data.shallow_clone(),
current_data: data.shallow_clone(),
},
);

Expand All @@ -114,7 +113,7 @@ impl<'arena> IntelliSense<'arena> {
error: typ.map(|t| t.error.clone()).flatten(),
kind: typ
.map(|t| t.kind.clone())
.unwrap_or_else(|| Rc::new(VariableType::Any)),
.unwrap_or_else(|| VariableType::Any),
});
});

Expand Down
8 changes: 4 additions & 4 deletions core/expression/src/intellisense/scope.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::variable::VariableType;

#[derive(Clone, Debug)]
pub struct IntelliSenseScope<'a> {
pub root_data: &'a VariableType,
pub current_data: &'a VariableType,
pub pointer_data: &'a VariableType,
pub struct IntelliSenseScope {
pub root_data: VariableType,
pub current_data: VariableType,
pub pointer_data: VariableType,
}
65 changes: 49 additions & 16 deletions core/expression/src/intellisense/types/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::intellisense::types::type_info::TypeInfo;
use crate::lexer::{ArithmeticOperator, ComparisonOperator, LogicalOperator, Operator};
use crate::parser::Node;
use crate::variable::VariableType;
use std::cell::RefCell;
use std::collections::HashMap;
use std::iter::once;
use std::ops::Deref;
Expand Down Expand Up @@ -52,14 +53,14 @@ impl TypesProvider {
}

#[cfg_attr(feature = "stack-protection", recursive::recursive)]
fn determine(&mut self, node: &Node, scope: IntelliSenseScope) -> TypeInfo {
fn determine(&mut self, node: &Node, mut scope: IntelliSenseScope) -> TypeInfo {
#[allow(non_snake_case)]
let V = |vt: VariableType| TypeInfo::from(vt);
#[allow(non_snake_case)]
let Const = |v: &str| TypeInfo::from(VariableType::Const(Rc::from(v)));
#[allow(non_snake_case)]
let Error = |error: String| TypeInfo {
kind: Rc::from(VariableType::Any),
kind: VariableType::Any,
error: Some(error),
};

Expand Down Expand Up @@ -111,15 +112,15 @@ impl TypesProvider {
}

Node::Array(items) => {
let mut type_list: Vec<Rc<VariableType>> = items
let mut type_list: Vec<VariableType> = items
.iter()
.map(|n| self.determine(n, scope.clone()).kind)
.collect();
let first = type_list.pop();
let all_same = type_list.iter().all(|t| Some(t) == first.as_ref());

match (first, all_same) {
(Some(typ), true) => V(VariableType::Array(typ)),
(Some(typ), true) => V(VariableType::Array(Rc::new(typ))),
_ => V(VariableType::Array(Rc::new(VariableType::Any))),
}
}
Expand All @@ -136,14 +137,41 @@ impl TypesProvider {
})
.collect();

V(VariableType::Object(obj_type))
V(VariableType::Object(Rc::new(RefCell::new(obj_type))))
}

Node::Assignments { list, output } => {
let obj_type: HashMap<Rc<str>, VariableType> = list
.iter()
.filter_map(|(k, v)| {
let key_type = self.determine(k, scope.clone()).as_const_str()?;
let value_type = self.determine(v, scope.clone());

if let Some(new_var) = scope
.root_data
.dot_insert_detached(key_type.as_ref(), value_type.kind.shallow_clone())
{
println!("NewVar: {new_var:?}");
scope.root_data = new_var;
};

Some((key_type, value_type.kind))
})
.collect();

if let Some(output) = output {
self.determine(output, scope.clone())
} else {
V(VariableType::Object(Rc::new(RefCell::new(obj_type))))
}
}

Node::Identifier(i) => TypeInfo::from(scope.root_data.get(i)),
Node::Member { node, property } => {
let node_type = self.determine(node, scope.clone());
let property_type = self.determine(property, scope.clone());

match node_type.kind.as_ref() {
match &node_type.kind {
VariableType::Any => V(VariableType::Any),
VariableType::Null => V(VariableType::Null),
VariableType::Array(inner) => {
Expand All @@ -164,10 +192,11 @@ impl TypesProvider {
);
}

let obj = obj.borrow();
match property_type.as_const_str() {
None => V(VariableType::Any),
Some(key) => TypeInfo::from(
obj.get(&key).cloned().unwrap_or(Rc::new(VariableType::Any)),
obj.get(&key).cloned().unwrap_or(VariableType::Any),
),
}
}
Expand Down Expand Up @@ -287,7 +316,7 @@ impl TypesProvider {
let true_type = self.determine(on_true, scope.clone());
let false_type = self.determine(on_false, scope.clone());

V(true_type.kind.merge(false_type.kind.as_ref()))
V(true_type.kind.merge(&false_type.kind))
}
Node::Unary { node, operator } => {
let node_type = self.determine(node, scope.clone());
Expand Down Expand Up @@ -325,7 +354,9 @@ impl TypesProvider {
| Operator::Comma
| Operator::Slice
| Operator::Dot
| Operator::QuestionMark => Error("Unsupported operator".to_string()),
| Operator::QuestionMark
| Operator::Assign
| Operator::Semi => Error("Unsupported operator".to_string()),
}
}
Node::Interval { left, right, .. } => {
Expand All @@ -352,7 +383,7 @@ impl TypesProvider {
V(VariableType::Interval)
}
Node::FunctionCall { arguments, kind } => {
let mut type_list: Vec<Rc<VariableType>> = arguments
let mut type_list: Vec<VariableType> = arguments
.iter()
.map(|n| self.determine(n, scope.clone()).kind)
.collect();
Expand All @@ -362,7 +393,7 @@ impl TypesProvider {
let new_type = self.determine(
arguments[1],
IntelliSenseScope {
pointer_data: &ptr_type,
pointer_data: ptr_type.deref().clone(),
current_data: scope.current_data,
root_data: scope.root_data,
},
Expand All @@ -383,7 +414,7 @@ impl TypesProvider {
}

TypeInfo {
kind: Rc::new(typecheck.return_type),
kind: typecheck.return_type,
error: typecheck.general,
}
}
Expand Down Expand Up @@ -423,7 +454,9 @@ impl TypesProvider {
ClosureFunction::One => V(VariableType::Bool),
ClosureFunction::Filter => TypeInfo::from(type_list[0].clone()),
ClosureFunction::Count => V(VariableType::Number),
ClosureFunction::Map => V(VariableType::Array(type_list[1].clone())),
ClosureFunction::Map => {
V(VariableType::Array(Rc::new(type_list[1].clone())))
}
ClosureFunction::FlatMap => V(VariableType::Any),
}
}
Expand All @@ -435,7 +468,7 @@ impl TypesProvider {
kind,
} => {
let this_type = self.determine(this, scope.clone());
let type_list: Vec<Rc<VariableType>> = once(this_type.kind)
let type_list: Vec<VariableType> = once(this_type.kind)
.chain(
arguments
.iter()
Expand All @@ -457,15 +490,15 @@ impl TypesProvider {
}

TypeInfo {
kind: Rc::new(typecheck.return_type),
kind: typecheck.return_type,
error: typecheck.general,
}
}
Node::Closure(c) => self.determine(c, scope.clone()),
Node::Parenthesized(c) => self.determine(c, scope.clone()),
Node::Error { node, error } => match node {
None => TypeInfo {
kind: Rc::new(VariableType::Any),
kind: VariableType::Any,
error: Some(error.to_string()),
},
Some(n) => {
Expand Down
8 changes: 4 additions & 4 deletions core/expression/src/intellisense/types/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::rc::Rc;

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TypeInfo {
pub(crate) kind: Rc<VariableType>,
pub(crate) kind: VariableType,
pub(crate) error: Option<String>,
}

Expand All @@ -26,7 +26,7 @@ impl Display for TypeInfo {
impl Default for TypeInfo {
fn default() -> Self {
Self {
kind: Rc::new(VariableType::Any),
kind: VariableType::Any,
error: None,
}
}
Expand All @@ -35,7 +35,7 @@ impl Default for TypeInfo {
impl From<VariableType> for TypeInfo {
fn from(value: VariableType) -> Self {
Self {
kind: Rc::new(value),
kind: value,
error: None,
}
}
Expand All @@ -44,7 +44,7 @@ impl From<VariableType> for TypeInfo {
impl From<Rc<VariableType>> for TypeInfo {
fn from(value: Rc<VariableType>) -> Self {
Self {
kind: value,
kind: value.deref().clone(),
error: None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/expression/src/lexer/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ macro_rules! token_type {
("space") => { ' ' | '\n' | '\t' };
("digit") => { '0'..='9' };
("bracket") => { '(' | ')' | '[' | ']' | '{' | '}' };
("cmp_operator") => { '>' | '<' | '!' | '=' };
("cmp_operator") => { '>' | '<' | '!' };
("operator") => { ',' | ':' | '+' | '-' | '/' | '*' | '^' | '%' };
("alpha") => { 'A'..='Z' | 'a'..='z' | '$' | '_' | '#' };
("alphanumeric") => { 'A'..='Z' | 'a'..='z' | '0'..='9' | '$' | '_' | '#' };
Expand Down
Loading
Loading