fix: warnings fix (after -Wall -Wextra)
This commit is contained in:
@@ -3,6 +3,12 @@ project(fz-lang
|
|||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
add_compile_options(-Wall -Wextra -Werror=implicit-function-declaration)
|
||||||
|
|
||||||
|
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
add_compile_options(-fsanitize=address,undefined -g)
|
||||||
|
add_link_options(-fsanitize=address,undefined)
|
||||||
|
endif()
|
||||||
|
|
||||||
include_directories(include)
|
include_directories(include)
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
// Constant types
|
// Constant types
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
FZL_CONSTTYPE_STRING,
|
FZL_CONSTTYPE_STRING,
|
||||||
FZL_CONSTTYPE_NUMBER
|
|
||||||
} fzl_consttype_t;
|
} fzl_consttype_t;
|
||||||
|
|
||||||
// Constant descriptor
|
// Constant descriptor
|
||||||
@@ -34,7 +34,7 @@ typedef struct
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
size_t pool_index;
|
ssize_t pool_index;
|
||||||
} fzl_const_hash_slot_t;
|
} fzl_const_hash_slot_t;
|
||||||
|
|
||||||
// Constant pool descriptor
|
// Constant pool descriptor
|
||||||
@@ -53,13 +53,13 @@ typedef struct
|
|||||||
bool fzl_consts_equal(fzl_const_t a, fzl_const_t b);
|
bool fzl_consts_equal(fzl_const_t a, fzl_const_t b);
|
||||||
|
|
||||||
// Get hash of specified constant
|
// Get hash of specified constant
|
||||||
static uint32_t fzl_hash_const(fzl_const_t c);
|
uint32_t fzl_hash_const(fzl_const_t c);
|
||||||
|
|
||||||
// Allocate constants hash pool
|
// Allocate constants hash pool
|
||||||
fzl_const_hash_pool_t *fzl_mkconstpool();
|
fzl_const_hash_pool_t *fzl_mkconstpool();
|
||||||
|
|
||||||
// Resize the hash table
|
// Resize the hash table
|
||||||
static void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool);
|
void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool);
|
||||||
|
|
||||||
// Push constant. Returns unique index
|
// Push constant. Returns unique index
|
||||||
size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c);
|
size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c);
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ void print_node(fzl_node_t *node, int level)
|
|||||||
if (!node)
|
if (!node)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
size_t lvllen = print_level(levelbuf, level);
|
|
||||||
strcpy(printbuf, levelbuf);
|
strcpy(printbuf, levelbuf);
|
||||||
|
|
||||||
switch (node->type)
|
switch (node->type)
|
||||||
|
|||||||
+11
-17
@@ -56,19 +56,6 @@ static fzl_token_t *peek(fzl_parser_t *parser)
|
|||||||
return parser->cursor->token;
|
return parser->cursor->token;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get token at (cursor + 1)
|
|
||||||
static fzl_token_t *next(fzl_parser_t *parser)
|
|
||||||
{
|
|
||||||
if (!parser || !parser->cursor)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
fzl_reader_ll_t *next = parser->cursor->next;
|
|
||||||
if (!next)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return next->token;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Consume token at cursor and move to the next
|
// Consume token at cursor and move to the next
|
||||||
static fzl_token_t *consume(fzl_parser_t *parser)
|
static fzl_token_t *consume(fzl_parser_t *parser)
|
||||||
{
|
{
|
||||||
@@ -204,8 +191,7 @@ fzl_node_t *parse_postfix(fzl_parser_t *parser)
|
|||||||
if (!parser)
|
if (!parser)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
fzl_node_t *left = parse_primary(parser);
|
fzl_node_t *left = parse_primary(parser);
|
||||||
fzl_node_t *right = NULL;
|
|
||||||
|
|
||||||
if (!left)
|
if (!left)
|
||||||
{
|
{
|
||||||
@@ -766,7 +752,15 @@ fzl_node_t *parse_function(fzl_parser_t *parser)
|
|||||||
if (!parser)
|
if (!parser)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
fzl_token_t *fn = consume(parser);
|
fzl_token_t *fn = consume(parser);
|
||||||
|
if (fn->type != FZL_TOKTYPE_FUNCTION)
|
||||||
|
{
|
||||||
|
snprintf(plast_err, sizeof(plast_err),
|
||||||
|
"'%s': expected keyword 'fn' at %ld:%ld", last_known_tok->filename,
|
||||||
|
last_known_tok->ln, last_known_tok->cn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
fzl_token_t *name = consume(parser);
|
fzl_token_t *name = consume(parser);
|
||||||
if (!name || name->type != FZL_TOKTYPE_NAME)
|
if (!name || name->type != FZL_TOKTYPE_NAME)
|
||||||
{
|
{
|
||||||
@@ -1131,7 +1125,7 @@ void fzl_freeast(fzl_node_t *root)
|
|||||||
|
|
||||||
fzl_node_t *next = root->next;
|
fzl_node_t *next = root->next;
|
||||||
free(root);
|
free(root);
|
||||||
root = root->next;
|
root = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -313,7 +313,7 @@ int fzl_read(fzl_reader_t *reader)
|
|||||||
token = add_single(reader, FZL_TOKTYPE_PERCENT);
|
token = add_single(reader, FZL_TOKTYPE_PERCENT);
|
||||||
else if (curr == '=')
|
else if (curr == '=')
|
||||||
token = add_single(reader, FZL_TOKTYPE_EQ);
|
token = add_single(reader, FZL_TOKTYPE_EQ);
|
||||||
else if (curr >= 'A' && curr <= 'Z' || curr >= 'a' && curr <= 'z' ||
|
else if ((curr >= 'A' && curr <= 'Z') || (curr >= 'a' && curr <= 'z') ||
|
||||||
curr == '_')
|
curr == '_')
|
||||||
token = parse_name(reader);
|
token = parse_name(reader);
|
||||||
else if (curr >= '0' && curr <= '9')
|
else if (curr >= '0' && curr <= '9')
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ fzl_walker_t *fzl_mkwalker(fzl_node_t *root)
|
|||||||
// --- Walking zone ---
|
// --- Walking zone ---
|
||||||
|
|
||||||
// Convert type token to `fzl_type_t`
|
// Convert type token to `fzl_type_t`
|
||||||
const fzl_type_t tok_to_type(fzl_token_t *tok)
|
fzl_type_t tok_to_type(fzl_token_t *tok)
|
||||||
{
|
{
|
||||||
if (tok->type == FZL_TOKTYPE_NAME)
|
if (tok->type == FZL_TOKTYPE_NAME)
|
||||||
return FZL_TYPE_OBJECT;
|
return FZL_TYPE_OBJECT;
|
||||||
@@ -403,7 +403,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: add sym_top limit check
|
// NOTE: add sym_top limit check
|
||||||
size_t lvl = walker->sym_top;
|
ssize_t lvl = walker->sym_top;
|
||||||
walker->syms[walker->sym_top++] = fzl_mksymt();
|
walker->syms[walker->sym_top++] = fzl_mksymt();
|
||||||
|
|
||||||
fzl_walk_ctx_t *newctx = nest_ctx(walker, FZL_WALK_CTX_TYPE_FN);
|
fzl_walk_ctx_t *newctx = nest_ctx(walker, FZL_WALK_CTX_TYPE_FN);
|
||||||
@@ -939,7 +939,6 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
}
|
}
|
||||||
else if (node->token->type == FZL_TOKTYPE_NULL_LITERAL)
|
else if (node->token->type == FZL_TOKTYPE_NULL_LITERAL)
|
||||||
{
|
{
|
||||||
fzl_token_t *tok = node->token;
|
|
||||||
push_op(walker, FZL_OPCODE_PUSHNUM);
|
push_op(walker, FZL_OPCODE_PUSHNUM);
|
||||||
push_word(walker, 0);
|
push_word(walker, 0);
|
||||||
push_op(walker, FZL_OPCODE_DEREFERENCE);
|
push_op(walker, FZL_OPCODE_DEREFERENCE);
|
||||||
@@ -1145,7 +1144,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
|||||||
if (obj_res.error)
|
if (obj_res.error)
|
||||||
return obj_res;
|
return obj_res;
|
||||||
|
|
||||||
fzl_ctable_t *ct;
|
fzl_ctable_t *ct = NULL;
|
||||||
for (size_t i = 0; i < walker->classes_count; i++)
|
for (size_t i = 0; i < walker->classes_count; i++)
|
||||||
{
|
{
|
||||||
if (strcmp(obj_res.classname, walker->classes[i]->name) == 0)
|
if (strcmp(obj_res.classname, walker->classes[i]->name) == 0)
|
||||||
@@ -1164,7 +1163,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};
|
||||||
}
|
}
|
||||||
|
|
||||||
fzl_ctable_field_t *ctf;
|
fzl_ctable_field_t *ctf = NULL;
|
||||||
for (size_t i = 0; i < ct->fields_count; i++)
|
for (size_t i = 0; i < ct->fields_count; i++)
|
||||||
{
|
{
|
||||||
if (strcmp(field_node->memb.field->token->value,
|
if (strcmp(field_node->memb.field->token->value,
|
||||||
|
|||||||
@@ -11,31 +11,31 @@ bool fzl_consts_equal(fzl_const_t a, fzl_const_t b)
|
|||||||
|
|
||||||
switch (a.type)
|
switch (a.type)
|
||||||
{
|
{
|
||||||
case FZL_CONSTTYPE_STRING:
|
case FZL_CONSTTYPE_STRING:
|
||||||
{
|
{
|
||||||
if (a.as.string.length != b.as.string.length)
|
if (a.as.string.length != b.as.string.length)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return memcmp(a.as.string.chars, b.as.string.chars, a.as.string.length) ==
|
return memcmp(a.as.string.chars, b.as.string.chars, a.as.string.length) ==
|
||||||
0;
|
0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t fzl_hash_const(fzl_const_t c)
|
uint32_t fzl_hash_const(fzl_const_t c)
|
||||||
{
|
{
|
||||||
uint32_t hash = 2166136261u;
|
uint32_t hash = 2166136261u;
|
||||||
const uint8_t *data = NULL;
|
const uint8_t *data = NULL;
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
|
||||||
switch (c.type)
|
switch (c.type)
|
||||||
{
|
{
|
||||||
case FZL_CONSTTYPE_STRING:
|
case FZL_CONSTTYPE_STRING:
|
||||||
data = (const uint8_t *)c.as.string.chars;
|
data = (const uint8_t *)c.as.string.chars;
|
||||||
size = c.as.string.length;
|
size = c.as.string.length;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < size; i++)
|
for (size_t i = 0; i < size; i++)
|
||||||
@@ -51,27 +51,27 @@ fzl_const_hash_pool_t *fzl_mkconstpool()
|
|||||||
{
|
{
|
||||||
fzl_const_hash_pool_t *pool = malloc(sizeof(fzl_const_hash_pool_t));
|
fzl_const_hash_pool_t *pool = malloc(sizeof(fzl_const_hash_pool_t));
|
||||||
|
|
||||||
pool->size = 0;
|
pool->size = 0;
|
||||||
pool->capacity = 8;
|
pool->capacity = 8;
|
||||||
pool->array = malloc(pool->capacity * sizeof(fzl_const_t));
|
pool->array = malloc(pool->capacity * sizeof(fzl_const_t));
|
||||||
|
|
||||||
pool->slots_count = 0;
|
pool->slots_count = 0;
|
||||||
pool->slots_capacity = 16;
|
pool->slots_capacity = 16;
|
||||||
pool->slots = malloc(pool->slots_capacity * sizeof(fzl_const_hash_slot_t));
|
pool->slots = malloc(pool->slots_capacity * sizeof(fzl_const_hash_slot_t));
|
||||||
for (int i = 0; i < pool->slots_capacity; i++)
|
for (size_t i = 0; i < pool->slots_capacity; i++)
|
||||||
pool->slots[i].pool_index = -1;
|
pool->slots[i].pool_index = -1;
|
||||||
|
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool)
|
void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool)
|
||||||
{
|
{
|
||||||
int old_capacity = pool->slots_capacity;
|
int old_capacity = pool->slots_capacity;
|
||||||
fzl_const_hash_slot_t *old_slots = pool->slots;
|
fzl_const_hash_slot_t *old_slots = pool->slots;
|
||||||
|
|
||||||
pool->slots_capacity = old_capacity * 2;
|
pool->slots_capacity = old_capacity * 2;
|
||||||
pool->slots = malloc(pool->slots_capacity * sizeof(fzl_const_hash_slot_t));
|
pool->slots = malloc(pool->slots_capacity * sizeof(fzl_const_hash_slot_t));
|
||||||
for (int i = 0; i < pool->slots_capacity; i++)
|
for (size_t i = 0; i < pool->slots_capacity; i++)
|
||||||
pool->slots[i].pool_index = -1;
|
pool->slots[i].pool_index = -1;
|
||||||
pool->slots_count = 0;
|
pool->slots_count = 0;
|
||||||
|
|
||||||
@@ -81,8 +81,8 @@ static void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
int index_in_pool = old_slots[i].pool_index;
|
int index_in_pool = old_slots[i].pool_index;
|
||||||
fzl_const_t c = pool->array[index_in_pool];
|
fzl_const_t c = pool->array[index_in_pool];
|
||||||
uint32_t hash = fzl_hash_const(c);
|
uint32_t hash = fzl_hash_const(c);
|
||||||
|
|
||||||
uint32_t bucket = hash & (pool->slots_capacity - 1);
|
uint32_t bucket = hash & (pool->slots_capacity - 1);
|
||||||
while (pool->slots[bucket].pool_index != -1)
|
while (pool->slots[bucket].pool_index != -1)
|
||||||
@@ -100,7 +100,7 @@ size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c)
|
|||||||
if (pool->slots_count * 4 >= pool->slots_capacity * 3)
|
if (pool->slots_count * 4 >= pool->slots_capacity * 3)
|
||||||
fzl_consts_pool_resize_hash(pool);
|
fzl_consts_pool_resize_hash(pool);
|
||||||
|
|
||||||
uint32_t hash = fzl_hash_const(c);
|
uint32_t hash = fzl_hash_const(c);
|
||||||
uint32_t bucket = hash & (pool->slots_capacity - 1);
|
uint32_t bucket = hash & (pool->slots_capacity - 1);
|
||||||
|
|
||||||
while (pool->slots[bucket].pool_index != -1)
|
while (pool->slots[bucket].pool_index != -1)
|
||||||
@@ -119,7 +119,7 @@ size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c)
|
|||||||
pool->capacity * sizeof(fzl_const_t));
|
pool->capacity * sizeof(fzl_const_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t new_index = pool->size;
|
size_t new_index = pool->size;
|
||||||
pool->array[new_index] = c;
|
pool->array[new_index] = c;
|
||||||
pool->size++;
|
pool->size++;
|
||||||
|
|
||||||
|
|||||||
@@ -72,9 +72,9 @@ bool fzl_linked_add_files(fzl_linked_t *l, char **files, size_t files_count)
|
|||||||
.end = current_start + file_sz - 1};
|
.end = current_start + file_sz - 1};
|
||||||
|
|
||||||
if (!l->source)
|
if (!l->source)
|
||||||
l->source = malloc(file_sz);
|
l->source = malloc(file_sz + 1);
|
||||||
else
|
else
|
||||||
l->source = realloc(l->source, current_start + file_sz);
|
l->source = realloc(l->source, current_start + file_sz + 1);
|
||||||
|
|
||||||
if (!l->source)
|
if (!l->source)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ fzl_options_t *fzl_mkoptions(int argc, char **argv)
|
|||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t i;
|
int i;
|
||||||
for (i = 1; i < argc; i++)
|
for (i = 1; i < argc; i++)
|
||||||
{
|
{
|
||||||
if (argv[i][0] != '-')
|
if (argv[i][0] != '-')
|
||||||
@@ -108,7 +108,7 @@ fzl_options_t *fzl_mkoptions(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
printf("%sError%s: flags should go before source files list.\n", FZL_CERROR,
|
printf("%sError%s: flags should go before source files list.\n", FZL_CERROR,
|
||||||
FZL_CRST);
|
FZL_CRST);
|
||||||
printf("Remain %zu arguments are not handled.\n", argc - 1 - i);
|
printf("Remain %d arguments are not handled.\n", argc - 1 - i);
|
||||||
|
|
||||||
free(files);
|
free(files);
|
||||||
free(opts);
|
free(opts);
|
||||||
|
|||||||
+10
-1
@@ -650,8 +650,17 @@ int fzl_vm_run(fzl_vm_t *vm)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case FZL_OPCODE_JMP:
|
case FZL_OPCODE_JMP:
|
||||||
vm->ip = vm->ops[++vm->ip] - 1;
|
{
|
||||||
|
if (vm->ops_count >= vm->ip)
|
||||||
|
{
|
||||||
|
snprintf(last_err, sizeof(last_err),
|
||||||
|
"runtime error - expected JMP addr");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
uint64_t jmp_ip = vm->ops[++vm->ip];
|
||||||
|
vm->ip = jmp_ip - 1;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case FZL_OPCODE_AND:
|
case FZL_OPCODE_AND:
|
||||||
if (vm->dtop < 2)
|
if (vm->dtop < 2)
|
||||||
|
|||||||
@@ -7,8 +7,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <vm/fzl_vm_env.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 *fzl_mkenv()
|
||||||
{
|
{
|
||||||
fzl_vm_env_t *env = malloc(sizeof(fzl_vm_env_t));
|
fzl_vm_env_t *env = malloc(sizeof(fzl_vm_env_t));
|
||||||
|
|||||||
+1
-1
@@ -33,7 +33,7 @@ run_args_t *parse_args(int argc, char **argv)
|
|||||||
args->help = false;
|
args->help = false;
|
||||||
args->debug = false;
|
args->debug = false;
|
||||||
|
|
||||||
for (size_t i = 0; i < argc; i++)
|
for (int i = 0; i < argc; i++)
|
||||||
{
|
{
|
||||||
if (argv[i][0] != '-')
|
if (argv[i][0] != '-')
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user