Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8769ac81c | ||
|
|
d86d95f4fa | ||
|
|
e7573743aa | ||
|
|
2c76277a68 |
@@ -20,5 +20,6 @@ add_executable(fzc
|
||||
# Virtual machine
|
||||
add_executable(fz
|
||||
src/vm/fzl_vm.c
|
||||
src/vm/fzl_vm_env.c
|
||||
src/vm/main.c)
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
uint work_it = 21 * 2 ^ 2
|
||||
int make_it = 10
|
||||
|
||||
u64 do_it
|
||||
do_it = 15
|
||||
|
||||
printline(work_it)
|
||||
printline(make_it + do_it)
|
||||
|
||||
char make_us
|
||||
string floyd = "hello, Pink Floyd!"
|
||||
|
||||
@@ -8,6 +8,12 @@ typedef enum
|
||||
FZL_OPCODE_CALL_BUILTIN = 0xF00002, // Call builtin function
|
||||
FZL_OPCODE_PUSHNUM = 0xF00003, // Push number into stack
|
||||
|
||||
FZL_OPCODE_MK_ENVTABLE = 0xF10001, // Allocate new environment table
|
||||
FZL_OPCODE_RM_ENVTABLE = 0xF10002, // Free environment table
|
||||
FZL_OPCODE_STORE = 0xF10003, // Push variable to environment table
|
||||
FZL_OPCODE_LOOKUP_ENV = 0xF10004, // Lookup name in environment table
|
||||
FZL_OPCODE_WRITE = 0xF10005, // Wirte into variable
|
||||
|
||||
FZL_OPCODE_ADD = 0xA00001, // Sum two numbers in arg strack
|
||||
FZL_OPCODE_SUB = 0xA00002, // Subract two numbers in arg stack
|
||||
FZL_OPCODE_MUL = 0xA00003, // Multiply two numbers in arg stack
|
||||
|
||||
@@ -65,6 +65,13 @@ typedef struct fzl_node
|
||||
{
|
||||
struct fzl_node *operand;
|
||||
} unary_op;
|
||||
|
||||
// Variables declaration and initializing
|
||||
struct
|
||||
{
|
||||
fzl_token_t *type;
|
||||
struct fzl_node *identifier;
|
||||
} var;
|
||||
};
|
||||
} fzl_node_t;
|
||||
|
||||
|
||||
+3
-1
@@ -5,6 +5,7 @@
|
||||
#include <compiler/utils/fzl_const_pool.h> // NOTE: move outside compiler scope to common
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <vm/fzl_vm_env.h>
|
||||
|
||||
// VM descriptor
|
||||
typedef struct
|
||||
@@ -26,7 +27,8 @@ typedef struct
|
||||
size_t ctop;
|
||||
|
||||
// Environment stack
|
||||
// TODO: implement `estack` and `etop`
|
||||
fzl_vm_env_t estack[256];
|
||||
size_t etop;
|
||||
} fzl_vm_t;
|
||||
|
||||
// Allocate FZL VM
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef FZL_VM_ENV_H
|
||||
#define FZL_VM_ENV_H
|
||||
|
||||
#include <common/fzl_value.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FZL_VM_ENV_WRESULT_OK,
|
||||
FZL_VM_ENV_WRESULT_ERR,
|
||||
FZL_VM_ENV_WRESULT_ERR_EXISTS,
|
||||
FZL_VM_ENV_WRESULT_ERR_NOT_FOUND
|
||||
} fzl_vm_env_wresult_t;
|
||||
|
||||
// Environment table entry descriptor
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
fzl_value_t value;
|
||||
} fzl_vm_env_entry_t;
|
||||
|
||||
// Environment table descriptor
|
||||
typedef struct
|
||||
{
|
||||
size_t size;
|
||||
size_t remainder;
|
||||
|
||||
fzl_vm_env_entry_t *table;
|
||||
} fzl_vm_env_t;
|
||||
|
||||
// Allocate env table
|
||||
fzl_vm_env_t *fzl_mkenv();
|
||||
|
||||
// Try get value by name
|
||||
fzl_value_t fzl_env_lookup(fzl_vm_env_t *env, char *name);
|
||||
|
||||
// Check name in env table
|
||||
bool fzl_env_contains(fzl_vm_env_t *env, char *name);
|
||||
|
||||
// Store new value
|
||||
fzl_vm_env_wresult_t fzl_env_storevalue(fzl_vm_env_t *env, char *name,
|
||||
fzl_value_t val);
|
||||
|
||||
// Overwrite existing value
|
||||
fzl_vm_env_wresult_t fzl_env_writevalue(fzl_vm_env_t *env, char *name,
|
||||
fzl_value_t newval);
|
||||
|
||||
// Free env table
|
||||
void fzl_freeenv(fzl_vm_env_t *env);
|
||||
|
||||
#endif // FZL_VM_ENV_H
|
||||
+59
-45
@@ -32,56 +32,70 @@ void print_node(fzl_node_t *node, int level)
|
||||
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_CALL:
|
||||
if (node->call.args_count > 0)
|
||||
{
|
||||
printf("%s Call fn '%s' with args:\n", levelbuf,
|
||||
(char *)node->token->value);
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
print_node(node->call.args[i], level + 1);
|
||||
}
|
||||
else
|
||||
printf("%s Call fn '%s' w/o args\n", levelbuf,
|
||||
(char *)node->token->value);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
printf("%s Literal '%s'\n", levelbuf, (char *)node->token->value);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
printf("%s Block: ", levelbuf);
|
||||
|
||||
if (node->list.entry_count < 1)
|
||||
{
|
||||
printf("<empty>\n");
|
||||
case FZL_NODETYPE_DECLARATION:
|
||||
printf("%s Declare variable with type %d:\n", levelbuf,
|
||||
node->var.type->type);
|
||||
print_node(node->var.identifier, level + 1);
|
||||
break;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
print_node(node->list.entries[i], level + 1);
|
||||
case FZL_NODETYPE_ASSIGN:
|
||||
printf("%s Assign:\n", levelbuf);
|
||||
printf("-%s Target:\n", levelbuf);
|
||||
print_node(node->binary_op.left, level + 2);
|
||||
printf("-%s Source:\n", levelbuf);
|
||||
print_node(node->binary_op.right, level + 2);
|
||||
break;
|
||||
|
||||
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:
|
||||
printf("%s Binary op (%d)\n", levelbuf, node->type);
|
||||
printf("-%s LEFT:\n", levelbuf);
|
||||
print_node(node->binary_op.left, level + 2);
|
||||
printf("-%s RIGHT:\n", levelbuf);
|
||||
print_node(node->binary_op.right, level + 2);
|
||||
break;
|
||||
case FZL_NODETYPE_CALL:
|
||||
if (node->call.args_count > 0)
|
||||
{
|
||||
printf("%s Call fn '%s' with args:\n", levelbuf,
|
||||
(char *)node->token->value);
|
||||
for (size_t i = 0; i < node->call.args_count; i++)
|
||||
print_node(node->call.args[i], level + 1);
|
||||
}
|
||||
else
|
||||
printf("%s Call fn '%s' w/o args\n", levelbuf,
|
||||
(char *)node->token->value);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
printf("%s Identifier ('%s')\n", levelbuf, (char *)node->token->value);
|
||||
break;
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
printf("%s Literal '%s'\n", levelbuf, (char *)node->token->value);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("%s Secure node\n", levelbuf);
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
printf("%s Block: ", levelbuf);
|
||||
|
||||
if (node->list.entry_count < 1)
|
||||
{
|
||||
printf("<empty>\n");
|
||||
break;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
print_node(node->list.entries[i], level + 1);
|
||||
|
||||
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:
|
||||
printf("%s Binary op (%d)\n", levelbuf, node->type);
|
||||
printf("-%s LEFT:\n", levelbuf);
|
||||
print_node(node->binary_op.left, level + 2);
|
||||
printf("-%s RIGHT:\n", levelbuf);
|
||||
print_node(node->binary_op.right, level + 2);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
printf("%s Identifier ('%s')\n", levelbuf, (char *)node->token->value);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("%s Secure node\n", levelbuf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+64
-20
@@ -4,6 +4,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char plast_err[256];
|
||||
|
||||
@@ -93,21 +94,21 @@ static fzl_nodetype_t math_op_to_type(fzl_toktype_t type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case FZL_TOKTYPE_PLUS:
|
||||
return FZL_NODETYPE_ADD;
|
||||
case FZL_TOKTYPE_MINUS:
|
||||
return FZL_NODETYPE_SUB;
|
||||
case FZL_TOKTYPE_STAR:
|
||||
return FZL_NODETYPE_MUL;
|
||||
case FZL_TOKTYPE_SLASH:
|
||||
return FZL_NODETYPE_DIV;
|
||||
case FZL_TOKTYPE_CARET:
|
||||
return FZL_NODETYPE_POW;
|
||||
case FZL_TOKTYPE_PERCENT:
|
||||
return FZL_NODETYPE_MOD;
|
||||
case FZL_TOKTYPE_PLUS:
|
||||
return FZL_NODETYPE_ADD;
|
||||
case FZL_TOKTYPE_MINUS:
|
||||
return FZL_NODETYPE_SUB;
|
||||
case FZL_TOKTYPE_STAR:
|
||||
return FZL_NODETYPE_MUL;
|
||||
case FZL_TOKTYPE_SLASH:
|
||||
return FZL_NODETYPE_DIV;
|
||||
case FZL_TOKTYPE_CARET:
|
||||
return FZL_NODETYPE_POW;
|
||||
case FZL_TOKTYPE_PERCENT:
|
||||
return FZL_NODETYPE_MOD;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,13 +116,13 @@ static fzl_nodetype_t unary_op_to_type(fzl_toktype_t type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case FZL_TOKTYPE_PLUS:
|
||||
return FZL_NODETYPE_UNARY_PLUS;
|
||||
case FZL_TOKTYPE_MINUS:
|
||||
return FZL_NODETYPE_UNARY_MINUS;
|
||||
case FZL_TOKTYPE_PLUS:
|
||||
return FZL_NODETYPE_UNARY_PLUS;
|
||||
case FZL_TOKTYPE_MINUS:
|
||||
return FZL_NODETYPE_UNARY_MINUS;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -380,6 +381,49 @@ fzl_node_t *parse_expression(fzl_parser_t *parser)
|
||||
// Statement parsing
|
||||
fzl_node_t *parse_statement(fzl_parser_t *parser)
|
||||
{
|
||||
if (is(parser, FZL_TOKTYPE_TYPE) || is(parser, FZL_TOKTYPE_NAME))
|
||||
{
|
||||
fzl_token_t *type = peek(parser);
|
||||
if (type->type == FZL_TOKTYPE_TYPE)
|
||||
consume(parser);
|
||||
|
||||
if (type->type == FZL_TOKTYPE_TYPE || next(parser)->type == FZL_TOKTYPE_EQ)
|
||||
{
|
||||
|
||||
fzl_node_t *identifier = parse_primary(parser);
|
||||
if (!identifier || identifier->type != FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
snprintf(plast_err, sizeof(plast_err), "expected identifier at %ld:%ld",
|
||||
type->ln, type->cn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fzl_node_t *left = fzl_mknode(FZL_NODETYPE_DECLARATION, type);
|
||||
left->var.type = type;
|
||||
left->var.identifier = identifier;
|
||||
|
||||
if (!is(parser, FZL_TOKTYPE_EQ))
|
||||
return left;
|
||||
|
||||
fzl_node_t *right;
|
||||
while (is(parser, FZL_TOKTYPE_EQ))
|
||||
{
|
||||
fzl_token_t *asn = consume(parser);
|
||||
right = parse_expression(parser);
|
||||
if (!right)
|
||||
return NULL;
|
||||
|
||||
fzl_node_t *node = fzl_mknode(FZL_NODETYPE_ASSIGN, asn);
|
||||
node->binary_op.left = left;
|
||||
node->binary_op.right = right;
|
||||
|
||||
left = node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
}
|
||||
|
||||
return parse_expression(parser);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,16 +59,30 @@ fzl_token_t *parse_name(fzl_reader_t *reader)
|
||||
lpos++;
|
||||
size++;
|
||||
} while ((reader->source[lpos] >= 'A' && reader->source[lpos] <= 'Z') ||
|
||||
(reader->source[lpos] >= 'a' && reader->source[lpos] <= 'z'));
|
||||
(reader->source[lpos] >= 'a' && reader->source[lpos] <= 'z') ||
|
||||
(reader->source[lpos] >= '0' && reader->source[lpos] <= '9') ||
|
||||
(reader->source[lpos] == '_'));
|
||||
|
||||
char *value = malloc(size);
|
||||
char *value = malloc(size + 1);
|
||||
memcpy(value, reader->source + reader->position, size);
|
||||
value[size] = '\0';
|
||||
|
||||
size_t start = reader->position;
|
||||
move(reader, size);
|
||||
|
||||
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_NAME, reader->line_n,
|
||||
start - reader->line_start, value, size);
|
||||
fzl_token_t *tok;
|
||||
size_t line = reader->line_n;
|
||||
size_t pos = start - reader->line_start;
|
||||
|
||||
if (strcmp(value, "str") == 0 || strcmp(value, "char") == 0 ||
|
||||
strcmp(value, "uint") == 0 || strcmp(value, "int") == 0 ||
|
||||
strcmp(value, "i8") == 0 || strcmp(value, "i16") == 0 ||
|
||||
strcmp(value, "i32") == 0 || strcmp(value, "i64") == 0 ||
|
||||
strcmp(value, "u8") == 0 || strcmp(value, "u16") == 0 ||
|
||||
strcmp(value, "u32") == 0 || strcmp(value, "u64") == 0)
|
||||
tok = fzl_mktoken(FZL_TOKTYPE_TYPE, line, pos, value, size);
|
||||
else
|
||||
tok = fzl_mktoken(FZL_TOKTYPE_NAME, line, pos, value, size);
|
||||
free(value);
|
||||
|
||||
return tok;
|
||||
@@ -174,7 +188,7 @@ int fzl_read(fzl_reader_t *reader)
|
||||
|
||||
if (curr == '\n' || curr == ' ' || curr == '\r')
|
||||
{
|
||||
reader->position++;
|
||||
move(reader, 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -198,7 +212,10 @@ int fzl_read(fzl_reader_t *reader)
|
||||
token = add_single(reader, FZL_TOKTYPE_CARET);
|
||||
else if (curr == '%')
|
||||
token = add_single(reader, FZL_TOKTYPE_PERCENT);
|
||||
else if (curr >= 'A' && curr <= 'Z' || curr >= 'a' && curr <= 'z')
|
||||
else if (curr == '=')
|
||||
token = add_single(reader, FZL_TOKTYPE_EQ);
|
||||
else if (curr >= 'A' && curr <= 'Z' || curr >= 'a' && curr <= 'z' ||
|
||||
curr == '_')
|
||||
token = parse_name(reader);
|
||||
else if (curr >= '0' && curr <= '9')
|
||||
token = parse_number(reader);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "common/fzl_types.h"
|
||||
#include <common/fzl_builtins.h>
|
||||
#include <common/fzl_opcodes.h>
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/fzl_walker.h>
|
||||
@@ -94,15 +94,29 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
if (!walker || !node)
|
||||
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;
|
||||
while (node != NULL)
|
||||
{
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_UNARY_PLUS:
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
{
|
||||
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);
|
||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_STRING, .error = false};
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
if (node->list.entry_count > 0)
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
@@ -247,6 +261,38 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
|
||||
push_op(walker, FZL_OPCODE_NEG);
|
||||
current_res = (fzl_walk_res_t){.type = res.type, .error = false};
|
||||
break;
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_DECLARATION:
|
||||
{
|
||||
fzl_walk_res_t rres = walk(walker, node->var.identifier);
|
||||
if (rres.error)
|
||||
return rres;
|
||||
|
||||
push_op(walker, FZL_OPCODE_STORE);
|
||||
|
||||
// NOTE: ignoring type field for now
|
||||
|
||||
current_res = (fzl_walk_res_t){.type = FZL_TYPE_NULL, .error = false};
|
||||
break;
|
||||
}
|
||||
|
||||
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);
|
||||
if (rres.error)
|
||||
return rres;
|
||||
|
||||
push_op(walker, FZL_OPCODE_WRITE);
|
||||
current_res = (fzl_walk_res_t){.type = rres.type, .error = false};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
-17
@@ -29,8 +29,13 @@ fzl_vm_t *fzl_mkvm(const char *filepath)
|
||||
vm->ops = NULL;
|
||||
vm->ops_count = 0;
|
||||
vm->ip = 0;
|
||||
vm->dtop = 0;
|
||||
vm->ctop = 0;
|
||||
vm->etop = 0;
|
||||
|
||||
memset(vm->dstack, 0, sizeof(vm->dstack));
|
||||
vm->dtop = 0;
|
||||
memset(vm->cstack, 0, sizeof(vm->cstack));
|
||||
memset(vm->estack, 0, sizeof(vm->estack));
|
||||
|
||||
FILE *fd = fopen(filepath, "rb");
|
||||
if (!fd)
|
||||
@@ -117,6 +122,11 @@ int fzl_vm_run(fzl_vm_t *vm)
|
||||
|
||||
switch (opcode)
|
||||
{
|
||||
case FZL_OPCODE_MK_ENVTABLE:
|
||||
case FZL_OPCODE_STORE:
|
||||
case FZL_OPCODE_WRITE:
|
||||
break;
|
||||
|
||||
case FZL_OPCODE_CALL_BUILTIN:
|
||||
{
|
||||
uint64_t fn_id = vm->ops[++vm->ip];
|
||||
@@ -127,24 +137,8 @@ int fzl_vm_run(fzl_vm_t *vm)
|
||||
printf("%s\n", arg.as.s.value);
|
||||
else if (arg.type == FZL_TYPE_CHAR)
|
||||
printf("%c\n", arg.as.c);
|
||||
else if (arg.type == FZL_TYPE_INT8)
|
||||
printf("%d\n", arg.as.i8);
|
||||
else if (arg.type == FZL_TYPE_INT16)
|
||||
printf("%d\n", arg.as.i16);
|
||||
else if (arg.type == FZL_TYPE_INT32)
|
||||
printf("%d\n", arg.as.i32);
|
||||
else if (arg.type == FZL_TYPE_INT64)
|
||||
printf("%ld\n", arg.as.i64);
|
||||
else if (arg.type == FZL_TYPE_INT)
|
||||
printf("%ld\n", arg.as.i);
|
||||
else if (arg.type == FZL_TYPE_UINT8)
|
||||
printf("%u\n", arg.as.u8);
|
||||
else if (arg.type == FZL_TYPE_UINT16)
|
||||
printf("%u\n", arg.as.u16);
|
||||
else if (arg.type == FZL_TYPE_UINT32)
|
||||
printf("%u\n", arg.as.u32);
|
||||
else if (arg.type == FZL_TYPE_UINT64)
|
||||
printf("%lu\n", arg.as.u64);
|
||||
else if (arg.type == FZL_TYPE_UINT)
|
||||
printf("%lu\n", arg.as.u);
|
||||
else if (arg.type == FZL_TYPE_BOOL)
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <common/fzl_value.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <vm/fzl_vm_env.h>
|
||||
|
||||
static const fzl_value_t null = {.type = FZL_TYPE_NULL};
|
||||
|
||||
fzl_vm_env_t *fzl_mkenv()
|
||||
{
|
||||
fzl_vm_env_t *env = malloc(sizeof(fzl_vm_env_t));
|
||||
env->remainder = 2;
|
||||
env->size = 0;
|
||||
env->table = malloc(env->remainder * sizeof(fzl_vm_env_entry_t));
|
||||
|
||||
return env;
|
||||
}
|
||||
|
||||
// Resize table if size eq to remainder
|
||||
static void resize_env_if_needed(fzl_vm_env_t *env)
|
||||
{
|
||||
if (env->remainder > env->size)
|
||||
return;
|
||||
|
||||
env->remainder *= 2;
|
||||
env->table = realloc(env->table, env->remainder * sizeof(fzl_vm_env_entry_t));
|
||||
}
|
||||
|
||||
fzl_value_t fzl_env_lookup(fzl_vm_env_t *env, char *name)
|
||||
{
|
||||
if (!env)
|
||||
return null;
|
||||
|
||||
for (size_t i = 0; i < env->size; i++)
|
||||
if (strcmp(env->table[i].name, name) == 0)
|
||||
return env->table[i].value;
|
||||
|
||||
return (fzl_value_t){.type = FZL_TYPE_NULL};
|
||||
}
|
||||
|
||||
bool fzl_env_contains(fzl_vm_env_t *env, char *name)
|
||||
{
|
||||
if (!env)
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; i < env->size; i++)
|
||||
if (strcmp(env->table[i].name, name) == 0)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fzl_vm_env_wresult_t fzl_env_storevalue(fzl_vm_env_t *env, char *name,
|
||||
fzl_value_t val)
|
||||
{
|
||||
if (!env)
|
||||
return FZL_VM_ENV_WRESULT_ERR;
|
||||
|
||||
resize_env_if_needed(env);
|
||||
if (fzl_env_contains(env, name))
|
||||
return FZL_VM_ENV_WRESULT_ERR_EXISTS;
|
||||
|
||||
env->table[env->size] = (fzl_vm_env_entry_t){.name = name, .value = val};
|
||||
|
||||
return FZL_VM_ENV_WRESULT_OK;
|
||||
}
|
||||
|
||||
fzl_vm_env_wresult_t fzl_env_writevalue(fzl_vm_env_t *env, char *name,
|
||||
fzl_value_t newval)
|
||||
{
|
||||
if (!env)
|
||||
return FZL_VM_ENV_WRESULT_ERR;
|
||||
|
||||
for (size_t i = 0; i < env->size; i++)
|
||||
{
|
||||
if (strcmp(env->table[i].name, name) == 0)
|
||||
{
|
||||
env->table[i].value = newval;
|
||||
return FZL_VM_ENV_WRESULT_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return FZL_VM_ENV_WRESULT_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
void fzl_freeenv(fzl_vm_env_t *env)
|
||||
{
|
||||
if (!env)
|
||||
return;
|
||||
|
||||
if (env->table)
|
||||
free(env->table);
|
||||
|
||||
free(env);
|
||||
}
|
||||
Reference in New Issue
Block a user