Add boolean operations
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dominic Grimm 2023-08-06 11:41:57 +02:00
parent 03a9b46e33
commit fa0f92a8ec
4 changed files with 116 additions and 0 deletions

View file

@ -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];

View file

@ -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,

View file

@ -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,

View file

@ -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,