#90: debugged program entry and tests, debugged stack management, #91: show stack level in interactive mode"

This commit is contained in:
Louis Rubet 2017-05-28 22:52:10 +02:00
parent fb0902cc00
commit 4a549d0116
12 changed files with 175 additions and 68 deletions

View file

@ -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 #endif

View file

@ -27,7 +27,7 @@ int main(int argc, char* argv[])
if (prog.run(s_global_stack, s_global_heap) == ret_good_bye) if (prog.run(s_global_stack, s_global_heap) == ret_good_bye)
break; break;
else 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 // run it
ret = prog.run(s_global_stack, s_global_heap); ret = prog.run(s_global_stack, s_global_heap);
program::show_stack(s_global_stack); program::show_stack(s_global_stack, true);
} }
} }

View file

@ -161,7 +161,7 @@ struct number : public object
struct ostring : public object struct ostring : public object
{ {
// // ostring may first have been allocated with len+1 bytes
void set(const char* value, unsigned int len) void set(const char* value, unsigned int len)
{ {
_type = cmd_string; _type = cmd_string;
@ -173,17 +173,20 @@ struct ostring : public object
_len = len; _len = len;
} }
else else
len = 0; {
_value[_len] = 0;
_len = 0;
}
} }
// // length of _value, not includiong the terminal '\0'
unsigned int _len; unsigned int _len;
char _value[0]; char _value[0];
}; };
struct oprogram : public object struct oprogram : public object
{ {
// // oprogram may first have been allocated with len+1 bytes
void set(const char* value, unsigned int len) void set(const char* value, unsigned int len)
{ {
_type = cmd_program; _type = cmd_program;
@ -195,17 +198,20 @@ struct oprogram : public object
_len = len; _len = len;
} }
else else
len = 0; {
_value[0] = 0;
_len = 0;
}
} }
// // length of _value, not includiong the terminal '\0'
unsigned int _len; unsigned int _len;
char _value[0]; char _value[0];
}; };
struct symbol : public object 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) void set(const char* value, unsigned int len, bool auto_eval)
{ {
_type = cmd_symbol; _type = cmd_symbol;
@ -219,18 +225,22 @@ struct symbol : public object
_len = len; _len = len;
} }
else else
len = 0; {
_value[0] = 0;
_len = 0;
}
} }
// //
bool _auto_eval; bool _auto_eval;
// length of _value, not includiong the terminal '\0'
unsigned int _len; unsigned int _len;
char _value[0]; char _value[0];
}; };
struct keyword : public object 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) void set(program_fn_t fn, const char* value, unsigned int len)
{ {
_type = cmd_keyword; _type = cmd_keyword;
@ -243,11 +253,15 @@ struct keyword : public object
_len = len; _len = len;
} }
else else
len = 0; {
_value[0] = 0;
_len = 0;
}
} }
// //
program_fn_t _fn; program_fn_t _fn;
// length of _value, not includiong the terminal '\0'
unsigned int _len; unsigned int _len;
char _value[0]; char _value[0];
}; };
@ -273,7 +287,10 @@ struct branch : public object
_len = len; _len = len;
} }
else else
len = 0; {
_value[0] = 0;
_len = 0;
}
} }
// branch function // branch function
@ -282,6 +299,8 @@ struct branch : public object
int arg1, arg2, arg3; int arg1, arg2, arg3;
number *farg1, *farg2; number *farg1, *farg2;
bool arg_bool; bool arg_bool;
// length of _value, not includiong the terminal '\0'
unsigned int _len; unsigned int _len;
char _value[0]; char _value[0];
}; };

View file

