Simplify code, document logic, refactor and better error messages.
This commit is contained in:
parent
883f08c026
commit
3d3b939ba6
264
src/engine.rs
264
src/engine.rs
@ -105,21 +105,16 @@ impl Engine<'_> {
|
|||||||
if let Some(f) = fn_def {
|
if let Some(f) = fn_def {
|
||||||
match *f {
|
match *f {
|
||||||
FnIntExt::Ext(ref f) => {
|
FnIntExt::Ext(ref f) => {
|
||||||
let r = f(args, pos);
|
let r = f(args, pos)?;
|
||||||
|
|
||||||
if r.is_err() {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
let callback = match spec.name.as_ref() {
|
let callback = match spec.name.as_ref() {
|
||||||
KEYWORD_PRINT => self.on_print.as_mut(),
|
KEYWORD_PRINT => self.on_print.as_mut(),
|
||||||
KEYWORD_DEBUG => self.on_debug.as_mut(),
|
KEYWORD_DEBUG => self.on_debug.as_mut(),
|
||||||
_ => return r,
|
_ => return Ok(r),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(callback(
|
Ok(callback(
|
||||||
&r.unwrap()
|
&r.downcast::<String>()
|
||||||
.downcast::<String>()
|
|
||||||
.map(|s| *s)
|
.map(|s| *s)
|
||||||
.unwrap_or("error: not a string".into()),
|
.unwrap_or("error: not a string".into()),
|
||||||
)
|
)
|
||||||
@ -172,6 +167,7 @@ impl Engine<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Chain-evaluate a dot setter
|
||||||
fn get_dot_val_helper(
|
fn get_dot_val_helper(
|
||||||
&mut self,
|
&mut self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
@ -181,6 +177,7 @@ impl Engine<'_> {
|
|||||||
use std::iter::once;
|
use std::iter::once;
|
||||||
|
|
||||||
match dot_rhs {
|
match dot_rhs {
|
||||||
|
// xxx.fn_name(args)
|
||||||
Expr::FunctionCall(fn_name, args, def_value, pos) => {
|
Expr::FunctionCall(fn_name, args, def_value, pos) => {
|
||||||
let mut args: Array = args
|
let mut args: Array = args
|
||||||
.iter()
|
.iter()
|
||||||
@ -194,17 +191,16 @@ impl Engine<'_> {
|
|||||||
self.call_fn_raw(fn_name, args, def_value.as_ref(), *pos)
|
self.call_fn_raw(fn_name, args, def_value.as_ref(), *pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// xxx.id
|
||||||
Expr::Identifier(id, pos) => {
|
Expr::Identifier(id, pos) => {
|
||||||
let get_fn_name = format!("get${}", id);
|
let get_fn_name = format!("get${}", id);
|
||||||
|
|
||||||
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)
|
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// xxx.lhs[idx_expr]
|
||||||
Expr::Index(lhs, idx_expr) => {
|
Expr::Index(lhs, idx_expr) => {
|
||||||
let idx = *self
|
let idx = self.eval_index_value(scope, idx_expr)?;
|
||||||
.eval_expr(scope, idx_expr)?
|
|
||||||
.downcast::<i64>()
|
|
||||||
.map_err(|_| EvalAltResult::ErrorIndexExpr(idx_expr.position()))?;
|
|
||||||
|
|
||||||
let (lhs_value, pos) = match lhs.as_ref() {
|
let (lhs_value, pos) = match lhs.as_ref() {
|
||||||
Expr::Identifier(id, pos) => {
|
Expr::Identifier(id, pos) => {
|
||||||
@ -220,18 +216,18 @@ impl Engine<'_> {
|
|||||||
Self::get_indexed_value(lhs_value, idx, pos).map(|(v, _)| v)
|
Self::get_indexed_value(lhs_value, idx, pos).map(|(v, _)| v)
|
||||||
}
|
}
|
||||||
|
|
||||||
Expr::Dot(inner_lhs, inner_rhs) => match inner_lhs.as_ref() {
|
// xxx.lhs.rhs
|
||||||
|
Expr::Dot(lhs, rhs) => match lhs.as_ref() {
|
||||||
|
// xxx.id.rhs
|
||||||
Expr::Identifier(id, pos) => {
|
Expr::Identifier(id, pos) => {
|
||||||
let get_fn_name = format!("get${}", id);
|
let get_fn_name = format!("get${}", id);
|
||||||
|
|
||||||
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)
|
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)
|
||||||
.and_then(|mut v| self.get_dot_val_helper(scope, v.as_mut(), inner_rhs))
|
.and_then(|mut v| self.get_dot_val_helper(scope, v.as_mut(), rhs))
|
||||||
}
|
}
|
||||||
|
// xxx.lhs[idx_expr].rhs
|
||||||
Expr::Index(lhs, idx_expr) => {
|
Expr::Index(lhs, idx_expr) => {
|
||||||
let idx = *self
|
let idx = self.eval_index_value(scope, idx_expr)?;
|
||||||
.eval_expr(scope, idx_expr)?
|
|
||||||
.downcast::<i64>()
|
|
||||||
.map_err(|_| EvalAltResult::ErrorIndexExpr(idx_expr.position()))?;
|
|
||||||
|
|
||||||
let (lhs_value, pos) = match lhs.as_ref() {
|
let (lhs_value, pos) = match lhs.as_ref() {
|
||||||
Expr::Identifier(id, pos) => {
|
Expr::Identifier(id, pos) => {
|
||||||
@ -245,16 +241,19 @@ impl Engine<'_> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Self::get_indexed_value(lhs_value, idx, pos).and_then(|(mut value, _)| {
|
Self::get_indexed_value(lhs_value, idx, pos).and_then(|(mut value, _)| {
|
||||||
self.get_dot_val_helper(scope, value.as_mut(), inner_rhs)
|
self.get_dot_val_helper(scope, value.as_mut(), rhs)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => Err(EvalAltResult::ErrorDotExpr(inner_lhs.position())),
|
// Syntax error
|
||||||
|
_ => Err(EvalAltResult::ErrorDotExpr(lhs.position())),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Syntax error
|
||||||
_ => Err(EvalAltResult::ErrorDotExpr(dot_rhs.position())),
|
_ => Err(EvalAltResult::ErrorDotExpr(dot_rhs.position())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Search for a variable within the scope, returning its value and index inside the Scope
|
||||||
fn search_scope<T>(
|
fn search_scope<T>(
|
||||||
scope: &Scope,
|
scope: &Scope,
|
||||||
id: &str,
|
id: &str,
|
||||||
@ -267,13 +266,26 @@ impl Engine<'_> {
|
|||||||
.and_then(move |(idx, _, val)| map(val).map(|v| (idx, v)))
|
.and_then(move |(idx, _, val)| map(val).map(|v| (idx, v)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Evaluate the value of an index (must evaluate to i64)
|
||||||
|
fn eval_index_value(
|
||||||
|
&mut self,
|
||||||
|
scope: &mut Scope,
|
||||||
|
idx_expr: &Expr,
|
||||||
|
) -> Result<i64, EvalAltResult> {
|
||||||
|
self.eval_expr(scope, idx_expr)?
|
||||||
|
.downcast::<i64>()
|
||||||
|
.map(|v| *v)
|
||||||
|
.map_err(|_| EvalAltResult::ErrorIndexExpr(idx_expr.position()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the value at the indexed position of a base type
|
||||||
fn get_indexed_value(
|
fn get_indexed_value(
|
||||||
val: Dynamic,
|
val: Dynamic,
|
||||||
idx: i64,
|
idx: i64,
|
||||||
pos: Position,
|
pos: Position,
|
||||||
) -> Result<(Dynamic, VariableType), EvalAltResult> {
|
) -> Result<(Dynamic, VariableType), EvalAltResult> {
|
||||||
if val.is::<Array>() {
|
if val.is::<Array>() {
|
||||||
let arr = val.downcast::<Array>().unwrap();
|
let arr = val.downcast::<Array>().expect("Array expected");
|
||||||
|
|
||||||
if idx >= 0 {
|
if idx >= 0 {
|
||||||
arr.get(idx as usize)
|
arr.get(idx as usize)
|
||||||
@ -284,7 +296,7 @@ impl Engine<'_> {
|
|||||||
Err(EvalAltResult::ErrorArrayBounds(arr.len(), idx, pos))
|
Err(EvalAltResult::ErrorArrayBounds(arr.len(), idx, pos))
|
||||||
}
|
}
|
||||||
} else if val.is::<String>() {
|
} else if val.is::<String>() {
|
||||||
let s = val.downcast::<String>().unwrap();
|
let s = val.downcast::<String>().expect("String expected");
|
||||||
|
|
||||||
if idx >= 0 {
|
if idx >= 0 {
|
||||||
s.chars()
|
s.chars()
|
||||||
@ -303,16 +315,14 @@ impl Engine<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Evaluate an index expression
|
||||||
fn eval_index_expr(
|
fn eval_index_expr(
|
||||||
&mut self,
|
&mut self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
lhs: &Expr,
|
lhs: &Expr,
|
||||||
idx_expr: &Expr,
|
idx_expr: &Expr,
|
||||||
) -> Result<(VariableType, Option<(String, usize)>, usize, Dynamic), EvalAltResult> {
|
) -> Result<(VariableType, Option<(String, usize)>, usize, Dynamic), EvalAltResult> {
|
||||||
let idx = *self
|
let idx = self.eval_index_value(scope, idx_expr)?;
|
||||||
.eval_expr(scope, idx_expr)?
|
|
||||||
.downcast::<i64>()
|
|
||||||
.map_err(|_| EvalAltResult::ErrorIndexExpr(idx_expr.position()))?;
|
|
||||||
|
|
||||||
match lhs {
|
match lhs {
|
||||||
Expr::Identifier(id, _) => Self::search_scope(
|
Expr::Identifier(id, _) => Self::search_scope(
|
||||||
@ -330,9 +340,10 @@ impl Engine<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Replace a character at an index position in a mutable string
|
||||||
fn str_replace_char(s: &mut String, idx: usize, new_ch: char) {
|
fn str_replace_char(s: &mut String, idx: usize, new_ch: char) {
|
||||||
// The new character
|
// The new character
|
||||||
let ch = s.chars().nth(idx).unwrap();
|
let ch = s.chars().nth(idx).expect("string index out of bounds");
|
||||||
|
|
||||||
// See if changed - if so, update the String
|
// See if changed - if so, update the String
|
||||||
if ch == new_ch {
|
if ch == new_ch {
|
||||||
@ -346,6 +357,35 @@ impl Engine<'_> {
|
|||||||
chars.iter().for_each(|&ch| s.push(ch));
|
chars.iter().for_each(|&ch| s.push(ch));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update the value at an index position in a variable inside the scope
|
||||||
|
fn update_indexed_variable_in_scope(
|
||||||
|
source_type: VariableType,
|
||||||
|
scope: &mut Scope,
|
||||||
|
id: &str,
|
||||||
|
src_idx: usize,
|
||||||
|
idx: usize,
|
||||||
|
val: Dynamic,
|
||||||
|
) -> Option<Dynamic> {
|
||||||
|
match source_type {
|
||||||
|
VariableType::Array => {
|
||||||
|
let arr = scope.get_mut_by_type::<Array>(id, src_idx);
|
||||||
|
Some((arr[idx as usize] = val).into_dynamic())
|
||||||
|
}
|
||||||
|
|
||||||
|
VariableType::String => {
|
||||||
|
let s = scope.get_mut_by_type::<String>(id, src_idx);
|
||||||
|
// Value must be a character
|
||||||
|
let ch = *val
|
||||||
|
.downcast::<char>()
|
||||||
|
.expect("value to update an index position in a string must be a char");
|
||||||
|
Some(Self::str_replace_char(s, idx as usize, ch).into_dynamic())
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Evaluate a dot chain getter
|
||||||
fn get_dot_val(
|
fn get_dot_val(
|
||||||
&mut self,
|
&mut self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
@ -353,6 +393,7 @@ impl Engine<'_> {
|
|||||||
dot_rhs: &Expr,
|
dot_rhs: &Expr,
|
||||||
) -> Result<Dynamic, EvalAltResult> {
|
) -> Result<Dynamic, EvalAltResult> {
|
||||||
match dot_lhs {
|
match dot_lhs {
|
||||||
|
// xxx.???
|
||||||
Expr::Identifier(id, pos) => {
|
Expr::Identifier(id, pos) => {
|
||||||
let (sc_idx, mut target) = Self::search_scope(scope, id, Ok, *pos)?;
|
let (sc_idx, mut target) = Self::search_scope(scope, id, Ok, *pos)?;
|
||||||
let value = self.get_dot_val_helper(scope, target.as_mut(), dot_rhs);
|
let value = self.get_dot_val_helper(scope, target.as_mut(), dot_rhs);
|
||||||
@ -364,6 +405,7 @@ impl Engine<'_> {
|
|||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lhs[idx_expr].???
|
||||||
Expr::Index(lhs, idx_expr) => {
|
Expr::Index(lhs, idx_expr) => {
|
||||||
let (source_type, src, idx, mut target) =
|
let (source_type, src, idx, mut target) =
|
||||||
self.eval_index_expr(scope, lhs, idx_expr)?;
|
self.eval_index_expr(scope, lhs, idx_expr)?;
|
||||||
@ -371,39 +413,27 @@ impl Engine<'_> {
|
|||||||
|
|
||||||
// In case the expression mutated `target`, we need to reassign it because
|
// In case the expression mutated `target`, we need to reassign it because
|
||||||
// of the above `clone`.
|
// of the above `clone`.
|
||||||
|
if let Some((id, src_idx)) = src {
|
||||||
match source_type {
|
Self::update_indexed_variable_in_scope(
|
||||||
VariableType::Array => {
|
source_type,
|
||||||
let src = src.unwrap();
|
scope,
|
||||||
scope
|
&id,
|
||||||
.get_mut(&src.0, src.1)
|
src_idx,
|
||||||
.downcast_mut::<Array>()
|
idx,
|
||||||
.unwrap()[idx] = target
|
target,
|
||||||
}
|
)
|
||||||
|
.expect("source_type must be either Array or String");
|
||||||
VariableType::String => {
|
|
||||||
let src = src.unwrap();
|
|
||||||
|
|
||||||
Self::str_replace_char(
|
|
||||||
scope
|
|
||||||
.get_mut(&src.0, src.1)
|
|
||||||
.downcast_mut::<String>()
|
|
||||||
.unwrap(), // Root is a string
|
|
||||||
idx,
|
|
||||||
*target.downcast::<char>().unwrap(), // Target should be a char
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => panic!("source_type must be either Array or String"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Syntax error
|
||||||
_ => Err(EvalAltResult::ErrorDotExpr(dot_lhs.position())),
|
_ => Err(EvalAltResult::ErrorDotExpr(dot_lhs.position())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Chain-evaluate a dot setter
|
||||||
fn set_dot_val_helper(
|
fn set_dot_val_helper(
|
||||||
&mut self,
|
&mut self,
|
||||||
this_ptr: &mut Variant,
|
this_ptr: &mut Variant,
|
||||||
@ -411,6 +441,7 @@ impl Engine<'_> {
|
|||||||
mut source_val: Dynamic,
|
mut source_val: Dynamic,
|
||||||
) -> Result<Dynamic, EvalAltResult> {
|
) -> Result<Dynamic, EvalAltResult> {
|
||||||
match dot_rhs {
|
match dot_rhs {
|
||||||
|
// xxx.id
|
||||||
Expr::Identifier(id, pos) => {
|
Expr::Identifier(id, pos) => {
|
||||||
let set_fn_name = format!("set${}", id);
|
let set_fn_name = format!("set${}", id);
|
||||||
|
|
||||||
@ -422,13 +453,14 @@ impl Engine<'_> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Expr::Dot(inner_lhs, inner_rhs) => match inner_lhs.as_ref() {
|
// xxx.lhs.rhs
|
||||||
|
Expr::Dot(lhs, rhs) => match lhs.as_ref() {
|
||||||
Expr::Identifier(id, pos) => {
|
Expr::Identifier(id, pos) => {
|
||||||
let get_fn_name = format!("get${}", id);
|
let get_fn_name = format!("get${}", id);
|
||||||
|
|
||||||
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)
|
self.call_fn_raw(&get_fn_name, vec![this_ptr], None, *pos)
|
||||||
.and_then(|mut v| {
|
.and_then(|mut v| {
|
||||||
self.set_dot_val_helper(v.as_mut(), inner_rhs, source_val)
|
self.set_dot_val_helper(v.as_mut(), rhs, source_val)
|
||||||
.map(|_| v) // Discard Ok return value
|
.map(|_| v) // Discard Ok return value
|
||||||
})
|
})
|
||||||
.and_then(|mut v| {
|
.and_then(|mut v| {
|
||||||
@ -437,13 +469,15 @@ impl Engine<'_> {
|
|||||||
self.call_fn_raw(&set_fn_name, vec![this_ptr, v.as_mut()], None, *pos)
|
self.call_fn_raw(&set_fn_name, vec![this_ptr, v.as_mut()], None, *pos)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => Err(EvalAltResult::ErrorDotExpr(inner_lhs.position())),
|
_ => Err(EvalAltResult::ErrorDotExpr(lhs.position())),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Syntax error
|
||||||
_ => Err(EvalAltResult::ErrorDotExpr(dot_rhs.position())),
|
_ => Err(EvalAltResult::ErrorDotExpr(dot_rhs.position())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Evaluate a dot chain setter
|
||||||
fn set_dot_val(
|
fn set_dot_val(
|
||||||
&mut self,
|
&mut self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
@ -452,6 +486,7 @@ impl Engine<'_> {
|
|||||||
source_val: Dynamic,
|
source_val: Dynamic,
|
||||||
) -> Result<Dynamic, EvalAltResult> {
|
) -> Result<Dynamic, EvalAltResult> {
|
||||||
match dot_lhs {
|
match dot_lhs {
|
||||||
|
// id.???
|
||||||
Expr::Identifier(id, pos) => {
|
Expr::Identifier(id, pos) => {
|
||||||
let (sc_idx, mut target) = Self::search_scope(scope, id, Ok, *pos)?;
|
let (sc_idx, mut target) = Self::search_scope(scope, id, Ok, *pos)?;
|
||||||
let value = self.set_dot_val_helper(target.as_mut(), dot_rhs, source_val);
|
let value = self.set_dot_val_helper(target.as_mut(), dot_rhs, source_val);
|
||||||
@ -463,6 +498,7 @@ impl Engine<'_> {
|
|||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lhs[idx_expr].???
|
||||||
Expr::Index(lhs, idx_expr) => {
|
Expr::Index(lhs, idx_expr) => {
|
||||||
let (source_type, src, idx, mut target) =
|
let (source_type, src, idx, mut target) =
|
||||||
self.eval_index_expr(scope, lhs, idx_expr)?;
|
self.eval_index_expr(scope, lhs, idx_expr)?;
|
||||||
@ -471,39 +507,27 @@ impl Engine<'_> {
|
|||||||
// In case the expression mutated `target`, we need to reassign it because
|
// In case the expression mutated `target`, we need to reassign it because
|
||||||
// of the above `clone`.
|
// of the above `clone`.
|
||||||
|
|
||||||
match source_type {
|
if let Some((id, src_idx)) = src {
|
||||||
VariableType::Array => {
|
Self::update_indexed_variable_in_scope(
|
||||||
let src = src.unwrap();
|
source_type,
|
||||||
let val = scope
|
scope,
|
||||||
.get_mut(&src.0, src.1)
|
&id,
|
||||||
.downcast_mut::<Array>()
|
src_idx,
|
||||||
.unwrap();
|
idx,
|
||||||
val[idx] = target
|
target,
|
||||||
}
|
)
|
||||||
|
.expect("source_type must be either Array or String");
|
||||||
VariableType::String => {
|
|
||||||
let src = src.unwrap();
|
|
||||||
|
|
||||||
Self::str_replace_char(
|
|
||||||
scope
|
|
||||||
.get_mut(&src.0, src.1)
|
|
||||||
.downcast_mut::<String>()
|
|
||||||
.unwrap(), // Root is a string
|
|
||||||
idx,
|
|
||||||
*target.downcast::<char>().unwrap(), // Target should be a char
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => panic!("source_type must be either Array or String"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Syntax error
|
||||||
_ => Err(EvalAltResult::ErrorDotExpr(dot_lhs.position())),
|
_ => Err(EvalAltResult::ErrorDotExpr(dot_lhs.position())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Evaluate an expression
|
||||||
fn eval_expr(&mut self, scope: &mut Scope, expr: &Expr) -> Result<Dynamic, EvalAltResult> {
|
fn eval_expr(&mut self, scope: &mut Scope, expr: &Expr) -> Result<Dynamic, EvalAltResult> {
|
||||||
match expr {
|
match expr {
|
||||||
Expr::IntegerConstant(i, _) => Ok((*i).into_dynamic()),
|
Expr::IntegerConstant(i, _) => Ok((*i).into_dynamic()),
|
||||||
@ -517,10 +541,12 @@ impl Engine<'_> {
|
|||||||
.eval_index_expr(scope, lhs, idx_expr)
|
.eval_index_expr(scope, lhs, idx_expr)
|
||||||
.map(|(_, _, _, x)| x),
|
.map(|(_, _, _, x)| x),
|
||||||
|
|
||||||
|
// lhs = rhs
|
||||||
Expr::Assignment(lhs, rhs) => {
|
Expr::Assignment(lhs, rhs) => {
|
||||||
let rhs_val = self.eval_expr(scope, rhs)?;
|
let rhs_val = self.eval_expr(scope, rhs)?;
|
||||||
|
|
||||||
match lhs.as_ref() {
|
match lhs.as_ref() {
|
||||||
|
// name = rhs
|
||||||
Expr::Identifier(name, pos) => {
|
Expr::Identifier(name, pos) => {
|
||||||
if let Some((idx, _, _)) = scope.get(name) {
|
if let Some((idx, _, _)) = scope.get(name) {
|
||||||
*scope.get_mut(name, idx) = rhs_val;
|
*scope.get_mut(name, idx) = rhs_val;
|
||||||
@ -530,50 +556,34 @@ impl Engine<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// idx_lhs[idx_expr] = rhs
|
||||||
Expr::Index(idx_lhs, idx_expr) => {
|
Expr::Index(idx_lhs, idx_expr) => {
|
||||||
let (source_type, src, idx, _) =
|
let (source_type, src, idx, _) =
|
||||||
self.eval_index_expr(scope, idx_lhs, idx_expr)?;
|
self.eval_index_expr(scope, idx_lhs, idx_expr)?;
|
||||||
|
|
||||||
match source_type {
|
if let Some((id, src_idx)) = src {
|
||||||
VariableType::Array => {
|
Self::update_indexed_variable_in_scope(
|
||||||
let src = src.unwrap();
|
source_type,
|
||||||
scope
|
scope,
|
||||||
.get_mut(&src.0, src.1)
|
&id,
|
||||||
.downcast_mut::<Array>()
|
src_idx,
|
||||||
.map(|arr| (arr[idx as usize] = rhs_val).into_dynamic())
|
idx,
|
||||||
.ok_or_else(|| {
|
rhs_val,
|
||||||
EvalAltResult::ErrorIndexExpr(idx_lhs.position())
|
)
|
||||||
})
|
} else {
|
||||||
}
|
None
|
||||||
|
|
||||||
VariableType::String => {
|
|
||||||
let src = src.unwrap();
|
|
||||||
scope
|
|
||||||
.get_mut(&src.0, src.1)
|
|
||||||
.downcast_mut::<String>()
|
|
||||||
.map(|s| {
|
|
||||||
Self::str_replace_char(
|
|
||||||
s,
|
|
||||||
idx as usize,
|
|
||||||
*rhs_val.downcast::<char>().unwrap(),
|
|
||||||
)
|
|
||||||
.into_dynamic()
|
|
||||||
})
|
|
||||||
.ok_or_else(|| {
|
|
||||||
EvalAltResult::ErrorIndexExpr(idx_lhs.position())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => Err(EvalAltResult::ErrorAssignmentToUnknownLHS(
|
|
||||||
idx_lhs.position(),
|
|
||||||
)),
|
|
||||||
}
|
}
|
||||||
|
.ok_or_else(|| {
|
||||||
|
EvalAltResult::ErrorAssignmentToUnknownLHS(idx_lhs.position())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dot_lhs.dot_rhs = rhs
|
||||||
Expr::Dot(dot_lhs, dot_rhs) => {
|
Expr::Dot(dot_lhs, dot_rhs) => {
|
||||||
self.set_dot_val(scope, dot_lhs, dot_rhs, rhs_val)
|
self.set_dot_val(scope, dot_lhs, dot_rhs, rhs_val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Syntax error
|
||||||
_ => Err(EvalAltResult::ErrorAssignmentToUnknownLHS(lhs.position())),
|
_ => Err(EvalAltResult::ErrorAssignmentToUnknownLHS(lhs.position())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -615,12 +625,13 @@ impl Engine<'_> {
|
|||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), lhs.position())
|
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), lhs.position())
|
||||||
})?
|
})?
|
||||||
&& *self
|
&& // Short-circuit using &&
|
||||||
.eval_expr(scope, &*rhs)?
|
*self
|
||||||
.downcast::<bool>()
|
.eval_expr(scope, &*rhs)?
|
||||||
.map_err(|_| {
|
.downcast::<bool>()
|
||||||
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), rhs.position())
|
.map_err(|_| {
|
||||||
})?,
|
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), rhs.position())
|
||||||
|
})?,
|
||||||
)),
|
)),
|
||||||
|
|
||||||
Expr::Or(lhs, rhs) => Ok(Box::new(
|
Expr::Or(lhs, rhs) => Ok(Box::new(
|
||||||
@ -630,12 +641,13 @@ impl Engine<'_> {
|
|||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), lhs.position())
|
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), lhs.position())
|
||||||
})?
|
})?
|
||||||
|| *self
|
|| // Short-circuit using ||
|
||||||
.eval_expr(scope, &*rhs)?
|
*self
|
||||||
.downcast::<bool>()
|
.eval_expr(scope, &*rhs)?
|
||||||
.map_err(|_| {
|
.downcast::<bool>()
|
||||||
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), rhs.position())
|
.map_err(|_| {
|
||||||
})?,
|
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), rhs.position())
|
||||||
|
})?,
|
||||||
)),
|
)),
|
||||||
|
|
||||||
Expr::True(_) => Ok(true.into_dynamic()),
|
Expr::True(_) => Ok(true.into_dynamic()),
|
||||||
@ -644,6 +656,7 @@ impl Engine<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Evaluate a statement
|
||||||
pub(crate) fn eval_stmt(
|
pub(crate) fn eval_stmt(
|
||||||
&mut self,
|
&mut self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
@ -679,8 +692,8 @@ impl Engine<'_> {
|
|||||||
.and_then(|guard_val| {
|
.and_then(|guard_val| {
|
||||||
if *guard_val {
|
if *guard_val {
|
||||||
self.eval_stmt(scope, body)
|
self.eval_stmt(scope, body)
|
||||||
} else if else_body.is_some() {
|
} else if let Some(stmt) = else_body {
|
||||||
self.eval_stmt(scope, else_body.as_ref().unwrap())
|
self.eval_stmt(scope, stmt.as_ref())
|
||||||
} else {
|
} else {
|
||||||
Ok(().into_dynamic())
|
Ok(().into_dynamic())
|
||||||
}
|
}
|
||||||
@ -775,6 +788,7 @@ impl Engine<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Map a type_name into a pretty-print name
|
||||||
pub(crate) fn map_type_name<'a>(&'a self, name: &'a str) -> &'a str {
|
pub(crate) fn map_type_name<'a>(&'a self, name: &'a str) -> &'a str {
|
||||||
self.type_names
|
self.type_names
|
||||||
.get(name)
|
.get(name)
|
||||||
|
17
src/error.rs
17
src/error.rs
@ -56,13 +56,13 @@ pub enum ParseErrorType {
|
|||||||
/// An unknown operator is encountered. Wrapped value is the operator.
|
/// An unknown operator is encountered. Wrapped value is the operator.
|
||||||
UnknownOperator(String),
|
UnknownOperator(String),
|
||||||
/// An open `(` is missing the corresponding closing `)`.
|
/// An open `(` is missing the corresponding closing `)`.
|
||||||
MissingRightParen,
|
MissingRightParen(String),
|
||||||
/// Expecting `(` but not finding one.
|
/// Expecting `(` but not finding one.
|
||||||
MissingLeftBrace,
|
MissingLeftBrace,
|
||||||
/// An open `{` is missing the corresponding closing `}`.
|
/// An open `{` is missing the corresponding closing `}`.
|
||||||
MissingRightBrace,
|
MissingRightBrace(String),
|
||||||
/// An open `[` is missing the corresponding closing `]`.
|
/// An open `[` is missing the corresponding closing `]`.
|
||||||
MissingRightBracket,
|
MissingRightBracket(String),
|
||||||
/// An expression in function call arguments `()` has syntax error.
|
/// An expression in function call arguments `()` has syntax error.
|
||||||
MalformedCallExpr,
|
MalformedCallExpr,
|
||||||
/// An expression in indexing brackets `[]` has syntax error.
|
/// An expression in indexing brackets `[]` has syntax error.
|
||||||
@ -104,10 +104,10 @@ impl Error for ParseError {
|
|||||||
ParseErrorType::BadInput(ref p) => p,
|
ParseErrorType::BadInput(ref p) => p,
|
||||||
ParseErrorType::InputPastEndOfFile => "Script is incomplete",
|
ParseErrorType::InputPastEndOfFile => "Script is incomplete",
|
||||||
ParseErrorType::UnknownOperator(_) => "Unknown operator",
|
ParseErrorType::UnknownOperator(_) => "Unknown operator",
|
||||||
ParseErrorType::MissingRightParen => "Expecting ')'",
|
ParseErrorType::MissingRightParen(_) => "Expecting ')'",
|
||||||
ParseErrorType::MissingLeftBrace => "Expecting '{'",
|
ParseErrorType::MissingLeftBrace => "Expecting '{'",
|
||||||
ParseErrorType::MissingRightBrace => "Expecting '}'",
|
ParseErrorType::MissingRightBrace(_) => "Expecting '}'",
|
||||||
ParseErrorType::MissingRightBracket => "Expecting ']'",
|
ParseErrorType::MissingRightBracket(_) => "Expecting ']'",
|
||||||
ParseErrorType::MalformedCallExpr => "Invalid expression in function call arguments",
|
ParseErrorType::MalformedCallExpr => "Invalid expression in function call arguments",
|
||||||
ParseErrorType::MalformedIndexExpr => "Invalid index in indexing expression",
|
ParseErrorType::MalformedIndexExpr => "Invalid index in indexing expression",
|
||||||
ParseErrorType::VarExpectsIdentifier => "Expecting name of a variable",
|
ParseErrorType::VarExpectsIdentifier => "Expecting name of a variable",
|
||||||
@ -130,6 +130,11 @@ impl fmt::Display for ParseError {
|
|||||||
ParseErrorType::FnMissingParams(ref s) => {
|
ParseErrorType::FnMissingParams(ref s) => {
|
||||||
write!(f, "Missing parameters for function '{}'", s)?
|
write!(f, "Missing parameters for function '{}'", s)?
|
||||||
}
|
}
|
||||||
|
ParseErrorType::MissingRightParen(ref s)
|
||||||
|
| ParseErrorType::MissingRightBrace(ref s)
|
||||||
|
| ParseErrorType::MissingRightBracket(ref s) => {
|
||||||
|
write!(f, "{} for {}", self.description(), s)?
|
||||||
|
}
|
||||||
_ => write!(f, "{}", self.description())?,
|
_ => write!(f, "{}", self.description())?,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1043,7 +1043,16 @@ fn parse_paren_expr<'a>(
|
|||||||
|
|
||||||
match input.next() {
|
match input.next() {
|
||||||
Some((Token::RightParen, _)) => Ok(expr),
|
Some((Token::RightParen, _)) => Ok(expr),
|
||||||
_ => Err(ParseError::new(PERR::MissingRightParen, Position::eof())),
|
Some((_, pos)) => {
|
||||||
|
return Err(ParseError::new(
|
||||||
|
PERR::MissingRightParen("a matching ( in the expression".into()),
|
||||||
|
pos,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
None => Err(ParseError::new(
|
||||||
|
PERR::MissingRightParen("a matching ( in the expression".into()),
|
||||||
|
Position::eof(),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1068,8 +1077,24 @@ fn parse_call_expr<'a>(
|
|||||||
return Ok(Expr::FunctionCall(id, args, None, begin));
|
return Ok(Expr::FunctionCall(id, args, None, begin));
|
||||||
}
|
}
|
||||||
Some(&(Token::Comma, _)) => (),
|
Some(&(Token::Comma, _)) => (),
|
||||||
Some(&(_, pos)) => return Err(ParseError::new(PERR::MalformedCallExpr, pos)),
|
Some(&(_, pos)) => {
|
||||||
None => return Err(ParseError::new(PERR::MalformedCallExpr, Position::eof())),
|
return Err(ParseError::new(
|
||||||
|
PERR::MissingRightParen(format!(
|
||||||
|
"closing the arguments list to function call of '{}'",
|
||||||
|
id
|
||||||
|
)),
|
||||||
|
pos,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return Err(ParseError::new(
|
||||||
|
PERR::MissingRightParen(format!(
|
||||||
|
"closing the arguments list to function call of '{}'",
|
||||||
|
id
|
||||||
|
)),
|
||||||
|
Position::eof(),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
input.next();
|
input.next();
|
||||||
@ -1080,17 +1105,24 @@ fn parse_index_expr<'a>(
|
|||||||
lhs: Box<Expr>,
|
lhs: Box<Expr>,
|
||||||
input: &mut Peekable<TokenIterator<'a>>,
|
input: &mut Peekable<TokenIterator<'a>>,
|
||||||
) -> Result<Expr, ParseError> {
|
) -> Result<Expr, ParseError> {
|
||||||
match parse_expr(input) {
|
parse_expr(input).and_then(|idx_expr| match input.peek() {
|
||||||
Ok(idx_expr) => match input.peek() {
|
Some(&(Token::RightBracket, _)) => {
|
||||||
Some(&(Token::RightBracket, _)) => {
|
input.next();
|
||||||
input.next();
|
return Ok(Expr::Index(lhs, Box::new(idx_expr)));
|
||||||
return Ok(Expr::Index(lhs, Box::new(idx_expr)));
|
}
|
||||||
}
|
Some(&(_, pos)) => {
|
||||||
Some(&(_, pos)) => return Err(ParseError::new(PERR::MalformedIndexExpr, pos)),
|
return Err(ParseError::new(
|
||||||
None => return Err(ParseError::new(PERR::MalformedIndexExpr, Position::eof())),
|
PERR::MissingRightBracket("index expression".into()),
|
||||||
},
|
pos,
|
||||||
Err(err) => return Err(ParseError::new(PERR::MalformedIndexExpr, err.position())),
|
))
|
||||||
}
|
}
|
||||||
|
None => {
|
||||||
|
return Err(ParseError::new(
|
||||||
|
PERR::MissingRightBracket("index expression".into()),
|
||||||
|
Position::eof(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_ident_expr<'a>(
|
fn parse_ident_expr<'a>(
|
||||||
@ -1141,8 +1173,14 @@ fn parse_array_expr<'a>(
|
|||||||
input.next();
|
input.next();
|
||||||
Ok(Expr::Array(arr, begin))
|
Ok(Expr::Array(arr, begin))
|
||||||
}
|
}
|
||||||
Some(&(_, pos)) => Err(ParseError::new(PERR::MissingRightBracket, pos)),
|
Some(&(_, pos)) => Err(ParseError::new(
|
||||||
None => Err(ParseError::new(PERR::MissingRightBracket, Position::eof())),
|
PERR::MissingRightBracket("the end of array literal".into()),
|
||||||
|
pos,
|
||||||
|
)),
|
||||||
|
None => Err(ParseError::new(
|
||||||
|
PERR::MissingRightBracket("the end of array literal".into()),
|
||||||
|
Position::eof(),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1462,7 +1500,7 @@ fn parse_binary_op<'a>(
|
|||||||
}
|
}
|
||||||
token => {
|
token => {
|
||||||
return Err(ParseError::new(
|
return Err(ParseError::new(
|
||||||
PERR::UnknownOperator(token.syntax().to_string()),
|
PERR::UnknownOperator(token.syntax().into()),
|
||||||
pos,
|
pos,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -1599,8 +1637,14 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
|||||||
input.next();
|
input.next();
|
||||||
Ok(Stmt::Block(statements))
|
Ok(Stmt::Block(statements))
|
||||||
}
|
}
|
||||||
Some(&(_, pos)) => Err(ParseError::new(PERR::MissingRightBrace, pos)),
|
Some(&(_, pos)) => Err(ParseError::new(
|
||||||
None => Err(ParseError::new(PERR::MissingRightBrace, Position::eof())),
|
PERR::MissingRightBrace("end of block".into()),
|
||||||
|
pos,
|
||||||
|
)),
|
||||||
|
None => Err(ParseError::new(
|
||||||
|
PERR::MissingRightBrace("end of block".into()),
|
||||||
|
Position::eof(),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,6 +85,13 @@ impl Scope {
|
|||||||
&mut entry.1
|
&mut entry.1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a mutable reference to a variable in the Scope and downcast it to a specific type
|
||||||
|
pub(crate) fn get_mut_by_type<T: Any + Clone>(&mut self, key: &str, index: usize) -> &mut T {
|
||||||
|
self.get_mut(key, index)
|
||||||
|
.downcast_mut::<T>()
|
||||||
|
.expect("wrong type cast")
|
||||||
|
}
|
||||||
|
|
||||||
/// Get an iterator to variables in the Scope.
|
/// Get an iterator to variables in the Scope.
|
||||||
pub fn iter(&self) -> impl Iterator<Item = (&str, &Dynamic)> {
|
pub fn iter(&self) -> impl Iterator<Item = (&str, &Dynamic)> {
|
||||||
self.0
|
self.0
|
||||||
|
Loading…
Reference in New Issue
Block a user