feat: implement math in compiler
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <common/fzl_builtins.h>
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/fzl_walker.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <stdbool.h>
|
||||
@@ -108,9 +109,9 @@ bool walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
case FZL_NODETYPE_CALL:
|
||||
if (node->list.entry_count > 0)
|
||||
{
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
{
|
||||
if (!walk(walker, node->list.entries[i]))
|
||||
if (!walk(walker, node->call.args[i]))
|
||||
return false;
|
||||
push_op(walker, FZL_OPCODE_PUSHARG);
|
||||
}
|
||||
@@ -129,13 +130,70 @@ bool walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
size_t n = add_const(
|
||||
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = node->token->value,
|
||||
.as.string.length = node->token->value_sz});
|
||||
if (node->token->type == FZL_TOKTYPE_STR_LITERAL)
|
||||
{
|
||||
size_t n = add_const(
|
||||
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||
.as.string.chars = node->token->value,
|
||||
.as.string.length = node->token->value_sz});
|
||||
|
||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||
push_word(walker, n);
|
||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||
push_word(walker, n);
|
||||
}
|
||||
else if (node->token->type == FZL_TOKTYPE_NUM_LITERAL)
|
||||
{
|
||||
push_op(walker, FZL_OPCODE_PUSHNUM);
|
||||
|
||||
uint64_t num = strtol(node->token->value, NULL, 10);
|
||||
push_word(walker, num);
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_ADD:
|
||||
case FZL_NODETYPE_SUB:
|
||||
case FZL_NODETYPE_MUL:
|
||||
case FZL_NODETYPE_DIV:
|
||||
case FZL_NODETYPE_POW:
|
||||
case FZL_NODETYPE_MOD:
|
||||
if (!walk(walker, node->binary_op.left))
|
||||
return false;
|
||||
push_op(walker, FZL_OPCODE_PUSHARG);
|
||||
|
||||
if (!walk(walker, node->binary_op.right))
|
||||
return false;
|
||||
push_op(walker, FZL_OPCODE_PUSHARG);
|
||||
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_ADD:
|
||||
push_op(walker, FZL_OPCODE_ADD);
|
||||
break;
|
||||
case FZL_NODETYPE_SUB:
|
||||
push_op(walker, FZL_OPCODE_SUB);
|
||||
break;
|
||||
case FZL_NODETYPE_MUL:
|
||||
push_op(walker, FZL_OPCODE_MUL);
|
||||
break;
|
||||
case FZL_NODETYPE_DIV:
|
||||
push_op(walker, FZL_OPCODE_DIV);
|
||||
break;
|
||||
case FZL_NODETYPE_POW:
|
||||
push_op(walker, FZL_OPCODE_POW);
|
||||
break;
|
||||
case FZL_NODETYPE_MOD:
|
||||
push_op(walker, FZL_OPCODE_MOD);
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_UNARY_MINUS:
|
||||
if (!walk(walker, node->unary_op.operand))
|
||||
return false;
|
||||
|
||||
push_op(walker, FZL_OPCODE_NEG);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user