From 99c8e92238986d00557ded56adc8495086d3c631 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 7 Nov 2012 19:08:15 +0100 Subject: fixed "," operator instead of pushing a special number (which wasn't that special...), use a dedicated "new" operator which does not count as an argument operator, nor does it count as an ordinary operator and it is popped before any number (and "number" operator) is pushed --- expressions.cpp | 41 +++++++++++++++++++++++------------------ expressions.h | 13 ++----------- parser.cpp | 5 ++--- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/expressions.cpp b/expressions.cpp index 8c8fb0c..38af2fc 100644 --- a/expressions.cpp +++ b/expressions.cpp @@ -23,8 +23,8 @@ Expressions::set_radix(gint r) gint64 Expressions::push(gint64 number) { - while (numbers.items() > 0 && numbers.peek() == G_MAXINT64) - pop_num(); + while (operators.items() && operators.peek() == OP_NEW) + pop_op(); push(OP_NUMBER); @@ -35,7 +35,7 @@ Expressions::push(gint64 number) gint64 Expressions::pop_num(int index) { - gint64 n = G_MAXINT64; + gint64 n = 0; pop_op(); if (numbers.items() > 0) { @@ -49,30 +49,17 @@ Expressions::pop_num(int index) gint64 Expressions::pop_num_calc(int index, gint64 imply) { - gint64 n = G_MAXINT64; - eval(); - if (args() > 0) - n = pop_num(index); - if (n == G_MAXINT64) - n = imply; - if (num_sign < 0) set_num_sign(1); - return n; + return args() > 0 ? pop_num(index) : imply; } gint64 Expressions::add_digit(gchar digit) { - gint64 n = 0; - - if (args() > 0) { - n = pop_num(); - if (n == G_MAXINT64) - n = 0; - } + gint64 n = args() > 0 ? pop_num() : 0; return push(n*radix + num_sign*(digit - '0')); } @@ -177,6 +164,24 @@ Expressions::args(void) return n; } +int +Expressions::first_op(void) +{ + int items = operators.items(); + + for (int i = 1; i <= items; i++) { + switch (operators.peek(i)) { + case OP_NUMBER: + case OP_NEW: + break; + default: + return i; + } + } + + return 0; +} + void Expressions::discard_args(void) { diff --git a/expressions.h b/expressions.h index 4a33632..0f4c9eb 100644 --- a/expressions.h +++ b/expressions.h @@ -110,6 +110,7 @@ public: OP_AND, // & OP_OR, // # // pseudo operators: + OP_NEW, OP_BRACE, OP_LOOP, OP_NUMBER @@ -130,11 +131,6 @@ public: gint64 push(gint64 number); - inline gint64 - peek_num(int index = 1) - { - return numbers.peek(index); - } gint64 pop_num(int index = 1); gint64 pop_num_calc(int index, gint64 imply); inline gint64 @@ -155,12 +151,7 @@ public: void eval(bool pop_brace = false); int args(void); - inline int - first_op(void) - { - int n = args() + 1; - return n > operators.items() ? 0 : n; - } + int first_op(void); void discard_args(void); } expressions; diff --git a/parser.cpp b/parser.cpp index 7978962..3178421 100644 --- a/parser.cpp +++ b/parser.cpp @@ -111,8 +111,7 @@ StateStart::custom(gchar chr) expressions.push_calc(Expressions::OP_ADD); break; case '-': - if (!expressions.args() || - expressions.peek_num() == G_MAXINT64) + if (!expressions.args()) expressions.set_num_sign(-expressions.num_sign); else expressions.push_calc(Expressions::OP_SUB); @@ -135,7 +134,7 @@ StateStart::custom(gchar chr) break; case ',': expressions.eval(); - expressions.push(G_MAXINT64); + expressions.push(Expressions::OP_NEW); break; /* * commands -- cgit v1.2.3