Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Nov 25, 2023
1 parent d89eafa commit bf99040
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/ptlang_ast/ptlang_ast_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ struct ptlang_ast_exp_s
PTLANG_AST_EXP_ARRAY_ELEMENT,
PTLANG_AST_EXP_REFERENCE,
PTLANG_AST_EXP_DEREFERENCE,
PTLANG_AST_EXP_BINARY,
} type;
union
{
Expand All @@ -228,6 +229,7 @@ struct ptlang_ast_exp_s
struct ptlang_ast_exp_struct_member_s struct_member;
struct ptlang_ast_exp_array_element_s array_element;
struct ptlang_ast_exp_reference_s reference;
uint8_t *binary;
} content;
ptlang_ast_code_position pos;
ptlang_ast_type ast_type; // use only after verify
Expand Down
99 changes: 96 additions & 3 deletions src/ptlang_verify/ptlang_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ static void ptlang_verify_decl(ptlang_ast_decl decl, size_t scope_offset, ptlang
}
}


// Algo to init global vars:
// * Create nodes: one node for each global var and each (recursive) element / member of a global var
// 1 Parse references to create edges
Expand All @@ -219,8 +218,6 @@ static void ptlang_verify_decl(ptlang_ast_decl decl, size_t scope_offset, ptlang
// * Go to 2
// * If any array index was substituted, got to 1



// ----

// u64 2_HOCH_11 = ${ 2<<11 };
Expand Down Expand Up @@ -1742,3 +1739,99 @@ static void ptlang_verify_exp_check_const(ptlang_ast_exp exp, ptlang_context *ct
}
}
}

static ptlang_ast_exp ptlang_verify_eval(ptlang_ast_exp exp, ptlang_context *ctx)
{
ptlang_ast_exp substituted = ptlang_malloc(sizeof(struct ptlang_ast_exp_s));;
switch (exp->type)
{
case PTLANG_AST_EXP_ADDITION:
case PTLANG_AST_EXP_SUBTRACTION:
case PTLANG_AST_EXP_MULTIPLICATION:
case PTLANG_AST_EXP_DIVISION:
case PTLANG_AST_EXP_MODULO:
case PTLANG_AST_EXP_REMAINDER:
case PTLANG_AST_EXP_EQUAL:
case PTLANG_AST_EXP_NOT_EQUAL:
case PTLANG_AST_EXP_GREATER:
case PTLANG_AST_EXP_GREATER_EQUAL:
case PTLANG_AST_EXP_LESS:
case PTLANG_AST_EXP_LESS_EQUAL:
case PTLANG_AST_EXP_LEFT_SHIFT:
case PTLANG_AST_EXP_RIGHT_SHIFT:
case PTLANG_AST_EXP_AND:
case PTLANG_AST_EXP_OR:
case PTLANG_AST_EXP_BITWISE_AND:
case PTLANG_AST_EXP_BITWISE_OR:
case PTLANG_AST_EXP_BITWISE_XOR:
{
ptlang_ast_exp left_value = ptlang_verify_eval(exp->content.binary_operator.left_value, ctx);
ptlang_ast_exp right_value = ptlang_verify_eval(exp->content.binary_operator.right_value, ctx);
*substituted = (struct ptlang_ast_exp_s){
.type = exp->type,
.content.binary_operator =
{
.left_value = left_value,
.right_value = right_value,
},
.pos = exp->pos,
};
}
case PTLANG_AST_EXP_NEGATION:
case PTLANG_AST_EXP_NOT:
case PTLANG_AST_EXP_BITWISE_INVERSE:
case PTLANG_AST_EXP_LENGTH:
case PTLANG_AST_EXP_DEREFERENCE:
{
ptlang_ast_exp operand = ptlang_verify_eval(exp->content.unary_operator, ctx);
*substituted = (struct ptlang_ast_exp_s){
.type = exp->type,
.content.unary_operator = operand,
};
}
// TODO
case PTLANG_AST_EXP_VARIABLE:
{
// assumes that global variable was already initialized
substituted = ptlang_verify_eval(ptlang_decl_list_find_last(ctx->scope, exp->content.str_prepresentation)->init, ctx);
break;
}
case PTLANG_AST_EXP_INTEGER:
case PTLANG_AST_EXP_FLOAT:
{
break;
}
case PTLANG_AST_EXP_STRUCT:
{
break;
}
case PTLANG_AST_EXP_ARRAY:
{
break;
}
case PTLANG_AST_EXP_TERNARY:
{
break;
}
case PTLANG_AST_EXP_CAST:
{
break;
}
case PTLANG_AST_EXP_STRUCT_MEMBER:
{
break;
}
case PTLANG_AST_EXP_ARRAY_ELEMENT:
{
break;
}
case PTLANG_AST_EXP_REFERENCE:
{
break;
}
}

ptlang_ast_exp evaluated = ptlang_eval_const_exp(substituted);
ptlang_free(substituted);
return evaluated;
}
2 changes: 2 additions & 0 deletions src/ptlang_verify/ptlang_verify_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,6 @@ static ptlang_ast_type ptlang_verify_unify_types(ptlang_ast_type type1, ptlang_a

static void ptlang_verify_exp_check_const(ptlang_ast_exp exp, ptlang_context *ctx, ptlang_error **errors);

static ptlang_ast_exp ptlang_verify_eval(ptlang_ast_exp exp, ptlang_context *ctx);

#endif

0 comments on commit bf99040

Please sign in to comment.