added not operator

This commit is contained in:
cozis
2021-12-08 13:20:34 +01:00
parent 51d744ef2e
commit dbf1c325b0
10 changed files with 216 additions and 1 deletions
+1
View File
@@ -42,6 +42,7 @@ static const InstrInfo instr_table[] = {
[OPCODE_POS] = {"POS", 0, NULL},
[OPCODE_NEG] = {"NEG", 0, NULL},
[OPCODE_NOT] = {"NOT", 0, NULL},
[OPCODE_ADD] = {"ADD", 0, NULL},
[OPCODE_SUB] = {"SUB", 0, NULL},
+1
View File
@@ -24,6 +24,7 @@ typedef enum {
OPCODE_NOPE,
OPCODE_POS,
OPCODE_NEG,
OPCODE_NOT,
OPCODE_ADD,
OPCODE_SUB,
OPCODE_MUL,
+2
View File
@@ -30,6 +30,8 @@ typedef enum {
EXPR_GRT,
EXPR_GEQ,
EXPR_NOT,
EXPR_ASS,
EXPR_INT,
EXPR_MAP,
+2
View File
@@ -27,6 +27,7 @@ static Opcode exprkind_to_opcode(ExprKind kind)
{
switch(kind)
{
case EXPR_NOT: return OPCODE_NOT;
case EXPR_POS: return OPCODE_POS;
case EXPR_NEG: return OPCODE_NEG;
case EXPR_ADD: return OPCODE_ADD;
@@ -56,6 +57,7 @@ static _Bool emit_instr_for_node(ExeBuilder *exeb, Node *node, Error *error)
ExprNode *expr = (ExprNode*) node;
switch(expr->kind)
{
case EXPR_NOT:
case EXPR_POS:
case EXPR_NEG:
case EXPR_ADD:
+14 -1
View File
@@ -58,6 +58,7 @@ typedef enum {
TKWIF,
TKWFUN,
TKWNOT,
TKWELSE,
TKWNONE,
TKWTRUE,
@@ -192,6 +193,10 @@ static Token *tokenize(Source *src, BPAlloc *alloc, Error *error)
{
tok->kind = TKWFUN;
}
else if(matchstr(str + tok->offset, tok->length, "not"))
{
tok->kind = TKWNOT;
}
else if(matchstr(str + tok->offset, tok->length, "else"))
{
tok->kind = TKWELSE;
@@ -541,6 +546,7 @@ static Node *parse_statement(Context *ctx)
case TFLOAT:
case TSTRING:
case TIDENT:
case TKWNOT:
case TKWNONE:
case TKWTRUE:
case TKWFALSE:
@@ -1064,6 +1070,7 @@ static Node *parse_primary_expresion(Context *ctx)
{
case '+':
case '-':
case TKWNOT:
{
Token *unary_operator = current_token(ctx);
@@ -1083,9 +1090,15 @@ static Node *parse_primary_expresion(Context *ctx)
temp->base.base.next = NULL;
temp->base.base.offset = unary_operator->offset;
temp->base.base.length = operand->offset + operand->length - unary_operator->offset;
temp->base.kind = unary_operator->kind == '+' ? EXPR_POS : EXPR_NEG;
temp->head = operand;
temp->count = 1;
switch(unary_operator->kind)
{
case '+': temp->base.kind = EXPR_POS; break;
case '-': temp->base.kind = EXPR_NEG; break;
case TKWNOT: temp->base.kind = EXPR_NOT; break;
}
}
return (Node*) temp;
}
+16
View File
@@ -9,6 +9,7 @@ Object *Object_NewNetworkBuiltinsMap(Runtime *runtime, Heap *heap, Error *err);
static Object *select_(Object *self, Object *key, Heap *heap, Error *err);
static Object *bin_type (Runtime *runtime, Object **argv, unsigned int argc, Error *error);
static Object *bin_print (Runtime *runtime, Object **argv, unsigned int argc, Error *error);
static Object *bin_count (Runtime *runtime, Object **argv, unsigned int argc, Error *error);
static Object *bin_assert(Runtime *runtime, Object **argv, unsigned int argc, Error *error);
@@ -51,6 +52,13 @@ static Object *select_(Object *self, Object *key, Heap *heap, Error *err)
switch(PAIR(n, s[0]))
{
case PAIR(4, 't'):
{
if(!strcmp(s, "type"))
return Object_FromNativeFunction(bm->runtime, bin_type, 1, heap, err);
return NULL;
}
case PAIR(sizeof("count")-1, 'c'):
{
if(!strcmp(s, "count"))
@@ -129,6 +137,14 @@ static Object *bin_print(Runtime *runtime, Object **argv, unsigned int argc, Err
return Object_NewNone(Runtime_GetHeap(runtime), error);
}
static Object *bin_type(Runtime *runtime, Object **argv, unsigned int argc, Error *error)
{
assert(argc == 1);
(void) runtime;
(void) error;
return (Object*) argv[0]->type;
}
static Object *bin_count(Runtime *runtime, Object **argv, unsigned int argc, Error *error)
{
assert(argc == 1);
+8
View File
@@ -2,12 +2,20 @@
#include "../utils/defs.h"
#include "objects.h"
static _Bool op_eql(Object *self, Object *other);
TypeObject t_type = {
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
.name = "type",
.size = sizeof (TypeObject),
.op_eql = op_eql,
};
static _Bool op_eql(Object *self, Object *other)
{
return self == other;
}
const char *Object_GetName(const Object *obj)
{
assert(obj != NULL);
+32
View File
@@ -557,6 +557,38 @@ static _Bool step(Runtime *runtime, Error *error)
Error_Report(error, 1, "NEG not implemented");
return 0;
case OPCODE_NOT:
{
assert(opc == 0);
if(runtime->frame->used == 0)
{
Error_Report(error, 1, "Frame doesn't have enough items on the stack to execute NOT");
return 0;
}
Object *top = Stack_Top(runtime->stack, 0);
if(!Runtime_Pop(runtime, error, 1))
return 0;
assert(top != NULL);
_Bool v = Object_ToBool(top, error);
if(error->occurred)
return 0;
Object *negated = Object_FromBool(!v, runtime->heap, error);
if(negated == NULL)
return 0;
if(!Runtime_Push(runtime, error, negated))
return 0;
return 1;
}
case OPCODE_ADD:
case OPCODE_SUB:
case OPCODE_MUL: