Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature: dot operator support #151

Closed
wants to merge 3 commits into from
Closed
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
51 changes: 51 additions & 0 deletions src/operator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::vec;

use crate::function::builtin::builtin_function;

use crate::{context::Context, error::*, value::Value, ContextWithMutableVariables};
Expand Down Expand Up @@ -446,6 +448,25 @@
VariableIdentifierRead { identifier } => {
expect_operator_argument_amount(arguments.len(), 0)?;

// object.attribute
if is_valid_start(identifier) {
if let Some((id, attribute)) = identifier.split_once('.') {
if is_valid_start(id) && is_valid_start(attribute) {
let object = match context.get_value(id) {
Some(x) => Ok(x),
None => Err(EvalexprError::VariableIdentifierNotFound(id.into())),
}?;
let params = vec![
object.clone(),
Value::String(attribute.into()),
Value::Empty,
];
// dot(object, "attribute", empty)
return context.call_function("dot", &Value::Tuple(params));
}
}
}

if let Some(value) = context.get_value(identifier).cloned() {
Ok(value)
} else {
Expand All @@ -458,6 +479,23 @@
expect_operator_argument_amount(arguments.len(), 1)?;
let arguments = &arguments[0];

// object.method()
if let Some((id, method)) = identifier.split_once('.') {
if is_valid_start(id) && is_valid_start(method) {
let object = match context.get_value(id) {
Some(x) => Ok(x),
None => Err(EvalexprError::VariableIdentifierNotFound(id.into())),
}?;
let params = vec![
object.clone(),
Value::String(method.into()),
arguments.clone(),
];
// dot(object, "method", arguments)
return context.call_function("dot", &Value::Tuple(params));
}
}

match context.call_function(identifier, arguments) {
Err(EvalexprError::FunctionIdentifierNotFound(_))
if !context.are_builtin_functions_disabled() =>
Expand Down Expand Up @@ -524,3 +562,16 @@
}
}
}

fn is_valid_start(id: &str) -> bool {
match id.chars().next() {
Some(c) => ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'),

Check failure on line 568 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, stable)

manual `RangeInclusive::contains` implementation

Check failure on line 568 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, stable)

manual `RangeInclusive::contains` implementation

Check failure on line 568 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, nightly)

manual `RangeInclusive::contains` implementation

Check failure on line 568 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, nightly)

manual `RangeInclusive::contains` implementation

Check failure on line 568 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, beta)

manual `RangeInclusive::contains` implementation

Check failure on line 568 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, beta)

manual `RangeInclusive::contains` implementation
None => false,
}
}

#[test]
fn test_is_valid_identifier() {
assert_eq!(is_valid_start("abc123"), true);

Check failure on line 575 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, stable)

used `assert_eq!` with a literal bool

Check failure on line 575 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, nightly)

used `assert_eq!` with a literal bool

Check failure on line 575 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, beta)

used `assert_eq!` with a literal bool
assert_eq!(is_valid_start("123abc"), false);

Check failure on line 576 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, stable)

used `assert_eq!` with a literal bool

Check failure on line 576 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, nightly)

used `assert_eq!` with a literal bool

Check failure on line 576 in src/operator/mod.rs

View workflow job for this annotation

GitHub Actions / Check, test, doc, format and lint with all features (ubuntu-latest, beta)

used `assert_eq!` with a literal bool
}
144 changes: 144 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,150 @@ fn test_with_context() {
);
}

#[test]
fn test_dot_attribute() {
let mut context = HashMapContext::new();

context
.set_function(
"array".to_string(),
Function::new(|argument| Ok(Value::Tuple(argument.as_tuple()?))),
)
.unwrap();

context
.set_function(
"dot".to_string(),
Function::new(move |argument| {
let x = argument.as_fixed_len_tuple(3)?;
if let (Value::Tuple(id), Value::String(method)) = (&x[0], &x[1]) {
match method.as_str() {
"push" => {
// array.push(x)
let mut array = id.clone();
array.push(x[2].clone());
return Ok(Value::Tuple(array));
},
"get" => {
// array.get(i)
let index = x[2].as_int()?;
let value = &id[index as usize];
return Ok(value.clone());
},
"length" => {
// array.length
return Ok(Value::Int(id.len() as i64));
},
_ => {},
}
}
Err(EvalexprError::CustomMessage("unexpected dot call".into()))
}),
)
.unwrap();

assert_eq!(
eval_with_context_mut(
"v = array(1,2,3,4,5);
v = v.push(6);
v.length == v.get(5)",
&mut context
),
Ok(Value::Boolean(true))
);

assert_eq!(
eval_with_context_mut("v.get(4)", &mut context),
Ok(Value::Int(5))
);

// attribute is a method with empty input
assert_eq!(
eval_with_context_mut("v.length() == v.length", &mut context),
Ok(Value::Boolean(true))
);

assert_eq!(
eval_with_context_mut("v.100(4)", &mut context),
Err(EvalexprError::FunctionIdentifierNotFound("v.100".into()))
);

assert_eq!(
eval_with_context_mut("v.100", &mut context),
Err(EvalexprError::VariableIdentifierNotFound("v.100".into()))
);

assert_eq!(
eval_with_context_mut("100.v100", &mut context),
Err(EvalexprError::VariableIdentifierNotFound("100.v100".into()))
);
}

#[test]
fn test_dot_function() {
let mut context = HashMapContext::new();
context
.set_function(
"dot".to_string(),
Function::new(|argument| {
let x = argument.as_fixed_len_tuple(3)?;
if let (Value::Int(id), Value::String(method), args) = (&x[0], &x[1], &x[2]) {
if method == "add" {
let input = args.as_fixed_len_tuple(2)?;
if let (Value::Int(a), Value::Int(b)) = (&input[0], &input[1]) {
return Ok(Value::Int(id + a + b));
}
}
}
Err(EvalexprError::CustomMessage("unexpected dot call".into()))
}),
)
.unwrap();
context
.set_value("object".to_string(), Value::Int(10))
.unwrap();

// success
assert_eq!(
eval_with_context("object.add(5, 6)", &context),
Ok(Value::Int(21))
);

assert_eq!(
eval_with_context_mut("alien = 100; alien.add(5, 6)", &mut context),
Ok(Value::Int(111))
);

// empty
assert_eq!(
eval_with_context("object.add()", &context),
Err(EvalexprError::ExpectedTuple {
actual: Value::Empty
})
);

// too many
assert_eq!(
eval_with_context("object.add(5, 6, 7)", &context),
Err(EvalexprError::ExpectedFixedLenTuple {
expected_len: 2,
actual: Value::Tuple(vec![Value::Int(5), Value::Int(6), Value::Int(7)])
})
);

// user dose not implement
assert_eq!(
eval_with_context("object.remove(5, 6)", &context),
Err(EvalexprError::CustomMessage("unexpected dot call".into()))
);

// no such identifier
assert_eq!(
eval_with_context("unkown.add(5)", &context),
Err(EvalexprError::VariableIdentifierNotFound("unkown".into()))
);
}

#[test]
fn test_functions() {
let mut context = HashMapContext::new();
Expand Down
Loading