diff --git a/src/debug.h b/src/debug.h index 072e0fb..0a304d1 100644 --- a/src/debug.h +++ b/src/debug.h @@ -44,6 +44,6 @@ static void dump8(unsigned char* to_dump, unsigned long offset, } // -#define TRACE(...) do { (void)printf(__FUNCTION__ ": "); (void)printf(__VA_ARGS__); } while(0) +#define TRACE(...) do { printf(__VA_ARGS__); } while(0) #endif diff --git a/src/main.cpp b/src/main.cpp index 78ed2d0..6f4370a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,7 +27,7 @@ int main(int argc, char* argv[]) if (prog.run(s_global_stack, s_global_heap) == ret_good_bye) break; else - program::show_stack(s_global_stack, false); + program::show_stack(s_global_stack); } } } @@ -53,7 +53,7 @@ int main(int argc, char* argv[]) // run it ret = prog.run(s_global_stack, s_global_heap); - program::show_stack(s_global_stack); + program::show_stack(s_global_stack, true); } } diff --git a/src/object.hpp b/src/object.hpp index 15477a6..1757d83 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -161,7 +161,7 @@ struct number : public object struct ostring : public object { - // + // ostring may first have been allocated with len+1 bytes void set(const char* value, unsigned int len) { _type = cmd_string; @@ -173,17 +173,20 @@ struct ostring : public object _len = len; } else - len = 0; + { + _value[_len] = 0; + _len = 0; + } } - // + // length of _value, not includiong the terminal '\0' unsigned int _len; char _value[0]; }; struct oprogram : public object { - // + // oprogram may first have been allocated with len+1 bytes void set(const char* value, unsigned int len) { _type = cmd_program; @@ -195,17 +198,20 @@ struct oprogram : public object _len = len; } else - len = 0; + { + _value[0] = 0; + _len = 0; + } } - // + // length of _value, not includiong the terminal '\0' unsigned int _len; char _value[0]; }; struct symbol : public object { - // + // symbol may first have been allocated with len+1 bytes void set(const char* value, unsigned int len, bool auto_eval) { _type = cmd_symbol; @@ -219,18 +225,22 @@ struct symbol : public object _len = len; } else - len = 0; + { + _value[0] = 0; + _len = 0; + } } // bool _auto_eval; + // length of _value, not includiong the terminal '\0' unsigned int _len; char _value[0]; }; struct keyword : public object { - // + // keyword may first have been allocated with len+1 bytes void set(program_fn_t fn, const char* value, unsigned int len) { _type = cmd_keyword; @@ -243,11 +253,15 @@ struct keyword : public object _len = len; } else - len = 0; + { + _value[0] = 0; + _len = 0; + } } // program_fn_t _fn; + // length of _value, not includiong the terminal '\0' unsigned int _len; char _value[0]; }; @@ -273,7 +287,10 @@ struct branch : public object _len = len; } else - len = 0; + { + _value[0] = 0; + _len = 0; + } } // branch function @@ -282,6 +299,8 @@ struct branch : public object int arg1, arg2, arg3; number *farg1, *farg2; bool arg_bool; + + // length of _value, not includiong the terminal '\0' unsigned int _len; char _value[0]; }; diff --git a/src/parse.hpp b/src/parse.hpp index c2573ae..2337bf2 100644 --- a/src/parse.hpp +++ b/src/parse.hpp @@ -140,7 +140,7 @@ static bool _cut(const char* entry, vector& entries) tmp = "<< "; // trim leading spaces - while (i < len && (entry[i]==' ' || entry[i]=='\t')) + while (i < len && isspace(entry[i])) i++; while(i < len) @@ -149,20 +149,25 @@ static bool _cut(const char* entry, vector& entries) { up++; i += 2; - tmp += " << "; + tmp += "<< "; // trim leading spaces - while (i < len && (entry[i] == ' ' || entry[i] == '\t')) + while (i < len && isspace(entry[i])) i++; } else if (strncmp(&entry[i], ">>", 2) == 0) - { + { + if (isspace(entry[i-1]) && entry[i-2]!='>') + tmp += ">>"; + else + tmp += " >>"; + up--; i += 2; - tmp += " >>"; // trim trailing spaces - while (i < len && (entry[i] == ' ' || entry[i] == '\t')) + while (i < len && isspace(entry[i])) i++; + // found end if (up == 0) break; @@ -174,9 +179,8 @@ static bool _cut(const char* entry, vector& entries) } } while((up--)>0) - { tmp += " >>"; - } + if (tmp.size()>0) { entries.push_back(tmp); @@ -185,15 +189,13 @@ static bool _cut(const char* entry, vector& entries) i--;// i has move 1 too far } else - { // reinject '<'' which is not a prog begin tmp = "<"; - } break; //other default: - if (entry[i] != ' ' && entry[i] != '\t') + if (!isspace(entry[i])) { tmp += entry[i]; } @@ -349,7 +351,7 @@ static bool get_string(const string& entry, program& prog, string& remaining_ent return ret; } -static bool get_program(const string& entry, program& prog, string& remaining_entry) +static bool get_program(string& entry, program& prog, string& remaining_entry) { bool ret = false; unsigned int obj_len; @@ -412,7 +414,7 @@ static bool get_number(const string& entry, program& prog, string& remaining_ent return ret; } -static bool _obj_from_string(const string& entry, program& prog, string& remaining_entry) +static bool _obj_from_string(string& entry, program& prog, string& remaining_entry) { bool ret = false; diff --git a/src/rpn-branch.hpp b/src/rpn-branch.hpp index 2bee27c..4c46b08 100644 --- a/src/rpn-branch.hpp +++ b/src/rpn-branch.hpp @@ -105,7 +105,6 @@ int rpn_for(branch& myobj) { // store symbol with first value _local_heap.add(sym->_value, (object*)myobj.farg1, myobj.farg1->size()); - (void)_stack->pop_back(); ret = myobj.arg1 + 1; } diff --git a/src/rpn-stack.hpp b/src/rpn-stack.hpp index 1b20244..9c03034 100644 --- a/src/rpn-stack.hpp +++ b/src/rpn-stack.hpp @@ -6,11 +6,12 @@ void swap(void) stack::copy_and_push_back(*_stack, _stack->size()-2, _branch_stack); (void)_stack->pop_back(); (void)_stack->pop_back(); - stack::copy_and_push_back(_branch_stack, 0, *_stack); - stack::copy_and_push_back(_branch_stack, 1, *_stack); - _branch_stack.erase(); -} + stack::copy_and_push_back(_branch_stack, _branch_stack.size()-2, *_stack); + stack::copy_and_push_back(_branch_stack, _branch_stack.size()-1, *_stack); + _branch_stack.pop_back(); + _branch_stack.pop_back(); +} void drop(void) { MIN_ARGUMENTS(1); @@ -95,10 +96,12 @@ void rot(void) (void)_stack->pop_back(); (void)_stack->pop_back(); (void)_stack->pop_back(); - stack::copy_and_push_back(_branch_stack, 1, *_stack); - stack::copy_and_push_back(_branch_stack, 2, *_stack); - stack::copy_and_push_back(_branch_stack, 0, *_stack); - _branch_stack.erase(); + stack::copy_and_push_back(_branch_stack, _branch_stack.size()-2, *_stack); + stack::copy_and_push_back(_branch_stack, _branch_stack.size()-1, *_stack); + stack::copy_and_push_back(_branch_stack, _branch_stack.size()-3, *_stack); + _branch_stack.pop_back(); + _branch_stack.pop_back(); + _branch_stack.pop_back(); } void depth(void) @@ -127,7 +130,8 @@ void roll(void) stack::copy_and_push_back(_branch_stack, args-1-i, *_stack); stack::copy_and_push_back(_branch_stack, args-1, *_stack); - _branch_stack.erase(); + for(int i=0;ipop_back(); } - stack::copy_and_push_back(_branch_stack, 0, *_stack); - for(int i=1;i stack size should be 1 -> stack should be 1 - drop + +# real decimal (2) 2.345 -> stack should be 2.345 - erase + +# real decimal (3) 1 2.345 3 4.9 -> stack size should be 4 -> stack should be 1, 2.345, 3, 4.9 +erase # real hex -erase 0x1234 0x10.10 -> stack should be 0x1.234p+12, 0x1.01p+4 @@ -27,16 +29,16 @@ dec swap dec swap erase # real binary -erase 0b11001100 -> stack should be 204 - erase + +# real binary (2) 0b11001100.1101 -> stack should be 204.8125 +erase # real inf, nan -erase inf @inf@ +inf @@ -46,46 +48,76 @@ inf nan @nan@ -> stack should be inf, inf, inf, inf, -inf, -inf, nan, nan +erase # symbol -erase 'test' -> stack size should be 1 -> stack should be 'test' - erase + +# symbol (2) 'test -> stack size should be 1 -> stack should be 'test' - erase + +# symbol (3) '' -> stack size should be 1 -> stack should be '' +erase + +# symbol (4) +' +-> stack size should be 1 +-> stack should be '' +erase # string -erase "test string" -> stack size should be 1 -> stack should be "test string" - erase + +# string (2) "test string -> stack size should be 1 -> stack should be "test string" +erase + +# string (3) +" +-> stack size should be 1 +-> stack should be "" +erase # program -erase << 'one' >> -> stack size should be 1 --> stack should be << 'one' >> - +-> stack should be << 'one' >> erase + +# program (2) << 'one' 2 -> stack size should be 1 -> stack should be << 'one' 2 >> - erase -<< -> n << n 2 + >> >> + +# program (3) +<< -> stack size should be 1 --> stack should be << -> n << n 2 + >> >> +-> stack should be << >> +erase + +# program (4) +<< << << << +-> stack size should be 1 +-> stack should be << << << << >> >> >> >> +erase + +# program (5) +<< -> n << n 2 * >> >> +-> stack size should be 1 +-> stack should be << -> n << n 2 * >> >> +erase diff --git a/test/03-general.txt b/test/03-general.txt index 58d0c42..662b6f6 100644 --- a/test/03-general.txt +++ b/test/03-general.txt @@ -21,7 +21,7 @@ erase << -> n << n >> >> type --> stack should be << -> n << n >> >>, 'program' +-> stack should be << -> n << n >> >>, 'program' erase # default diff --git a/test/05-stack.txt b/test/05-stack.txt index e1e08ce..511d038 100644 --- a/test/05-stack.txt +++ b/test/05-stack.txt @@ -7,14 +7,36 @@ erase depth -> stack should be 0 +# swap +erase +1 2 swap +-> stack size should be 2 +-> stack should be 2, 1 + +# swap with filled stack +erase +5 6 1 2 swap +-> stack size should be 4 +-> stack should be 5, 6, 2, 1 + +# swap error +erase +5 swap +-> stack size should be 1 +-> error should 2 + # drop erase 1 2 3 drop -> stack size should be 2 +-> stack should be 1, 2 # drop2 +3 drop2 --> stack size should be 0 +-> stack size should be 1 +-> stack should be 1 +erase # drop error drop @@ -46,6 +68,22 @@ drop2 drop2 -> stack should be 2, 3, 1 erase +# test rot with start +5 6 7 1 2 start rot next +-> stack should be 7, 5, 6 +erase + +# test rot with next +5 6 7 1 2 for i rot next +-> stack should be 7, 5, 6 +erase + +# test rot with filled stack +5 6 1 2 3 rot +-> stack size should be 5 +-> stack should be 5, 6, 2, 3, 1 +erase + # test depth 1 2 3 depth @@ -93,6 +131,11 @@ erase 1 2 3 4 5 4 roll -> stack should be 1, 3, 4, 5, 2 +# test roll with filled stack +erase +10 11 1 2 3 4 5 4 roll +-> stack should be 10, 11, 1, 3, 4, 5, 2 + # test roll error erase 1 2 3 4 5 6 roll @@ -104,9 +147,14 @@ erase 10 20 30 40 50 3 rolld -> stack should be 10, 20, 50, 30, 40 -# test roll error +# test rolld with filled stack erase -1 2 3 4 5 6 roll +80 90 10 20 30 40 50 3 rolld +-> stack should be 80, 90, 10, 20, 50, 30, 40 + +# test rolld error +erase +1 2 3 4 5 6 rolld -> stack size should be 6 -> error should be 2 diff --git a/test/07-string.txt b/test/07-string.txt index c6d6fc7..54e787c 100644 --- a/test/07-string.txt +++ b/test/07-string.txt @@ -86,5 +86,5 @@ erase # str-> on program "<< -> n << n >> >>" str-> --> stack should be << -> n << n >> >> +-> stack should be << -> n << n >> >> erase