Rust 2018
This commit is contained in:
45
src/any.rs
45
src/any.rs
@@ -4,7 +4,7 @@ use std::fmt;
|
||||
pub trait Any: StdAny {
|
||||
fn type_id(&self) -> TypeId;
|
||||
|
||||
fn box_clone(&self) -> Box<Any>;
|
||||
fn box_clone(&self) -> Box<dyn Any>;
|
||||
|
||||
/// This type may only be implemented by `rhai`.
|
||||
#[doc(hidden)]
|
||||
@@ -12,8 +12,8 @@ pub trait Any: StdAny {
|
||||
}
|
||||
|
||||
impl<T> Any for T
|
||||
where
|
||||
T: Clone + StdAny + ?Sized
|
||||
where
|
||||
T: Clone + StdAny + ?Sized,
|
||||
{
|
||||
#[inline]
|
||||
fn type_id(&self) -> TypeId {
|
||||
@@ -21,23 +21,24 @@ impl<T> Any for T
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn box_clone(&self) -> Box<Any> {
|
||||
fn box_clone(&self) -> Box<dyn Any> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn _closed(&self) -> _Private { _Private }
|
||||
fn _closed(&self) -> _Private {
|
||||
_Private
|
||||
}
|
||||
}
|
||||
|
||||
impl Any {
|
||||
#[inline]
|
||||
fn box_clone(&self) -> Box<Any> {
|
||||
Any::box_clone(self)
|
||||
}
|
||||
|
||||
impl dyn Any {
|
||||
//#[inline]
|
||||
// fn box_clone(&self) -> Box<dyn Any> {
|
||||
// Any::box_clone(self)
|
||||
// }
|
||||
#[inline]
|
||||
pub fn is<T: Any>(&self) -> bool {
|
||||
let t = TypeId::of::<T>();
|
||||
let boxed = <Any as Any>::type_id(self);
|
||||
let boxed = <dyn Any as Any>::type_id(self);
|
||||
|
||||
t == boxed
|
||||
}
|
||||
@@ -45,9 +46,7 @@ impl Any {
|
||||
#[inline]
|
||||
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
|
||||
if self.is::<T>() {
|
||||
unsafe {
|
||||
Some(&*(self as *const Any as *const T))
|
||||
}
|
||||
unsafe { Some(&*(self as *const dyn Any as *const T)) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -56,23 +55,21 @@ impl Any {
|
||||
#[inline]
|
||||
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
|
||||
if self.is::<T>() {
|
||||
unsafe {
|
||||
Some(&mut *(self as *mut Any as *mut T))
|
||||
}
|
||||
unsafe { Some(&mut *(self as *mut dyn Any as *mut T)) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Box<Any> {
|
||||
impl Clone for Box<dyn Any> {
|
||||
fn clone(&self) -> Self {
|
||||
Any::box_clone(self.as_ref() as &Any)
|
||||
Any::box_clone(self.as_ref() as &dyn Any)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Any {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
impl fmt::Debug for dyn Any {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.pad("Any")
|
||||
}
|
||||
}
|
||||
@@ -81,11 +78,11 @@ pub trait AnyExt: Sized {
|
||||
fn downcast<T: Any + Clone>(self) -> Result<Box<T>, Self>;
|
||||
}
|
||||
|
||||
impl AnyExt for Box<Any> {
|
||||
impl AnyExt for Box<dyn Any> {
|
||||
fn downcast<T: Any + Clone>(self) -> Result<Box<T>, Self> {
|
||||
if self.is::<T>() {
|
||||
unsafe {
|
||||
let raw: *mut Any = Box::into_raw(self);
|
||||
let raw: *mut dyn Any = Box::into_raw(self);
|
||||
Ok(Box::from_raw(raw as *mut T))
|
||||
}
|
||||
} else {
|
||||
|
@@ -1,10 +1,10 @@
|
||||
//! Helper module which defines `FnArgs`
|
||||
//! to make function calling easier.
|
||||
|
||||
use any::Any;
|
||||
use crate::any::Any;
|
||||
|
||||
pub trait FunArgs<'a> {
|
||||
fn into_vec(self) -> Vec<&'a mut Any>;
|
||||
fn into_vec(self) -> Vec<&'a mut dyn Any>;
|
||||
}
|
||||
|
||||
macro_rules! impl_args {
|
||||
@@ -13,11 +13,12 @@ macro_rules! impl_args {
|
||||
where
|
||||
$($p: Any + Clone),*
|
||||
{
|
||||
fn into_vec(self) -> Vec<&'a mut Any> {
|
||||
fn into_vec(self) -> Vec<&'a mut dyn Any> {
|
||||
let ($($p,)*) = self;
|
||||
|
||||
#[allow(unused_variables, unused_mut)]
|
||||
let mut v = Vec::new();
|
||||
$(v.push($p as &mut Any);)*
|
||||
$(v.push($p as &mut dyn Any);)*
|
||||
|
||||
v
|
||||
}
|
||||
|
210
src/engine.rs
210
src/engine.rs
@@ -1,16 +1,15 @@
|
||||
use std::any::TypeId;
|
||||
use std::borrow::Borrow;
|
||||
use std::cmp::{PartialEq, PartialOrd};
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub};
|
||||
use std::sync::Arc;
|
||||
use std::ops::{Add, BitAnd, BitOr, BitXor, Deref, Div, Mul, Neg, Rem, Shl, Shr, Sub};
|
||||
|
||||
use any::{Any, AnyExt};
|
||||
use fn_register::{Mut, RegisterFn};
|
||||
use parser::{lex, parse, Expr, FnDef, Stmt};
|
||||
use call::FunArgs;
|
||||
use crate::any::{Any, AnyExt};
|
||||
use crate::call::FunArgs;
|
||||
use crate::fn_register::RegisterFn;
|
||||
use crate::parser::{lex, parse, Expr, FnDef, Stmt};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum EvalAltResult {
|
||||
@@ -26,7 +25,7 @@ pub enum EvalAltResult {
|
||||
ErrorCantOpenScriptFile,
|
||||
InternalErrorMalformedDotExpression,
|
||||
LoopBreak,
|
||||
Return(Box<Any>),
|
||||
Return(Box<dyn Any>),
|
||||
}
|
||||
|
||||
impl EvalAltResult {
|
||||
@@ -35,7 +34,7 @@ impl EvalAltResult {
|
||||
EvalAltResult::ErrorVariableNotFound(ref s) => Some(s.as_str()),
|
||||
EvalAltResult::ErrorFunctionNotFound(ref s) => Some(s.as_str()),
|
||||
EvalAltResult::ErrorMismatchOutputType(ref s) => Some(s.as_str()),
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,13 +88,13 @@ impl Error for EvalAltResult {
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
fn cause(&self) -> Option<&dyn Error> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for EvalAltResult {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if let Some(s) = self.as_str() {
|
||||
write!(f, "{}: {}", self.description(), s)
|
||||
} else {
|
||||
@@ -128,7 +127,7 @@ pub struct FnSpec {
|
||||
pub struct Engine {
|
||||
/// A hashmap containing all functions known to the engine
|
||||
pub fns: HashMap<FnSpec, Arc<FnIntExt>>,
|
||||
pub type_names: HashMap<TypeId,String>,
|
||||
pub type_names: HashMap<TypeId, String>,
|
||||
}
|
||||
|
||||
pub enum FnIntExt {
|
||||
@@ -136,7 +135,7 @@ pub enum FnIntExt {
|
||||
Int(FnDef),
|
||||
}
|
||||
|
||||
pub type FnAny = Fn(Vec<&mut Any>) -> Result<Box<Any>, EvalAltResult>;
|
||||
pub type FnAny = dyn Fn(Vec<&mut dyn Any>) -> Result<Box<dyn Any>, EvalAltResult>;
|
||||
|
||||
/// A type containing information about current scope.
|
||||
/// Useful for keeping state between `Engine` runs
|
||||
@@ -152,7 +151,7 @@ pub type FnAny = Fn(Vec<&mut Any>) -> Result<Box<Any>, EvalAltResult>;
|
||||
/// ```
|
||||
///
|
||||
/// Between runs, `Engine` only remembers functions when not using own `Scope`.
|
||||
pub type Scope = Vec<(String, Box<Any>)>;
|
||||
pub type Scope = Vec<(String, Box<dyn Any>)>;
|
||||
|
||||
impl Engine {
|
||||
pub fn call_fn<'a, I, A, T>(&self, ident: I, args: A) -> Result<T, EvalAltResult>
|
||||
@@ -174,8 +173,8 @@ impl Engine {
|
||||
pub fn call_fn_raw(
|
||||
&self,
|
||||
ident: String,
|
||||
args: Vec<&mut Any>,
|
||||
) -> Result<Box<Any>, EvalAltResult> {
|
||||
args: Vec<&mut dyn Any>,
|
||||
) -> Result<Box<dyn Any>, EvalAltResult> {
|
||||
debug_println!(
|
||||
"Trying to call function {:?} with args {:?}",
|
||||
ident,
|
||||
@@ -184,17 +183,27 @@ impl Engine {
|
||||
|
||||
let spec = FnSpec {
|
||||
ident: ident.clone(),
|
||||
args: Some(args.iter().map(|a| <Any as Any>::type_id(&**a)).collect()),
|
||||
args: Some(
|
||||
args.iter()
|
||||
.map(|a| <dyn Any as Any>::type_id(&**a))
|
||||
.collect(),
|
||||
),
|
||||
};
|
||||
|
||||
self.fns
|
||||
.get(&spec)
|
||||
.or_else(|| {
|
||||
let spec1 = FnSpec { ident: ident.clone(), args: None };
|
||||
let spec1 = FnSpec {
|
||||
ident: ident.clone(),
|
||||
args: None,
|
||||
};
|
||||
self.fns.get(&spec1)
|
||||
})
|
||||
.ok_or_else(|| {
|
||||
let typenames = args.iter().map(|x| self.nice_type_name((&**x).box_clone())).collect::<Vec<_>>();
|
||||
let typenames = args
|
||||
.iter()
|
||||
.map(|x| self.nice_type_name((&**x).box_clone()))
|
||||
.collect::<Vec<_>>();
|
||||
EvalAltResult::ErrorFunctionNotFound(format!("{} ({})", ident, typenames.join(",")))
|
||||
})
|
||||
.and_then(move |f| match **f {
|
||||
@@ -272,14 +281,15 @@ impl Engine {
|
||||
fn get_dot_val_helper(
|
||||
&self,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Any,
|
||||
this_ptr: &mut dyn Any,
|
||||
dot_rhs: &Expr,
|
||||
) -> Result<Box<Any>, EvalAltResult> {
|
||||
) -> Result<Box<dyn Any>, EvalAltResult> {
|
||||
use std::iter::once;
|
||||
|
||||
match *dot_rhs {
|
||||
Expr::FnCall(ref fn_name, ref args) => {
|
||||
let mut args: Vec<Box<Any>> = args.iter()
|
||||
let mut args: Vec<Box<dyn Any>> = args
|
||||
.iter()
|
||||
.map(|arg| self.eval_expr(scope, arg))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let args = once(this_ptr)
|
||||
@@ -299,7 +309,7 @@ impl Engine {
|
||||
|
||||
let mut val = self.call_fn_raw(get_fn_name, vec![this_ptr])?;
|
||||
|
||||
((*val).downcast_mut() as Option<&mut Vec<Box<Any>>>)
|
||||
((*val).downcast_mut() as Option<&mut Vec<Box<dyn Any>>>)
|
||||
.and_then(|arr| idx.downcast_ref::<i64>().map(|idx| (arr, *idx as usize)))
|
||||
.map(|(arr, idx)| arr[idx].clone())
|
||||
.ok_or(EvalAltResult::ErrorIndexMismatch)
|
||||
@@ -322,7 +332,7 @@ impl Engine {
|
||||
map: F,
|
||||
) -> Result<(usize, T), EvalAltResult>
|
||||
where
|
||||
F: FnOnce(&'a mut Any) -> Result<T, EvalAltResult>,
|
||||
F: FnOnce(&'a mut dyn Any) -> Result<T, EvalAltResult>,
|
||||
{
|
||||
scope
|
||||
.iter_mut()
|
||||
@@ -338,13 +348,14 @@ impl Engine {
|
||||
scope: &mut Scope,
|
||||
id: &str,
|
||||
idx: &Expr,
|
||||
) -> Result<(usize, usize, Box<Any>), EvalAltResult> {
|
||||
let idx_boxed = self.eval_expr(scope, idx)?
|
||||
) -> Result<(usize, usize, Box<dyn Any>), EvalAltResult> {
|
||||
let idx_boxed = self
|
||||
.eval_expr(scope, idx)?
|
||||
.downcast::<i64>()
|
||||
.map_err(|_| EvalAltResult::ErrorIndexMismatch)?;
|
||||
let idx = *idx_boxed as usize;
|
||||
let (idx_sc, val) = Self::search_scope(scope, id, |val| {
|
||||
((*val).downcast_mut() as Option<&mut Vec<Box<Any>>>)
|
||||
((*val).downcast_mut() as Option<&mut Vec<Box<dyn Any>>>)
|
||||
.map(|arr| arr[idx].clone())
|
||||
.ok_or(EvalAltResult::ErrorIndexMismatch)
|
||||
})?;
|
||||
@@ -357,7 +368,7 @@ impl Engine {
|
||||
scope: &mut Scope,
|
||||
dot_lhs: &Expr,
|
||||
dot_rhs: &Expr,
|
||||
) -> Result<Box<Any>, EvalAltResult> {
|
||||
) -> Result<Box<dyn Any>, EvalAltResult> {
|
||||
match *dot_lhs {
|
||||
Expr::Identifier(ref id) => {
|
||||
let (sc_idx, mut target) = Self::search_scope(scope, id, |x| Ok(x.box_clone()))?;
|
||||
@@ -375,7 +386,7 @@ impl Engine {
|
||||
|
||||
// In case the expression mutated `target`, we need to reassign it because
|
||||
// of the above `clone`.
|
||||
scope[sc_idx].1.downcast_mut::<Vec<Box<Any>>>().unwrap()[idx] = target;
|
||||
scope[sc_idx].1.downcast_mut::<Vec<Box<dyn Any>>>().unwrap()[idx] = target;
|
||||
|
||||
value
|
||||
}
|
||||
@@ -385,10 +396,10 @@ impl Engine {
|
||||
|
||||
fn set_dot_val_helper(
|
||||
&self,
|
||||
this_ptr: &mut Any,
|
||||
this_ptr: &mut dyn Any,
|
||||
dot_rhs: &Expr,
|
||||
mut source_val: Box<Any>,
|
||||
) -> Result<Box<Any>, EvalAltResult> {
|
||||
mut source_val: Box<dyn Any>,
|
||||
) -> Result<Box<dyn Any>, EvalAltResult> {
|
||||
match *dot_rhs {
|
||||
Expr::Identifier(ref id) => {
|
||||
let set_fn_name = "set$".to_string() + id;
|
||||
@@ -419,8 +430,8 @@ impl Engine {
|
||||
scope: &mut Scope,
|
||||
dot_lhs: &Expr,
|
||||
dot_rhs: &Expr,
|
||||
source_val: Box<Any>,
|
||||
) -> Result<Box<Any>, EvalAltResult> {
|
||||
source_val: Box<dyn Any>,
|
||||
) -> Result<Box<dyn Any>, EvalAltResult> {
|
||||
match *dot_lhs {
|
||||
Expr::Identifier(ref id) => {
|
||||
let (sc_idx, mut target) = Self::search_scope(scope, id, |x| Ok(x.box_clone()))?;
|
||||
@@ -438,7 +449,7 @@ impl Engine {
|
||||
|
||||
// In case the expression mutated `target`, we need to reassign it because
|
||||
// of the above `clone`.
|
||||
scope[sc_idx].1.downcast_mut::<Vec<Box<Any>>>().unwrap()[idx] = target;
|
||||
scope[sc_idx].1.downcast_mut::<Vec<Box<dyn Any>>>().unwrap()[idx] = target;
|
||||
|
||||
value
|
||||
}
|
||||
@@ -446,7 +457,7 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_expr(&self, scope: &mut Scope, expr: &Expr) -> Result<Box<Any>, EvalAltResult> {
|
||||
fn eval_expr(&self, scope: &mut Scope, expr: &Expr) -> Result<Box<dyn Any>, EvalAltResult> {
|
||||
match *expr {
|
||||
Expr::IntConst(i) => Ok(Box::new(i)),
|
||||
Expr::FloatConst(i) => Ok(Box::new(i)),
|
||||
@@ -484,7 +495,7 @@ impl Engine {
|
||||
if *id == *name {
|
||||
if let Some(i) = idx.downcast_ref::<i64>() {
|
||||
if let Some(arr_typed) =
|
||||
(*val).downcast_mut() as Option<&mut Vec<Box<Any>>>
|
||||
(*val).downcast_mut() as Option<&mut Vec<Box<dyn Any>>>
|
||||
{
|
||||
arr_typed[*i as usize] = rhs_val;
|
||||
return Ok(Box::new(()));
|
||||
@@ -520,7 +531,7 @@ impl Engine {
|
||||
fn_name.to_owned(),
|
||||
args.iter()
|
||||
.map(|ex| self.eval_expr(scope, ex))
|
||||
.collect::<Result<Vec<Box<Any>>, _>>()?
|
||||
.collect::<Result<Vec<Box<dyn Any>>, _>>()?
|
||||
.iter_mut()
|
||||
.map(|b| b.as_mut())
|
||||
.collect(),
|
||||
@@ -531,12 +542,12 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_stmt(&self, scope: &mut Scope, stmt: &Stmt) -> Result<Box<Any>, EvalAltResult> {
|
||||
fn eval_stmt(&self, scope: &mut Scope, stmt: &Stmt) -> Result<Box<dyn Any>, EvalAltResult> {
|
||||
match *stmt {
|
||||
Stmt::Expr(ref e) => self.eval_expr(scope, e),
|
||||
Stmt::Block(ref b) => {
|
||||
let prev_len = scope.len();
|
||||
let mut last_result: Result<Box<Any>, EvalAltResult> = Ok(Box::new(()));
|
||||
let mut last_result: Result<Box<dyn Any>, EvalAltResult> = Ok(Box::new(()));
|
||||
|
||||
for s in b.iter() {
|
||||
last_result = self.eval_stmt(scope, s);
|
||||
@@ -621,8 +632,8 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
fn nice_type_name(&self, b: Box<Any>) -> String {
|
||||
let tid = <Any as Any>::type_id(&*b);
|
||||
fn nice_type_name(&self, b: Box<dyn Any>) -> String {
|
||||
let tid = <dyn Any as Any>::type_id(&*b);
|
||||
if let Some(name) = self.type_names.get(&tid) {
|
||||
name.to_string()
|
||||
} else {
|
||||
@@ -650,7 +661,7 @@ impl Engine {
|
||||
|
||||
/// Evaluate a string
|
||||
pub fn eval<T: Any + Clone>(&mut self, input: &str) -> Result<T, EvalAltResult> {
|
||||
let mut scope: Scope = Vec::new();
|
||||
let mut scope = Scope::new();
|
||||
|
||||
self.eval_with_scope(&mut scope, input)
|
||||
}
|
||||
@@ -668,7 +679,7 @@ impl Engine {
|
||||
|
||||
match tree {
|
||||
Ok((ref os, ref fns)) => {
|
||||
let mut x: Result<Box<Any>, EvalAltResult> = Ok(Box::new(()));
|
||||
let mut x: Result<Box<dyn Any>, EvalAltResult> = Ok(Box::new(()));
|
||||
|
||||
for f in fns {
|
||||
let name = f.name.clone();
|
||||
@@ -693,7 +704,9 @@ impl Engine {
|
||||
|
||||
match x.downcast::<T>() {
|
||||
Ok(out) => Ok(*out),
|
||||
Err(a) => Err(EvalAltResult::ErrorMismatchOutputType(self.nice_type_name(a))),
|
||||
Err(a) => Err(EvalAltResult::ErrorMismatchOutputType(
|
||||
self.nice_type_name(a),
|
||||
)),
|
||||
}
|
||||
}
|
||||
Err(_) => Err(EvalAltResult::ErrorFunctionArgMismatch),
|
||||
@@ -786,12 +799,12 @@ impl Engine {
|
||||
engine.register_type_name::<String>("string");
|
||||
engine.register_type_name::<char>("char");
|
||||
engine.register_type_name::<bool>("boolean");
|
||||
engine.register_type_name::<Vec<Box<Any>>>("array");
|
||||
engine.register_type_name::<Vec<Box<dyn Any>>>("array");
|
||||
|
||||
macro_rules! reg_op {
|
||||
($engine:expr, $x:expr, $op:expr, $( $y:ty ),*) => (
|
||||
$(
|
||||
$engine.register_fn($x, ($op as fn(x: $y, y: $y)->$y));
|
||||
$engine.register_fn($x, $op as fn(x: $y, y: $y)->$y);
|
||||
)*
|
||||
)
|
||||
}
|
||||
@@ -799,7 +812,7 @@ impl Engine {
|
||||
macro_rules! reg_un {
|
||||
($engine:expr, $x:expr, $op:expr, $( $y:ty ),*) => (
|
||||
$(
|
||||
$engine.register_fn($x, ($op as fn(x: $y)->$y));
|
||||
$engine.register_fn($x, $op as fn(x: $y)->$y);
|
||||
)*
|
||||
)
|
||||
}
|
||||
@@ -807,36 +820,86 @@ impl Engine {
|
||||
macro_rules! reg_cmp {
|
||||
($engine:expr, $x:expr, $op:expr, $( $y:ty ),*) => (
|
||||
$(
|
||||
$engine.register_fn($x, ($op as fn(x: $y, y: $y)->bool));
|
||||
$engine.register_fn($x, $op as fn(x: $y, y: $y)->bool);
|
||||
)*
|
||||
)
|
||||
}
|
||||
|
||||
fn add<T: Add>(x: T, y: T) -> <T as Add>::Output { x + y }
|
||||
fn sub<T: Sub>(x: T, y: T) -> <T as Sub>::Output { x - y }
|
||||
fn mul<T: Mul>(x: T, y: T) -> <T as Mul>::Output { x * y }
|
||||
fn div<T: Div>(x: T, y: T) -> <T as Div>::Output { x / y }
|
||||
fn neg<T: Neg>(x: T) -> <T as Neg>::Output { -x }
|
||||
fn lt<T: PartialOrd>(x: T, y: T) -> bool { x < y }
|
||||
fn lte<T: PartialOrd>(x: T, y: T) -> bool { x <= y }
|
||||
fn gt<T: PartialOrd>(x: T, y: T) -> bool { x > y }
|
||||
fn gte<T: PartialOrd>(x: T, y: T) -> bool { x >= y }
|
||||
fn eq<T: PartialEq>(x: T, y: T) -> bool { x == y }
|
||||
fn ne<T: PartialEq>(x: T, y: T) -> bool { x != y }
|
||||
fn and(x: bool, y: bool) -> bool { x && y }
|
||||
fn or(x: bool, y: bool) -> bool { x || y }
|
||||
fn not(x: bool) -> bool { !x }
|
||||
fn concat(x: String, y: String) -> String { x + &y }
|
||||
fn binary_and<T: BitAnd>(x: T, y: T) -> <T as BitAnd>::Output { x & y }
|
||||
fn binary_or<T: BitOr>(x: T, y: T) -> <T as BitOr>::Output { x | y }
|
||||
fn binary_xor<T: BitXor>(x: T, y: T) -> <T as BitXor>::Output { x ^ y }
|
||||
fn left_shift<T: Shl<T>>(x: T, y: T) -> <T as Shl<T>>::Output { x.shl(y) }
|
||||
fn right_shift<T: Shr<T>>(x: T, y: T) -> <T as Shr<T>>::Output { x.shr(y) }
|
||||
fn modulo<T: Rem<T>>(x: T, y: T) -> <T as Rem<T>>::Output { x % y }
|
||||
fn pow_i64_i64(x: i64, y: i64) -> i64 { x.pow(y as u32) }
|
||||
fn pow_f64_f64(x: f64, y: f64) -> f64 { x.powf(y) }
|
||||
fn pow_f64_i64(x: f64, y: i64) -> f64 { x.powi(y as i32) }
|
||||
fn unit_eq(a: (), b: ()) -> bool { true }
|
||||
fn add<T: Add>(x: T, y: T) -> <T as Add>::Output {
|
||||
x + y
|
||||
}
|
||||
fn sub<T: Sub>(x: T, y: T) -> <T as Sub>::Output {
|
||||
x - y
|
||||
}
|
||||
fn mul<T: Mul>(x: T, y: T) -> <T as Mul>::Output {
|
||||
x * y
|
||||
}
|
||||
fn div<T: Div>(x: T, y: T) -> <T as Div>::Output {
|
||||
x / y
|
||||
}
|
||||
fn neg<T: Neg>(x: T) -> <T as Neg>::Output {
|
||||
-x
|
||||
}
|
||||
fn lt<T: PartialOrd>(x: T, y: T) -> bool {
|
||||
x < y
|
||||
}
|
||||
fn lte<T: PartialOrd>(x: T, y: T) -> bool {
|
||||
x <= y
|
||||
}
|
||||
fn gt<T: PartialOrd>(x: T, y: T) -> bool {
|
||||
x > y
|
||||
}
|
||||
fn gte<T: PartialOrd>(x: T, y: T) -> bool {
|
||||
x >= y
|
||||
}
|
||||
fn eq<T: PartialEq>(x: T, y: T) -> bool {
|
||||
x == y
|
||||
}
|
||||
fn ne<T: PartialEq>(x: T, y: T) -> bool {
|
||||
x != y
|
||||
}
|
||||
fn and(x: bool, y: bool) -> bool {
|
||||
x && y
|
||||
}
|
||||
fn or(x: bool, y: bool) -> bool {
|
||||
x || y
|
||||
}
|
||||
fn not(x: bool) -> bool {
|
||||
!x
|
||||
}
|
||||
fn concat(x: String, y: String) -> String {
|
||||
x + &y
|
||||
}
|
||||
fn binary_and<T: BitAnd>(x: T, y: T) -> <T as BitAnd>::Output {
|
||||
x & y
|
||||
}
|
||||
fn binary_or<T: BitOr>(x: T, y: T) -> <T as BitOr>::Output {
|
||||
x | y
|
||||
}
|
||||
fn binary_xor<T: BitXor>(x: T, y: T) -> <T as BitXor>::Output {
|
||||
x ^ y
|
||||
}
|
||||
fn left_shift<T: Shl<T>>(x: T, y: T) -> <T as Shl<T>>::Output {
|
||||
x.shl(y)
|
||||
}
|
||||
fn right_shift<T: Shr<T>>(x: T, y: T) -> <T as Shr<T>>::Output {
|
||||
x.shr(y)
|
||||
}
|
||||
fn modulo<T: Rem<T>>(x: T, y: T) -> <T as Rem<T>>::Output {
|
||||
x % y
|
||||
}
|
||||
fn pow_i64_i64(x: i64, y: i64) -> i64 {
|
||||
x.pow(y as u32)
|
||||
}
|
||||
fn pow_f64_f64(x: f64, y: f64) -> f64 {
|
||||
x.powf(y)
|
||||
}
|
||||
fn pow_f64_i64(x: f64, y: i64) -> f64 {
|
||||
x.powi(y as i32)
|
||||
}
|
||||
fn unit_eq(_a: (), _b: ()) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
reg_op!(engine, "+", add, i32, i64, u32, u64, f32, f64);
|
||||
reg_op!(engine, "-", sub, i32, i64, u32, u64, f32, f64);
|
||||
@@ -874,7 +937,6 @@ impl Engine {
|
||||
// FIXME? Registering array lookups are a special case because we want to return boxes
|
||||
// directly let ent = engine.fns.entry("[]".to_string()).or_insert_with(Vec::new);
|
||||
// (*ent).push(FnType::ExternalFn2(Box::new(idx)));
|
||||
|
||||
}
|
||||
|
||||
/// Make a new engine
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use std::any::TypeId;
|
||||
|
||||
use any::Any;
|
||||
use engine::{Engine, EvalAltResult};
|
||||
use crate::any::Any;
|
||||
use crate::engine::{Engine, EvalAltResult};
|
||||
|
||||
pub trait RegisterFn<FN, ARGS, RET> {
|
||||
fn register_fn(&mut self, name: &str, f: FN);
|
||||
@@ -27,13 +27,14 @@ macro_rules! def_register {
|
||||
RET: Any,
|
||||
{
|
||||
fn register_fn(&mut self, name: &str, f: FN) {
|
||||
let fun = move |mut args: Vec<&mut Any>| {
|
||||
let fun = move |mut args: Vec<&mut dyn Any>| {
|
||||
// Check for length at the beginning to avoid
|
||||
// per-element bound checks.
|
||||
if args.len() != count_args!($($par)*) {
|
||||
return Err(EvalAltResult::ErrorFunctionArgMismatch);
|
||||
}
|
||||
|
||||
#[allow(unused_variables, unused_mut)]
|
||||
let mut drain = args.drain(..);
|
||||
$(
|
||||
// Downcast every element, return in case of a type mismatch
|
||||
@@ -43,7 +44,7 @@ macro_rules! def_register {
|
||||
|
||||
// Call the user-supplied function using ($clone) to
|
||||
// potentially clone the value, otherwise pass the reference.
|
||||
Ok(Box::new(f($(($clone)($par)),*)) as Box<Any>)
|
||||
Ok(Box::new(f($(($clone)($par)),*)) as Box<dyn Any>)
|
||||
};
|
||||
self.register_fn_raw(name.to_owned(), Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun));
|
||||
}
|
||||
|
@@ -30,9 +30,7 @@
|
||||
//!
|
||||
//! [Check out the README on GitHub for more information!](https://github.com/jonathandturner/rhai)
|
||||
|
||||
// lints required by Rhai
|
||||
#![allow(warnings, unknown_lints, type_complexity, new_without_default_derive,
|
||||
needless_pass_by_value, too_many_arguments)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
// needs to be here, because order matters for macros
|
||||
macro_rules! debug_println {
|
||||
@@ -50,4 +48,3 @@ mod parser;
|
||||
pub use any::Any;
|
||||
pub use engine::{Engine, EvalAltResult, Scope};
|
||||
pub use fn_register::RegisterFn;
|
||||
|
||||
|
547
src/parser.rs
547
src/parser.rs
@@ -1,8 +1,8 @@
|
||||
use std::char;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::iter::Peekable;
|
||||
use std::str::Chars;
|
||||
use std::char;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum LexError {
|
||||
@@ -10,7 +10,7 @@ pub enum LexError {
|
||||
MalformedEscapeSequence,
|
||||
MalformedNumber,
|
||||
MalformedChar,
|
||||
Nothing
|
||||
Nothing,
|
||||
}
|
||||
|
||||
impl Error for LexError {
|
||||
@@ -20,13 +20,13 @@ impl Error for LexError {
|
||||
LexError::MalformedEscapeSequence => "Unexpected values in escape sequence",
|
||||
LexError::MalformedNumber => "Unexpected characters in number",
|
||||
LexError::MalformedChar => "Char constant not a single character",
|
||||
LexError::Nothing => "This error is for internal use only"
|
||||
LexError::Nothing => "This error is for internal use only",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LexError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.description())
|
||||
}
|
||||
}
|
||||
@@ -65,11 +65,13 @@ impl Error for ParseError {
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> { None }
|
||||
fn cause(&self) -> Option<&dyn Error> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ParseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.description())
|
||||
}
|
||||
}
|
||||
@@ -268,11 +270,7 @@ impl Token {
|
||||
use self::Token::*;
|
||||
|
||||
match *self {
|
||||
UnaryPlus |
|
||||
UnaryMinus |
|
||||
Equals |
|
||||
Bang |
|
||||
Return => true,
|
||||
UnaryPlus | UnaryMinus | Equals | Bang | Return => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -390,14 +388,14 @@ impl<'a> TokenIterator<'a> {
|
||||
fn inner_next(&mut self) -> Option<Token> {
|
||||
while let Some(c) = self.char_stream.next() {
|
||||
match c {
|
||||
'0'...'9' => {
|
||||
'0'..='9' => {
|
||||
let mut result = Vec::new();
|
||||
let mut radix_base: Option<u32> = None;
|
||||
result.push(c);
|
||||
|
||||
while let Some(&nxt) = self.char_stream.peek() {
|
||||
match nxt {
|
||||
'0'...'9' => {
|
||||
'0'..='9' => {
|
||||
result.push(nxt);
|
||||
self.char_stream.next();
|
||||
}
|
||||
@@ -406,7 +404,7 @@ impl<'a> TokenIterator<'a> {
|
||||
self.char_stream.next();
|
||||
while let Some(&nxt_float) = self.char_stream.peek() {
|
||||
match nxt_float {
|
||||
'0'...'9' => {
|
||||
'0'..='9' => {
|
||||
result.push(nxt_float);
|
||||
self.char_stream.next();
|
||||
}
|
||||
@@ -419,7 +417,7 @@ impl<'a> TokenIterator<'a> {
|
||||
self.char_stream.next();
|
||||
while let Some(&nxt_hex) = self.char_stream.peek() {
|
||||
match nxt_hex {
|
||||
'0'...'9' | 'a'...'f' | 'A'...'F' => {
|
||||
'0'..='9' | 'a'..='f' | 'A'..='F' => {
|
||||
result.push(nxt_hex);
|
||||
self.char_stream.next();
|
||||
}
|
||||
@@ -433,7 +431,7 @@ impl<'a> TokenIterator<'a> {
|
||||
self.char_stream.next();
|
||||
while let Some(&nxt_oct) = self.char_stream.peek() {
|
||||
match nxt_oct {
|
||||
'0'...'8' => {
|
||||
'0'..='8' => {
|
||||
result.push(nxt_oct);
|
||||
self.char_stream.next();
|
||||
}
|
||||
@@ -461,7 +459,12 @@ impl<'a> TokenIterator<'a> {
|
||||
}
|
||||
|
||||
if let Some(radix) = radix_base {
|
||||
let out: String = result.iter().cloned().skip(2).filter(|c| c != &'_').collect();
|
||||
let out: String = result
|
||||
.iter()
|
||||
.cloned()
|
||||
.skip(2)
|
||||
.filter(|c| c != &'_')
|
||||
.collect();
|
||||
if let Ok(val) = i64::from_str_radix(&out, radix) {
|
||||
return Some(Token::IntConst(val));
|
||||
}
|
||||
@@ -476,7 +479,7 @@ impl<'a> TokenIterator<'a> {
|
||||
}
|
||||
return Some(Token::LexErr(LexError::MalformedNumber));
|
||||
}
|
||||
'A'...'Z' | 'a'...'z' | '_' => {
|
||||
'A'..='Z' | 'a'..='z' | '_' => {
|
||||
let mut result = Vec::new();
|
||||
result.push(c);
|
||||
|
||||
@@ -505,30 +508,26 @@ impl<'a> TokenIterator<'a> {
|
||||
x => return Some(Token::Identifier(x.to_string())),
|
||||
}
|
||||
}
|
||||
'"' => {
|
||||
match self.parse_string_const('"') {
|
||||
Ok(out) => return Some(Token::StringConst(out)),
|
||||
Err(e) => return Some(Token::LexErr(e)),
|
||||
}
|
||||
}
|
||||
'\'' => {
|
||||
match self.parse_string_const('\'') {
|
||||
Ok(result) => {
|
||||
let mut chars = result.chars();
|
||||
'"' => match self.parse_string_const('"') {
|
||||
Ok(out) => return Some(Token::StringConst(out)),
|
||||
Err(e) => return Some(Token::LexErr(e)),
|
||||
},
|
||||
'\'' => match self.parse_string_const('\'') {
|
||||
Ok(result) => {
|
||||
let mut chars = result.chars();
|
||||
|
||||
if let Some(out) = chars.next() {
|
||||
println!("result: {}", result);
|
||||
if chars.count() != 0 {
|
||||
return Some(Token::LexErr(LexError::MalformedChar));
|
||||
}
|
||||
return Some(Token::CharConst(out));
|
||||
} else {
|
||||
if let Some(out) = chars.next() {
|
||||
println!("result: {}", result);
|
||||
if chars.count() != 0 {
|
||||
return Some(Token::LexErr(LexError::MalformedChar));
|
||||
}
|
||||
return Some(Token::CharConst(out));
|
||||
} else {
|
||||
return Some(Token::LexErr(LexError::MalformedChar));
|
||||
}
|
||||
Err(e) => return Some(Token::LexErr(e)),
|
||||
}
|
||||
}
|
||||
Err(e) => return Some(Token::LexErr(e)),
|
||||
},
|
||||
'{' => return Some(Token::LCurly),
|
||||
'}' => return Some(Token::RCurly),
|
||||
'(' => return Some(Token::LParen),
|
||||
@@ -540,182 +539,168 @@ impl<'a> TokenIterator<'a> {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
Some(Token::PlusAssign)
|
||||
},
|
||||
}
|
||||
_ if self.last.is_next_unary() => Some(Token::UnaryPlus),
|
||||
_ => Some(Token::Plus),
|
||||
}
|
||||
},
|
||||
}
|
||||
'-' => {
|
||||
return match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
Some(Token::MinusAssign)
|
||||
},
|
||||
}
|
||||
_ if self.last.is_next_unary() => Some(Token::UnaryMinus),
|
||||
_ => Some(Token::Minus),
|
||||
}
|
||||
},
|
||||
}
|
||||
'*' => {
|
||||
return match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
Some(Token::MultiplyAssign)
|
||||
},
|
||||
_ => Some(Token::Multiply)
|
||||
}
|
||||
},
|
||||
'/' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'/') => {
|
||||
self.char_stream.next();
|
||||
while let Some(c) = self.char_stream.next() {
|
||||
if c == '\n' { break; }
|
||||
}
|
||||
}
|
||||
Some(&'*') => {
|
||||
let mut level = 1;
|
||||
self.char_stream.next();
|
||||
while let Some(c) = self.char_stream.next() {
|
||||
match c {
|
||||
'/' => if let Some('*') = self.char_stream.next() {
|
||||
level+=1;
|
||||
}
|
||||
'*' => if let Some('/') = self.char_stream.next() {
|
||||
level-=1;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if level == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::DivideAssign);
|
||||
}
|
||||
_ => return Some(Token::Divide),
|
||||
_ => Some(Token::Multiply),
|
||||
}
|
||||
}
|
||||
'/' => match self.char_stream.peek() {
|
||||
Some(&'/') => {
|
||||
self.char_stream.next();
|
||||
while let Some(c) = self.char_stream.next() {
|
||||
if c == '\n' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(&'*') => {
|
||||
let mut level = 1;
|
||||
self.char_stream.next();
|
||||
while let Some(c) = self.char_stream.next() {
|
||||
match c {
|
||||
'/' => {
|
||||
if let Some('*') = self.char_stream.next() {
|
||||
level += 1;
|
||||
}
|
||||
}
|
||||
'*' => {
|
||||
if let Some('/') = self.char_stream.next() {
|
||||
level -= 1;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if level == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::DivideAssign);
|
||||
}
|
||||
_ => return Some(Token::Divide),
|
||||
},
|
||||
';' => return Some(Token::Semicolon),
|
||||
':' => return Some(Token::Colon),
|
||||
',' => return Some(Token::Comma),
|
||||
'.' => return Some(Token::Period),
|
||||
'=' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::EqualTo);
|
||||
}
|
||||
_ => return Some(Token::Equals),
|
||||
}
|
||||
}
|
||||
'<' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::LessThanEqual);
|
||||
}
|
||||
Some(&'<') => {
|
||||
self.char_stream.next();
|
||||
return match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
Some(Token::LeftShiftAssign)
|
||||
},
|
||||
_ => {
|
||||
self.char_stream.next();
|
||||
Some(Token::LeftShift)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return Some(Token::LessThan),
|
||||
}
|
||||
}
|
||||
'>' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::GreaterThanEqual);
|
||||
}
|
||||
Some(&'>') => {
|
||||
self.char_stream.next();
|
||||
return match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
Some(Token::RightShiftAssign)
|
||||
},
|
||||
_ => {
|
||||
self.char_stream.next();
|
||||
Some(Token::RightShift)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return Some(Token::GreaterThan),
|
||||
}
|
||||
}
|
||||
'!' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::NotEqualTo);
|
||||
}
|
||||
_ => return Some(Token::Bang),
|
||||
}
|
||||
}
|
||||
'|' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'|') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::Or);
|
||||
}
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::OrAssign);
|
||||
}
|
||||
_ => return Some(Token::Pipe),
|
||||
}
|
||||
}
|
||||
'&' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'&') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::And);
|
||||
}
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::AndAssign);
|
||||
}
|
||||
_ => return Some(Token::Ampersand),
|
||||
}
|
||||
}
|
||||
'^' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::XOrAssign);
|
||||
}
|
||||
_ => return Some(Token::XOr)
|
||||
'=' => match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::EqualTo);
|
||||
}
|
||||
_ => return Some(Token::Equals),
|
||||
},
|
||||
'%' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::ModuloAssign);
|
||||
}
|
||||
_ => return Some(Token::Modulo)
|
||||
'<' => match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::LessThanEqual);
|
||||
}
|
||||
Some(&'<') => {
|
||||
self.char_stream.next();
|
||||
return match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
Some(Token::LeftShiftAssign)
|
||||
}
|
||||
_ => {
|
||||
self.char_stream.next();
|
||||
Some(Token::LeftShift)
|
||||
}
|
||||
};
|
||||
}
|
||||
_ => return Some(Token::LessThan),
|
||||
},
|
||||
'~' => {
|
||||
match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::PowerOfAssign);
|
||||
}
|
||||
_ => return Some(Token::PowerOf)
|
||||
'>' => match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::GreaterThanEqual);
|
||||
}
|
||||
Some(&'>') => {
|
||||
self.char_stream.next();
|
||||
return match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
Some(Token::RightShiftAssign)
|
||||
}
|
||||
_ => {
|
||||
self.char_stream.next();
|
||||
Some(Token::RightShift)
|
||||
}
|
||||
};
|
||||
}
|
||||
_ => return Some(Token::GreaterThan),
|
||||
},
|
||||
'!' => match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::NotEqualTo);
|
||||
}
|
||||
_ => return Some(Token::Bang),
|
||||
},
|
||||
'|' => match self.char_stream.peek() {
|
||||
Some(&'|') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::Or);
|
||||
}
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::OrAssign);
|
||||
}
|
||||
_ => return Some(Token::Pipe),
|
||||
},
|
||||
'&' => match self.char_stream.peek() {
|
||||
Some(&'&') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::And);
|
||||
}
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::AndAssign);
|
||||
}
|
||||
_ => return Some(Token::Ampersand),
|
||||
},
|
||||
'^' => match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::XOrAssign);
|
||||
}
|
||||
_ => return Some(Token::XOr),
|
||||
},
|
||||
'%' => match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::ModuloAssign);
|
||||
}
|
||||
_ => return Some(Token::Modulo),
|
||||
},
|
||||
'~' => match self.char_stream.peek() {
|
||||
Some(&'=') => {
|
||||
self.char_stream.next();
|
||||
return Some(Token::PowerOfAssign);
|
||||
}
|
||||
_ => return Some(Token::PowerOf),
|
||||
},
|
||||
_x if _x.is_whitespace() => (),
|
||||
_ => return Some(Token::LexErr(LexError::UnexpectedChar)),
|
||||
@@ -739,8 +724,11 @@ impl<'a> Iterator for TokenIterator<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lex(input: &str) -> TokenIterator {
|
||||
TokenIterator { last: Token::LexErr(LexError::Nothing), char_stream: input.chars().peekable() }
|
||||
pub fn lex(input: &str) -> TokenIterator<'_> {
|
||||
TokenIterator {
|
||||
last: Token::LexErr(LexError::Nothing),
|
||||
char_stream: input.chars().peekable(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_precedence(token: &Token) -> i32 {
|
||||
@@ -757,24 +745,17 @@ fn get_precedence(token: &Token) -> i32 {
|
||||
| Token::XOrAssign
|
||||
| Token::ModuloAssign
|
||||
| Token::PowerOfAssign => 10,
|
||||
Token::Or
|
||||
| Token::XOr
|
||||
| Token::Pipe => 11,
|
||||
Token::And
|
||||
| Token::Ampersand => 12,
|
||||
Token::Or | Token::XOr | Token::Pipe => 11,
|
||||
Token::And | Token::Ampersand => 12,
|
||||
Token::LessThan
|
||||
| Token::LessThanEqual
|
||||
| Token::GreaterThan
|
||||
| Token::GreaterThanEqual
|
||||
| Token::EqualTo
|
||||
| Token::NotEqualTo => 15,
|
||||
Token::Plus
|
||||
| Token::Minus => 20,
|
||||
Token::Divide
|
||||
| Token::Multiply
|
||||
| Token::PowerOf => 40,
|
||||
Token::LeftShift
|
||||
| Token::RightShift => 50,
|
||||
Token::Plus | Token::Minus => 20,
|
||||
Token::Divide | Token::Multiply | Token::PowerOf => 40,
|
||||
Token::LeftShift | Token::RightShift => 50,
|
||||
Token::Modulo => 60,
|
||||
Token::Period => 100,
|
||||
_ => -1,
|
||||
@@ -782,7 +763,7 @@ fn get_precedence(token: &Token) -> i32 {
|
||||
}
|
||||
|
||||
fn parse_paren_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
let expr = try!(parse_expr(input));
|
||||
let expr = parse_expr(input)?;
|
||||
|
||||
match input.next() {
|
||||
Some(Token::RParen) => Ok(expr),
|
||||
@@ -790,9 +771,10 @@ fn parse_paren_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_call_expr<'a>(id: String,
|
||||
input: &mut Peekable<TokenIterator<'a>>)
|
||||
-> Result<Expr, ParseError> {
|
||||
fn parse_call_expr<'a>(
|
||||
id: String,
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
) -> Result<Expr, ParseError> {
|
||||
let mut args = Vec::new();
|
||||
|
||||
if let Some(&Token::RParen) = input.peek() {
|
||||
@@ -820,9 +802,10 @@ fn parse_call_expr<'a>(id: String,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_index_expr<'a>(id: String,
|
||||
input: &mut Peekable<TokenIterator<'a>>)
|
||||
-> Result<Expr, ParseError> {
|
||||
fn parse_index_expr<'a>(
|
||||
id: String,
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
) -> Result<Expr, ParseError> {
|
||||
if let Ok(idx) = parse_expr(input) {
|
||||
match input.peek() {
|
||||
Some(&Token::RSquare) => {
|
||||
@@ -836,9 +819,10 @@ fn parse_index_expr<'a>(id: String,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_ident_expr<'a>(id: String,
|
||||
input: &mut Peekable<TokenIterator<'a>>)
|
||||
-> Result<Expr, ParseError> {
|
||||
fn parse_ident_expr<'a>(
|
||||
id: String,
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
) -> Result<Expr, ParseError> {
|
||||
match input.peek() {
|
||||
Some(&Token::LParen) => {
|
||||
input.next();
|
||||
@@ -862,12 +846,14 @@ fn parse_array_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
|
||||
|
||||
if !skip_contents {
|
||||
while let Some(_) = input.peek() {
|
||||
arr.push(try!(parse_expr(input)));
|
||||
arr.push(parse_expr(input)?);
|
||||
if let Some(&Token::Comma) = input.peek() {
|
||||
input.next();
|
||||
}
|
||||
|
||||
if let Some(&Token::RSquare) = input.peek() { break }
|
||||
if let Some(&Token::RSquare) = input.peek() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -878,7 +864,6 @@ fn parse_array_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr,
|
||||
}
|
||||
_ => Err(ParseError::MissingRSquare),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn parse_primary<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
|
||||
@@ -914,17 +899,27 @@ fn parse_unary<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, Pars
|
||||
};
|
||||
|
||||
match tok {
|
||||
Token::UnaryMinus => { input.next(); Ok(Expr::FnCall("-".to_string(), vec![parse_primary(input)?])) }
|
||||
Token::UnaryPlus => { input.next(); parse_primary(input) }
|
||||
Token::Bang => { input.next(); Ok(Expr::FnCall("!".to_string(), vec![parse_primary(input)?])) }
|
||||
_ => parse_primary(input)
|
||||
Token::UnaryMinus => {
|
||||
input.next();
|
||||
Ok(Expr::FnCall("-".to_string(), vec![parse_primary(input)?]))
|
||||
}
|
||||
Token::UnaryPlus => {
|
||||
input.next();
|
||||
parse_primary(input)
|
||||
}
|
||||
Token::Bang => {
|
||||
input.next();
|
||||
Ok(Expr::FnCall("!".to_string(), vec![parse_primary(input)?]))
|
||||
}
|
||||
_ => parse_primary(input),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>,
|
||||
prec: i32,
|
||||
lhs: Expr)
|
||||
-> Result<Expr, ParseError> {
|
||||
fn parse_binop<'a>(
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
prec: i32,
|
||||
lhs: Expr,
|
||||
) -> Result<Expr, ParseError> {
|
||||
let mut lhs_curr = lhs;
|
||||
|
||||
loop {
|
||||
@@ -939,7 +934,7 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>,
|
||||
}
|
||||
|
||||
if let Some(op_token) = input.next() {
|
||||
let mut rhs = try!(parse_unary(input));
|
||||
let mut rhs = parse_unary(input)?;
|
||||
|
||||
let mut next_prec = -1;
|
||||
|
||||
@@ -948,10 +943,10 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>,
|
||||
}
|
||||
|
||||
if curr_prec < next_prec {
|
||||
rhs = try!(parse_binop(input, curr_prec + 1, rhs));
|
||||
rhs = parse_binop(input, curr_prec + 1, rhs)?;
|
||||
} else if curr_prec >= 100 {
|
||||
// Always bind right to left for precedence over 100
|
||||
rhs = try!(parse_binop(input, curr_prec, rhs));
|
||||
rhs = parse_binop(input, curr_prec, rhs)?;
|
||||
}
|
||||
|
||||
lhs_curr = match op_token {
|
||||
@@ -960,31 +955,27 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>,
|
||||
Token::Multiply => Expr::FnCall("*".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::Divide => Expr::FnCall("/".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::Equals => Expr::Assignment(Box::new(lhs_curr), Box::new(rhs)),
|
||||
Token::PlusAssign => {
|
||||
Token::PlusAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("+".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("+".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
Token::MinusAssign => {
|
||||
}
|
||||
Token::MinusAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("-".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("-".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
}
|
||||
Token::Period => Expr::Dot(Box::new(lhs_curr), Box::new(rhs)),
|
||||
Token::EqualTo => Expr::FnCall("==".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::NotEqualTo => Expr::FnCall("!=".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::LessThan => Expr::FnCall("<".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::LessThanEqual => {
|
||||
Expr::FnCall("<=".to_string(), vec![lhs_curr, rhs])
|
||||
}
|
||||
Token::LessThanEqual => Expr::FnCall("<=".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::GreaterThan => Expr::FnCall(">".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::GreaterThanEqual => {
|
||||
Expr::FnCall(">=".to_string(), vec![lhs_curr, rhs])
|
||||
}
|
||||
Token::GreaterThanEqual => Expr::FnCall(">=".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::Or => Expr::FnCall("||".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::And => Expr::FnCall("&&".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::XOr => Expr::FnCall("^".to_string(), vec![lhs_curr, rhs]),
|
||||
@@ -992,73 +983,71 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>,
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("|".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("|".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
}
|
||||
Token::AndAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("&".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("&".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
}
|
||||
Token::XOrAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("^".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("^".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
}
|
||||
Token::MultiplyAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("*".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("*".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
}
|
||||
Token::DivideAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("/".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("/".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
Token::Pipe => {
|
||||
Expr::FnCall("|".to_string(), vec![lhs_curr, rhs])
|
||||
},
|
||||
}
|
||||
Token::Pipe => Expr::FnCall("|".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::LeftShift => Expr::FnCall("<<".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::RightShift => Expr::FnCall(">>".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::LeftShiftAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("<<".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("<<".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
}
|
||||
Token::RightShiftAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall(">>".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall(">>".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
}
|
||||
Token::Ampersand => Expr::FnCall("&".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::Modulo => Expr::FnCall("%".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::ModuloAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("%".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("%".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
}
|
||||
Token::PowerOf => Expr::FnCall("~".to_string(), vec![lhs_curr, rhs]),
|
||||
Token::PowerOfAssign => {
|
||||
let lhs_copy = lhs_curr.clone();
|
||||
Expr::Assignment(
|
||||
Box::new(lhs_curr),
|
||||
Box::new(Expr::FnCall("~".to_string(), vec![lhs_copy, rhs]))
|
||||
Box::new(Expr::FnCall("~".to_string(), vec![lhs_copy, rhs])),
|
||||
)
|
||||
},
|
||||
}
|
||||
_ => return Err(ParseError::UnknownOperator),
|
||||
};
|
||||
}
|
||||
@@ -1069,7 +1058,7 @@ fn parse_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, Parse
|
||||
match input.peek() {
|
||||
Some(Token::RParen) => Ok(Expr::Unit),
|
||||
_ => {
|
||||
let lhs = try!(parse_unary(input));
|
||||
let lhs = parse_unary(input)?;
|
||||
|
||||
parse_binop(input, 0, lhs)
|
||||
}
|
||||
@@ -1079,14 +1068,18 @@ fn parse_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, Parse
|
||||
fn parse_if<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseError> {
|
||||
input.next();
|
||||
|
||||
let guard = try!(parse_expr(input));
|
||||
let body = try!(parse_block(input));
|
||||
let guard = parse_expr(input)?;
|
||||
let body = parse_block(input)?;
|
||||
|
||||
match input.peek() {
|
||||
Some(&Token::Else) => {
|
||||
input.next();
|
||||
let else_body = try!(parse_block(input));
|
||||
Ok(Stmt::IfElse(Box::new(guard), Box::new(body), Box::new(else_body)))
|
||||
let else_body = parse_block(input)?;
|
||||
Ok(Stmt::IfElse(
|
||||
Box::new(guard),
|
||||
Box::new(body),
|
||||
Box::new(else_body),
|
||||
))
|
||||
}
|
||||
_ => Ok(Stmt::If(Box::new(guard), Box::new(body))),
|
||||
}
|
||||
@@ -1095,8 +1088,8 @@ fn parse_if<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseEr
|
||||
fn parse_while<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseError> {
|
||||
input.next();
|
||||
|
||||
let guard = try!(parse_expr(input));
|
||||
let body = try!(parse_block(input));
|
||||
let guard = parse_expr(input)?;
|
||||
let body = parse_block(input)?;
|
||||
|
||||
Ok(Stmt::While(Box::new(guard), Box::new(body)))
|
||||
}
|
||||
@@ -1104,7 +1097,7 @@ fn parse_while<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
fn parse_loop<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseError> {
|
||||
input.next();
|
||||
|
||||
let body = try!(parse_block(input));
|
||||
let body = parse_block(input)?;
|
||||
|
||||
Ok(Stmt::Loop(Box::new(body)))
|
||||
}
|
||||
@@ -1120,7 +1113,7 @@ fn parse_var<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseE
|
||||
match input.peek() {
|
||||
Some(&Token::Equals) => {
|
||||
input.next();
|
||||
let initializer = try!(parse_expr(input));
|
||||
let initializer = parse_expr(input)?;
|
||||
Ok(Stmt::Var(name, Some(Box::new(initializer))))
|
||||
}
|
||||
_ => Ok(Stmt::Var(name, None)),
|
||||
@@ -1144,13 +1137,15 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
|
||||
if !skip_body {
|
||||
while let Some(_) = input.peek() {
|
||||
stmts.push(try!(parse_stmt(input)));
|
||||
stmts.push(parse_stmt(input)?);
|
||||
|
||||
if let Some(&Token::Semicolon) = input.peek() {
|
||||
input.next();
|
||||
}
|
||||
|
||||
if let Some(&Token::RCurly) = input.peek() { break }
|
||||
if let Some(&Token::RCurly) = input.peek() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1164,7 +1159,7 @@ fn parse_block<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Pars
|
||||
}
|
||||
|
||||
fn parse_expr_stmt<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseError> {
|
||||
let expr = try!(parse_expr(input));
|
||||
let expr = parse_expr(input)?;
|
||||
Ok(Stmt::Expr(Box::new(expr)))
|
||||
}
|
||||
|
||||
@@ -1182,7 +1177,7 @@ fn parse_stmt<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Parse
|
||||
match input.peek() {
|
||||
Some(&Token::Semicolon) => Ok(Stmt::Return),
|
||||
_ => {
|
||||
let ret = try!(parse_expr(input));
|
||||
let ret = parse_expr(input)?;
|
||||
Ok(Stmt::ReturnWithVal(Box::new(ret)))
|
||||
}
|
||||
}
|
||||
@@ -1240,15 +1235,16 @@ fn parse_fn<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<FnDef, ParseE
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_top_level<'a>(input: &mut Peekable<TokenIterator<'a>>)
|
||||
-> Result<(Vec<Stmt>, Vec<FnDef>), ParseError> {
|
||||
fn parse_top_level<'a>(
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
) -> Result<(Vec<Stmt>, Vec<FnDef>), ParseError> {
|
||||
let mut stmts = Vec::new();
|
||||
let mut fndefs = Vec::new();
|
||||
|
||||
while let Some(_) = input.peek() {
|
||||
match input.peek() {
|
||||
Some(&Token::Fn) => fndefs.push(try!(parse_fn(input))),
|
||||
_ => stmts.push(try!(parse_stmt(input))),
|
||||
Some(&Token::Fn) => fndefs.push(parse_fn(input)?),
|
||||
_ => stmts.push(parse_stmt(input)?),
|
||||
}
|
||||
|
||||
if let Some(&Token::Semicolon) = input.peek() {
|
||||
@@ -1259,7 +1255,8 @@ fn parse_top_level<'a>(input: &mut Peekable<TokenIterator<'a>>)
|
||||
Ok((stmts, fndefs))
|
||||
}
|
||||
|
||||
pub fn parse<'a>(input: &mut Peekable<TokenIterator<'a>>)
|
||||
-> Result<(Vec<Stmt>, Vec<FnDef>), ParseError> {
|
||||
pub fn parse<'a>(
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
) -> Result<(Vec<Stmt>, Vec<FnDef>), ParseError> {
|
||||
parse_top_level(input)
|
||||
}
|
||||
|
Reference in New Issue
Block a user