diff options
| author | dan <[email protected]> | 2023-05-24 06:52:22 -0400 | 
|---|---|---|
| committer | dan <[email protected]> | 2023-05-24 06:52:22 -0400 | 
| commit | 73135a47e94480bd0b99e925153455798b507679 (patch) | |
| tree | 88ad1147c1f74295d32c2ba43aba0887e39ccf4b | |
| parent | 2a960c5081782989e46a0c158841d404aa0a4227 (diff) | |
| download | forth-73135a47e94480bd0b99e925153455798b507679.tar.gz forth-73135a47e94480bd0b99e925153455798b507679.tar.bz2 forth-73135a47e94480bd0b99e925153455798b507679.zip  | |
add builtins "/" and "*"
| -rw-r--r-- | forth.c | 19 | 
1 files changed, 16 insertions, 3 deletions
@@ -100,13 +100,23 @@ void eq(stack *s) {  }  void add(stack *s) { -      int x = pop(s);      int y = pop(s); - //   fprintf(stderr, "adding %d + %d",x,y);      push(s, x + y);  } +void mult(stack *s) { +    int x = pop(s); +    int y = pop(s); +    push(s, x * y); +} + +void s_div(stack *s) { +    int x = pop(s); +    int y = pop(s); +    push(s, y / x); +} +  void sub(stack *s) {      int x = pop(s);      int y = pop(s); @@ -137,6 +147,8 @@ typedef struct {  wordop optable[OPTABLE_MAX_SIZE] = {      {"+", false, add},      {"-", false, sub}, +    {"*", false, mult}, +    {"/", false, s_div},      {"dup", false, s_dup},      {"dump", false, dump},      {"not", false, not}, @@ -146,7 +158,7 @@ wordop optable[OPTABLE_MAX_SIZE] = {      {"over", false, over},      {"rot", false, rot},  }; -int optablelen = 10; +int optablelen = 12;  const int DEFINED_FUNC_MAX_LENGTH = 1024;  const int WORD_LEN_LIMIT = 255; @@ -177,6 +189,7 @@ int defineop(int starti, char *input) {      }      funcscript[funcscripti] = '\0'; +    // optable bounds check      if (optablelen >= OPTABLE_MAX_SIZE) {          // Error          fprintf(stderr, "Error: optable reached max size, failed to create new user defined operation");  | 
