added functions to scope

This commit is contained in:
Sven Vogel 2024-05-21 11:59:44 +02:00
parent 690e847d54
commit 4a3b974d9f
7 changed files with 120 additions and 35 deletions

View File

@ -0,0 +1,28 @@
#ifndef LLVM_TYPES_FUNCTION_TYPES_H_
#define LLVM_TYPES_FUNCTION_TYPES_H_
#include <llvm/types/structs.h>
#include <glib.h>
enum IO_Qualifier_t {
Unspec,
In,
Out,
InOut
};
typedef struct GemstoneParam_t {
const char* name;
enum IO_Qualifier_t qualifier;
GemstoneTypeRef typename;
} GemstoneParam;
typedef struct GemstoneFun_t {
const char* name;
GArray* params;
} GemstoneFun;
typedef GemstoneFun* GemstoneFunRef;
#endif // LLVM_TYPES_FUNCTION_TYPES_H_

View File

@ -1,6 +1,6 @@
#include "ast/ast.h" #include <ast/ast.h>
#include "llvm/types/scope.h" #include <llvm/types/scope.h>
#include <llvm/function/function.h> #include <llvm/function/function.h>
#include <llvm/types/type.h> #include <llvm/types/type.h>
#include <string.h> #include <string.h>
@ -74,3 +74,34 @@ GemstoneParam param_from_ast(const TypeScopeRef scope, const AST_NODE_PTR node)
return param; return param;
} }
GemstoneFunRef fun_from_ast(const TypeScopeRef scope, const AST_NODE_PTR node) {
if (node->kind != AST_Fun) {
PANIC("Node must be of type AST_Fun: %s", AST_node_to_string(node));
}
GemstoneFunRef function = malloc(sizeof(GemstoneFun));
function->name = AST_get_node(node, 0)->value;
function->params = g_array_new(FALSE, FALSE, sizeof(GemstoneParam));
AST_NODE_PTR list = AST_get_node(node, 1);
for (size_t i = 0; i < list->child_count; i++) {
AST_NODE_PTR param_list = AST_get_node(list, i);
for (size_t k = 0; k < param_list->child_count; k++) {
AST_NODE_PTR param = AST_get_node(param_list, k);
GemstoneParam par = param_from_ast(scope, param);
g_array_append_val(function->params, par);
}
}
// TODO: parse function body
return function;
}
void fun_delete(const GemstoneFunRef fun) {
g_array_free(fun->params, TRUE);
free(fun);
}

View File

@ -3,25 +3,8 @@
#define LLVM_FUNCTION_H_ #define LLVM_FUNCTION_H_
#include <ast/ast.h> #include <ast/ast.h>
#include <llvm/types/type.h> #include <llvm/function/function-types.h>
#include <llvm/types/scope.h>
enum IO_Qualifier_t {
Unspec,
In,
Out,
InOut
};
typedef struct GemstoneParam_t {
const char* name;
enum IO_Qualifier_t qualifier;
GemstoneTypeRef typename;
} GemstoneParam;
typedef struct GemstoneFun_t {
const char* name;
// TODO: add array of parameters
} GemstoneFun;
/** /**
* @brief Convert an AST node into a function parameter struct * @brief Convert an AST node into a function parameter struct
@ -35,8 +18,15 @@ GemstoneParam param_from_ast(const TypeScopeRef scope, const AST_NODE_PTR node);
* @brief Convert an AST node into a function * @brief Convert an AST node into a function
* *
* @param node the node starting a function * @param node the node starting a function
* @return GemstoneFun * @return GemstoneFunRef
*/ */
GemstoneFun fun_from_ast(const TypeScopeRef scope, const AST_NODE_PTR node); GemstoneFunRef fun_from_ast(const TypeScopeRef scope, const AST_NODE_PTR node);
/**
* @brief Delete the given function
*
* @param fun
*/
void fun_delete(const GemstoneFunRef fun);
#endif // LLVM_FUNCTION_H_ #endif // LLVM_FUNCTION_H_

View File

