wip: working on analyzer
- Feat: now symbols table store node on PUT - Feat: call analyzer with callee resolving and arguments validate - Feat: identifier analyzer - Feat: analyzer utils (types compat, tok-to-type, type-to-string)
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *tmp_method_name = NULL;
|
||||
char call_problem[256] = {0};
|
||||
|
||||
// Try to get symbol name from callee
|
||||
char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
|
||||
{
|
||||
*method = false;
|
||||
|
||||
if (callee->type == FZL_NODETYPE_IDENTIFIER)
|
||||
return (char *)callee->token->value;
|
||||
|
||||
if (callee->type != FZL_NODETYPE_FIELD)
|
||||
return NULL;
|
||||
|
||||
*method = true;
|
||||
fzl_an_res_t parent_res = analyze_common(analyzer, callee->memb.parent);
|
||||
if (parent_res.type != FZL_TYPE_OBJECT || !parent_res.classname)
|
||||
return NULL;
|
||||
|
||||
char *fn_name = callee->memb.field->token->value;
|
||||
size_t classname_len = strlen(parent_res.classname);
|
||||
size_t methodname_len = strlen(fn_name);
|
||||
tmp_method_name = malloc(classname_len + methodname_len + 2);
|
||||
if (tmp_method_name)
|
||||
{
|
||||
an_push_error(analyzer, callee->token,
|
||||
"failed to allocate space for method name");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(tmp_method_name, parent_res.classname);
|
||||
tmp_method_name[classname_len] = '_';
|
||||
strcpy(tmp_method_name + classname_len + 1, fn_name);
|
||||
tmp_method_name[classname_len + methodname_len + 1] = 0;
|
||||
|
||||
return tmp_method_name;
|
||||
}
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc);
|
||||
extern char *an_typetostr(fzl_type_t type, char *classname);
|
||||
extern fzl_type_t an_toktotype(fzl_token_t *tok);
|
||||
|
||||
// Analyze call node
|
||||
fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
{
|
||||
if (!call_node || call_node->type != FZL_NODETYPE_CALL)
|
||||
{
|
||||
an_push_error(analyzer, call_node->token,
|
||||
"provided node is not typeof CALL");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *callee = call_node->call.callee;
|
||||
fzl_an_res_t callee_res = analyze_common(analyzer, callee);
|
||||
if (callee_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
bool is_method = false;
|
||||
char *callee_name = resolve_callee(analyzer, callee, &is_method);
|
||||
if (!callee_name)
|
||||
{
|
||||
an_push_error(analyzer, callee->token, "expression is not callable");
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
free(tmp_method_name);
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_sym_t sym;
|
||||
bool sym_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((sym_found = fzl_symt_get(analyzer->symbols[i], callee_name, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem), "cannot resolve symbol '%s'",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (sym.node->type != FZL_NODETYPE_FUNCTION)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem), "'%s' is not callable",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (sym.node->fn.args_count != call_node->call.args_count)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem),
|
||||
"expected %zu argument(s) while %zu is specified",
|
||||
sym.node->fn.args_count, call_node->call.args_count);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < sym.node->fn.args_count; i++)
|
||||
{
|
||||
fzl_an_res_t req = analyze_common(analyzer, sym.node->fn.args[i]);
|
||||
fzl_an_res_t got = analyze_common(analyzer, call_node->call.args[i]);
|
||||
if (!an_is_compatible(req.type, got.type, req.classname, got.classname))
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem),
|
||||
"expect argument %zu to be %s, got '%s'", i + 1,
|
||||
an_typetostr(req.type, req.classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
fzl_type_t type = an_toktotype(sym.node->fn.type);
|
||||
char *classname = sym.classname;
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = type, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// TODO:
|
||||
// 1. check for manual `this` declaration
|
||||
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
|
||||
// Analyze class declaration
|
||||
fzl_an_res_t analyze_class(fzl_node_t *class_node) {}
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "compiler/fzl_token.h"
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// Analyzer main function
|
||||
fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node)
|
||||
{
|
||||
fzl_an_res_t res = {.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
res = analyze_identifier(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
for (size_t i = 0; i < node->list.entry_count; i++)
|
||||
{
|
||||
fzl_an_res_t entry_res =
|
||||
analyze_common(analyzer, node->list.entries[i]);
|
||||
if (entry_res.error)
|
||||
res.error = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CALL:
|
||||
res = analyze_call(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
switch (node->token->type)
|
||||
{
|
||||
case FZL_TOKTYPE_STR_LITERAL:
|
||||
res.type = FZL_TYPE_STRING;
|
||||
break;
|
||||
|
||||
case FZL_TOKTYPE_NUM_LITERAL:
|
||||
res.type = FZL_TYPE_INT;
|
||||
break;
|
||||
|
||||
case FZL_TOKTYPE_BOOL_LITERAL:
|
||||
res.type = FZL_TYPE_BOOL;
|
||||
break;
|
||||
|
||||
case FZL_TOKTYPE_NULL_LITERAL:
|
||||
res.type = FZL_TYPE_OBJECT;
|
||||
res.classname = "null";
|
||||
break;
|
||||
|
||||
default:
|
||||
an_push_error(analyzer, node->token, "unknown literal type");
|
||||
res.error = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char id_problem[256];
|
||||
|
||||
// Analyze identifier
|
||||
fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!id || id->type != FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
snprintf(id_problem, sizeof(id_problem), "cannot parse identifier");
|
||||
an_push_error(analyzer, NULL, id_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *symname = id->token->value;
|
||||
fzl_sym_t sym;
|
||||
bool sym_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((sym_found = fzl_symt_get(analyzer->symbols[i], symname, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
{
|
||||
snprintf(id_problem, sizeof(id_problem), "cannot find symbol '%s'",
|
||||
symname);
|
||||
an_push_error(analyzer, id->token, id_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = sym.type, .classname = sym.classname};
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// Check compatibility between A and B types (and classnames)
|
||||
bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc)
|
||||
{
|
||||
if (a == b)
|
||||
return true;
|
||||
|
||||
if (a == FZL_TYPE_OBJECT && b == FZL_TYPE_OBJECT)
|
||||
{
|
||||
if (strcmp(ac, "null") == 0 || strcmp(bc, "null") == 0)
|
||||
return true;
|
||||
|
||||
return strcmp(ac, bc) == 0;
|
||||
}
|
||||
|
||||
if (a >= FZL_TYPE_INT8 && b >= FZL_TYPE_INT8)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Convert type to string
|
||||
char *an_typetostr(fzl_type_t type, char *classname)
|
||||
{
|
||||
if (classname)
|
||||
return classname;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case FZL_TYPE_NULL:
|
||||
return "null";
|
||||
|
||||
case FZL_TYPE_BOOL:
|
||||
return "boolean";
|
||||
|
||||
case FZL_TYPE_STRING:
|
||||
return "string";
|
||||
|
||||
case FZL_TYPE_CHAR:
|
||||
return "char";
|
||||
|
||||
case FZL_TYPE_INT8:
|
||||
return "i8";
|
||||
|
||||
case FZL_TYPE_INT16:
|
||||
return "i16";
|
||||
|
||||
case FZL_TYPE_INT32:
|
||||
return "i32";
|
||||
|
||||
case FZL_TYPE_INT64:
|
||||
return "i64";
|
||||
|
||||
case FZL_TYPE_UINT8:
|
||||
return "u8";
|
||||
|
||||
case FZL_TYPE_UINT16:
|
||||
return "u16";
|
||||
|
||||
case FZL_TYPE_UINT32:
|
||||
return "u32";
|
||||
|
||||
case FZL_TYPE_UINT64:
|
||||
return "u64";
|
||||
|
||||
case FZL_TYPE_INT:
|
||||
return "int";
|
||||
|
||||
case FZL_TYPE_UINT:
|
||||
return "uint";
|
||||
|
||||
default:
|
||||
return "<unknown>";
|
||||
}
|
||||
}
|
||||
|
||||
// Convert type token to type
|
||||
fzl_type_t an_toktotype(fzl_token_t *tok)
|
||||
{
|
||||
if (!tok)
|
||||
return FZL_TYPE_NULL;
|
||||
|
||||
if (tok->type == FZL_TOKTYPE_NAME)
|
||||
return FZL_TYPE_OBJECT;
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user