Skip to content

Commit

Permalink
merged fastn-grammar into fastn-type
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 14, 2024
1 parent a64156b commit d8ccd51
Show file tree
Hide file tree
Showing 47 changed files with 248 additions and 297 deletions.
11 changes: 1 addition & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ members = [
"fastn-core",
"fastn-ds",
"fastn-expr",
"fastn-grammar",
"fastn-issues",
"fastn-js",
"fastn-lang",
Expand Down Expand Up @@ -99,7 +98,6 @@ fastn-utils.path = "fastn-utils"
fastn-runtime.path = "fastn-runtime"
fastn-wasm.path = "fastn-wasm"
fbt-lib.path = "fbt_lib"
fastn-grammar.path = "fastn-grammar"
fastn-expr.path = "fastn-expr"
format_num = "0.1"
ftd.path = "ftd"
Expand Down
14 changes: 0 additions & 14 deletions fastn-grammar/Cargo.toml

This file was deleted.

5 changes: 0 additions & 5 deletions fastn-grammar/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion fastn-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ homepage.workspace = true
pretty.workspace = true
itertools.workspace = true
indoc.workspace = true
fastn-grammar.workspace = true
fastn-type.workspace = true
prettify-js.workspace = true
thiserror.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion fastn-js/src/conditional_component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[derive(Debug)]
pub struct ConditionalComponent {
pub deps: Vec<String>,
pub condition: fastn_grammar::evalexpr::ExprNode,
pub condition: fastn_type::evalexpr::ExprNode,
pub statements: Vec<fastn_js::ComponentStatement>,
pub parent: String,
pub should_return: bool,
Expand Down
2 changes: 1 addition & 1 deletion fastn-js/src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Formula {

#[derive(Debug)]
pub struct ConditionalValue {
pub condition: Option<fastn_grammar::evalexpr::ExprNode>,
pub condition: Option<fastn_type::evalexpr::ExprNode>,
pub expression: SetPropertyValue,
}

Expand Down
52 changes: 25 additions & 27 deletions fastn-js/src/to_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,13 +886,13 @@ impl UDFStatement {
pub struct ExpressionGenerator;

impl ExpressionGenerator {
pub fn to_js(&self, node: &fastn_grammar::evalexpr::ExprNode) -> String {
pub fn to_js(&self, node: &fastn_type::evalexpr::ExprNode) -> String {
self.to_js_(node, true, &[], false)
}

pub fn to_js_(
&self,
node: &fastn_grammar::evalexpr::ExprNode,
node: &fastn_type::evalexpr::ExprNode,
root: bool,
arguments: &[(String, Option<String>)],
no_getter: bool,
Expand Down Expand Up @@ -1010,12 +1010,12 @@ impl ExpressionGenerator {
if let Some(mut operator) = self.has_operator(node.operator()) {
// Todo: if node.children().len() != 2 {throw error}
let first = node.children().first().unwrap(); //todo remove unwrap()
if matches!(node.operator(), fastn_grammar::evalexpr::Operator::Not)
|| matches!(node.operator(), fastn_grammar::evalexpr::Operator::Neg)
if matches!(node.operator(), fastn_type::evalexpr::Operator::Not)
|| matches!(node.operator(), fastn_type::evalexpr::Operator::Neg)
{
return [operator, self.to_js_(first, false, arguments, false)].join("");
}
if matches!(node.operator(), fastn_grammar::evalexpr::Operator::Neq) {
if matches!(node.operator(), fastn_type::evalexpr::Operator::Neq) {
// For js conversion
operator = "!==".to_string();
}
Expand Down Expand Up @@ -1073,56 +1073,54 @@ impl ExpressionGenerator {
}
}

pub fn has_value(&self, operator: &fastn_grammar::evalexpr::Operator) -> Option<String> {
pub fn has_value(&self, operator: &fastn_type::evalexpr::Operator) -> Option<String> {
match operator {
fastn_grammar::evalexpr::Operator::Const { .. }
| fastn_grammar::evalexpr::Operator::VariableIdentifierRead { .. }
| fastn_grammar::evalexpr::Operator::VariableIdentifierWrite { .. } => {
fastn_type::evalexpr::Operator::Const { .. }
| fastn_type::evalexpr::Operator::VariableIdentifierRead { .. }
| fastn_type::evalexpr::Operator::VariableIdentifierWrite { .. } => {
Some(operator.to_string())
}
_ => None,
}
}

pub fn has_function(&self, operator: &fastn_grammar::evalexpr::Operator) -> Option<String> {
pub fn has_function(&self, operator: &fastn_type::evalexpr::Operator) -> Option<String> {
match operator {
fastn_grammar::evalexpr::Operator::FunctionIdentifier { .. } => {
Some(operator.to_string())
}
fastn_type::evalexpr::Operator::FunctionIdentifier { .. } => Some(operator.to_string()),
_ => None,
}
}

pub fn is_assignment(&self, operator: &fastn_grammar::evalexpr::Operator) -> bool {
matches!(operator, fastn_grammar::evalexpr::Operator::Assign)
pub fn is_assignment(&self, operator: &fastn_type::evalexpr::Operator) -> bool {
matches!(operator, fastn_type::evalexpr::Operator::Assign)
}

pub fn is_chain(&self, operator: &fastn_grammar::evalexpr::Operator) -> bool {
matches!(operator, fastn_grammar::evalexpr::Operator::Chain)
pub fn is_chain(&self, operator: &fastn_type::evalexpr::Operator) -> bool {
matches!(operator, fastn_type::evalexpr::Operator::Chain)
}

pub fn is_tuple(&self, operator: &fastn_grammar::evalexpr::Operator) -> bool {
matches!(operator, fastn_grammar::evalexpr::Operator::Tuple)
pub fn is_tuple(&self, operator: &fastn_type::evalexpr::Operator) -> bool {
matches!(operator, fastn_type::evalexpr::Operator::Tuple)
}

pub fn is_null(&self, operator: &fastn_grammar::evalexpr::Operator) -> bool {
pub fn is_null(&self, operator: &fastn_type::evalexpr::Operator) -> bool {
matches!(
operator,
fastn_grammar::evalexpr::Operator::Const {
value: fastn_grammar::evalexpr::Value::Empty,
fastn_type::evalexpr::Operator::Const {
value: fastn_type::evalexpr::Value::Empty,
}
)
}

pub fn function_name(&self, operator: &fastn_grammar::evalexpr::Operator) -> Option<String> {
if let fastn_grammar::evalexpr::Operator::FunctionIdentifier { identifier } = operator {
pub fn function_name(&self, operator: &fastn_type::evalexpr::Operator) -> Option<String> {
if let fastn_type::evalexpr::Operator::FunctionIdentifier { identifier } = operator {
Some(identifier.to_string())
} else {
None
}
}

pub fn has_operator(&self, operator: &fastn_grammar::evalexpr::Operator) -> Option<String> {
pub fn has_operator(&self, operator: &fastn_type::evalexpr::Operator) -> Option<String> {
if self.has_value(operator).is_none()
&& self.has_function(operator).is_none()
&& !self.is_chain(operator)
Expand All @@ -1136,8 +1134,8 @@ impl ExpressionGenerator {
}
}

pub fn is_root(&self, operator: &fastn_grammar::evalexpr::Operator) -> bool {
matches!(operator, fastn_grammar::evalexpr::Operator::RootNode)
pub fn is_root(&self, operator: &fastn_type::evalexpr::Operator) -> bool {
matches!(operator, fastn_type::evalexpr::Operator::RootNode)
}
}

Expand Down
4 changes: 2 additions & 2 deletions fastn-js/src/udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ pub struct UDF {
pub name: String,
pub params: Vec<String>,
pub args: Vec<(String, fastn_js::SetPropertyValue)>,
pub body: Vec<fastn_grammar::evalexpr::ExprNode>,
pub body: Vec<fastn_type::evalexpr::ExprNode>,
pub is_external_js_present: bool,
}

pub fn udf_with_arguments(
name: &str,
body: Vec<fastn_grammar::evalexpr::ExprNode>,
body: Vec<fastn_type::evalexpr::ExprNode>,
args: Vec<(String, fastn_js::SetPropertyValue)>,
is_external_js_present: bool,
) -> fastn_js::Ast {
Expand Down
1 change: 0 additions & 1 deletion fastn-type/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ homepage.workspace = true
[dependencies]
serde.workspace = true
# serde_json.workspace = true
fastn-grammar.workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use std::{collections::HashMap, iter};

use fastn_grammar::evalexpr::{
use fastn_type::evalexpr::{
function::Function,
value::{value_type::ValueType, Value},
EvalexprError, EvalexprResult,
Expand Down Expand Up @@ -176,9 +176,9 @@ impl<'a> IterateVariablesContext<'a> for HashMapContext {
/// # Examples
///
/// ```rust
/// use fastn_grammar::evalexpr::*;
/// use fastn_type::evalexpr::*;
///
/// let ctx = fastn_grammar::context_map! {
/// let ctx = fastn_type::context_map! {
/// "x" => 8,
/// "f" => Function::new(|_| Ok(42.into()))
/// }.unwrap(); // Do proper error handling here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[macro_export]
macro_rules! math_consts_context {
() => {
$fastn_grammar::evalexpr::math_consts_context!(
$fastn_type::evalexpr::math_consts_context!(
PI,
TAU,
FRAC_PI_2,
Expand All @@ -27,8 +27,8 @@ macro_rules! math_consts_context {
)
};
($($name:ident),*) => {{
use $fastn_grammar::evalexpr::ContextWithMutableVariables;
$fastn_grammar::evalexpr::context_map! {
use $fastn_type::evalexpr::ContextWithMutableVariables;
$fastn_type::evalexpr::context_map! {
$(
stringify!($name) => core::f64::consts::$name,
)*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::fmt;

use fastn_grammar::evalexpr::EvalexprError;
use fastn_type::evalexpr::EvalexprError;

impl fmt::Display for EvalexprError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use fastn_grammar::evalexpr::EvalexprError::*;
use fastn_type::evalexpr::EvalexprError::*;
match self {
WrongOperatorArgumentAmount { expected, actual } => write!(
f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
//! The module also contains some helper functions starting with `expect_` that check for a condition and return `Err(_)` if the condition is not fulfilled.
//! They are meant as shortcuts to not write the same error checking code everywhere.
use fastn_grammar::evalexpr::{token::PartialToken, value::value_type::ValueType};
use fastn_type::evalexpr::{token::PartialToken, value::value_type::ValueType};

use fastn_grammar::evalexpr::{operator::Operator, value::Value};
use fastn_type::evalexpr::{operator::Operator, value::Value};

// Exclude error display code from test coverage, as the code does not make sense to test.
mod display;
Expand Down Expand Up @@ -368,7 +368,7 @@ pub type EvalexprResult<T> = Result<T, EvalexprError>;

#[cfg(test)]
mod tests {
use fastn_grammar::evalexpr::{EvalexprError, Value, ValueType};
use fastn_type::evalexpr::{EvalexprError, Value, ValueType};

/// Tests whose only use is to bring test coverage of trivial lines up, like trivial constructors.
#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fastn_grammar::evalexpr::{interface::build_operator_tree, ExprNode};
use fastn_type::evalexpr::{interface::build_operator_tree, ExprNode};
use serde::{de, Deserialize, Deserializer};
use std::fmt;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fastn_grammar::evalexpr::{
use fastn_type::evalexpr::{
value::{FloatType, IntType},
EvalexprError, Function, Value, ValueType,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use fastn_grammar::evalexpr::{error::EvalexprResult, value::Value};
use fastn_type::evalexpr::{error::EvalexprResult, value::Value};

pub(crate) mod builtin;

Expand Down Expand Up @@ -30,7 +30,7 @@ where
/// # Examples
///
/// ```rust
/// use fastn_grammar::evalexpr::*;
/// use fastn_type::evalexpr::*;
///
/// let mut context = HashMapContext::new();
/// context.set_function("id".into(), Function::new(|argument| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fastn_grammar::evalexpr::{
use fastn_type::evalexpr::{
token, tree, value::TupleType, Context, ContextWithMutableVariables, EmptyType, EvalexprError,
EvalexprResult, ExprNode, FloatType, HashMapContext, IntType, Value, EMPTY_VALUE,
};
Expand All @@ -8,7 +8,7 @@ use fastn_grammar::evalexpr::{
/// # Examples
///
/// ```rust
/// use fastn_grammar::evalexpr::*;
/// use fastn_type::evalexpr::*;
///
/// assert_eq!(eval("1 + 2 + 3"), Ok(Value::from(6)));
/// ```
Expand All @@ -23,7 +23,7 @@ pub fn eval(string: &str) -> EvalexprResult<Value> {
/// # Examples
///
/// ```rust
/// use fastn_grammar::evalexpr::*;
/// use fastn_type::evalexpr::*;
///
/// let mut context = HashMapContext::new();
/// context.set_value("one".into(), 1.into()).unwrap(); // Do proper error handling here
Expand All @@ -42,7 +42,7 @@ pub fn eval_with_context<C: Context>(string: &str, context: &C) -> EvalexprResul
/// # Examples
///
/// ```rust
/// use fastn_grammar::evalexpr::*;
/// use fastn_type::evalexpr::*;
///
/// let mut context = HashMapContext::new();
/// context.set_value("one".into(), 1.into()).unwrap(); // Do proper error handling here
Expand All @@ -67,7 +67,7 @@ pub fn eval_with_context_mut<C: ContextWithMutableVariables>(
/// # Examples
///
/// ```rust
/// use fastn_grammar::evalexpr::*;
/// use fastn_type::evalexpr::*;
///
/// let precomputed = build_operator_tree("one + two + three").unwrap(); // Do proper error handling here
///
Expand Down
Loading

0 comments on commit d8ccd51

Please sign in to comment.