@ -1,4 +1,5 @@
#include <llvm/function/function-types.h>
#include <llvm/types/type.h> #include <llvm/types/type.h>
#include <llvm/types/scope.h> #include <llvm/types/scope.h>
#include <string.h> #include <string.h>
@ -6,6 +7,7 @@
struct TypeScope_t { struct TypeScope_t {
GArray* types; GArray* types;
GArray* scopes; GArray* scopes;
GArray* funcs;
TypeScopeRef parent; TypeScopeRef parent;
}; };
@ -15,6 +17,7 @@ TypeScopeRef type_scope_new() {
// neither zero termination no initialisazion to zero needed // neither zero termination no initialisazion to zero needed
scope->scopes = g_array_new(FALSE, FALSE, sizeof(TypeScopeRef)); scope->scopes = g_array_new(FALSE, FALSE, sizeof(TypeScopeRef));
scope->types = g_array_new(FALSE, FALSE, sizeof(GemstoneTypedefRef)); scope->types = g_array_new(FALSE, FALSE, sizeof(GemstoneTypedefRef));
scope->funcs = g_array_new(FALSE, FALSE, sizeof(GemstoneFunRef));
scope->parent = NULL; scope->parent = NULL;
return scope; return scope;
@ -70,6 +73,27 @@ void type_scope_delete(TypeScopeRef scope) {
g_array_free(scope->scopes, TRUE); g_array_free(scope->scopes, TRUE);
g_array_free(scope->types, TRUE); g_array_free(scope->types, TRUE);
g_array_free(scope->funcs, TRUE);
free(scope); free(scope);
} }
void type_scope_add_fun(TypeScopeRef scope, GemstoneFunRef function) {
g_array_append_val(scope->funcs, function);
}
GemstoneFunRef type_scope_get_fun_from_name(TypeScopeRef scope, const char* name) {
for (guint i = 0; i < scope->funcs->len; i++) {
GemstoneFunRef funref = ((GemstoneFunRef*) scope->funcs->data)[i];
if (strcmp(funref->name, name) == 0) {
return funref;
}
}
if (scope->parent == NULL) {
return NULL;
}
return type_scope_get_fun_from_name(scope->parent, name);
}

View File

@ -2,6 +2,7 @@
#ifndef LLVM_TYPE_SCOPE_H_ #ifndef LLVM_TYPE_SCOPE_H_
#define LLVM_TYPE_SCOPE_H_ #define LLVM_TYPE_SCOPE_H_
#include <llvm/function/function-types.h>
#include <glib.h> #include <glib.h>
#include <llvm/types/structs.h> #include <llvm/types/structs.h>
@ -80,4 +81,22 @@ GemstoneTypedefRef type_scope_get_type_from_name(TypeScopeRef scope, const char*
[[gnu::nonnull(1)]] [[gnu::nonnull(1)]]
void type_scope_delete(TypeScopeRef scope); void type_scope_delete(TypeScopeRef scope);
/**
* @brief Add a function ot the type scope
*
* @param scope
* @param function
*/
void type_scope_add_fun(TypeScopeRef scope, GemstoneFunRef function);
/**
* @brief Attempts to find a function by its name in the current scope
*
* @param scope
* @param name
* @return GemstoneFunRef
*/
[[gnu::nonnull(1), gnu::nonnull(2)]]
GemstoneFunRef type_scope_get_fun_from_name(TypeScopeRef scope, const char* name);
#endif // LLVM_TYPE_SCOPE_H_ #endif // LLVM_TYPE_SCOPE_H_

View File

@ -102,17 +102,10 @@ int main(int argc, char *argv[]) {
TypeScopeRef scope = type_scope_new(); TypeScopeRef scope = type_scope_new();
AST_NODE_PTR fun = AST_get_node(root, 0); AST_NODE_PTR fun = AST_get_node(root, 0);
AST_NODE_PTR list = AST_get_node(fun, 1);
for (size_t i = 0; i < list->child_count; i++) { GemstoneFunRef function = fun_from_ast(scope, fun);
AST_NODE_PTR param_list = AST_get_node(list, i); type_scope_add_fun(scope, function);
assert(function->params->len == 3);
for (size_t k = 0; k < param_list->child_count; k++) {
AST_NODE_PTR param = AST_get_node(param_list, i);
GemstoneParam par = param_from_ast(scope, param);
}
}
type_scope_delete(scope); type_scope_delete(scope);

View File

@ -1,4 +1,4 @@
fun a(in int: a, in float: b) { fun a(in out int: a, in float: b)(out float: c) {
a = 0 a = 0
} }