@ -140,7 +140,7 @@ static bool _cut(const char* entry, vector<string>& entries)
tmp = "<< "; tmp = "<< ";
// trim leading spaces // trim leading spaces
while (i < len && (entry[i]==' ' || entry[i]=='\t')) while (i < len && isspace(entry[i]))
i++; i++;
while(i < len) while(i < len)
@ -151,18 +151,23 @@ static bool _cut(const char* entry, vector<string>& entries)
i += 2; i += 2;
tmp += "<< "; tmp += "<< ";
// trim leading spaces // trim leading spaces
while (i < len && (entry[i] == ' ' || entry[i] == '\t')) while (i < len && isspace(entry[i]))
i++; i++;
} }
else if (strncmp(&entry[i], ">>", 2) == 0) else if (strncmp(&entry[i], ">>", 2) == 0)
{ {
up--; if (isspace(entry[i-1]) && entry[i-2]!='>')
i += 2; tmp += ">>";
else
tmp += " >>"; tmp += " >>";
up--;
i += 2;
// trim trailing spaces // trim trailing spaces
while (i < len && (entry[i] == ' ' || entry[i] == '\t')) while (i < len && isspace(entry[i]))
i++; i++;
// found end // found end
if (up == 0) if (up == 0)
break; break;
@ -174,9 +179,8 @@ static bool _cut(const char* entry, vector<string>& entries)
} }
} }
while((up--)>0) while((up--)>0)
{
tmp += " >>"; tmp += " >>";
}
if (tmp.size()>0) if (tmp.size()>0)
{ {
entries.push_back(tmp); entries.push_back(tmp);
@ -185,15 +189,13 @@ static bool _cut(const char* entry, vector<string>& entries)
i--;// i has move 1 too far i--;// i has move 1 too far
} }
else else
{
// reinject '<'' which is not a prog begin // reinject '<'' which is not a prog begin
tmp = "<"; tmp = "<";
}
break; break;
//other //other
default: default:
if (entry[i] != ' ' && entry[i] != '\t') if (!isspace(entry[i]))
{ {
tmp += entry[i]; tmp += entry[i];
} }
@ -349,7 +351,7 @@ static bool get_string(const string& entry, program& prog, string& remaining_ent
return ret; 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; bool ret = false;
unsigned int obj_len; unsigned int obj_len;
@ -412,7 +414,7 @@ static bool get_number(const string& entry, program& prog, string& remaining_ent
return ret; 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; bool ret = false;

View file

@ -105,7 +105,6 @@ int rpn_for(branch& myobj)
{ {
// store symbol with first value // store symbol with first value
_local_heap.add(sym->_value, (object*)myobj.farg1, myobj.farg1->size()); _local_heap.add(sym->_value, (object*)myobj.farg1, myobj.farg1->size());
(void)_stack->pop_back();
ret = myobj.arg1 + 1; ret = myobj.arg1 + 1;
} }

View file

@ -6,11 +6,12 @@ void swap(void)
stack::copy_and_push_back(*_stack, _stack->size()-2, _branch_stack); stack::copy_and_push_back(*_stack, _stack->size()-2, _branch_stack);
(void)_stack->pop_back(); (void)_stack->pop_back();
(void)_stack->pop_back(); (void)_stack->pop_back();
stack::copy_and_push_back(_branch_stack, 0, *_stack); stack::copy_and_push_back(_branch_stack, _branch_stack.size()-2, *_stack);
stack::copy_and_push_back(_branch_stack, 1, *_stack); stack::copy_and_push_back(_branch_stack, _branch_stack.size()-1, *_stack);
_branch_stack.erase(); _branch_stack.pop_back();
} _branch_stack.pop_back();
}
void drop(void) void drop(void)
{ {
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
@ -95,10 +96,12 @@ void rot(void)
(void)_stack->pop_back(); (void)_stack->pop_back();
(void)_stack->pop_back(); (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, _branch_stack.size()-2, *_stack);
stack::copy_and_push_back(_branch_stack, 2, *_stack); stack::copy_and_push_back(_branch_stack, _branch_stack.size()-1, *_stack);
stack::copy_and_push_back(_branch_stack, 0, *_stack); stack::copy_and_push_back(_branch_stack, _branch_stack.size()-3, *_stack);
_branch_stack.erase(); _branch_stack.pop_back();
_branch_stack.pop_back();
_branch_stack.pop_back();
} }
void depth(void) 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-i, *_stack);
stack::copy_and_push_back(_branch_stack, args-1, *_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) void rolld(void)
@ -145,11 +149,13 @@ void rolld(void)
(void)_stack->pop_back(); (void)_stack->pop_back();
} }
stack::copy_and_push_back(_branch_stack, 0, *_stack); stack::copy_and_push_back(_branch_stack, _branch_stack.size()-args, *_stack);
for(int i=1;i<args;i++)
stack::copy_and_push_back(_branch_stack, args-i, *_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) void over(void)

View file

@ -121,15 +121,15 @@ public:
return _count; return _count;
} }
// stack access (index is counted from back) // stack access (stack_level=0=first out)
object* get_obj(unsigned int index) 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() object* back()

