summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonsykkel <jonrevold@gmail.com>2021-09-09 12:19:15 +0200
committerjonsykkel <jonrevold@gmail.com>2021-09-09 12:37:15 +0200
commit5ea08bdece750c899460c3e9fe2fd88b21a9783f (patch)
tree1c36e2cdaf43554f86d519ae46979664a364402a
parent977e8c8bc60bcc90bb25f0b010adf3c25664f8fa (diff)
downloadokeffa-5ea08bdece750c899460c3e9fe2fd88b21a9783f.tar.gz
im do this
-rw-r--r--calc/main.c113
1 files changed, 61 insertions, 52 deletions
diff --git a/calc/main.c b/calc/main.c
index 6f5533e..7397522 100644
--- a/calc/main.c
+++ b/calc/main.c
@@ -12,32 +12,39 @@
#include <ctype.h>
#include <assert.h>
+#define STACK(I) (stack+fl*(I))
+#define STACK_INIT ((size_t)-1)
#define STACK_SIZE 65536
-#define _STR(X) #X
-#define STR(X) _STR(X)
-#define STACK(I) stack[fl*(I)]
-#define STACK_INIT ((size_t)-1)
+//calc configuration
+static size_t fl; //wordness, fz len
+static size_t sh; //stack height
-static size_t bitness;
-static size_t fl; //wordness, fz len
+//calc state
static word_t stack[STACK_SIZE];
-static size_t sh; //stack height
-static size_t sp = STACK_INIT; //stack ptr
-static wbool_t flag = 0; //carry/borrow
-static size_t pos = 0;
-static size_t quote_lev = 0;
-static size_t comm_lev = 0;
-static size_t cond_lev = 0;
+static size_t sp; //stack ptr
+static wbool_t flag; //carry/borrow
+static size_t pos;
+static size_t quote_lev;
+static size_t comm_lev;
+static size_t cond_lev;
static void zap(void){
for(size_t x = 0;x < sh;x++){
- fz_clear(&STACK(x),fl);
+ fz_clear(STACK(x),fl);
}
sp = STACK_INIT;
flag = 0;
}
+static void init(void){
+ zap();
+ pos = 0;
+ quote_lev = 0;
+ comm_lev = 0;
+ cond_lev = 0;
+}
+
static void e(char const *str){
eggog("pos: %zu: %s",pos,str);
}
@@ -48,7 +55,7 @@ static void push(void){
static void drop(void){
assert(sp != STACK_INIT);
- fz_clear(&STACK(sp--),fl);
+ fz_clear(STACK(sp--),fl);
}
static void want(size_t n){
@@ -81,7 +88,7 @@ static word_t char_getnib(char c){
static void puts_stack(size_t p){
printf("%3zu ",p);
- dump_fz(&STACK(p),fl);
+ dump_fz(STACK(p),fl);
write_nl();
}
@@ -109,7 +116,7 @@ static void op_normal(char c){
case '{': //enter taken conditional branch
want(1);
- if(fz_zero(&STACK(sp),fl)){
+ if(fz_zero(STACK(sp),fl)){
cond_lev = 1;
}
drop();
@@ -117,7 +124,7 @@ static void op_normal(char c){
case '}': //exit non-taken conditional branch
push();
- fz_frombool(&STACK(sp),fl,0);
+ fz_frombool(STACK(sp),fl,0);
break;
////////////////////////////////////////////////////////////////
@@ -126,13 +133,13 @@ static void op_normal(char c){
case '.': //push 0 onto stack
push();
- fz_clear(&STACK(sp),fl);
+ fz_clear(STACK(sp),fl);
break;
case '"': //duplicate top item
want(1);
push();
- fz_copy(&STACK(sp-1),&STACK(sp),fl);
+ fz_copy(STACK(sp-1),STACK(sp),fl);
break;
case '_': //drop top item
@@ -142,13 +149,13 @@ static void op_normal(char c){
case '\'': //swap 2 top items
want(2);
- fz_swap(&STACK(sp),&STACK(sp-1),fl);
+ fz_swap(STACK(sp),STACK(sp-1),fl);
break;
case '`': //push copy of sp-1 to stack (over)
want(2);
push();
- fz_copy(&STACK(sp-2),&STACK(sp),fl);
+ fz_copy(STACK(sp-2),STACK(sp),fl);
break;
////////////////////////////////////////////////////////////////
@@ -157,23 +164,23 @@ static void op_normal(char c){
case '=': //equality
want(2);
- p = fz_eq(&STACK(sp-1),&STACK(sp),fl);
+ p = fz_eq(STACK(sp-1),STACK(sp),fl);
drop();
- fz_frombool(&STACK(sp),fl,p);
+ fz_frombool(STACK(sp),fl,p);
break;
case '<': //less than
want(2);
- p = fz_lt(&STACK(sp-1),&STACK(sp),fl);
+ p = fz_lt(STACK(sp-1),STACK(sp),fl);
drop();
- fz_frombool(&STACK(sp),fl,p);
+ fz_frombool(STACK(sp),fl,p);
break;
case '>': //greater than
want(2);
- p = fz_gt(&STACK(sp-1),&STACK(sp),fl);
+ p = fz_gt(STACK(sp-1),STACK(sp),fl);
drop();
- fz_frombool(&STACK(sp),fl,p);
+ fz_frombool(STACK(sp),fl,p);
break;
////////////////////////////////////////////////////////////////
@@ -182,38 +189,38 @@ static void op_normal(char c){
case '+': //add
want(2);
- flag = fz_add(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
+ flag = fz_add(STACK(sp-1),STACK(sp),STACK(sp-1),fl);
drop();
break;
case '-': //subtract
want(2);
- flag = fz_sub(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
+ flag = fz_sub(STACK(sp-1),STACK(sp),STACK(sp-1),fl);
drop();
break;
case '*': //multiply
want(2);
- fz_mul_egypt(&STACK(sp-1),&STACK(sp),&STACK(sp-1),&STACK(sp),fl);
+ fz_mul_egypt(STACK(sp-1),STACK(sp),STACK(sp-1),STACK(sp),fl);
break;
case '\\': //divide, keep quotient and remainder
want(2);
- mustnotzero(&STACK(sp));
- fz_idiv(&STACK(sp-1),&STACK(sp),&STACK(sp-1),&STACK(sp),fl);
+ mustnotzero(STACK(sp));
+ fz_idiv(STACK(sp-1),STACK(sp),STACK(sp-1),STACK(sp),fl);
break;
case '/': //divide, keep only quotient
want(2);
- mustnotzero(&STACK(sp));
- fz_div(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
+ mustnotzero(STACK(sp));
+ fz_div(STACK(sp-1),STACK(sp),STACK(sp-1),fl);
drop();
break;
case '%': //divide, keep only remainder
want(2);
- mustnotzero(&STACK(sp));
- fz_mod(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
+ mustnotzero(STACK(sp));
+ fz_mod(STACK(sp-1),STACK(sp),STACK(sp-1),fl);
drop();
break;
@@ -223,25 +230,25 @@ static void op_normal(char c){
case '&': //bitwise and
want(2);
- fz_and(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
+ fz_and(STACK(sp-1),STACK(sp),STACK(sp-1),fl);
drop();
break;
case '|': //bitwise or
want(2);
- fz_or(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
+ fz_or(STACK(sp-1),STACK(sp),STACK(sp-1),fl);
drop();
break;
case '^': //bitwise xor
want(2);
- fz_xor(&STACK(sp-1),&STACK(sp),&STACK(sp-1),fl);
+ fz_xor(STACK(sp-1),STACK(sp),STACK(sp-1),fl);
drop();
break;
case '~': //bitwise not
want(1);
- fz_not(&STACK(sp),&STACK(sp),fl);
+ fz_not(STACK(sp),STACK(sp),fl);
break;
////////////////////////////////////////////////////////////////
@@ -250,20 +257,20 @@ static void op_normal(char c){
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);
+ wbool_t sel = fz_nzero(STACK(sp),fl);
+ fz_mux(STACK(sp-2),STACK(sp-1),STACK(sp-2),fl,sel);
drop();
drop();
break;
case 'O': //push carry/borrow flag to stack
push();
- fz_frombool(&STACK(sp),fl,flag);
+ fz_frombool(STACK(sp),fl,flag);
break;
case '#': //print top item and drop it
want(1);
- //dump_fz(&STACK(sp),fl);
+ //dump_fz(STACK(sp),fl);
puts_stack(sp);
drop();
break;
@@ -274,7 +281,7 @@ static void op_normal(char c){
case 'Q': //quit with stack trace
for(size_t x = sp;x != STACK_INIT;x--){
- //dump_fz(&STACK(x),fl);
+ //dump_fz(STACK(x),fl);
puts_stack(x);
}
exit(0);
@@ -287,7 +294,7 @@ static void op_normal(char c){
default:
if(char_isnib(c)){ //shift nibble into top item
want(1);
- ins_nib(&STACK(sp),char_getnib(c));
+ ins_nib(STACK(sp),char_getnib(c));
}
break;
}
@@ -316,7 +323,7 @@ static void op(char c){
cond_lev--;
if(cond_lev == 0){
push();
- fz_frombool(&STACK(sp),fl,1);
+ fz_frombool(STACK(sp),fl,1);
}
break;
}
@@ -326,6 +333,8 @@ static void op(char c){
}
int main(int argc,char **argv){
+ size_t bitness;
+
if(argc != 3){
eggog("usage: ffa_calc bitness stack_height");
}
@@ -338,16 +347,16 @@ int main(int argc,char **argv){
}
if(bitness/IRON_BYTEBITS*sh > STACK_SIZE){
- eggog("max stack size: "STR(STACK_SIZE));
+ eggog("max stack size: %zu",STACK_SIZE);
}
fl = bitness/WORD_BITNESS;
- char c;
- while(c = read_char()){
+ init();
+ for(char c;c = read_char();){
op(c);
pos++;
}
- zap();
return 0;
}
+