added: parameter to backend
This commit is contained in:
parent
6afa552347
commit
f243bb6bfe
|
@ -27,7 +27,7 @@ Target create_native_target() {
|
||||||
target.features.allocation = LLVM;
|
target.features.allocation = LLVM;
|
||||||
assert(target.features.str != NULL);
|
assert(target.features.str != NULL);
|
||||||
|
|
||||||
target.opt = LLVMCodeGenLevelDefault;
|
target.opt = LLVMCodeGenLevelNone;
|
||||||
target.reloc = LLVMRelocDefault;
|
target.reloc = LLVMRelocDefault;
|
||||||
target.model = LLVMCodeModelDefault;
|
target.model = LLVMCodeModelDefault;
|
||||||
|
|
||||||
|
|
|
@ -186,6 +186,8 @@ BackendError impl_relational_operation(LLVMBackendCompileUnit *unit,
|
||||||
PANIC("invalid type for relational operator");
|
PANIC("invalid type for relational operator");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// *llvm_result = convert_integral_to_boolean(builder, *llvm_result);
|
||||||
|
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -337,7 +339,7 @@ BackendError impl_typecast(LLVMBackendCompileUnit *unit, LLVMLocalScope *scope,
|
||||||
const LLVMOpcode opcode =
|
const LLVMOpcode opcode =
|
||||||
LLVMGetCastOpcode(operand, src_signed, target_type, dst_signed);
|
LLVMGetCastOpcode(operand, src_signed, target_type, dst_signed);
|
||||||
*llvm_result =
|
*llvm_result =
|
||||||
LLVMBuildCast(builder, opcode, operand, target_type, "transmute");
|
LLVMBuildCast(builder, opcode, operand, target_type, "typecast");
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
@ -357,6 +359,23 @@ BackendError impl_variable_load(LLVMBackendCompileUnit *unit, LLVMLocalScope *sc
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BackendError impl_address_of(LLVMBackendCompileUnit *unit, LLVMLocalScope *scope,
|
||||||
|
LLVMBuilderRef builder, AddressOf* addressOf,
|
||||||
|
LLVMValueRef *llvm_result) {
|
||||||
|
|
||||||
|
LLVMValueRef llvm_variable = NULL;
|
||||||
|
BackendError err = impl_expr(unit, scope, builder, addressOf->variable, &llvm_variable);
|
||||||
|
|
||||||
|
if (err.kind != Success) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
LLVMValueRef zero = LLVMConstInt(LLVMInt32Type(), 0, 0);
|
||||||
|
*llvm_result = LLVMBuildGEP2(builder, LLVMTypeOf(llvm_variable), llvm_variable, &zero, 1, "address of");
|
||||||
|
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
BackendError impl_expr(LLVMBackendCompileUnit *unit, LLVMLocalScope *scope,
|
BackendError impl_expr(LLVMBackendCompileUnit *unit, LLVMLocalScope *scope,
|
||||||
LLVMBuilderRef builder, Expression *expr,
|
LLVMBuilderRef builder, Expression *expr,
|
||||||
LLVMValueRef *llvm_result) {
|
LLVMValueRef *llvm_result) {
|
||||||
|
@ -384,6 +403,10 @@ BackendError impl_expr(LLVMBackendCompileUnit *unit, LLVMLocalScope *scope,
|
||||||
err = impl_variable_load(unit, scope, builder, expr->impl.variable,
|
err = impl_variable_load(unit, scope, builder, expr->impl.variable,
|
||||||
llvm_result);
|
llvm_result);
|
||||||
break;
|
break;
|
||||||
|
case ExpressionKindAddressOf:
|
||||||
|
err = impl_address_of(unit, scope, builder, &expr->impl.addressOf,
|
||||||
|
llvm_result);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
err = new_backend_impl_error(Implementation, NULL, "unknown expression");
|
err = new_backend_impl_error(Implementation, NULL, "unknown expression");
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <llvm/llvm-ir/variables.h>
|
#include <llvm/llvm-ir/variables.h>
|
||||||
#include <set/types.h>
|
#include <set/types.h>
|
||||||
#include <sys/log.h>
|
#include <sys/log.h>
|
||||||
|
#include <mem/cache.h>
|
||||||
|
|
||||||
LLVMLocalScope* new_local_scope(LLVMLocalScope* parent) {
|
LLVMLocalScope* new_local_scope(LLVMLocalScope* parent) {
|
||||||
LLVMLocalScope* scope = malloc(sizeof(LLVMLocalScope));
|
LLVMLocalScope* scope = malloc(sizeof(LLVMLocalScope));
|
||||||
|
@ -90,18 +91,19 @@ BackendError impl_func_decl(LLVMBackendCompileUnit* unit,
|
||||||
DEBUG("implementing function declaration: %s()", name);
|
DEBUG("implementing function declaration: %s()", name);
|
||||||
BackendError err = SUCCESS;
|
BackendError err = SUCCESS;
|
||||||
|
|
||||||
Parameter* params = (Parameter*)fundef->parameter;
|
|
||||||
GArray* llvm_params = g_array_new(FALSE, FALSE, sizeof(LLVMTypeRef));
|
GArray* llvm_params = g_array_new(FALSE, FALSE, sizeof(LLVMTypeRef));
|
||||||
|
|
||||||
for (size_t i = 0; i < fundef->parameter->len; i++) {
|
for (size_t i = 0; i < fundef->parameter->len; i++) {
|
||||||
Parameter* param = ¶ms[i];
|
Parameter* param = &g_array_index(fundef->parameter, Parameter, i);
|
||||||
|
|
||||||
LLVMTypeRef llvm_type = NULL;
|
LLVMTypeRef llvm_type = NULL;
|
||||||
err = impl_param_type(unit, scope, param, &llvm_type);
|
err = impl_param_type(unit, scope, param, &llvm_type);
|
||||||
|
|
||||||
if (err.kind != Success) {
|
if (err.kind != Success) {
|
||||||
break;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g_array_append_val(llvm_params, llvm_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG("implemented %ld parameter", llvm_params->len);
|
DEBUG("implemented %ld parameter", llvm_params->len);
|
||||||
|
@ -144,11 +146,15 @@ BackendError impl_func_def(LLVMBackendCompileUnit* unit,
|
||||||
LLVMPositionBuilderAtEnd(builder, entry);
|
LLVMPositionBuilderAtEnd(builder, entry);
|
||||||
|
|
||||||
// create value references for parameter
|
// create value references for parameter
|
||||||
const size_t params = fundef->parameter->len;
|
for (guint i = 0; i < fundef->parameter->len; i++) {
|
||||||
for (size_t i = 0; i < params; i++) {
|
Parameter* param = &g_array_index(fundef->parameter, Parameter, i);
|
||||||
const Parameter* param = ((Parameter*)fundef->parameter) + i;
|
LLVMValueRef llvm_param = LLVMGetParam(llvm_func, i);
|
||||||
g_hash_table_insert(func_scope->params, (gpointer)param->name,
|
|
||||||
LLVMGetParam(llvm_func, i));
|
if (llvm_param == NULL) {
|
||||||
|
return new_backend_impl_error(Implementation, NULL, "invalid parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
g_hash_table_insert(func_scope->params, (gpointer)param->name, llvm_param);
|
||||||
}
|
}
|
||||||
|
|
||||||
LLVMBasicBlockRef llvm_start_body_block = NULL;
|
LLVMBasicBlockRef llvm_start_body_block = NULL;
|
||||||
|
|
|
@ -219,7 +219,7 @@ BackendError impl_branch(LLVMBackendCompileUnit *unit,
|
||||||
LLVMBasicBlockRef end_body_block = NULL;
|
LLVMBasicBlockRef end_body_block = NULL;
|
||||||
LLVMValueRef cond_value = NULL;
|
LLVMValueRef cond_value = NULL;
|
||||||
|
|
||||||
err = impl_cond_block(unit, builder, scope, (Expression *) &branch->ifBranch.conditon, &branch->ifBranch.block,
|
err = impl_cond_block(unit, builder, scope, branch->ifBranch.conditon, &branch->ifBranch.block,
|
||||||
&cond_block,
|
&cond_block,
|
||||||
&start_body_block, &end_body_block, &cond_value);
|
&start_body_block, &end_body_block, &cond_value);
|
||||||
|
|
||||||
|
@ -230,46 +230,52 @@ BackendError impl_branch(LLVMBackendCompileUnit *unit,
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate else if(s)
|
// generate else if(s)
|
||||||
for (size_t i = 0; i < branch->elseIfBranches->len; i++) {
|
if (branch->elseIfBranches != NULL) {
|
||||||
LLVMBasicBlockRef cond_block = NULL;
|
for (size_t i = 0; i < branch->elseIfBranches->len; i++) {
|
||||||
LLVMBasicBlockRef start_body_block = NULL;
|
LLVMBasicBlockRef cond_block = NULL;
|
||||||
LLVMBasicBlockRef end_body_block = NULL;
|
LLVMBasicBlockRef start_body_block = NULL;
|
||||||
LLVMValueRef cond_value = NULL;
|
LLVMBasicBlockRef end_body_block = NULL;
|
||||||
|
LLVMValueRef cond_value = NULL;
|
||||||
|
|
||||||
ElseIf *elseIf = ((ElseIf *) branch->elseIfBranches->data) + i;
|
ElseIf *elseIf = ((ElseIf *) branch->elseIfBranches->data) + i;
|
||||||
|
|
||||||
err = impl_cond_block(unit, builder, scope, elseIf->conditon, &elseIf->block, &cond_block,
|
err = impl_cond_block(unit, builder, scope, elseIf->conditon, &elseIf->block, &cond_block,
|
||||||
&start_body_block, &end_body_block, &cond_value);
|
&start_body_block, &end_body_block, &cond_value);
|
||||||
|
|
||||||
g_array_append_val(cond_blocks, cond_block);
|
g_array_append_val(cond_blocks, cond_block);
|
||||||
g_array_append_val(start_body_blocks, start_body_block);
|
g_array_append_val(start_body_blocks, start_body_block);
|
||||||
g_array_append_val(end_body_blocks, end_body_block);
|
g_array_append_val(end_body_blocks, end_body_block);
|
||||||
g_array_append_val(cond_values, cond_value);
|
g_array_append_val(cond_values, cond_value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LLVMBasicBlockRef after_block = NULL;
|
||||||
|
|
||||||
// else block
|
// else block
|
||||||
if (branch->elseBranch.nodePtr != NULL) {
|
if (branch->elseBranch.block.statemnts != NULL) {
|
||||||
LLVMBasicBlockRef start_else_block = NULL;
|
LLVMBasicBlockRef start_else_block = NULL;
|
||||||
LLVMBasicBlockRef end_else_block = NULL;
|
err = impl_basic_block(unit, builder, scope, &branch->elseBranch.block, &start_else_block, &after_block);
|
||||||
err = impl_basic_block(unit, builder, scope, &branch->elseBranch.block, &start_else_block, &end_else_block);
|
|
||||||
g_array_append_val(cond_blocks, start_else_block);
|
g_array_append_val(cond_blocks, start_else_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
LLVMBasicBlockRef after_block = LLVMAppendBasicBlockInContext(unit->context, scope->func_scope->llvm_func,
|
if (after_block == NULL) {
|
||||||
"stmt.branch.after");
|
after_block = LLVMAppendBasicBlockInContext(unit->context, scope->func_scope->llvm_func,
|
||||||
|
"stmt.branch.after");
|
||||||
|
}
|
||||||
|
|
||||||
LLVMPositionBuilderAtEnd(builder, after_block);
|
LLVMPositionBuilderAtEnd(builder, after_block);
|
||||||
// in case no else block is present
|
// in case no else block is present
|
||||||
// make the after block the else
|
// make the after block the else
|
||||||
if (branch->elseBranch.nodePtr == NULL) {
|
if (branch->elseBranch.block.statemnts == NULL) {
|
||||||
g_array_append_val(cond_blocks, after_block);
|
g_array_append_val(cond_blocks, after_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < cond_blocks->len - 1; i++) {
|
for (size_t i = 0; i < cond_blocks->len - 1; i++) {
|
||||||
LLVMBasicBlockRef next_block = ((LLVMBasicBlockRef *) cond_blocks->data)[i + 1];
|
LLVMBasicBlockRef next_block = g_array_index(cond_blocks, LLVMBasicBlockRef, i + 1);
|
||||||
LLVMBasicBlockRef cond_block = ((LLVMBasicBlockRef *) cond_blocks->data)[i];
|
LLVMBasicBlockRef cond_block = g_array_index(cond_blocks, LLVMBasicBlockRef, i);
|
||||||
LLVMBasicBlockRef start_body_block = ((LLVMBasicBlockRef *) start_body_blocks->data)[i];
|
LLVMBasicBlockRef start_body_block = g_array_index(start_body_blocks, LLVMBasicBlockRef, i);
|
||||||
LLVMBasicBlockRef end_body_block = ((LLVMBasicBlockRef *) end_body_blocks->data)[i];
|
LLVMBasicBlockRef end_body_block = g_array_index(end_body_blocks, LLVMBasicBlockRef, i);
|
||||||
LLVMValueRef cond_value = ((LLVMValueRef *) cond_values->data)[i];
|
LLVMValueRef cond_value = g_array_index(cond_values, LLVMValueRef, i);
|
||||||
|
|
||||||
LLVMPositionBuilderAtEnd(builder, cond_block);
|
LLVMPositionBuilderAtEnd(builder, cond_block);
|
||||||
LLVMBuildCondBr(builder, cond_value, start_body_block, next_block);
|
LLVMBuildCondBr(builder, cond_value, start_body_block, next_block);
|
||||||
|
@ -279,7 +285,7 @@ BackendError impl_branch(LLVMBackendCompileUnit *unit,
|
||||||
}
|
}
|
||||||
|
|
||||||
*branch_start_block = g_array_index(cond_blocks, LLVMBasicBlockRef, 0);
|
*branch_start_block = g_array_index(cond_blocks, LLVMBasicBlockRef, 0);
|
||||||
*branch_end_block = g_array_index(cond_blocks, LLVMBasicBlockRef, cond_blocks->len - 1);
|
*branch_end_block = after_block;
|
||||||
|
|
||||||
g_array_free(cond_blocks, TRUE);
|
g_array_free(cond_blocks, TRUE);
|
||||||
g_array_free(start_body_blocks, TRUE);
|
g_array_free(start_body_blocks, TRUE);
|
||||||
|
|
|
@ -179,9 +179,7 @@ BackendError impl_box_type(LLVMBackendCompileUnit* unit, LLVMGlobalScope* scope,
|
||||||
BackendError get_type_impl(LLVMBackendCompileUnit* unit, LLVMGlobalScope* scope,
|
BackendError get_type_impl(LLVMBackendCompileUnit* unit, LLVMGlobalScope* scope,
|
||||||
Type* gemstone_type, LLVMTypeRef* llvm_type) {
|
Type* gemstone_type, LLVMTypeRef* llvm_type) {
|
||||||
DEBUG("retrieving type implementation...");
|
DEBUG("retrieving type implementation...");
|
||||||
BackendError err =
|
BackendError err;
|
||||||
new_backend_impl_error(Implementation, gemstone_type->nodePtr,
|
|
||||||
"No type implementation covers type");
|
|
||||||
|
|
||||||
switch (gemstone_type->kind) {
|
switch (gemstone_type->kind) {
|
||||||
case TypeKindPrimitive:
|
case TypeKindPrimitive:
|
||||||
|
|
195
src/set/set.c
195
src/set/set.c
|
@ -231,6 +231,21 @@ int set_impl_composite_type(AST_NODE_PTR ast_type, CompositeType *composite) {
|
||||||
return check_scale_factor(ast_type, composite->scale);
|
return check_scale_factor(ast_type, composite->scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int set_get_type_impl(AST_NODE_PTR currentNode, Type **type);
|
||||||
|
|
||||||
|
int set_impl_reference_type(AST_NODE_PTR currentNode, Type **type) {
|
||||||
|
DEBUG("implementing reference type");
|
||||||
|
ReferenceType reference;
|
||||||
|
|
||||||
|
int status = set_get_type_impl(AST_get_node(currentNode, 0), &reference);
|
||||||
|
|
||||||
|
*type = mem_alloc(MemoryNamespaceSet, sizeof(Type));
|
||||||
|
(*type)->kind = TypeKindReference;
|
||||||
|
(*type)->impl.reference = reference;
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts the given AST node to a gemstone type implementation.
|
* @brief Converts the given AST node to a gemstone type implementation.
|
||||||
* @param currentNode AST node of type kind type
|
* @param currentNode AST node of type kind type
|
||||||
|
@ -239,21 +254,22 @@ int set_impl_composite_type(AST_NODE_PTR ast_type, CompositeType *composite) {
|
||||||
*/
|
*/
|
||||||
int set_get_type_impl(AST_NODE_PTR currentNode, Type **type) {
|
int set_get_type_impl(AST_NODE_PTR currentNode, Type **type) {
|
||||||
assert(currentNode != NULL);
|
assert(currentNode != NULL);
|
||||||
assert(currentNode->kind == AST_Type);
|
assert(currentNode->kind == AST_Type || currentNode->kind == AST_Reference);
|
||||||
assert(currentNode->child_count > 0);
|
assert(currentNode->child_count > 0);
|
||||||
DEBUG("start Type");
|
DEBUG("start Type");
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
|
if (currentNode->kind == AST_Reference) {
|
||||||
|
return set_impl_reference_type(currentNode, type);
|
||||||
|
}
|
||||||
|
|
||||||
const char *typekind = currentNode->children[currentNode->child_count - 1]->value;
|
const char *typekind = currentNode->children[currentNode->child_count - 1]->value;
|
||||||
|
|
||||||
//find type in composites
|
//find type in composites
|
||||||
if (g_hash_table_contains(declaredComposites, typekind) == TRUE) {
|
if (g_hash_table_contains(declaredComposites, typekind) == TRUE) {
|
||||||
*type = g_hash_table_lookup(declaredComposites, typekind);
|
*type = g_hash_table_lookup(declaredComposites, typekind);
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_hash_table_contains(declaredBoxes, typekind) == TRUE) {
|
if (g_hash_table_contains(declaredBoxes, typekind) == TRUE) {
|
||||||
|
@ -416,6 +432,7 @@ int createDef(AST_NODE_PTR currentNode, GArray **variables) {
|
||||||
DEBUG("fill Qualifier");
|
DEBUG("fill Qualifier");
|
||||||
decl.qualifier = Qualifier_from_string(declaration->children[i]->value);
|
decl.qualifier = Qualifier_from_string(declaration->children[i]->value);
|
||||||
break;
|
break;
|
||||||
|
case AST_Reference:
|
||||||
case AST_Type:
|
case AST_Type:
|
||||||
DEBUG("fill Type");
|
DEBUG("fill Type");
|
||||||
status = set_get_type_impl(declaration->children[i], &decl.type);
|
status = set_get_type_impl(declaration->children[i], &decl.type);
|
||||||
|
@ -469,13 +486,12 @@ int getVariableFromScope(const char *name, Variable **variable) {
|
||||||
}
|
}
|
||||||
for(size_t i = 0; i < Scope->len; i++) {
|
for(size_t i = 0; i < Scope->len; i++) {
|
||||||
|
|
||||||
|
|
||||||
GHashTable* variable_table = g_array_index(Scope,GHashTable* ,i );
|
GHashTable* variable_table = g_array_index(Scope,GHashTable* ,i );
|
||||||
|
|
||||||
if(g_hash_table_contains(variable_table, name)) {
|
if(g_hash_table_contains(variable_table, name)) {
|
||||||
if(found == 0){
|
if(found == 0){
|
||||||
*variable = g_hash_table_lookup(variable_table, name);
|
*variable = g_hash_table_lookup(variable_table, name);
|
||||||
}
|
}
|
||||||
found += 1;
|
found += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -483,10 +499,10 @@ int getVariableFromScope(const char *name, Variable **variable) {
|
||||||
DEBUG("Var: %s",(*variable)->name);
|
DEBUG("Var: %s",(*variable)->name);
|
||||||
DEBUG("Var Typekind: %d", (*variable)->kind);
|
DEBUG("Var Typekind: %d", (*variable)->kind);
|
||||||
DEBUG("Found var");
|
DEBUG("Found var");
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
}else if (found > 1) {
|
}else if (found > 1) {
|
||||||
WARN("Variable %s is a parameter and a declared variable. Returning parameter", name);
|
WARN("Variable %s is a parameter and a declared variable. Returning parameter", name);
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
}
|
}
|
||||||
DEBUG("nothing found");
|
DEBUG("nothing found");
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
|
@ -509,7 +525,7 @@ int fillTablesWithVars(GHashTable *variableTable, const GArray* variables) {
|
||||||
|
|
||||||
for(size_t i = 0; i < variables->len; i++) {
|
for(size_t i = 0; i < variables->len; i++) {
|
||||||
|
|
||||||
|
|
||||||
Variable* var = g_array_index(variables,Variable *,i);
|
Variable* var = g_array_index(variables,Variable *,i);
|
||||||
|
|
||||||
// this variable is discarded, only need status code
|
// this variable is discarded, only need status code
|
||||||
|
@ -517,9 +533,9 @@ int fillTablesWithVars(GHashTable *variableTable, const GArray* variables) {
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_hash_table_insert(variableTable, (gpointer) var->name, var);
|
g_hash_table_insert(variableTable, (gpointer) var->name, var);
|
||||||
}
|
}
|
||||||
|
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -545,7 +561,7 @@ TypeValue createTypeValue(AST_NODE_PTR currentNode){
|
||||||
PANIC("Node is not an expression but from kind: %i", currentNode->kind);
|
PANIC("Node is not an expression but from kind: %i", currentNode->kind);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
value.nodePtr = currentNode;
|
value.nodePtr = currentNode;
|
||||||
value.value = currentNode->value;
|
value.value = currentNode->value;
|
||||||
return value;
|
return value;
|
||||||
|
@ -559,7 +575,7 @@ TypeValue createString(AST_NODE_PTR currentNode) {
|
||||||
Type *type = CLONE(StringLiteralType);
|
Type *type = CLONE(StringLiteralType);
|
||||||
value.type = type;
|
value.type = type;
|
||||||
value.nodePtr = currentNode;
|
value.nodePtr = currentNode;
|
||||||
value.value = currentNode->value;
|
value.value = currentNode->value;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -569,7 +585,7 @@ Type* createTypeFromOperands(Type* LeftOperandType, Type* RightOperandType, AST_
|
||||||
result->nodePtr = currentNode;
|
result->nodePtr = currentNode;
|
||||||
DEBUG("LeftOperandType->kind: %i", LeftOperandType->kind);
|
DEBUG("LeftOperandType->kind: %i", LeftOperandType->kind);
|
||||||
DEBUG("RightOperandType->kind: %i", RightOperandType->kind);
|
DEBUG("RightOperandType->kind: %i", RightOperandType->kind);
|
||||||
|
|
||||||
if (LeftOperandType->kind == TypeKindComposite && RightOperandType->kind == TypeKindComposite) {
|
if (LeftOperandType->kind == TypeKindComposite && RightOperandType->kind == TypeKindComposite) {
|
||||||
result->kind = TypeKindComposite;
|
result->kind = TypeKindComposite;
|
||||||
CompositeType resultImpl;
|
CompositeType resultImpl;
|
||||||
|
@ -580,11 +596,11 @@ Type* createTypeFromOperands(Type* LeftOperandType, Type* RightOperandType, AST_
|
||||||
resultImpl.primitive = MAX(LeftOperandType->impl.composite.primitive , RightOperandType->impl.composite.primitive);
|
resultImpl.primitive = MAX(LeftOperandType->impl.composite.primitive , RightOperandType->impl.composite.primitive);
|
||||||
|
|
||||||
result->impl.composite = resultImpl;
|
result->impl.composite = resultImpl;
|
||||||
|
|
||||||
} else if (LeftOperandType->kind == TypeKindPrimitive && RightOperandType->kind == TypeKindPrimitive) {
|
} else if (LeftOperandType->kind == TypeKindPrimitive && RightOperandType->kind == TypeKindPrimitive) {
|
||||||
DEBUG("both operands are primitive");
|
DEBUG("both operands are primitive");
|
||||||
result->kind = TypeKindPrimitive;
|
result->kind = TypeKindPrimitive;
|
||||||
|
|
||||||
result->impl.primitive = MAX(LeftOperandType->impl.primitive , RightOperandType->impl.primitive);
|
result->impl.primitive = MAX(LeftOperandType->impl.primitive , RightOperandType->impl.primitive);
|
||||||
|
|
||||||
} else if (LeftOperandType->kind == TypeKindPrimitive && RightOperandType->kind == TypeKindComposite) {
|
} else if (LeftOperandType->kind == TypeKindPrimitive && RightOperandType->kind == TypeKindComposite) {
|
||||||
|
@ -632,7 +648,7 @@ int createTypeCastFromExpression(Expression * expression, Type * resultType, Exp
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int createArithOperation(Expression* ParentExpression, AST_NODE_PTR currentNode, [[maybe_unused]] size_t expectedChildCount) {
|
int createArithOperation(Expression* ParentExpression, AST_NODE_PTR currentNode, size_t expectedChildCount) {
|
||||||
DEBUG("create arithmetic operation");
|
DEBUG("create arithmetic operation");
|
||||||
ParentExpression->impl.operation.kind = Arithmetic;
|
ParentExpression->impl.operation.kind = Arithmetic;
|
||||||
ParentExpression->impl.operation.nodePtr = currentNode;
|
ParentExpression->impl.operation.nodePtr = currentNode;
|
||||||
|
@ -672,10 +688,10 @@ int createArithOperation(Expression* ParentExpression, AST_NODE_PTR currentNode,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ParentExpression->impl.operation.impl.arithmetic == Negate) {
|
if (ParentExpression->impl.operation.impl.arithmetic == Negate) {
|
||||||
|
|
||||||
Type* result = g_array_index(ParentExpression->impl.operation.operands,Expression *,0)->result;
|
Type* result = g_array_index(ParentExpression->impl.operation.operands,Expression *,0)->result;
|
||||||
result->nodePtr = currentNode;
|
result->nodePtr = currentNode;
|
||||||
|
|
||||||
if (result->kind == TypeKindReference || result->kind == TypeKindBox) {
|
if (result->kind == TypeKindReference || result->kind == TypeKindBox) {
|
||||||
print_diagnostic(current_file, ¤tNode->location, Error, "Invalid type for arithmetic operation");
|
print_diagnostic(current_file, ¤tNode->location, Error, "Invalid type for arithmetic operation");
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
|
@ -683,10 +699,10 @@ int createArithOperation(Expression* ParentExpression, AST_NODE_PTR currentNode,
|
||||||
result->impl.composite.sign = Signed;
|
result->impl.composite.sign = Signed;
|
||||||
}
|
}
|
||||||
ParentExpression->result = result;
|
ParentExpression->result = result;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Type* LeftOperandType = g_array_index(ParentExpression->impl.operation.operands,Expression *,0)->result;
|
Type* LeftOperandType = g_array_index(ParentExpression->impl.operation.operands,Expression *, 0)->result;
|
||||||
Type* RightOperandType = g_array_index(ParentExpression->impl.operation.operands,Expression *,1)->result;
|
Type* RightOperandType = g_array_index(ParentExpression->impl.operation.operands,Expression *, 1)->result;
|
||||||
|
|
||||||
ParentExpression->result = createTypeFromOperands(LeftOperandType, RightOperandType, currentNode);
|
ParentExpression->result = createTypeFromOperands(LeftOperandType, RightOperandType, currentNode);
|
||||||
}
|
}
|
||||||
|
@ -769,6 +785,7 @@ int createBoolOperation(Expression *ParentExpression, AST_NODE_PTR currentNode)
|
||||||
// fill kind and Nodeptr
|
// fill kind and Nodeptr
|
||||||
ParentExpression->impl.operation.kind = Boolean;
|
ParentExpression->impl.operation.kind = Boolean;
|
||||||
ParentExpression->impl.operation.nodePtr = currentNode;
|
ParentExpression->impl.operation.nodePtr = currentNode;
|
||||||
|
ParentExpression->impl.operation.operands = g_array_new(FALSE,FALSE,sizeof(Expression*));
|
||||||
|
|
||||||
// fill Operands
|
// fill Operands
|
||||||
for (size_t i = 0; i < currentNode->child_count; i++){
|
for (size_t i = 0; i < currentNode->child_count; i++){
|
||||||
|
@ -794,8 +811,8 @@ int createBoolOperation(Expression *ParentExpression, AST_NODE_PTR currentNode)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Expression* lhs = ((Expression**) ParentExpression->impl.operation.operands->data)[0];
|
Expression* lhs = g_array_index(ParentExpression->impl.operation.operands, Expression*, 0);
|
||||||
Expression* rhs = ((Expression**) ParentExpression->impl.operation.operands->data)[1];
|
Expression* rhs = g_array_index(ParentExpression->impl.operation.operands, Expression*, 1);
|
||||||
|
|
||||||
Type* LeftOperandType = lhs->result;
|
Type* LeftOperandType = lhs->result;
|
||||||
Type* RightOperandType = rhs->result;
|
Type* RightOperandType = rhs->result;
|
||||||
|
@ -852,6 +869,7 @@ int createBoolNotOperation(Expression *ParentExpression, AST_NODE_PTR currentNod
|
||||||
//fill kind and Nodeptr
|
//fill kind and Nodeptr
|
||||||
ParentExpression->impl.operation.kind = Boolean;
|
ParentExpression->impl.operation.kind = Boolean;
|
||||||
ParentExpression->impl.operation.nodePtr = currentNode;
|
ParentExpression->impl.operation.nodePtr = currentNode;
|
||||||
|
ParentExpression->impl.operation.operands = g_array_new(FALSE,FALSE,sizeof(Expression*));
|
||||||
|
|
||||||
//fill Operand
|
//fill Operand
|
||||||
Expression* expression = createExpression(currentNode->children[0]);
|
Expression* expression = createExpression(currentNode->children[0]);
|
||||||
|
@ -862,7 +880,7 @@ int createBoolNotOperation(Expression *ParentExpression, AST_NODE_PTR currentNod
|
||||||
|
|
||||||
ParentExpression->impl.operation.impl.boolean = BooleanNot;
|
ParentExpression->impl.operation.impl.boolean = BooleanNot;
|
||||||
|
|
||||||
Type* Operand = ((Expression**)ParentExpression->impl.operation.operands)[0]->result;
|
Type* Operand = g_array_index(ParentExpression->impl.operation.operands, Expression*, 0)->result;
|
||||||
|
|
||||||
Type* result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
|
Type* result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
|
||||||
result->nodePtr = currentNode;
|
result->nodePtr = currentNode;
|
||||||
|
@ -879,14 +897,14 @@ int createBoolNotOperation(Expression *ParentExpression, AST_NODE_PTR currentNod
|
||||||
}
|
}
|
||||||
result->kind = Operand->kind;
|
result->kind = Operand->kind;
|
||||||
result->impl = Operand->impl;
|
result->impl = Operand->impl;
|
||||||
|
|
||||||
} else if(Operand->kind == TypeKindComposite) {
|
} else if(Operand->kind == TypeKindComposite) {
|
||||||
if (Operand->impl.composite.primitive == Float) {
|
if (Operand->impl.composite.primitive == Float) {
|
||||||
print_diagnostic(current_file, &Operand->nodePtr->location, Error, "Operand must be a variant of primitive type int");
|
print_diagnostic(current_file, &Operand->nodePtr->location, Error, "Operand must be a variant of primitive type int");
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
result->kind = Operand->kind;
|
result->kind = Operand->kind;
|
||||||
result->impl = Operand->impl;
|
result->impl = Operand->impl;
|
||||||
}
|
}
|
||||||
|
|
||||||
ParentExpression->result = result;
|
ParentExpression->result = result;
|
||||||
|
@ -908,7 +926,7 @@ int createBoolNotOperation(Expression *ParentExpression, AST_NODE_PTR currentNod
|
||||||
bool isScaleEqual(double leftScale, double rightScale) {
|
bool isScaleEqual(double leftScale, double rightScale) {
|
||||||
int leftIntScale = (int) (leftScale * BASE_BYTES);
|
int leftIntScale = (int) (leftScale * BASE_BYTES);
|
||||||
int rightIntScale = (int) (rightScale * BASE_BYTES);
|
int rightIntScale = (int) (rightScale * BASE_BYTES);
|
||||||
|
|
||||||
return leftIntScale == rightIntScale;
|
return leftIntScale == rightIntScale;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -916,6 +934,7 @@ int createBitOperation(Expression* ParentExpression, AST_NODE_PTR currentNode) {
|
||||||
// fill kind and Nodeptr
|
// fill kind and Nodeptr
|
||||||
ParentExpression->impl.operation.kind = Boolean;
|
ParentExpression->impl.operation.kind = Boolean;
|
||||||
ParentExpression->impl.operation.nodePtr = currentNode;
|
ParentExpression->impl.operation.nodePtr = currentNode;
|
||||||
|
ParentExpression->impl.operation.operands = g_array_new(FALSE,FALSE,sizeof(Expression*));
|
||||||
|
|
||||||
// fill Operands
|
// fill Operands
|
||||||
for (size_t i = 0; i < currentNode->child_count; i++) {
|
for (size_t i = 0; i < currentNode->child_count; i++) {
|
||||||
|
@ -946,9 +965,9 @@ int createBitOperation(Expression* ParentExpression, AST_NODE_PTR currentNode) {
|
||||||
Type *result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
|
Type *result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
|
||||||
result->nodePtr = currentNode;
|
result->nodePtr = currentNode;
|
||||||
|
|
||||||
Expression* lhs = ((Expression**) ParentExpression->impl.operation.operands->data)[0];
|
Expression* lhs = g_array_index(ParentExpression->impl.operation.operands, Expression*, 0);
|
||||||
Expression* rhs = ((Expression**) ParentExpression->impl.operation.operands->data)[1];
|
Expression* rhs = g_array_index(ParentExpression->impl.operation.operands, Expression*, 1);
|
||||||
|
|
||||||
Type* LeftOperandType = lhs->result;
|
Type* LeftOperandType = lhs->result;
|
||||||
Type* RightOperandType = rhs->result;
|
Type* RightOperandType = rhs->result;
|
||||||
|
|
||||||
|
@ -1052,6 +1071,7 @@ int createBitNotOperation(Expression* ParentExpression, AST_NODE_PTR currentNode
|
||||||
//fill kind and Nodeptr
|
//fill kind and Nodeptr
|
||||||
ParentExpression->impl.operation.kind = Bitwise;
|
ParentExpression->impl.operation.kind = Bitwise;
|
||||||
ParentExpression->impl.operation.nodePtr = currentNode;
|
ParentExpression->impl.operation.nodePtr = currentNode;
|
||||||
|
ParentExpression->impl.operation.operands = g_array_new(FALSE, FALSE,sizeof(Expression*));
|
||||||
|
|
||||||
//fill Operand
|
//fill Operand
|
||||||
Expression* expression = createExpression(currentNode->children[0]);
|
Expression* expression = createExpression(currentNode->children[0]);
|
||||||
|
@ -1062,11 +1082,11 @@ int createBitNotOperation(Expression* ParentExpression, AST_NODE_PTR currentNode
|
||||||
|
|
||||||
ParentExpression->impl.operation.impl.bitwise = BitwiseNot;
|
ParentExpression->impl.operation.impl.bitwise = BitwiseNot;
|
||||||
|
|
||||||
Type* Operand = ((Expression**) ParentExpression->impl.operation.operands)[0]->result;
|
Type* Operand = g_array_index(ParentExpression->impl.operation.operands, Expression*, 0)->result;
|
||||||
|
|
||||||
Type* result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
|
Type* result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
|
||||||
result->nodePtr = currentNode;
|
result->nodePtr = currentNode;
|
||||||
|
|
||||||
if (Operand->kind == TypeKindPrimitive) {
|
if (Operand->kind == TypeKindPrimitive) {
|
||||||
|
|
||||||
if (Operand->impl.primitive == Float) {
|
if (Operand->impl.primitive == Float) {
|
||||||
|
@ -1088,8 +1108,8 @@ int createBitNotOperation(Expression* ParentExpression, AST_NODE_PTR currentNode
|
||||||
result->impl.composite.primitive = Int;
|
result->impl.composite.primitive = Int;
|
||||||
result->impl.composite.sign = Operand->impl.composite.sign;
|
result->impl.composite.sign = Operand->impl.composite.sign;
|
||||||
result->impl.composite.scale = Operand->impl.composite.scale;
|
result->impl.composite.scale = Operand->impl.composite.scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
ParentExpression->result = result;
|
ParentExpression->result = result;
|
||||||
|
|
||||||
for (size_t i = 0 ; i < ParentExpression->impl.operation.operands->len; i++) {
|
for (size_t i = 0 ; i < ParentExpression->impl.operation.operands->len; i++) {
|
||||||
|
@ -1165,7 +1185,7 @@ int createBoxAccess(Expression* ParentExpression,AST_NODE_PTR currentNode) {
|
||||||
Type* boxType;
|
Type* boxType;
|
||||||
|
|
||||||
if(boxVariable->kind == VariableKindDeclaration){
|
if(boxVariable->kind == VariableKindDeclaration){
|
||||||
|
|
||||||
boxType = boxVariable->impl.declaration.type;
|
boxType = boxVariable->impl.declaration.type;
|
||||||
} else if (boxVariable->kind == VariableKindDefinition){
|
} else if (boxVariable->kind == VariableKindDefinition){
|
||||||
boxType = boxVariable->impl.definiton.declaration.type;
|
boxType = boxVariable->impl.definiton.declaration.type;
|
||||||
|
@ -1175,7 +1195,7 @@ int createBoxAccess(Expression* ParentExpression,AST_NODE_PTR currentNode) {
|
||||||
if (boxType->kind != TypeKindBox) {
|
if (boxType->kind != TypeKindBox) {
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// filling boxAccess variable
|
// filling boxAccess variable
|
||||||
ParentExpression->impl.variable->kind = VariableKindBoxMember;
|
ParentExpression->impl.variable->kind = VariableKindBoxMember;
|
||||||
ParentExpression->impl.variable->nodePtr = currentNode;
|
ParentExpression->impl.variable->nodePtr = currentNode;
|
||||||
|
@ -1209,7 +1229,7 @@ int createBoxAccess(Expression* ParentExpression,AST_NODE_PTR currentNode) {
|
||||||
int createTypeCast(Expression* ParentExpression, AST_NODE_PTR currentNode){
|
int createTypeCast(Expression* ParentExpression, AST_NODE_PTR currentNode){
|
||||||
DEBUG("create type cast");
|
DEBUG("create type cast");
|
||||||
ParentExpression->impl.typecast.nodePtr = currentNode;
|
ParentExpression->impl.typecast.nodePtr = currentNode;
|
||||||
|
|
||||||
ParentExpression->impl.typecast.operand = createExpression(currentNode->children[0]);
|
ParentExpression->impl.typecast.operand = createExpression(currentNode->children[0]);
|
||||||
if (ParentExpression->impl.typecast.operand == NULL){
|
if (ParentExpression->impl.typecast.operand == NULL){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
|
@ -1235,7 +1255,7 @@ int createTypeCast(Expression* ParentExpression, AST_NODE_PTR currentNode){
|
||||||
int createTransmute(Expression* ParentExpression, AST_NODE_PTR currentNode){
|
int createTransmute(Expression* ParentExpression, AST_NODE_PTR currentNode){
|
||||||
ParentExpression->impl.transmute.nodePtr = currentNode;
|
ParentExpression->impl.transmute.nodePtr = currentNode;
|
||||||
ParentExpression->impl.transmute.operand = createExpression(currentNode->children[0]);
|
ParentExpression->impl.transmute.operand = createExpression(currentNode->children[0]);
|
||||||
|
|
||||||
if (ParentExpression->impl.transmute.operand == NULL){
|
if (ParentExpression->impl.transmute.operand == NULL){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
@ -1249,7 +1269,7 @@ int createTransmute(Expression* ParentExpression, AST_NODE_PTR currentNode){
|
||||||
|
|
||||||
ParentExpression->impl.typecast.targetType = target;
|
ParentExpression->impl.typecast.targetType = target;
|
||||||
ParentExpression->result = target;
|
ParentExpression->result = target;
|
||||||
|
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1458,7 +1478,7 @@ Expression *createExpression(AST_NODE_PTR currentNode){
|
||||||
DEBUG("successfully created Expression");
|
DEBUG("successfully created Expression");
|
||||||
return expression;
|
return expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool compareTypes(Type * leftType, Type * rightType) {
|
bool compareTypes(Type * leftType, Type * rightType) {
|
||||||
if (leftType->kind != rightType->kind) {
|
if (leftType->kind != rightType->kind) {
|
||||||
|
@ -1553,7 +1573,7 @@ int fillBlock(Block * block,AST_NODE_PTR currentNode){
|
||||||
|
|
||||||
g_hash_table_destroy(lowerScope);
|
g_hash_table_destroy(lowerScope);
|
||||||
g_array_remove_index(Scope, Scope->len-1);
|
g_array_remove_index(Scope, Scope->len-1);
|
||||||
|
|
||||||
DEBUG("created Block successfully");
|
DEBUG("created Block successfully");
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
}
|
}
|
||||||
|
@ -1585,14 +1605,14 @@ int createWhile(Statement * ParentStatement, AST_NODE_PTR currentNode){
|
||||||
int createIf(Branch* Parentbranch, AST_NODE_PTR currentNode){
|
int createIf(Branch* Parentbranch, AST_NODE_PTR currentNode){
|
||||||
If ifbranch;
|
If ifbranch;
|
||||||
ifbranch.nodePtr = currentNode;
|
ifbranch.nodePtr = currentNode;
|
||||||
|
|
||||||
Expression* expression = createExpression(currentNode->children[0]);
|
Expression* expression = createExpression(currentNode->children[0]);
|
||||||
if (NULL == expression) {
|
if (NULL == expression) {
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
ifbranch.conditon = expression;
|
ifbranch.conditon = expression;
|
||||||
int status = fillBlock(&ifbranch.block, currentNode->children[1]);
|
int status = fillBlock(&ifbranch.block, currentNode->children[1]);
|
||||||
|
|
||||||
if(status){
|
if(status){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
@ -1603,9 +1623,9 @@ int createIf(Branch* Parentbranch, AST_NODE_PTR currentNode){
|
||||||
int createElse(Branch* Parentbranch, AST_NODE_PTR currentNode){
|
int createElse(Branch* Parentbranch, AST_NODE_PTR currentNode){
|
||||||
Else elseBranch;
|
Else elseBranch;
|
||||||
elseBranch.nodePtr = currentNode;
|
elseBranch.nodePtr = currentNode;
|
||||||
|
|
||||||
int status = fillBlock(&elseBranch.block, currentNode->children[0]);
|
int status = fillBlock(&elseBranch.block, currentNode->children[0]);
|
||||||
|
|
||||||
if(status){
|
if(status){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
@ -1616,14 +1636,18 @@ int createElse(Branch* Parentbranch, AST_NODE_PTR currentNode){
|
||||||
int createElseIf(Branch* Parentbranch, AST_NODE_PTR currentNode){
|
int createElseIf(Branch* Parentbranch, AST_NODE_PTR currentNode){
|
||||||
ElseIf elseIfBranch;
|
ElseIf elseIfBranch;
|
||||||
elseIfBranch.nodePtr = currentNode;
|
elseIfBranch.nodePtr = currentNode;
|
||||||
|
|
||||||
|
if (Parentbranch->elseIfBranches == NULL) {
|
||||||
|
Parentbranch->elseIfBranches = mem_new_g_array(MemoryNamespaceSet, sizeof(ElseIf));
|
||||||
|
}
|
||||||
|
|
||||||
Expression* expression = createExpression(currentNode->children[0]);
|
Expression* expression = createExpression(currentNode->children[0]);
|
||||||
if (NULL == expression) {
|
if (NULL == expression) {
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
elseIfBranch.conditon = expression;
|
elseIfBranch.conditon = expression;
|
||||||
int status = fillBlock(&elseIfBranch.block, currentNode->children[1]);
|
int status = fillBlock(&elseIfBranch.block, currentNode->children[1]);
|
||||||
|
|
||||||
if(status){
|
if(status){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
@ -1631,27 +1655,28 @@ int createElseIf(Branch* Parentbranch, AST_NODE_PTR currentNode){
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int createBranch(Statement* ParentStatement,AST_NODE_PTR currentNode){
|
int createBranch(Statement* ParentStatement,AST_NODE_PTR currentNode){
|
||||||
Branch Branch;
|
Branch branch;
|
||||||
Branch.nodePtr = currentNode;
|
branch.nodePtr = currentNode;
|
||||||
|
branch.elseBranch.block.statemnts = NULL;
|
||||||
|
branch.elseIfBranches = NULL;
|
||||||
|
|
||||||
for (size_t i = 0; i < currentNode->child_count; i++ ){
|
for (size_t i = 0; i < currentNode->child_count; i++ ){
|
||||||
switch (currentNode->children[i]->kind){
|
switch (currentNode->children[i]->kind){
|
||||||
case AST_If:
|
case AST_If:
|
||||||
if(createIf(&Branch, currentNode->children[i])){
|
if(createIf(&branch, currentNode->children[i])){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AST_IfElse:
|
case AST_IfElse:
|
||||||
if(createElseIf(&Branch, currentNode)){
|
if(createElseIf(&branch, currentNode->children[i])){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AST_Else:
|
case AST_Else:
|
||||||
if(createElse(&Branch, currentNode->children[i])){
|
if(createElse(&branch, currentNode->children[i])){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -1661,7 +1686,7 @@ int createBranch(Statement* ParentStatement,AST_NODE_PTR currentNode){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ParentStatement->impl.branch = Branch;
|
ParentStatement->impl.branch = branch;
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1743,11 +1768,11 @@ int createfuncall(Statement *parentStatement, AST_NODE_PTR currentNode) {
|
||||||
|
|
||||||
int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
|
int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
|
||||||
DEBUG("create Statement");
|
DEBUG("create Statement");
|
||||||
|
|
||||||
switch(currentNode->kind){
|
switch(currentNode->kind){
|
||||||
case AST_Decl:{
|
case AST_Decl:{
|
||||||
GArray *variable= g_array_new(FALSE, FALSE, sizeof(Variable*));
|
GArray *variable= g_array_new(FALSE, FALSE, sizeof(Variable*));
|
||||||
|
|
||||||
int status = createDecl(currentNode, &variable);
|
int status = createDecl(currentNode, &variable);
|
||||||
if(status){
|
if(status){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
|
@ -1758,7 +1783,7 @@ int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
|
||||||
statement->nodePtr = currentNode;
|
statement->nodePtr = currentNode;
|
||||||
statement->kind = StatementKindDeclaration;
|
statement->kind = StatementKindDeclaration;
|
||||||
|
|
||||||
|
|
||||||
statement->impl.variable = g_array_index(variable,Variable *,i);
|
statement->impl.variable = g_array_index(variable,Variable *,i);
|
||||||
g_array_append_val(Parentblock->statemnts,statement);
|
g_array_append_val(Parentblock->statemnts,statement);
|
||||||
}
|
}
|
||||||
|
@ -1767,7 +1792,7 @@ int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
|
||||||
|
|
||||||
case AST_Def:{
|
case AST_Def:{
|
||||||
GArray *variable= g_array_new(FALSE, FALSE, sizeof(Variable*));
|
GArray *variable= g_array_new(FALSE, FALSE, sizeof(Variable*));
|
||||||
|
|
||||||
int status = createDef(currentNode, &variable);
|
int status = createDef(currentNode, &variable);
|
||||||
|
|
||||||
if(status){
|
if(status){
|
||||||
|
@ -1778,11 +1803,11 @@ int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
|
||||||
Statement * statement = mem_alloc(MemoryNamespaceSet,sizeof(Statement));
|
Statement * statement = mem_alloc(MemoryNamespaceSet,sizeof(Statement));
|
||||||
statement->nodePtr = currentNode;
|
statement->nodePtr = currentNode;
|
||||||
statement->kind = StatementKindDefinition;
|
statement->kind = StatementKindDefinition;
|
||||||
|
|
||||||
statement->impl.variable = g_array_index(variable,Variable *,i);
|
statement->impl.variable = g_array_index(variable,Variable *,i);
|
||||||
g_array_append_val(Parentblock->statemnts,statement);
|
g_array_append_val(Parentblock->statemnts,statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case AST_While:{
|
case AST_While:{
|
||||||
|
@ -1828,7 +1853,7 @@ int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
|
||||||
PANIC("Node is not a statement");
|
PANIC("Node is not a statement");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1858,7 +1883,7 @@ int createParam(GArray * Paramlist ,AST_NODE_PTR currentNode){
|
||||||
}else{
|
}else{
|
||||||
PANIC("IO_Qualifier has not the right amount of children");
|
PANIC("IO_Qualifier has not the right amount of children");
|
||||||
}
|
}
|
||||||
|
|
||||||
int signal = set_get_type_impl(paramdecl->children[0], &(decl.type));
|
int signal = set_get_type_impl(paramdecl->children[0], &(decl.type));
|
||||||
if(signal){
|
if(signal){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
|
@ -1868,7 +1893,7 @@ int createParam(GArray * Paramlist ,AST_NODE_PTR currentNode){
|
||||||
param.kind = ParameterDeclarationKind;
|
param.kind = ParameterDeclarationKind;
|
||||||
param.impl.declaration = decl;
|
param.impl.declaration = decl;
|
||||||
param.name = paramdecl->children[1]->value;
|
param.name = paramdecl->children[1]->value;
|
||||||
|
|
||||||
DEBUG("param name: %s", param.name);
|
DEBUG("param name: %s", param.name);
|
||||||
g_array_append_val(Paramlist, param);
|
g_array_append_val(Paramlist, param);
|
||||||
|
|
||||||
|
@ -1919,18 +1944,18 @@ int createFunDef(Function * Parentfunction ,AST_NODE_PTR currentNode){
|
||||||
DEBUG("param child count: %i", AST_get_node(paramlist, j)->child_count);
|
DEBUG("param child count: %i", AST_get_node(paramlist, j)->child_count);
|
||||||
int signal = createParam(fundef.parameter ,AST_get_node(paramlist, j));
|
int signal = createParam(fundef.parameter ,AST_get_node(paramlist, j));
|
||||||
//all params per list
|
//all params per list
|
||||||
|
|
||||||
if (signal){
|
if (signal){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DEBUG("End of Paramlist");
|
DEBUG("End of Paramlist");
|
||||||
}
|
}
|
||||||
int signal = fillBlock(fundef.body, statementlist);
|
int signal = fillBlock(fundef.body, statementlist);
|
||||||
if(signal){
|
if(signal){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
Parentfunction->nodePtr = currentNode;
|
Parentfunction->nodePtr = currentNode;
|
||||||
Parentfunction->kind = FunctionDefinitionKind;
|
Parentfunction->kind = FunctionDefinitionKind;
|
||||||
Parentfunction->impl.definition = fundef;
|
Parentfunction->impl.definition = fundef;
|
||||||
|
@ -2018,7 +2043,7 @@ int createFunDecl(Function * Parentfunction ,AST_NODE_PTR currentNode){
|
||||||
fundecl.name = nameNode->value;
|
fundecl.name = nameNode->value;
|
||||||
fundecl.parameter = mem_alloc(MemoryNamespaceSet,sizeof(GArray));
|
fundecl.parameter = mem_alloc(MemoryNamespaceSet,sizeof(GArray));
|
||||||
|
|
||||||
|
|
||||||
for(size_t i = 0; i < paramlistlist->child_count; i++){
|
for(size_t i = 0; i < paramlistlist->child_count; i++){
|
||||||
|
|
||||||
//all parameterlists
|
//all parameterlists
|
||||||
|
@ -2032,7 +2057,7 @@ int createFunDecl(Function * Parentfunction ,AST_NODE_PTR currentNode){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Parentfunction->nodePtr = currentNode;
|
Parentfunction->nodePtr = currentNode;
|
||||||
Parentfunction->kind = FunctionDefinitionKind;
|
Parentfunction->kind = FunctionDefinitionKind;
|
||||||
|
@ -2046,7 +2071,7 @@ int createFunction(Function ** function, AST_NODE_PTR currentNode){
|
||||||
assert(currentNode->kind == AST_Fun);
|
assert(currentNode->kind == AST_Fun);
|
||||||
Function * fun = mem_alloc(MemoryNamespaceSet,sizeof(Function));
|
Function * fun = mem_alloc(MemoryNamespaceSet,sizeof(Function));
|
||||||
functionParameter = g_hash_table_new(g_str_hash,g_str_equal);
|
functionParameter = g_hash_table_new(g_str_hash,g_str_equal);
|
||||||
|
|
||||||
if(currentNode->child_count == 2){
|
if(currentNode->child_count == 2){
|
||||||
int signal = createFunDecl(fun, currentNode);
|
int signal = createFunDecl(fun, currentNode);
|
||||||
if (signal){
|
if (signal){
|
||||||
|
@ -2072,7 +2097,7 @@ int createFunction(Function ** function, AST_NODE_PTR currentNode){
|
||||||
|
|
||||||
*function = fun;
|
*function = fun;
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -2110,12 +2135,12 @@ int createDefMember(BoxType *ParentBox, AST_NODE_PTR currentNode){
|
||||||
if(status){
|
if(status){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
Expression * init = createExpression(expressionNode);;
|
Expression * init = createExpression(expressionNode);;
|
||||||
if (init == NULL){
|
if (init == NULL){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < nameList->child_count; i++){
|
for (size_t i = 0; i < nameList->child_count; i++){
|
||||||
BoxMember *def = mem_alloc(MemoryNamespaceSet,sizeof(BoxMember));
|
BoxMember *def = mem_alloc(MemoryNamespaceSet,sizeof(BoxMember));
|
||||||
def->box = ParentBox;
|
def->box = ParentBox;
|
||||||
|
@ -2161,7 +2186,7 @@ int createBoxFunction(const char* boxName, Type *ParentBoxType, AST_NODE_PTR cur
|
||||||
|
|
||||||
int createBox(GHashTable *boxes, AST_NODE_PTR currentNode){
|
int createBox(GHashTable *boxes, AST_NODE_PTR currentNode){
|
||||||
BoxType * box = mem_alloc(MemoryNamespaceSet,sizeof(BoxType));
|
BoxType * box = mem_alloc(MemoryNamespaceSet,sizeof(BoxType));
|
||||||
|
|
||||||
box->nodePtr = currentNode;
|
box->nodePtr = currentNode;
|
||||||
const char * boxName = currentNode->children[0]->value;
|
const char * boxName = currentNode->children[0]->value;
|
||||||
AST_NODE_PTR boxMemberList = currentNode->children[1];
|
AST_NODE_PTR boxMemberList = currentNode->children[1];
|
||||||
|
@ -2192,7 +2217,7 @@ int createBox(GHashTable *boxes, AST_NODE_PTR currentNode){
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if(g_hash_table_contains(boxes, (gpointer)boxName)){
|
if(g_hash_table_contains(boxes, (gpointer)boxName)){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
|
@ -2202,26 +2227,26 @@ int createBox(GHashTable *boxes, AST_NODE_PTR currentNode){
|
||||||
|
|
||||||
|
|
||||||
return SEMANTIC_OK;
|
return SEMANTIC_OK;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int createTypeDef(GHashTable *types, AST_NODE_PTR currentNode){
|
int createTypeDef(GHashTable *types, AST_NODE_PTR currentNode){
|
||||||
DEBUG("create Type define");
|
DEBUG("create Type define");
|
||||||
AST_NODE_PTR typeNode = currentNode->children[0];
|
AST_NODE_PTR typeNode = currentNode->children[0];
|
||||||
AST_NODE_PTR nameNode = currentNode->children[1];
|
AST_NODE_PTR nameNode = currentNode->children[1];
|
||||||
|
|
||||||
|
|
||||||
Type * type = mem_alloc(MemoryNamespaceSet,sizeof(Type));
|
Type * type = mem_alloc(MemoryNamespaceSet,sizeof(Type));
|
||||||
int status = set_get_type_impl(typeNode, &type);
|
int status = set_get_type_impl(typeNode, &type);
|
||||||
if(status){
|
if(status){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
Typedefine *def = mem_alloc(MemoryNamespaceSet,sizeof(Typedefine));
|
Typedefine *def = mem_alloc(MemoryNamespaceSet,sizeof(Typedefine));
|
||||||
def->name = nameNode->value;
|
def->name = nameNode->value;
|
||||||
def->nodePtr = currentNode;
|
def->nodePtr = currentNode;
|
||||||
def->type = type;
|
def->type = type;
|
||||||
|
|
||||||
if(g_hash_table_contains(types, (gpointer)def->name)){
|
if(g_hash_table_contains(types, (gpointer)def->name)){
|
||||||
return SEMANTIC_ERROR;
|
return SEMANTIC_ERROR;
|
||||||
}
|
}
|
||||||
|
@ -2258,7 +2283,7 @@ Module *create_set(AST_NODE_PTR currentNode){
|
||||||
GHashTable *functions = g_hash_table_new(g_str_hash,g_str_equal);
|
GHashTable *functions = g_hash_table_new(g_str_hash,g_str_equal);
|
||||||
GHashTable *variables = g_hash_table_new(g_str_hash,g_str_equal);
|
GHashTable *variables = g_hash_table_new(g_str_hash,g_str_equal);
|
||||||
GArray *imports = g_array_new(FALSE, FALSE, sizeof(const char*));
|
GArray *imports = g_array_new(FALSE, FALSE, sizeof(const char*));
|
||||||
|
|
||||||
rootModule->boxes = boxes;
|
rootModule->boxes = boxes;
|
||||||
rootModule->types = types;
|
rootModule->types = types;
|
||||||
rootModule->functions = functions;
|
rootModule->functions = functions;
|
||||||
|
|
Loading…
Reference in New Issue