View file

@ -6,3 +6,4 @@
#include test/07-string.txt #include test/07-string.txt
#include test/08-test.txt #include test/08-test.txt
#include test/09-store.txt #include test/09-store.txt
#include test/10-program.txt

View file

@ -6,18 +6,20 @@ erase
1 1
-> stack size should be 1 -> stack size should be 1
-> stack should be 1 -> stack should be 1
drop drop
# real decimal (2)
2.345 2.345
-> stack should be 2.345 -> stack should be 2.345
erase erase
# real decimal (3)
1 2.345 3 4.9 1 2.345 3 4.9
-> stack size should be 4 -> stack size should be 4
-> stack should be 1, 2.345, 3, 4.9 -> stack should be 1, 2.345, 3, 4.9
erase
# real hex # real hex
erase
0x1234 0x10.10 0x1234 0x10.10
-> stack should be 0x1.234p+12, 0x1.01p+4 -> stack should be 0x1.234p+12, 0x1.01p+4
@ -27,16 +29,16 @@ dec swap dec swap
erase erase
# real binary # real binary
erase
0b11001100 0b11001100
-> stack should be 204 -> stack should be 204
erase erase
# real binary (2)
0b11001100.1101 0b11001100.1101
-> stack should be 204.8125 -> stack should be 204.8125
erase
# real inf, nan # real inf, nan
erase
inf inf
@inf@ @inf@
+inf +inf
@ -46,46 +48,76 @@ inf
nan nan
@nan@ @nan@
-> stack should be inf, inf, inf, inf, -inf, -inf, nan, nan -> stack should be inf, inf, inf, inf, -inf, -inf, nan, nan
erase
# symbol # symbol
erase
'test' 'test'
-> stack size should be 1 -> stack size should be 1
-> stack should be 'test' -> stack should be 'test'
erase erase
# symbol (2)
'test 'test
-> stack size should be 1 -> stack size should be 1
-> stack should be 'test' -> stack should be 'test'
erase erase
# symbol (3)
'' ''
-> stack size should be 1 -> stack size should be 1
-> stack should be '' -> stack should be ''
erase
# symbol (4)
'
-> stack size should be 1
-> stack should be ''
erase
# string # string
erase
"test string" "test string"
-> stack size should be 1 -> stack size should be 1
-> stack should be "test string" -> stack should be "test string"
erase erase
# string (2)
"test string "test string
-> stack size should be 1 -> stack size should be 1
-> stack should be "test string" -> stack should be "test string"
erase
# string (3)
"
-> stack size should be 1
-> stack should be ""
erase
# program # program
erase
<< 'one' >> << 'one' >>
-> stack size should be 1 -> stack size should be 1
-> stack should be << 'one' >> -> stack should be << 'one' >>
erase erase
# program (2)
<< 'one' 2 << 'one' 2
-> stack size should be 1 -> stack size should be 1
-> stack should be << 'one' 2 >> -> stack should be << 'one' 2 >>
erase erase
<< -> n << n 2 + >> >>
# program (3)
<<
-> stack size should be 1 -> 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

View file

@ -7,14 +7,36 @@
erase depth erase depth
-> stack should be 0 -> 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 # drop
erase erase
1 2 3 drop 1 2 3 drop
-> stack size should be 2 -> stack size should be 2
-> stack should be 1, 2
# drop2 # drop2
3
drop2 drop2
-> stack size should be 0 -> stack size should be 1
-> stack should be 1
erase
# drop error # drop error
drop drop
@ -46,6 +68,22 @@ drop2 drop2
-> stack should be 2, 3, 1 -> stack should be 2, 3, 1
erase 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 # test depth
1 2 3 1 2 3
depth depth
@ -93,6 +131,11 @@ erase
1 2 3 4 5 4 roll 1 2 3 4 5 4 roll
-> stack should be 1, 3, 4, 5, 2 -> 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 # test roll error
erase erase
1 2 3 4 5 6 roll 1 2 3 4 5 6 roll
@ -104,9 +147,14 @@ erase
10 20 30 40 50 3 rolld 10 20 30 40 50 3 rolld
-> stack should be 10, 20, 50, 30, 40 -> stack should be 10, 20, 50, 30, 40
# test roll error # test rolld with filled stack
erase 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 -> stack size should be 6
-> error should be 2 -> error should be 2