Compare commits
3
Commits
b8769ac81c
...
c0dc14a0c7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0dc14a0c7 | ||
|
|
7e52c671aa | ||
|
|
8043c30d9d |
@@ -15,11 +15,13 @@ add_executable(fzc
|
|||||||
src/compiler/fzl_walker.c
|
src/compiler/fzl_walker.c
|
||||||
src/compiler/fzl_writer.c
|
src/compiler/fzl_writer.c
|
||||||
src/compiler/utils/fzl_const_pool.c
|
src/compiler/utils/fzl_const_pool.c
|
||||||
|
src/compiler/utils/fzl_symtable.c
|
||||||
src/compiler/main.c)
|
src/compiler/main.c)
|
||||||
|
|
||||||
# Virtual machine
|
# Virtual machine
|
||||||
add_executable(fz
|
add_executable(fz
|
||||||
src/vm/fzl_vm.c
|
src/vm/fzl_vm.c
|
||||||
src/vm/fzl_vm_env.c
|
src/vm/fzl_vm_env.c
|
||||||
|
src/vm/fzl_vm_debug.c
|
||||||
src/vm/main.c)
|
src/vm/main.c)
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -8,5 +8,6 @@ printline(work_it)
|
|||||||
printline(make_it + do_it)
|
printline(make_it + do_it)
|
||||||
|
|
||||||
char make_us
|
char make_us
|
||||||
string floyd = "hello, Pink Floyd!"
|
str floyd = "hello, Pink Floyd!"
|
||||||
|
printline(floyd)
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ typedef enum
|
|||||||
|
|
||||||
FZL_TYPE_STRING,
|
FZL_TYPE_STRING,
|
||||||
FZL_TYPE_CHAR,
|
FZL_TYPE_CHAR,
|
||||||
|
FZL_TYPE_BOOL,
|
||||||
|
|
||||||
FZL_TYPE_INT8,
|
FZL_TYPE_INT8,
|
||||||
FZL_TYPE_UINT8,
|
FZL_TYPE_UINT8,
|
||||||
@@ -17,9 +18,7 @@ typedef enum
|
|||||||
FZL_TYPE_INT64,
|
FZL_TYPE_INT64,
|
||||||
FZL_TYPE_UINT64,
|
FZL_TYPE_UINT64,
|
||||||
FZL_TYPE_INT, // machine word int
|
FZL_TYPE_INT, // machine word int
|
||||||
FZL_TYPE_UINT, // machine word unsigned int
|
FZL_TYPE_UINT // machine word unsigned int
|
||||||
|
|
||||||
FZL_TYPE_BOOL
|
|
||||||
} fzl_type_t;
|
} fzl_type_t;
|
||||||
|
|
||||||
#endif // FZL_TYPES_H
|
#endif // FZL_TYPES_H
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <common/fzl_types.h>
|
#include <common/fzl_types.h>
|
||||||
#include <compiler/fzl_parser.h>
|
#include <compiler/fzl_parser.h>
|
||||||
#include <compiler/utils/fzl_const_pool.h>
|
#include <compiler/utils/fzl_const_pool.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
@@ -26,6 +27,9 @@ typedef struct
|
|||||||
uint64_t *ops;
|
uint64_t *ops;
|
||||||
size_t op_count;
|
size_t op_count;
|
||||||
size_t op_remainder;
|
size_t op_remainder;
|
||||||
|
|
||||||
|
fzl_symtable_t *syms[64];
|
||||||
|
size_t sym_top;
|
||||||
} fzl_walker_t;
|
} fzl_walker_t;
|
||||||
|
|
||||||
// Allocate new walker
|
// Allocate new walker
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#ifndef FZL_SYMTABLE_H
|
||||||
|
#define FZL_SYMTABLE_H
|
||||||
|
|
||||||
|
#include <common/fzl_types.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
FZL_SYMTABLE_RESULT_OK,
|
||||||
|
FZL_SYMTABLE_RESULT_ERR,
|
||||||
|
FZL_SYMTABLE_RESULT_EXISTS
|
||||||
|
} fzl_symtable_res_t;
|
||||||
|
|
||||||
|
// Symbols table entry descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
fzl_type_t type;
|
||||||
|
} fzl_sym_t;
|
||||||
|
|
||||||
|
// Symbols table descriptor
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
size_t size;
|
||||||
|
size_t remainder;
|
||||||
|
|
||||||
|
fzl_sym_t *symbols;
|
||||||
|
} fzl_symtable_t;
|
||||||
|
|
||||||
|
// Allocate new symbtable
|
||||||
|
fzl_symtable_t *fzl_mksymt();
|
||||||
|
|
||||||
|
// Put new entry into symtable
|
||||||
|
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
||||||
|
fzl_type_t type);
|
||||||
|
|
||||||
|
// Get entry from specified symtable
|
||||||
|
bool fzl_symt_get(fzl_symtable_t *symt, char *name, fzl_sym_t *res);
|
||||||
|
|
||||||
|
// Free symbtable
|
||||||
|
void fzl_freesymt(fzl_symtable_t *symt);
|
||||||
|
|
||||||
|
#endif // FZL_SYMTABLE_H
|
||||||
+4
-1
@@ -27,7 +27,7 @@ typedef struct
|
|||||||
size_t ctop;
|
size_t ctop;
|
||||||
|
|
||||||
// Environment stack
|
// Environment stack
|
||||||
fzl_vm_env_t estack[256];
|
fzl_vm_env_t *estack[256];
|
||||||
size_t etop;
|
size_t etop;
|
||||||
} fzl_vm_t;
|
} fzl_vm_t;
|
||||||
|
|
||||||
@@ -40,6 +40,9 @@ int fzl_vm_run(fzl_vm_t *vm);
|
|||||||
// Get last VM error message
|
// Get last VM error message
|
||||||
char *fzl_vm_geterror();
|
char *fzl_vm_geterror();
|
||||||
|
|
||||||
|
// Print opcodes sequence
|
||||||
|
void fzl_vm_print_ops(fzl_vm_t *vm);
|
||||||
|
|
||||||
// Free VM
|
// Free VM
|
||||||
void fzl_freevm(fzl_vm_t *vm);
|
void fzl_freevm(fzl_vm_t *vm);
|
||||||
|
|
||||||
|
|||||||
@@ -381,7 +381,7 @@ fzl_node_t *parse_expression(fzl_parser_t *parser)
|
|||||||
// Statement parsing
|
// Statement parsing
|
||||||
fzl_node_t *parse_statement(fzl_parser_t *parser)
|
fzl_node_t *parse_statement(fzl_parser_t *parser)
|
||||||
{
|
{
|
||||||
if (is(parser, FZL_TOKTYPE_TYPE) || is(parser, FZL_TOKTYPE_NAME))
|
if (is(parser, FZL_TOKTYPE_TYPE))
|
||||||
{
|
{
|
||||||
fzl_token_t *type = peek(parser);
|
fzl_token_t *type = peek(parser);
|
||||||
if (type->type == FZL_TOKTYPE_TYPE)
|
if (type->type == FZL_TOKTYPE_TYPE)
|
||||||
@@ -424,6 +424,29 @@ fzl_node_t *parse_statement(fzl_parser_t *parser)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is(parser, FZL_TOKTYPE_NAME) && next(parser)->type == FZL_TOKTYPE_EQ)
|
||||||
|
{
|
||||||
|
fzl_token_t *name = consume(parser);
|
||||||
|
fzl_node_t *left = fzl_mknode(FZL_NODETYPE_IDENTIFIER, name);
|
||||||
|
|
||||||
|
while (is(parser, FZL_TOKTYPE_EQ))
|
||||||
|
{
|
||||||
|
fzl_token_t *eq = consume(parser);
|
||||||
|
|
||||||
|
fzl_node_t *right = parse_expression(parser);
|
||||||
|
if (!right)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
fzl_node_t *assn = fzl_mknode(FZL_NODETYPE_ASSIGN, eq);
|
||||||
|
assn->binary_op.left = left;
|
||||||
|
assn->binary_op.right = right;
|
||||||
|
|
||||||
|
left = assn;
|
||||||
|
}
|
||||||
|
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
|
||||||
return parse_expression(parser);
|
return parse_expression(parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+128
-18
@@ -5,6 +5,7 @@
|
|||||||
#include <compiler/fzl_token.h>
|
#include <compiler/fzl_token.h>
|
||||||
#include <compiler/fzl_walker.h>
|
#include <compiler/fzl_walker.h>
|
||||||
#include <compiler/utils/fzl_const_pool.h>
|
#include <compiler/utils/fzl_const_pool.h>
|
||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@@ -43,11 +44,63 @@ fzl_walker_t *fzl_mkwalker(fzl_node_t *root)
|
|||||||
walker->op_remainder = 2;
|
walker->op_remainder = 2;
|
||||||
walker->ops = malloc(walker->op_remainder * sizeof(uint64_t));
|
walker->ops = malloc(walker->op_remainder * sizeof(uint64_t));
|
||||||
|
|
||||||
|
walker->sym_top = 0;
|
||||||
|
walker->syms[walker->sym_top++] = fzl_mksymt();
|
||||||
|
if (!walker->syms[walker->sym_top - 1])
|
||||||
|
{
|
||||||
|
snprintf(wlast_err, sizeof(wlast_err), "failed to create global symtable");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return walker;
|
return walker;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Walking zone ---
|
// --- Walking zone ---
|
||||||
|
|
||||||
|
// Convert type token to `fzl_type_t`
|
||||||
|
const fzl_type_t tok_to_type(fzl_token_t *tok)
|
||||||
|
{
|
||||||
|
if (strcmp((char *)tok->value, "i8") == 0)
|
||||||
|
return FZL_TYPE_INT8;
|
||||||
|
if (strcmp((char *)tok->value, "i16") == 0)
|
||||||
|
return FZL_TYPE_INT16;
|
||||||
|
if (strcmp((char *)tok->value, "i32") == 0)
|
||||||
|
return FZL_TYPE_INT32;
|
||||||
|
if (strcmp((char *)tok->value, "i64") == 0)
|
||||||
|
return FZL_TYPE_INT64;
|
||||||
|
|
||||||
|
if (strcmp((char *)tok->value, "u8") == 0)
|
||||||
|
return FZL_TYPE_UINT8;
|
||||||
|
if (strcmp((char *)tok->value, "u16") == 0)
|
||||||
|
return FZL_TYPE_UINT16;
|
||||||
|
if (strcmp((char *)tok->value, "u32") == 0)
|
||||||
|
return FZL_TYPE_UINT32;
|
||||||
|
if (strcmp((char *)tok->value, "u64") == 0)
|
||||||
|
return FZL_TYPE_UINT64;
|
||||||
|
|
||||||
|
if (strcmp((char *)tok->value, "int") == 0)
|
||||||
|
return FZL_TYPE_INT;
|
||||||
|
if (strcmp((char *)tok->value, "uint") == 0)
|
||||||
|
return FZL_TYPE_UINT;
|
||||||
|
|
||||||
|
if (strcmp((char *)tok->value, "bool") == 0)
|
||||||
|
return FZL_TYPE_BOOL;
|
||||||
|
if (strcmp((char *)tok->value, "str") == 0)
|
||||||
|
return FZL_TYPE_STRING;
|
||||||
|
if (strcmp((char *)tok->value, "char") == 0)
|
||||||
|
return FZL_TYPE_CHAR;
|
||||||
|
|
||||||
|
return FZL_TYPE_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool is_compatible(fzl_type_t t1, fzl_type_t t2)
|
||||||
|
{
|
||||||
|
if (t1 == t2)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return t1 >= FZL_TYPE_INT8 && t2 >= FZL_TYPE_INT8;
|
||||||
|
}
|
||||||
|
|
||||||
// Add const to consts table
|
// Add const to consts table
|
||||||
size_t add_const(fzl_walker_t *walker, fzl_const_t c)
|
size_t add_const(fzl_walker_t *walker, fzl_const_t c)
|
||||||
{
|
{
|
||||||
@@ -94,8 +147,6 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
if (!walker || !node)
|
if (!walker || !node)
|
||||||
return (fzl_walk_res_t){.error = true, .error_msg = "walker is null"};
|
return (fzl_walk_res_t){.error = true, .error_msg = "walker is null"};
|
||||||
|
|
||||||
push_op(walker, FZL_OPCODE_MK_ENVTABLE);
|
|
||||||
|
|
||||||
fzl_walk_res_t current_res;
|
fzl_walk_res_t current_res;
|
||||||
while (node != NULL)
|
while (node != NULL)
|
||||||
{
|
{
|
||||||
@@ -111,13 +162,28 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
.as.string.chars = node->token->value,
|
.as.string.chars = node->token->value,
|
||||||
.as.string.length = node->token->value_sz});
|
.as.string.length = node->token->value_sz});
|
||||||
|
|
||||||
|
fzl_sym_t sym;
|
||||||
|
bool exists = fzl_symt_get(walker->syms[walker->sym_top - 1],
|
||||||
|
node->token->value, &sym);
|
||||||
|
if (!exists)
|
||||||
|
{
|
||||||
|
snprintf(wlast_err, sizeof(wlast_err),
|
||||||
|
"cannot find symbol '%s' at %ld:%ld",
|
||||||
|
(char *)node->token->value, node->token->ln,
|
||||||
|
node->token->cn);
|
||||||
|
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||||
|
}
|
||||||
|
|
||||||
push_op(walker, FZL_OPCODE_LOADCONST);
|
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||||
push_word(walker, n);
|
push_word(walker, n);
|
||||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_STRING, .error = false};
|
push_op(walker, FZL_OPCODE_LOOKUP_ENV);
|
||||||
|
|
||||||
|
current_res = (fzl_walk_res_t){.type = sym.type, .error = false};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case FZL_NODETYPE_BLOCK:
|
case FZL_NODETYPE_BLOCK:
|
||||||
|
push_op(walker, FZL_OPCODE_MK_ENVTABLE);
|
||||||
if (node->list.entry_count > 0)
|
if (node->list.entry_count > 0)
|
||||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||||
{
|
{
|
||||||
@@ -125,10 +191,11 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
if (res.error)
|
if (res.error)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FZL_NODETYPE_CALL:
|
case FZL_NODETYPE_CALL:
|
||||||
if (node->list.entry_count > 0)
|
if (node->call.args_count > 0)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < node->call.args_count; i++)
|
for (size_t i = 0; i < node->call.args_count; i++)
|
||||||
{
|
{
|
||||||
@@ -198,7 +265,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
if (rres.error)
|
if (rres.error)
|
||||||
return rres;
|
return rres;
|
||||||
|
|
||||||
if (lres.type == FZL_TYPE_STRING)
|
if (lres.type < FZL_TYPE_INT8)
|
||||||
{
|
{
|
||||||
fzl_token_t *ltok = node->binary_op.left->token;
|
fzl_token_t *ltok = node->binary_op.left->token;
|
||||||
snprintf(wlast_err, sizeof(wlast_err),
|
snprintf(wlast_err, sizeof(wlast_err),
|
||||||
@@ -207,7 +274,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rres.type == FZL_TYPE_STRING)
|
if (rres.type < FZL_TYPE_INT8)
|
||||||
{
|
{
|
||||||
fzl_token_t *rtok = node->binary_op.right->token;
|
fzl_token_t *rtok = node->binary_op.right->token;
|
||||||
snprintf(
|
snprintf(
|
||||||
@@ -250,7 +317,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
if (res.error)
|
if (res.error)
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
if (res.type == FZL_TYPE_STRING)
|
if (res.type < FZL_TYPE_INT8)
|
||||||
{
|
{
|
||||||
fzl_token_t *tok = node->unary_op.operand->token;
|
fzl_token_t *tok = node->unary_op.operand->token;
|
||||||
snprintf(wlast_err, sizeof(wlast_err),
|
snprintf(wlast_err, sizeof(wlast_err),
|
||||||
@@ -266,13 +333,22 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
|
|
||||||
case FZL_NODETYPE_DECLARATION:
|
case FZL_NODETYPE_DECLARATION:
|
||||||
{
|
{
|
||||||
fzl_walk_res_t rres = walk(walker, node->var.identifier);
|
fzl_symtable_res_t res = fzl_symt_put(
|
||||||
if (rres.error)
|
walker->syms[walker->sym_top - 1],
|
||||||
return rres;
|
node->var.identifier->token->value, tok_to_type(node->var.type));
|
||||||
|
|
||||||
|
fzl_token_t *id_tok = node->var.identifier->token;
|
||||||
|
size_t const_n = add_const(
|
||||||
|
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||||
|
.as.string.chars = id_tok->value,
|
||||||
|
.as.string.length = id_tok->value_sz});
|
||||||
|
|
||||||
|
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||||
|
push_word(walker, const_n);
|
||||||
|
|
||||||
push_op(walker, FZL_OPCODE_STORE);
|
push_op(walker, FZL_OPCODE_STORE);
|
||||||
|
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||||
// NOTE: ignoring type field for now
|
push_word(walker, const_n);
|
||||||
|
|
||||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||||
break;
|
break;
|
||||||
@@ -280,16 +356,48 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
|
|
||||||
case FZL_NODETYPE_ASSIGN:
|
case FZL_NODETYPE_ASSIGN:
|
||||||
{
|
{
|
||||||
fzl_walk_res_t lres = walk(walker, node->binary_op.left);
|
|
||||||
if (lres.error)
|
|
||||||
return lres;
|
|
||||||
|
|
||||||
push_op(walker, FZL_OPCODE_LOOKUP_ENV);
|
|
||||||
|
|
||||||
fzl_walk_res_t rres = walk(walker, node->binary_op.right);
|
fzl_walk_res_t rres = walk(walker, node->binary_op.right);
|
||||||
if (rres.error)
|
if (rres.error)
|
||||||
return rres;
|
return rres;
|
||||||
|
|
||||||
|
if (node->binary_op.left->type == FZL_NODETYPE_DECLARATION)
|
||||||
|
{
|
||||||
|
fzl_walk_res_t lres = walk(walker, node->binary_op.left);
|
||||||
|
if (lres.error)
|
||||||
|
return lres;
|
||||||
|
}
|
||||||
|
else if (node->binary_op.left->type == FZL_NODETYPE_IDENTIFIER)
|
||||||
|
{
|
||||||
|
fzl_sym_t sym;
|
||||||
|
bool res = fzl_symt_get(walker->syms[walker->sym_top - 1],
|
||||||
|
node->binary_op.left->token->value, &sym);
|
||||||
|
if (!res) // NOTE: refactor this before implement blocks, fns, etc
|
||||||
|
{
|
||||||
|
fzl_token_t *tok = node->binary_op.left->token->value;
|
||||||
|
snprintf(wlast_err, sizeof(wlast_err),
|
||||||
|
"name '%s' is not exists at %ld:%ld", (char *)tok->value,
|
||||||
|
tok->ln, tok->cn);
|
||||||
|
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_compatible(sym.type, rres.type))
|
||||||
|
{
|
||||||
|
fzl_token_t *tok = node->binary_op.right->token;
|
||||||
|
snprintf(wlast_err, sizeof(wlast_err),
|
||||||
|
"R-Value is not compatible with L-Value at %ld:%ld",
|
||||||
|
tok->ln, tok->cn);
|
||||||
|
return (fzl_walk_res_t){.error = true, .error_msg = wlast_err};
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t const_n = add_const(
|
||||||
|
walker, (fzl_const_t){.type = FZL_CONSTTYPE_STRING,
|
||||||
|
.as.string.chars = sym.name,
|
||||||
|
.as.string.length =
|
||||||
|
node->binary_op.left->token->value_sz});
|
||||||
|
push_op(walker, FZL_OPCODE_LOADCONST);
|
||||||
|
push_word(walker, const_n);
|
||||||
|
}
|
||||||
|
|
||||||
push_op(walker, FZL_OPCODE_WRITE);
|
push_op(walker, FZL_OPCODE_WRITE);
|
||||||
current_res = (fzl_walk_res_t){.type = rres.type, .error = false};
|
current_res = (fzl_walk_res_t){.type = rres.type, .error = false};
|
||||||
break;
|
break;
|
||||||
@@ -311,7 +419,9 @@ int fzl_walk(fzl_walker_t *walker)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
push_op(walker, FZL_OPCODE_MK_ENVTABLE);
|
||||||
fzl_walk_res_t result = walk(walker, walker->root);
|
fzl_walk_res_t result = walk(walker, walker->root);
|
||||||
|
push_op(walker, FZL_OPCODE_RM_ENVTABLE);
|
||||||
|
|
||||||
if (result.error && !wlast_err[0])
|
if (result.error && !wlast_err[0])
|
||||||
snprintf(wlast_err, sizeof(wlast_err), "%s", result.error_msg);
|
snprintf(wlast_err, sizeof(wlast_err), "%s", result.error_msg);
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
#include <compiler/utils/fzl_symtable.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
fzl_symtable_t *fzl_mksymt()
|
||||||
|
{
|
||||||
|
fzl_symtable_t *tbl = malloc(sizeof(fzl_symtable_t));
|
||||||
|
if (!tbl)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
tbl->remainder = 2;
|
||||||
|
tbl->size = 0;
|
||||||
|
tbl->symbols = malloc(tbl->remainder * sizeof(fzl_sym_t));
|
||||||
|
if (!tbl->symbols)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return tbl;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name,
|
||||||
|
fzl_type_t type)
|
||||||
|
{
|
||||||
|
if (!symt)
|
||||||
|
return FZL_SYMTABLE_RESULT_ERR;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < symt->size; i++)
|
||||||
|
if (strcmp(symt->symbols[i].name, name) == 0)
|
||||||
|
return FZL_SYMTABLE_RESULT_EXISTS;
|
||||||
|
|
||||||
|
if (symt->remainder <= symt->size)
|
||||||
|
{
|
||||||
|
symt->remainder *= 2;
|
||||||
|
symt->symbols = realloc(symt->symbols, symt->remainder * sizeof(fzl_sym_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
symt->symbols[symt->size++] = (fzl_sym_t){.name = name, .type = type};
|
||||||
|
return FZL_SYMTABLE_RESULT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fzl_symt_get(fzl_symtable_t *symt, char *name, fzl_sym_t *res)
|
||||||
|
{
|
||||||
|
if (!symt)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < symt->size; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(symt->symbols[i].name, name) == 0)
|
||||||
|
{
|
||||||
|
*res = symt->symbols[i];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fzl_freesymt(fzl_symtable_t *symt)
|
||||||
|
{
|
||||||
|
if (!symt)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (symt->symbols)
|
||||||
|
free(symt->symbols);
|
||||||
|
|
||||||
|
free(symt);
|
||||||
|
}
|
||||||
+81
-2
@@ -1,3 +1,4 @@
|
|||||||
|
#include "vm/fzl_vm_env.h"
|
||||||
#include <common/fzl_builtins.h>
|
#include <common/fzl_builtins.h>
|
||||||
#include <common/fzl_opcodes.h>
|
#include <common/fzl_opcodes.h>
|
||||||
#include <common/fzl_types.h>
|
#include <common/fzl_types.h>
|
||||||
@@ -122,11 +123,87 @@ int fzl_vm_run(fzl_vm_t *vm)
|
|||||||
|
|
||||||
switch (opcode)
|
switch (opcode)
|
||||||
{
|
{
|
||||||
case FZL_OPCODE_MK_ENVTABLE:
|
|
||||||
case FZL_OPCODE_STORE:
|
case FZL_OPCODE_STORE:
|
||||||
case FZL_OPCODE_WRITE:
|
if (vm->dtop < 1)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err),
|
||||||
|
"runtime error - expected var name in data stack");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_value_t varname = vm->dstack[--vm->dtop];
|
||||||
|
fzl_env_storevalue(vm->estack[vm->etop - 1], (char *)varname.as.s.value,
|
||||||
|
(fzl_value_t){});
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_WRITE:
|
||||||
|
if (vm->dtop < 2)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err),
|
||||||
|
"runtime error - expected 2 values in data stack");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_value_t writevar = vm->dstack[--vm->dtop];
|
||||||
|
fzl_value_t value = vm->dstack[--vm->dtop];
|
||||||
|
|
||||||
|
fzl_vm_env_wresult_t wresult = fzl_env_writevalue(
|
||||||
|
vm->estack[vm->etop - 1], (char *)writevar.as.s.value, value);
|
||||||
|
if (wresult != FZL_VM_ENV_WRESULT_OK)
|
||||||
|
{
|
||||||
|
switch (wresult)
|
||||||
|
{
|
||||||
|
case FZL_VM_ENV_WRESULT_ERR:
|
||||||
|
snprintf(last_err, sizeof(last_err),
|
||||||
|
"runtime error - failed to write variable");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case FZL_VM_ENV_WRESULT_ERR_EXISTS:
|
||||||
|
snprintf(last_err, sizeof(last_err),
|
||||||
|
"runtime error - redefenition of '%s'",
|
||||||
|
writevar.as.s.value);
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case FZL_VM_ENV_WRESULT_ERR_NOT_FOUND:
|
||||||
|
snprintf(last_err, sizeof(last_err),
|
||||||
|
"runtime error - cannot find name '%s'",
|
||||||
|
writevar.as.s.value);
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vm->dstack[vm->dtop++] = value;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_MK_ENVTABLE:
|
||||||
|
vm->estack[vm->etop++] = fzl_mkenv();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_RM_ENVTABLE:
|
||||||
|
fzl_freeenv(vm->estack[--vm->etop]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_LOOKUP_ENV:
|
||||||
|
{
|
||||||
|
if (vm->dtop < 1)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err),
|
||||||
|
"runtime error - LOOKUP_ENV: empty data stack");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fzl_value_t val = vm->dstack[--vm->dtop];
|
||||||
|
fzl_value_t env_val =
|
||||||
|
fzl_env_lookup(vm->estack[vm->etop - 1], (char *)val.as.s.value);
|
||||||
|
vm->dstack[vm->dtop++] = env_val;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case FZL_OPCODE_CALL_BUILTIN:
|
case FZL_OPCODE_CALL_BUILTIN:
|
||||||
{
|
{
|
||||||
uint64_t fn_id = vm->ops[++vm->ip];
|
uint64_t fn_id = vm->ops[++vm->ip];
|
||||||
@@ -143,6 +220,8 @@ int fzl_vm_run(fzl_vm_t *vm)
|
|||||||
printf("%lu\n", arg.as.u);
|
printf("%lu\n", arg.as.u);
|
||||||
else if (arg.type == FZL_TYPE_BOOL)
|
else if (arg.type == FZL_TYPE_BOOL)
|
||||||
printf("%b\n", arg.as.b);
|
printf("%b\n", arg.as.b);
|
||||||
|
else if (arg.type == FZL_TYPE_NULL)
|
||||||
|
printf("<null>\n");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
#include "common/fzl_opcodes.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <vm/fzl_vm.h>
|
||||||
|
|
||||||
|
void fzl_vm_print_ops(fzl_vm_t *vm)
|
||||||
|
{
|
||||||
|
if (!vm)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (vm->ops_count < 1)
|
||||||
|
{
|
||||||
|
printf("no opcodes\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < vm->ops_count; i++)
|
||||||
|
{
|
||||||
|
switch (vm->ops[i])
|
||||||
|
{
|
||||||
|
case FZL_OPCODE_LOADCONST:
|
||||||
|
printf("LOADCONST #%lu\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_CALL_BUILTIN:
|
||||||
|
printf("CALL_BUILTIN #%lu\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_PUSHNUM:
|
||||||
|
printf("PUSHNUM %lu\n", vm->ops[++i]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_MK_ENVTABLE:
|
||||||
|
printf("MK_ENVTABLE\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_RM_ENVTABLE:
|
||||||
|
printf("RM_ENVTABLE\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_STORE:
|
||||||
|
printf("STORE\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_LOOKUP_ENV:
|
||||||
|
printf("LOOKUP_ENV\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_WRITE:
|
||||||
|
printf("WRITE\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_ADD:
|
||||||
|
case FZL_OPCODE_SUB:
|
||||||
|
case FZL_OPCODE_MUL:
|
||||||
|
case FZL_OPCODE_DIV:
|
||||||
|
case FZL_OPCODE_POW:
|
||||||
|
case FZL_OPCODE_MOD:
|
||||||
|
printf("Math opcode\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FZL_OPCODE_NEG:
|
||||||
|
printf("NEG\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("Unknown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -62,7 +62,7 @@ fzl_vm_env_wresult_t fzl_env_storevalue(fzl_vm_env_t *env, char *name,
|
|||||||
if (fzl_env_contains(env, name))
|
if (fzl_env_contains(env, name))
|
||||||
return FZL_VM_ENV_WRESULT_ERR_EXISTS;
|
return FZL_VM_ENV_WRESULT_ERR_EXISTS;
|
||||||
|
|
||||||
env->table[env->size] = (fzl_vm_env_entry_t){.name = name, .value = val};
|
env->table[env->size++] = (fzl_vm_env_entry_t){.name = name, .value = val};
|
||||||
|
|
||||||
return FZL_VM_ENV_WRESULT_OK;
|
return FZL_VM_ENV_WRESULT_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ typedef struct
|
|||||||
{
|
{
|
||||||
char *filepath;
|
char *filepath;
|
||||||
bool help;
|
bool help;
|
||||||
|
bool debug;
|
||||||
} run_args_t;
|
} run_args_t;
|
||||||
|
|
||||||
void print_hint()
|
void print_hint()
|
||||||
@@ -30,6 +31,7 @@ run_args_t *parse_args(int argc, char **argv)
|
|||||||
run_args_t *args = malloc(sizeof(run_args_t));
|
run_args_t *args = malloc(sizeof(run_args_t));
|
||||||
args->filepath = NULL;
|
args->filepath = NULL;
|
||||||
args->help = false;
|
args->help = false;
|
||||||
|
args->debug = false;
|
||||||
|
|
||||||
for (size_t i = 0; i < argc; i++)
|
for (size_t i = 0; i < argc; i++)
|
||||||
{
|
{
|
||||||
@@ -41,6 +43,8 @@ run_args_t *parse_args(int argc, char **argv)
|
|||||||
args->help = true;
|
args->help = true;
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--debug") == 0)
|
||||||
|
args->debug = true;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Error: unknown flag '%s'.\n", argv[i]);
|
printf("Error: unknown flag '%s'.\n", argv[i]);
|
||||||
@@ -79,12 +83,17 @@ int main(int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (args->debug)
|
||||||
|
fzl_vm_print_ops(vm);
|
||||||
|
else
|
||||||
|
{
|
||||||
int result = fzl_vm_run(vm);
|
int result = fzl_vm_run(vm);
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
printf("Error: %s\n", fzl_vm_geterror());
|
printf("Error: %s\n", fzl_vm_geterror());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
free(args);
|
free(args);
|
||||||
fzl_freevm(vm);
|
fzl_freevm(vm);
|
||||||
|
|||||||
Reference in New Issue
Block a user