added the bool type
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user