feat+ref: refactor + new types

This commit is contained in:
samsonium
2026-10-11 08:51:54 +03:00
parent e3b8ec6b7d
commit 83e20d78f3
13 changed files with 532 additions and 341 deletions
+145 -85
View File
@@ -1,3 +1,4 @@
#include "common/fzl_types.h"
#include <common/fzl_builtins.h>
#include <common/fzl_opcodes.h>
#include <compiler/fzl_parser.h>
@@ -35,12 +36,12 @@ fzl_walker_t *fzl_mkwalker(fzl_node_t *root)
return NULL;
}
walker->root = root;
walker->root = root;
walker->consts = pool;
walker->op_count = 0;
walker->op_count = 0;
walker->op_remainder = 2;
walker->ops = malloc(walker->op_remainder * sizeof(uint64_t));
walker->ops = malloc(walker->op_remainder * sizeof(uint64_t));
return walker;
}
@@ -88,116 +89,171 @@ void push_word(fzl_walker_t *walker, uint64_t word)
walker->ops[walker->op_count++] = word;
}
bool walk(fzl_walker_t *walker, fzl_node_t *node)
fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
{
if (!walker || !node)
return false;
return (fzl_walk_res_t){.error = true, .error_msg = "walker is null"};
fzl_walk_res_t current_res;
while (node != NULL)
{
switch (node->type)
{
case FZL_NODETYPE_BLOCK:
// NOTE: create block env here
if (node->list.entry_count > 0)
for (size_t i = 0; i < node->list.entry_count; i++)
if (!walk(walker, node->list.entries[i]))
return false;
// NOTE: destroy block env here
break;
case FZL_NODETYPE_UNARY_PLUS:
case FZL_NODETYPE_IDENTIFIER:
break;
case FZL_NODETYPE_CALL:
if (node->list.entry_count > 0)
{
for (size_t i = 0; i < node->call.args_count; i++)
case FZL_NODETYPE_BLOCK:
if (node->list.entry_count > 0)
for (size_t i = 0; i < node->list.entry_count; i++)
{
fzl_walk_res_t res = walk(walker, node->list.entries[i]);
if (res.error)
return res;
}
break;
case FZL_NODETYPE_CALL:
if (node->list.entry_count > 0)
{
if (!walk(walker, node->call.args[i]))
return false;
for (size_t i = 0; i < node->call.args_count; i++)
{
fzl_walk_res_t res = walk(walker, node->call.args[i]);
if (res.error)
return res;
}
}
}
// NOTE: you need to implement dynamic names call
push_op(walker, FZL_OPCODE_CALL_BUILTIN);
// NOTE: you need to implement dynamic names call too
push_op(walker, FZL_OPCODE_CALL_BUILTIN);
if (strcmp(node->token->value, "printline") == 0)
push_word(walker, FZL_BUILTIN_PRINTLINE);
else
{
snprintf(wlast_err, sizeof(wlast_err), "call to unknown function '%s'",
(char *)node->token->value);
return false;
}
break;
if (strcmp(node->token->value, "printline") == 0)
{
push_word(walker, FZL_BUILTIN_PRINTLINE);
if (node->call.args_count < 1)
{
snprintf(wlast_err, sizeof(wlast_err),
"'printline' call with zero arguments at %ld:%ld",
node->token->ln, node->token->cn);
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
}
}
else
{
snprintf(wlast_err, sizeof(wlast_err),
"call to unknown function '%s'", (char *)node->token->value);
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
}
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
break;
case FZL_NODETYPE_LITERAL:
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});
case FZL_NODETYPE_LITERAL:
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);
}
else if (node->token->type == FZL_TOKTYPE_NUM_LITERAL)
{
push_op(walker, FZL_OPCODE_PUSHNUM);
push_op(walker, FZL_OPCODE_LOADCONST);
push_word(walker, n);
current_res =
(fzl_walk_res_t){.type = FZL_TYPE_STRING, .error = false};
}
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;
uint64_t num = strtol(node->token->value, NULL, 10);
push_word(walker, num);
current_res = (fzl_walk_res_t){.type = FZL_TYPE_INT, .error = false};
}
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;
if (!walk(walker, node->binary_op.right))
return false;
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);
{
fzl_walk_res_t lres = walk(walker, node->binary_op.left);
if (lres.error)
return lres;
fzl_walk_res_t rres = walk(walker, node->binary_op.right);
if (rres.error)
return rres;
if (lres.type == FZL_TYPE_STRING)
{
fzl_token_t *ltok = node->binary_op.left->token;
snprintf(wlast_err, sizeof(wlast_err),
"left operand at %ld:%ld is not compatible to integer types",
ltok->ln, ltok->cn);
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
}
if (rres.type == FZL_TYPE_STRING)
{
fzl_token_t *rtok = node->binary_op.right->token;
snprintf(
wlast_err, sizeof(wlast_err),
"right operand at %ld:%ld is not compatible to integer types",
rtok->ln, rtok->cn);
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
}
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:
break;
}
current_res = (fzl_walk_res_t){.type = lres.type, .error = false};
break;
default:
return false;
}
break;
case FZL_NODETYPE_UNARY_MINUS:
if (!walk(walker, node->unary_op.operand))
return false;
case FZL_NODETYPE_UNARY_MINUS:
{
fzl_walk_res_t res = walk(walker, node->unary_op.operand);
if (res.error)
return res;
push_op(walker, FZL_OPCODE_NEG);
break;
if (res.type == FZL_TYPE_STRING)
{
fzl_token_t *tok = node->unary_op.operand->token;
snprintf(wlast_err, sizeof(wlast_err),
"incompatible with unary operator type at %ld:%ld", tok->ln,
tok->cn);
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
}
push_op(walker, FZL_OPCODE_NEG);
current_res = (fzl_walk_res_t){.type = res.type, .error = false};
}
}
node = node->next;
}
return true;
return current_res;
}
// Walk entire AST recursevily
@@ -209,8 +265,12 @@ int fzl_walk(fzl_walker_t *walker)
return -1;
}
bool result = walk(walker, walker->root);
return !result && wlast_err[0] ? -1 : 0;
fzl_walk_res_t result = walk(walker, walker->root);
if (result.error && !wlast_err[0])
snprintf(wlast_err, sizeof(wlast_err), "%s", result.error_msg);
return result.error ? -1 : 0;
}
// --- Walking zone end ---