From fa0f92a8ec2be70d2cd19d2009e5e844827dfbe7 Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Sun, 6 Aug 2023 11:41:57 +0200 Subject: [PATCH] Add boolean operations --- include/hydroforth/interpreter.h | 2 + include/hydroforth/keywords.h | 18 +++++++ include/hydroforth/parser.h | 7 +++ src/hydroforth/interpreter.c | 89 ++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) diff --git a/include/hydroforth/interpreter.h b/include/hydroforth/interpreter.h index e96af9a..d132fab 100644 --- a/include/hydroforth/interpreter.h +++ b/include/hydroforth/interpreter.h @@ -40,6 +40,8 @@ extern void hf__interpreter__free(struct hf__interpreter *interpreter); typedef struct hf__result (*hf__interpreter__word_func_t)( struct hf__interpreter *const, const struct hf__node *const); +extern int hf__interpreter__bool_to_flag(const bool x); + extern const hf__interpreter__word_func_t HF__INTERPRETER__WORD_FUNCTION[__HF__NODE_TYPE__N]; diff --git a/include/hydroforth/keywords.h b/include/hydroforth/keywords.h index 4135d95..3cb5dd6 100644 --- a/include/hydroforth/keywords.h +++ b/include/hydroforth/keywords.h @@ -13,6 +13,13 @@ enum HF__KEYWORD { HF__KEYWORD__ADD, HF__KEYWORD__SUB, + HF__KEYWORD__EQ, + HF__KEYWORD__LT, + HF__KEYWORD__GT, + HF__KEYWORD__AND, + HF__KEYWORD__OR, + HF__KEYWORD__INVERT, + HF__KEYWORD__PERIOD, HF__KEYWORD__EMIT, HF__KEYWORD__SPACE, @@ -34,6 +41,10 @@ static const char *const HF__KEYWORD_STR[__HF__KEYWORD__N] = { [HF__KEYWORD__ADD] = "+", [HF__KEYWORD__SUB] = "-", + [HF__KEYWORD__EQ] = "=", [HF__KEYWORD__LT] = "<", + [HF__KEYWORD__GT] = ">", [HF__KEYWORD__AND] = "and", + [HF__KEYWORD__OR] = "or", [HF__KEYWORD__INVERT] = "invert", + [HF__KEYWORD__EMIT] = "emit", [HF__KEYWORD__PERIOD] = ".", [HF__KEYWORD__SPACE] = "space", [HF__KEYWORD__SPACES] = "spaces", [HF__KEYWORD__CR] = "cr", [HF__KEYWORDS__CRS] = "crs", @@ -53,6 +64,13 @@ static const enum hf__node_type HF__KEYWORD_NODE_TYPE[__HF__KEYWORD__N] = { [HF__KEYWORD__ADD] = HF__NODE_TYPE__ADD, [HF__KEYWORD__SUB] = HF__NODE_TYPE__SUB, + [HF__KEYWORD__EQ] = HF__NODE_TYPE__EQ, + [HF__KEYWORD__LT] = HF__NODE_TYPE__LT, + [HF__KEYWORD__GT] = HF__NODE_TYPE__GT, + [HF__KEYWORD__AND] = HF__NODE_TYPE__AND, + [HF__KEYWORD__OR] = HF__NODE_TYPE__OR, + [HF__KEYWORD__INVERT] = HF__NODE_TYPE__INVERT, + [HF__KEYWORD__PERIOD] = HF__NODE_TYPE__DOT, [HF__KEYWORD__EMIT] = HF__NODE_TYPE__EMIT, [HF__KEYWORD__SPACE] = HF__NODE_TYPE__SPACE, diff --git a/include/hydroforth/parser.h b/include/hydroforth/parser.h index 62b87a2..7b9ce3c 100644 --- a/include/hydroforth/parser.h +++ b/include/hydroforth/parser.h @@ -24,6 +24,13 @@ enum hf__node_type { HF__NODE_TYPE__ADD, HF__NODE_TYPE__SUB, + HF__NODE_TYPE__EQ, + HF__NODE_TYPE__LT, + HF__NODE_TYPE__GT, + HF__NODE_TYPE__AND, + HF__NODE_TYPE__OR, + HF__NODE_TYPE__INVERT, + HF__NODE_TYPE__DOT, HF__NODE_TYPE__EMIT, HF__NODE_TYPE__SPACE, diff --git a/src/hydroforth/interpreter.c b/src/hydroforth/interpreter.c index a3a582c..35fbc1c 100644 --- a/src/hydroforth/interpreter.c +++ b/src/hydroforth/interpreter.c @@ -25,6 +25,8 @@ void hf__interpreter__free(struct hf__interpreter *interpreter) { free(interpreter->stack); } +int hf__interpreter__bool_to_flag(const bool x) { return x ? -1 : 0; } + struct hf__result words__number(struct hf__interpreter *const interpreter, const struct hf__node *const node) { hf__interpreter__stack_push(&interpreter->stack, &interpreter->stack_len, @@ -215,6 +217,86 @@ struct hf__result words__sub(struct hf__interpreter *const interpreter, return HF__OK; } +struct hf__result words__eq(struct hf__interpreter *const interpreter, + const struct hf__node *const node) { + if (interpreter->stack_len < 2) { + return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW); + } + + const int top = interpreter->stack[--interpreter->stack_len]; + interpreter->stack[interpreter->stack_len - 1] = + hf__interpreter__bool_to_flag( + top == interpreter->stack[interpreter->stack_len - 1]); + + return HF__OK; +} + +struct hf__result words__lt(struct hf__interpreter *const interpreter, + const struct hf__node *const node) { + if (interpreter->stack_len < 2) { + return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW); + } + + const int top = interpreter->stack[--interpreter->stack_len]; + interpreter->stack[interpreter->stack_len - 1] = + hf__interpreter__bool_to_flag( + top > interpreter->stack[interpreter->stack_len - 1]); + + return HF__OK; +} + +struct hf__result words__gt(struct hf__interpreter *const interpreter, + const struct hf__node *const node) { + if (interpreter->stack_len < 2) { + return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW); + } + + const int top = interpreter->stack[--interpreter->stack_len]; + interpreter->stack[interpreter->stack_len - 1] = + hf__interpreter__bool_to_flag( + top < interpreter->stack[interpreter->stack_len - 1]); + + return HF__OK; +} + +struct hf__result words__and(struct hf__interpreter *const interpreter, + const struct hf__node *const node) { + if (interpreter->stack_len < 2) { + return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW); + } + + const int top = interpreter->stack[--interpreter->stack_len]; + interpreter->stack[interpreter->stack_len - 1] = + top & interpreter->stack[interpreter->stack_len - 1]; + + return HF__OK; +} + +struct hf__result words__or(struct hf__interpreter *const interpreter, + const struct hf__node *const node) { + if (interpreter->stack_len < 2) { + return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW); + } + + const int top = interpreter->stack[--interpreter->stack_len]; + interpreter->stack[interpreter->stack_len - 1] = + top | interpreter->stack[interpreter->stack_len - 1]; + + return HF__OK; +} + +struct hf__result words__invert(struct hf__interpreter *const interpreter, + const struct hf__node *const node) { + if (interpreter->stack_len < 1) { + return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW); + } + + interpreter->stack[interpreter->stack_len - 1] = + ~interpreter->stack[interpreter->stack_len - 1]; + + return HF__OK; +} + struct hf__result words__dot(struct hf__interpreter *const interpreter, const struct hf__node *const node) { if (interpreter->stack_len < 1) { @@ -330,6 +412,13 @@ const hf__interpreter__word_func_t [HF__NODE_TYPE__ADD] = words__add, [HF__NODE_TYPE__SUB] = words__sub, + [HF__NODE_TYPE__EQ] = words__eq, + [HF__NODE_TYPE__LT] = words__lt, + [HF__NODE_TYPE__GT] = words__gt, + [HF__NODE_TYPE__AND] = words__and, + [HF__NODE_TYPE__OR] = words__or, + [HF__NODE_TYPE__INVERT] = words__invert, + [HF__NODE_TYPE__DOT] = words__dot, [HF__NODE_TYPE__EMIT] = words__emit, [HF__NODE_TYPE__SPACE] = words__space,