summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonsykkel <jonrevold@gmail.com>2021-09-07 16:34:02 +0200
committerjonsykkel <jonrevold@gmail.com>2021-09-07 16:40:10 +0200
commit33cbc43ea455a88bd8176b708d51a0d221df98cb (patch)
treee91cd73c935c7ae13f477de1772715c57391b9fe
parenteccf62b57ce13bb0d19afcd5e2e76686f756d442 (diff)
downloadokeffa-33cbc43ea455a88bd8176b708d51a0d221df98cb.tar.gz
goments
-rw-r--r--calc/main.c83
1 files changed, 52 insertions, 31 deletions
diff --git a/calc/main.c b/calc/main.c
index 0910967..f21905d 100644
--- a/calc/main.c
+++ b/calc/main.c
@@ -80,22 +80,25 @@ static void op_normal(char c){
switch(c){
wbool_t p;
+ ////////////////////////////////////////////////////////////////
//stickies
- case '(':
+ ////////////////////////////////////////////////////////////////
+
+ case '(': //enter comment
comm_lev = 1;
break;
- case ')':
+ case ')': //exit comment (but we are not in one)
e("mismatched close-comment parenthesis!");
- case '[':
+ case '[': //enter quote
quote_lev = 1;
break;
- case ']':
+ case ']': //exit quote (but we are not in one)
e("mismatched close-quote bracket!");
- case '{':
+ case '{': //enter taken conditional branch
want(1);
if(fz_zero(&STACK(sp),fl)){
cond_lev = 1;
@@ -103,100 +106,115 @@ static void op_normal(char c){
drop();
break;
- case '}':
+ case '}': //exit non-taken conditional branch
push();
fz_frombool(&STACK(sp),fl,0);
break;
+ ////////////////////////////////////////////////////////////////
//stack motion
- case '.':
+ ////////////////////////////////////////////////////////////////
+
+ case '.': //push 0 onto stack
push();
fz_clear(&STACK(sp),fl);
break;
- case '"':
+ case '"': //duplicate top item
want(1);
push();
fz_copy(&STACK(sp-1),&STACK(sp),fl);
break;
- case '_':
+ case '_': //drop top item
want(1);
drop();
break;
- case '\'':
+ case '\'': //swap 2 top items
want(2);
fz_swap(&STACK(sp),&STACK(sp-1),fl);
break;
- case '`':
+ case '`': //push copy of sp-1 to stack (over)
want(2);
push();
fz_copy(&STACK(sp-2),&STACK(sp),fl);
break;
+ ////////////////////////////////////////////////////////////////
//predicates
- case '=':
+ ////////////////////////////////////////////////////////////////
+
+ case '=': //equality
want(2);
p = fz_eq(&STACK(sp-1),&STACK(sp),fl);
drop();
fz_frombool(&STACK(sp),fl,p);
break;
- case '<':
+ case '<': //less than
want(2);
p = fz_lt(&STACK(sp-1),&STACK(sp),fl);
drop();
fz_frombool(&STACK(sp),fl,p);
break;
- case '>':
+ case '>': //greater than
want(2);
p = fz_gt(&STACK(sp-1),&STACK(sp),fl);
drop();
fz_frombool(&STACK(sp),fl,p);
break;
- //arithmetic
- case '+':
+ ////////////////////////////////////////////////////////////////
+ //arithmetic ops
+ ////////////////////////////////////////////////////////////////
+
+ case '+': //add
want(2);
flag = fz_add(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
drop();
break;
- case '-':
+ case '-': //subtract
want(2);
flag = fz_sub(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
drop();
break;
- //bitwise
- case '&':
+ ////////////////////////////////////////////////////////////////
+ //bitwise ops
+ ////////////////////////////////////////////////////////////////
+
+ case '&': //bitwise and
want(2);
fz_and(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
drop();
break;
- case '|':
+ case '|': //bitwise or
want(2);
fz_or(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
drop();
break;
- case '^':
+ case '^': //bitwise xor
want(2);
fz_xor(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
drop();
break;
- case '~':
+ case '~': //bitwise not
want(1);
fz_not(&STACK(sp),&STACK(sp),fl);
break;
+ ////////////////////////////////////////////////////////////////
//other
- case 'U':
+ ////////////////////////////////////////////////////////////////
+
+ case 'U': //mUx
want(3);
wbool_t sel = fz_nzero(&STACK(sp),fl);
fz_mux(&STACK(sp-2),&STACK(sp-1),&STACK(sp-2),fl,sel);
@@ -204,31 +222,34 @@ static void op_normal(char c){
drop();
break;
- case 'O':
+ case 'O': //push carry/borrow flag to stack
push();
fz_frombool(&STACK(sp),fl,flag);
break;
- case '#':
+ case '#': //print top item and drop it
want(1);
- dump_fz(&STACK(p),fl);
+ dump_fz(&STACK(sp),fl);
drop();
break;
- case 'Z':
+ case 'Z': //zap the stack
zap();
break;
- case 'Q':
+ case 'Q': //quit with stack trace
for(size_t x = sp;x != STACK_INIT;x--){
- dump_fz(&STACK(p),fl);
+ dump_fz(&STACK(x),fl);
}
exit(0);
break;
- default:
+ ////////////////////////////////////////////////////////////////
//immediates
- if(char_isnib(c)){
+ ////////////////////////////////////////////////////////////////
+
+ default:
+ if(char_isnib(c)){ //shift nibble into top item
want(1);
ins_nib(&STACK(sp),char_getnib(c));
}