Ast modified, Ast_Expr is not a union instead it uses inheritence
This commit is contained in:
@@ -112,9 +112,9 @@ eval_typespec(Ast_Typespec *ast){
|
||||
Ast_Begin(AST_TYPESPEC_LAMBDA, Ast_Typespec){
|
||||
Scratch scratch;
|
||||
Ast_Resolved_Type *ret = eval_typespec(node->lambda->ret);
|
||||
Array<Ast_Resolved_Type *> params = {scratch};
|
||||
For(node->lambda->params) params.add(eval_typespec(it[0]->lambda_param.typespec));
|
||||
node->resolved_type = type_lambda(ret, params);
|
||||
Array<Ast_Resolved_Type *> args = {scratch};
|
||||
For(node->lambda->args) args.add(eval_typespec(it[0]->typespec));
|
||||
node->resolved_type = type_lambda(ret, args);
|
||||
return node->resolved_type;
|
||||
Ast_End();
|
||||
}
|
||||
@@ -161,19 +161,19 @@ resolve_type_pair(Token *pos, Ast_Resolved_Type *a, Ast_Resolved_Type *b){
|
||||
function Operand
|
||||
eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type){
|
||||
switch(ast->kind){
|
||||
Ast_Begin(AST_INT, Ast_Expr){
|
||||
Ast_Begin(AST_INT, Ast_Atom){
|
||||
Operand result = {type_int, true, {.int_val=(S64)node->int_val}};
|
||||
return result;
|
||||
Ast_End();
|
||||
}
|
||||
|
||||
Ast_Begin(AST_STR, Ast_Expr){
|
||||
Ast_Begin(AST_STR, Ast_Atom){
|
||||
Operand result = {type_string, true, {.intern_val = node->intern_val}};
|
||||
return result;
|
||||
Ast_End();
|
||||
}
|
||||
|
||||
Ast_Begin(AST_IDENT, Ast_Expr){
|
||||
Ast_Begin(AST_IDENT, Ast_Atom){
|
||||
Sym *sym = sym_get(node->intern_val);
|
||||
if(!sym){
|
||||
parsing_error(node->pos, "Identifier is undefined");
|
||||
@@ -193,12 +193,12 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type){
|
||||
else if(expected_type->kind == TYPE_Pointer){
|
||||
result.int_val = 0;
|
||||
node->kind = AST_IDENT;
|
||||
node->intern_val = pctx->intern("null_pointer"_s);
|
||||
node->intern_val = pctx->intern("NULL_POINTER"_s);
|
||||
}
|
||||
else if(expected_type->kind == TYPE_Lambda){
|
||||
result.int_val = 0;
|
||||
node->kind = AST_IDENT;
|
||||
node->intern_val = pctx->intern("null_lambda"_s);
|
||||
node->intern_val = pctx->intern("NULL_LAMBDA"_s);
|
||||
}
|
||||
else if(expected_type == type_bool){
|
||||
result.int_val = 0;
|
||||
@@ -228,9 +228,9 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type){
|
||||
Ast_End();
|
||||
}
|
||||
|
||||
Ast_Begin(AST_INDEX, Ast_Expr){
|
||||
Operand left = eval_expr(node->index.expr);
|
||||
Operand index = eval_expr(node->index.index);
|
||||
Ast_Begin(AST_INDEX, Ast_Index){
|
||||
Operand left = eval_expr(node->expr);
|
||||
Operand index = eval_expr(node->index);
|
||||
if(left.type->kind != TYPE_Array) parsing_error(node->pos, "Indexing variable that is not an array");
|
||||
if(index.type != type_int) parsing_error(node->pos, "Trying to index the array with invalid type, expected int");
|
||||
Operand result = {left.type->arr.base};
|
||||
@@ -238,30 +238,30 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type){
|
||||
Ast_End();
|
||||
}
|
||||
|
||||
Ast_Begin(AST_COMPOUND, Ast_Expr){
|
||||
Ast_Resolved_Type *type = eval_typespec(node->compound.typespec);
|
||||
Ast_Begin(AST_COMPOUND, Ast_Compound){
|
||||
Ast_Resolved_Type *type = eval_typespec(node->typespec);
|
||||
Ast_Resolved_Type *variable_type = expected_type;
|
||||
if(!type && variable_type) type = variable_type;
|
||||
else if(!variable_type && type);
|
||||
else if(variable_type != type) parsing_error(node->pos, "Variable type different from explicit compound type");
|
||||
node->compound.type = type;
|
||||
node->type = type;
|
||||
|
||||
if(type->kind == TYPE_Array){
|
||||
if(node->compound.exprs.len > type->arr.size) parsing_error(node->pos, "compound statement has too many items for this type");
|
||||
if(node->exprs.len > type->arr.size) parsing_error(node->pos, "compound statement has too many items for this type");
|
||||
Ast_Resolved_Type *item_type = type->arr.base;
|
||||
|
||||
For(node->compound.exprs){
|
||||
For(node->exprs){
|
||||
assert(it[0]->kind == AST_COMPOUND_ITEM);
|
||||
Ast_Expr *i = (Ast_Expr *)it[0];
|
||||
Ast_Compound_Item *i = (Ast_Compound_Item *)it[0];
|
||||
assert(i->kind == AST_COMPOUND_ITEM);
|
||||
if(i->compound_item.name) parsing_error(i->pos, "Invalid indexing kind in a compound expression of type %s", type_names[type->kind]);
|
||||
if(i->compound_item.index){
|
||||
Operand index_op = eval_expr(i->compound_item.index);
|
||||
if(i->name) parsing_error(i->pos, "Invalid indexing kind in a compound expression of type %s", type_names[type->kind]);
|
||||
if(i->index){
|
||||
Operand index_op = eval_expr(i->index);
|
||||
if(!index_op.is_const) parsing_error(i->pos, "Index in a compound expression is not a constant");
|
||||
if(index_op.type != type_int) parsing_error(i->pos, "Index should be of type int");
|
||||
if(index_op.int_val > (type->arr.size - 1)) parsing_error(i->pos, "Invalid index in compound expression, larger then type can store");
|
||||
}
|
||||
Operand expr = eval_expr(i->compound_item.item, item_type);
|
||||
Operand expr = eval_expr(i->item, item_type);
|
||||
resolve_type_pair(i->pos, expr.type, item_type);
|
||||
}
|
||||
}
|
||||
@@ -272,9 +272,9 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type){
|
||||
Ast_End();
|
||||
}
|
||||
|
||||
Ast_Begin(AST_CAST, Ast_Expr){
|
||||
Operand expr = eval_expr(node->cast.expr);
|
||||
Ast_Resolved_Type *type = eval_typespec(node->cast.typespec);
|
||||
Ast_Begin(AST_CAST, Ast_Cast){
|
||||
Operand expr = eval_expr(node->expr);
|
||||
Ast_Resolved_Type *type = eval_typespec(node->typespec);
|
||||
|
||||
if(type == expr.type) return expr;
|
||||
|
||||
@@ -298,9 +298,9 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type){
|
||||
Ast_End();
|
||||
}
|
||||
|
||||
Ast_Begin(AST_UNARY, Ast_Expr){
|
||||
Operand value = eval_expr(node->unary.expr);
|
||||
switch(node->unary.op){
|
||||
Ast_Begin(AST_UNARY, Ast_Unary){
|
||||
Operand value = eval_expr(node->expr);
|
||||
switch(node->op){
|
||||
case TK_Pointer:{
|
||||
if(value.type->kind != TYPE_Pointer) parsing_error(node->pos, "Dereferencing a value that is not a pointer");
|
||||
Operand result = {value.type->base};
|
||||
@@ -316,15 +316,15 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type){
|
||||
Ast_End();
|
||||
}
|
||||
|
||||
Ast_Begin(AST_BINARY, Ast_Expr){
|
||||
Operand left = eval_expr(ast->binary.left);
|
||||
Operand right = eval_expr(ast->binary.right);
|
||||
Ast_Begin(AST_BINARY, Ast_Binary){
|
||||
Operand left = eval_expr(node->left);
|
||||
Operand right = eval_expr(node->right);
|
||||
Operand result = {};
|
||||
result.type = resolve_type_pair(node->pos, left.type, right.type);
|
||||
if(left.is_const && right.is_const){
|
||||
result.is_const = true;
|
||||
if(result.type == type_int){
|
||||
switch(node->binary.op){
|
||||
switch(node->op){
|
||||
case TK_Add: result.int_val = left.int_val + right.int_val; break;
|
||||
case TK_Sub: result.int_val = left.int_val - right.int_val; break;
|
||||
case TK_Mul: result.int_val = left.int_val * right.int_val; break;
|
||||
|
||||
Reference in New Issue
Block a user