mirror of
https://github.com/louisrubet/rpn
synced 2024-12-28 09:58:52 +01:00
#90: debugged program entry and tests, debugged stack management, #91: show stack level in interactive mode"
This commit is contained in:
parent
fb0902cc00
commit
4a549d0116
12 changed files with 175 additions and 68 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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];
|
||||
};
|
||||
|
|
|
@ -140,7 +140,7 @@ static bool _cut(const char* entry, vector<string>& 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<string>& 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<string>& entries)
|
|||
}
|
||||
}
|
||||
while((up--)>0)
|
||||
{
|
||||
tmp += " >>";
|
||||
}
|
||||
|
||||
if (tmp.size()>0)
|
||||
{
|
||||
entries.push_back(tmp);
|
||||
|
@ -185,15 +189,13 @@ static bool _cut(const char* entry, vector<string>& 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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;i<args;i++)
|
||||
_branch_stack.pop_back();
|
||||
}
|
||||
|
||||
void rolld(void)
|
||||
|
@ -145,11 +149,13 @@ void rolld(void)
|
|||
(void)_stack->pop_back();
|
||||
}
|
||||
|
||||
stack::copy_and_push_back(_branch_stack, 0, *_stack);
|
||||
for(int i=1;i<args;i++)
|
||||
stack::copy_and_push_back(_branch_stack, args-i, *_stack);
|
||||
stack::copy_and_push_back(_branch_stack, _branch_stack.size()-args, *_stack);
|
||||
|
||||
_branch_stack.erase();
|
||||
for(int i=1;i<args;i++)
|
||||
stack::copy_and_push_back(_branch_stack, _branch_stack.size()-i, *_stack);
|
||||
|
||||
for(int i=0;i<args;i++)
|
||||
_branch_stack.pop_back();
|
||||
}
|
||||
|
||||
void over(void)
|
||||
|
|
|
@ -121,15 +121,15 @@ public:
|
|||
return _count;
|
||||
}
|
||||
|
||||
// stack access (index is counted from back)
|
||||
object* get_obj(unsigned int index)
|
||||
// stack access (stack_level=0=first out)
|
||||
object* get_obj(unsigned int stack_level)
|
||||
{
|
||||
return seq_obj(_count - index - 1);
|
||||
return seq_obj(_count - stack_level - 1);
|
||||
}
|
||||
|
||||
object* operator[](unsigned int index)
|
||||
object* operator[](unsigned int stack_level)
|
||||
{
|
||||
return seq_obj(_count - index - 1);
|
||||
return seq_obj(_count - stack_level - 1);
|
||||
}
|
||||
|
||||
object* back()
|
||||
|
|
|
@ -6,3 +6,4 @@
|
|||
#include test/07-string.txt
|
||||
#include test/08-test.txt
|
||||
#include test/09-store.txt
|
||||
#include test/10-program.txt
|
||||
|
|
|
@ -6,18 +6,20 @@ erase
|
|||
1
|
||||
-> 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
|
||||
|
|
|
@ -21,7 +21,7 @@ erase
|
|||
|
||||
<< -> n << n >> >>
|
||||
type
|
||||
-> stack should be << -> n << n >> >>, 'program'
|
||||
-> stack should be << -> n << n >> >>, 'program'
|
||||
erase
|
||||
|
||||
# default
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -86,5 +86,5 @@ erase
|
|||
# str-> on program
|
||||
"<< -> n << n >> >>"
|
||||
str->
|
||||
-> stack should be << -> n << n >> >>
|
||||
-> stack should be << -> n << n >> >>
|
||||
erase
|
||||
|
|
Loading…
Reference in a new issue