From 73135a47e94480bd0b99e925153455798b507679 Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 24 May 2023 06:52:22 -0400 Subject: add builtins "/" and "*" --- forth.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'forth.c') diff --git a/forth.c b/forth.c index 56918d9..80ff670 100644 --- a/forth.c +++ b/forth.c @@ -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"); -- cgit v1.2.3