added the bool type
This commit is contained in:
@@ -48,10 +48,13 @@ static const InstrInfo instr_table[] = {
|
||||
[OPCODE_MUL] = {"MUL", 2, 0, NULL},
|
||||
[OPCODE_DIV] = {"DIV", 2, 0, NULL},
|
||||
|
||||
[OPCODE_PUSHI] = {"PUSHI", 1, 1, (OperandType[]) {OPTP_INT}},
|
||||
[OPCODE_PUSHF] = {"PUSHF", 1, 1, (OperandType[]) {OPTP_FLOAT}},
|
||||
[OPCODE_PUSHS] = {"PUSHS", 1, 1, (OperandType[]) {OPTP_STRING}},
|
||||
[OPCODE_PUSHV] = {"PUSHV", 1, 1, (OperandType[]) {OPTP_STRING}},
|
||||
[OPCODE_PUSHINT] = {"PUSHINT", 0, 1, (OperandType[]) {OPTP_INT}},
|
||||
[OPCODE_PUSHFLT] = {"PUSHFLT", 0, 1, (OperandType[]) {OPTP_FLOAT}},
|
||||
[OPCODE_PUSHSTR] = {"PUSHSTR", 0, 1, (OperandType[]) {OPTP_STRING}},
|
||||
[OPCODE_PUSHVAR] = {"PUSHVAR", 0, 1, (OperandType[]) {OPTP_STRING}},
|
||||
[OPCODE_PUSHTRU] = {"PUSHTRU", 0, 0, NULL},
|
||||
[OPCODE_PUSHFLS] = {"PUSHFLS", 0, 0, NULL},
|
||||
[OPCODE_PUSHNNE] = {"PUSHNNE", 0, 0, NULL},
|
||||
|
||||
[OPCODE_RETURN] = {"RETURN", 0, 0, NULL},
|
||||
|
||||
|
||||
@@ -28,10 +28,13 @@ typedef enum {
|
||||
OPCODE_SUB,
|
||||
OPCODE_MUL,
|
||||
OPCODE_DIV,
|
||||
OPCODE_PUSHI,
|
||||
OPCODE_PUSHF,
|
||||
OPCODE_PUSHS,
|
||||
OPCODE_PUSHV,
|
||||
OPCODE_PUSHINT,
|
||||
OPCODE_PUSHFLT,
|
||||
OPCODE_PUSHSTR,
|
||||
OPCODE_PUSHVAR,
|
||||
OPCODE_PUSHTRU,
|
||||
OPCODE_PUSHFLS,
|
||||
OPCODE_PUSHNNE,
|
||||
OPCODE_RETURN,
|
||||
OPCODE_JUMPIFNOTANDPOP,
|
||||
OPCODE_JUMP,
|
||||
|
||||
@@ -17,6 +17,9 @@ typedef enum {
|
||||
EXPR_MUL,
|
||||
EXPR_DIV,
|
||||
EXPR_INT,
|
||||
EXPR_NONE,
|
||||
EXPR_TRUE,
|
||||
EXPR_FALSE,
|
||||
EXPR_FLOAT,
|
||||
EXPR_STRING,
|
||||
EXPR_IDENT,
|
||||
|
||||
+13
-4
@@ -54,30 +54,39 @@ static _Bool emit_instr_for_node(ExeBuilder *exeb, Node *node, Error *error)
|
||||
{
|
||||
IntExprNode *p = (IntExprNode*) expr;
|
||||
Operand op = { .type = OPTP_INT, .as_int = p->val };
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHI, &op, 1, node->offset, node->length);
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHINT, &op, 1, node->offset, node->length);
|
||||
}
|
||||
|
||||
case EXPR_FLOAT:
|
||||
{
|
||||
FloatExprNode *p = (FloatExprNode*) expr;
|
||||
Operand op = { .type = OPTP_FLOAT, .as_float = p->val };
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHF, &op, 1, node->offset, node->length);
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHFLT, &op, 1, node->offset, node->length);
|
||||
}
|
||||
|
||||
case EXPR_STRING:
|
||||
{
|
||||
StringExprNode *p = (StringExprNode*) expr;
|
||||
Operand op = { .type = OPTP_STRING, .as_string = p->val };
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHS, &op, 1, node->offset, node->length);
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHSTR, &op, 1, node->offset, node->length);
|
||||
}
|
||||
|
||||
case EXPR_IDENT:
|
||||
{
|
||||
IdentExprNode *p = (IdentExprNode*) expr;
|
||||
Operand op = { .type = OPTP_STRING, .as_string = p->val };
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHV, &op, 1, node->offset, node->length);
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHVAR, &op, 1, node->offset, node->length);
|
||||
}
|
||||
|
||||
case EXPR_NONE:
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHNNE, NULL, 0, node->offset, node->length);
|
||||
|
||||
case EXPR_TRUE:
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHTRU, NULL, 0, node->offset, node->length);
|
||||
|
||||
case EXPR_FALSE:
|
||||
return ExeBuilder_Append(exeb, error, OPCODE_PUSHFLS, NULL, 0, node->offset, node->length);
|
||||
|
||||
default:
|
||||
UNREACHABLE;
|
||||
break;
|
||||
|
||||
@@ -26,6 +26,9 @@ typedef enum {
|
||||
|
||||
TKWIF,
|
||||
TKWELSE,
|
||||
TKWNONE,
|
||||
TKWTRUE,
|
||||
TKWFALSE,
|
||||
} TokenKind;
|
||||
|
||||
typedef struct Token Token;
|
||||
@@ -124,6 +127,18 @@ AST *parse(Source *src, BPAlloc *alloc, Error *error)
|
||||
{
|
||||
tok->kind = TKWELSE;
|
||||
}
|
||||
else if(matchstr(str + tok->offset, tok->length, "none"))
|
||||
{
|
||||
tok->kind = TKWNONE;
|
||||
}
|
||||
else if(matchstr(str + tok->offset, tok->length, "true"))
|
||||
{
|
||||
tok->kind = TKWTRUE;
|
||||
}
|
||||
else if(matchstr(str + tok->offset, tok->length, "false"))
|
||||
{
|
||||
tok->kind = TKWFALSE;
|
||||
}
|
||||
|
||||
#undef matchstr
|
||||
}
|
||||
@@ -330,6 +345,9 @@ static Node *parse_statement(Context *ctx)
|
||||
case TFLOAT:
|
||||
case TSTRING:
|
||||
case TIDENT:
|
||||
case TKWNONE:
|
||||
case TKWTRUE:
|
||||
case TKWFALSE:
|
||||
return parse_expression_statement(ctx);
|
||||
|
||||
case TKWIF:
|
||||
@@ -653,6 +671,78 @@ static Node *parse_primary_expresion(Context *ctx)
|
||||
case TSTRING:
|
||||
return parse_string_primary_expression(ctx);
|
||||
|
||||
case TKWNONE:
|
||||
{
|
||||
next(ctx);
|
||||
|
||||
ExprNode *node;
|
||||
{
|
||||
node = BPAlloc_Malloc(ctx->alloc, sizeof(ExprNode));
|
||||
|
||||
if(node == NULL)
|
||||
{
|
||||
Error_Report(ctx->error, 1, "No memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->base.kind = NODE_EXPR;
|
||||
node->base.next = NULL;
|
||||
node->base.offset = ctx->token->offset;
|
||||
node->base.length = ctx->token->length;
|
||||
node->kind = EXPR_NONE;
|
||||
}
|
||||
|
||||
return (Node*) node;
|
||||
}
|
||||
|
||||
case TKWTRUE:
|
||||
{
|
||||
next(ctx);
|
||||
|
||||
ExprNode *node;
|
||||
{
|
||||
node = BPAlloc_Malloc(ctx->alloc, sizeof(ExprNode));
|
||||
|
||||
if(node == NULL)
|
||||
{
|
||||
Error_Report(ctx->error, 1, "No memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->base.kind = NODE_EXPR;
|
||||
node->base.next = NULL;
|
||||
node->base.offset = ctx->token->offset;
|
||||
node->base.length = ctx->token->length;
|
||||
node->kind = EXPR_TRUE;
|
||||
}
|
||||
|
||||
return (Node*) node;
|
||||
}
|
||||
|
||||
case TKWFALSE:
|
||||
{
|
||||
next(ctx);
|
||||
|
||||
ExprNode *node;
|
||||
{
|
||||
node = BPAlloc_Malloc(ctx->alloc, sizeof(ExprNode));
|
||||
|
||||
if(node == NULL)
|
||||
{
|
||||
Error_Report(ctx->error, 1, "No memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->base.kind = NODE_EXPR;
|
||||
node->base.next = NULL;
|
||||
node->base.offset = ctx->token->offset;
|
||||
node->base.length = ctx->token->length;
|
||||
node->kind = EXPR_FALSE;
|
||||
}
|
||||
|
||||
return (Node*) node;
|
||||
}
|
||||
|
||||
case TIDENT:
|
||||
{
|
||||
char *copy;
|
||||
|
||||
@@ -4,23 +4,23 @@
|
||||
|
||||
static _Bool to_bool(Object *obj, Error *err);
|
||||
|
||||
static const Type t_int = {
|
||||
static const Type t_bool = {
|
||||
.base = (Object) { .type = &t_type, .flags = Object_STATIC },
|
||||
.name = "int",
|
||||
.name = "bool",
|
||||
.size = sizeof (Object),
|
||||
.atomic = ATMTP_BOOL,
|
||||
.to_bool = to_bool,
|
||||
};
|
||||
|
||||
static Object the_true_object = {
|
||||
.type = t_bool,
|
||||
.type = &t_bool,
|
||||
.flags = Object_STATIC,
|
||||
}
|
||||
};
|
||||
|
||||
static Object the_false_object = {
|
||||
.type = t_bool,
|
||||
.type = &t_bool,
|
||||
.flags = Object_STATIC,
|
||||
}
|
||||
};
|
||||
|
||||
static _Bool to_bool(Object *obj, Error *err)
|
||||
{
|
||||
|
||||
+46
-4
@@ -363,7 +363,7 @@ static _Bool step(Runtime *runtime, Error *error)
|
||||
break;
|
||||
}
|
||||
|
||||
case OPCODE_PUSHI:
|
||||
case OPCODE_PUSHINT:
|
||||
{
|
||||
assert(opc == 1);
|
||||
assert(ops[0].type == OPTP_INT);
|
||||
@@ -378,7 +378,7 @@ static _Bool step(Runtime *runtime, Error *error)
|
||||
return 1;
|
||||
}
|
||||
|
||||
case OPCODE_PUSHF:
|
||||
case OPCODE_PUSHFLT:
|
||||
{
|
||||
assert(opc == 1);
|
||||
assert(ops[0].type == OPTP_FLOAT);
|
||||
@@ -393,7 +393,7 @@ static _Bool step(Runtime *runtime, Error *error)
|
||||
return 1;
|
||||
}
|
||||
|
||||
case OPCODE_PUSHS:
|
||||
case OPCODE_PUSHSTR:
|
||||
{
|
||||
assert(opc == 1);
|
||||
assert(ops[0].type == OPTP_STRING);
|
||||
@@ -408,7 +408,7 @@ static _Bool step(Runtime *runtime, Error *error)
|
||||
return 1;
|
||||
}
|
||||
|
||||
case OPCODE_PUSHV:
|
||||
case OPCODE_PUSHVAR:
|
||||
{
|
||||
assert(opc == 1);
|
||||
assert(ops[0].type == OPTP_STRING);
|
||||
@@ -430,6 +430,48 @@ static _Bool step(Runtime *runtime, Error *error)
|
||||
return 1;
|
||||
}
|
||||
|
||||
case OPCODE_PUSHNNE:
|
||||
{
|
||||
assert(opc == 0);
|
||||
|
||||
Object *obj = Object_NewNone(runtime->heap, error);
|
||||
|
||||
if(obj == NULL)
|
||||
return 0;
|
||||
|
||||
if(!Runtime_Push(runtime, error, obj))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
case OPCODE_PUSHTRU:
|
||||
{
|
||||
assert(opc == 0);
|
||||
|
||||
Object *obj = Object_FromBool(1, runtime->heap, error);
|
||||
|
||||
if(obj == NULL)
|
||||
return 0;
|
||||
|
||||
if(!Runtime_Push(runtime, error, obj))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
case OPCODE_PUSHFLS:
|
||||
{
|
||||
assert(opc == 0);
|
||||
|
||||
Object *obj = Object_FromBool(0, runtime->heap, error);
|
||||
|
||||
if(obj == NULL)
|
||||
return 0;
|
||||
|
||||
if(!Runtime_Push(runtime, error, obj))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
case OPCODE_RETURN:
|
||||
return 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user