Replace manual type_id with builtin function of trait Any

Relates to #44
This commit is contained in:
Sebastian Schmidt 2019-04-13 20:31:47 +02:00
parent eb5c6e99bd
commit 2af40309f6
2 changed files with 3 additions and 97 deletions

View File

@ -7,10 +7,6 @@ use crate::{context::Context, error::*, value::Value};
mod display;
pub trait Operator: Debug + Display {
/// A numeric id to compare operator types.
// Make this a const fn once #57563 is resolved
fn id(&self) -> i32;
/// Returns the precedence of the operator.
/// A high precedence means that the operator has priority to be deeper in the tree.
// Make this a const fn once #57563 is resolved
@ -133,10 +129,6 @@ impl FunctionIdentifier {
}
impl Operator for RootNode {
fn id(&self) -> i32 {
1
}
fn precedence(&self) -> i32 {
200
}
@ -155,10 +147,6 @@ impl Operator for RootNode {
}
impl Operator for Add {
fn id(&self) -> i32 {
2
}
fn precedence(&self) -> i32 {
95
}
@ -196,10 +184,6 @@ impl Operator for Add {
}
impl Operator for Sub {
fn id(&self) -> i32 {
3
}
fn precedence(&self) -> i32 {
95
}
@ -232,10 +216,6 @@ impl Operator for Sub {
}
impl Operator for Neg {
fn id(&self) -> i32 {
4
}
fn precedence(&self) -> i32 {
110
}
@ -262,10 +242,6 @@ impl Operator for Neg {
}
impl Operator for Mul {
fn id(&self) -> i32 {
5
}
fn precedence(&self) -> i32 {
100
}
@ -298,10 +274,6 @@ impl Operator for Mul {
}
impl Operator for Div {
fn id(&self) -> i32 {
6
}
fn precedence(&self) -> i32 {
100
}
@ -334,10 +306,6 @@ impl Operator for Div {
}
impl Operator for Mod {
fn id(&self) -> i32 {
7
}
fn precedence(&self) -> i32 {
100
}
@ -370,10 +338,6 @@ impl Operator for Mod {
}
impl Operator for Exp {
fn id(&self) -> i32 {
8
}
fn precedence(&self) -> i32 {
120
}
@ -397,10 +361,6 @@ impl Operator for Exp {
}
impl Operator for Eq {
fn id(&self) -> i32 {
9
}
fn precedence(&self) -> i32 {
80
}
@ -421,10 +381,6 @@ impl Operator for Eq {
}
impl Operator for Neq {
fn id(&self) -> i32 {
10
}
fn precedence(&self) -> i32 {
80
}
@ -445,10 +401,6 @@ impl Operator for Neq {
}
impl Operator for Gt {
fn id(&self) -> i32 {
11
}
fn precedence(&self) -> i32 {
80
}
@ -485,10 +437,6 @@ impl Operator for Gt {
}
impl Operator for Lt {
fn id(&self) -> i32 {
12
}
fn precedence(&self) -> i32 {
80
}
@ -525,10 +473,6 @@ impl Operator for Lt {
}
impl Operator for Geq {
fn id(&self) -> i32 {
13
}
fn precedence(&self) -> i32 {
80
}
@ -565,10 +509,6 @@ impl Operator for Geq {
}
impl Operator for Leq {
fn id(&self) -> i32 {
14
}
fn precedence(&self) -> i32 {
80
}
@ -605,10 +545,6 @@ impl Operator for Leq {
}
impl Operator for And {
fn id(&self) -> i32 {
15
}
fn precedence(&self) -> i32 {
75
}
@ -631,10 +567,6 @@ impl Operator for And {
}
impl Operator for Or {
fn id(&self) -> i32 {
16
}
fn precedence(&self) -> i32 {
70
}
@ -657,10 +589,6 @@ impl Operator for Or {
}
impl Operator for Not {
fn id(&self) -> i32 {
17
}
fn precedence(&self) -> i32 {
110
}
@ -682,10 +610,6 @@ impl Operator for Not {
}
impl Operator for Tuple {
fn id(&self) -> i32 {
18
}
fn precedence(&self) -> i32 {
40
}
@ -725,10 +649,6 @@ impl Operator for Tuple {
}
impl Operator for Assign {
fn id(&self) -> i32 {
19
}
fn precedence(&self) -> i32 {
50
}
@ -755,10 +675,6 @@ impl Operator for Assign {
}
impl Operator for Chain {
fn id(&self) -> i32 {
20
}
fn precedence(&self) -> i32 {
0
}
@ -781,10 +697,6 @@ impl Operator for Chain {
}
impl Operator for Const {
fn id(&self) -> i32 {
21
}
fn precedence(&self) -> i32 {
200
}
@ -801,10 +713,6 @@ impl Operator for Const {
}
impl Operator for VariableIdentifier {
fn id(&self) -> i32 {
22
}
fn precedence(&self) -> i32 {
200
}
@ -829,10 +737,6 @@ impl Operator for VariableIdentifier {
}
impl Operator for FunctionIdentifier {
fn id(&self) -> i32 {
23
}
fn precedence(&self) -> i32 {
190
}

View File

@ -11,6 +11,8 @@ use crate::{
operator::*,
value::Value,
};
use std::error::Error;
use std::any::Any;
mod display;
mod iter;
@ -337,7 +339,7 @@ impl Node {
.last_mut()
.unwrap()
.insert_back_prioritized(node, false)
} else if self.children.last().unwrap().operator().id() == node.operator().id() && node.operator().is_flatten_chains() && !self.children.last().unwrap().has_enough_children() {
} else if self.children.last().unwrap().operator().type_id() == node.operator().type_id() && node.operator().is_flatten_chains() && !self.children.last().unwrap().has_enough_children() {
// The operators will be chained together, and the next value will be added to this nodes last child.
Ok(())
} else {