aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/expressions.h
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2015-09-23 17:53:39 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2015-09-23 17:53:39 +0200
commitecbc58fb917c292f05bed401afe7be0a80971d80 (patch)
treecbd6b0e2bb8d5f2638acae023b50134f516371e9 /src/expressions.h
parentc3f7aa7252ad9adb51cef1e35f566883ef953aad (diff)
downloadsciteco-ecbc58fb917c292f05bed401afe7be0a80971d80.tar.gz
different operators can have the same precedence now
* SciTECO now has the same operator precedence table as C. * It is numerically important whether different operators have the same precedence. E.g. "5*2/4" used to be evaluated by SciTECO as "5*(2/4)" since division had a higher precedence than multiplication. Within in real (!) numbers this would be the expected evaluation order. Users of other programming languages however would expect the expression to be evaluated as "(5*2)/4" which makes a numerical difference when working with integers. * Operator precedence has been implemented by encoding it into the enumeration values used to represent different operators. Calculating the precedence of a given operator can then be done very efficiently and elegantly (in our case using a plain right shift operation). * documentation updated. We use a precedence table now.
Diffstat (limited to 'src/expressions.h')
-rw-r--r--src/expressions.h34
1 files changed, 24 insertions, 10 deletions
diff --git a/src/expressions.h b/src/expressions.h
index f97ec1c..e49880c 100644
--- a/src/expressions.h
+++ b/src/expressions.h
@@ -150,31 +150,45 @@ public:
}
};
-/*
+/**
* Arithmetic expression stacks
*/
extern class Expressions {
public:
- /* reflects also operator precedence */
+ /**
+ * Operator type.
+ * The enumeration value divided by 16 represents
+ * its precedence (the lower, the higher).
+ * In other words, the value's lower nibble is
+ * reserved for enumerating operators of the
+ * same precedence.
+ */
enum Operator {
- OP_NIL = 0,
- OP_POW, // ^*
- OP_MOD, // ^/
+ OP_NIL = 0,
+ OP_POW = 0x10, // ^*
+ OP_MOD = 0x20, // ^/
OP_DIV, // /
OP_MUL, // *
- OP_SUB, // -
+ OP_SUB = 0x30, // -
OP_ADD, // +
- OP_AND, // &
- OP_XOR, // ^#
- OP_OR, // #
+ OP_AND = 0x40, // &
+ OP_XOR = 0x50, // ^#
+ OP_OR = 0x60, // #
// pseudo operators:
- OP_NEW,
+ OP_NEW = 0xF0,
OP_BRACE,
OP_LOOP,
OP_NUMBER
};
private:
+ /** Get operator precedence */
+ inline gint
+ precedence(Operator op)
+ {
+ return op >> 4;
+ }
+
ValueStack<tecoInt> numbers;
ValueStack<Operator> operators;