aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--code/basic.felan98
-rw-r--r--code/main.felan131
-rw-r--r--src/compiler/ast-tree.c173
-rw-r--r--src/compiler/ast-tree.h3
5 files changed, 202 insertions, 207 deletions
diff --git a/Makefile b/Makefile
index 62214dc..d9f18bf 100644
--- a/Makefile
+++ b/Makefile
@@ -20,9 +20,9 @@ INC_DIRS := $(SRC_DIR)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
# OP_FLAG := -Ofast
-# OP_FLAG := -O3
+OP_FLAG := -O3
# OP_FLAG := -Oz
-OP_FLAG := -g
+# OP_FLAG := -g
# CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -DPRINT_STATISTICS -DPRINT_COMPILE_TREE $(OP_FLAG)
CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -DPRINT_STATISTICS $(OP_FLAG)
diff --git a/code/basic.felan b/code/basic.felan
index a5d5f39..b248493 100644
--- a/code/basic.felan
+++ b/code/basic.felan
@@ -46,51 +46,51 @@ __minus__ :: (value:i64) -> i64 {
return @neg(value);
};
-__add__ :: (left:u8,right:u8) -> u8 {
+__sum__ :: (left:u8,right:u8) -> u8 {
return @add(left,right);
};
-__add__ :: (left:i8,right:i8) -> i8 {
+__sum__ :: (left:i8,right:i8) -> i8 {
return @add(left,right);
};
-__add__ :: (left:u16,right:u16) -> u16 {
+__sum__ :: (left:u16,right:u16) -> u16 {
return @add(left,right);
};
-__add__ :: (left:i16,right:i16) -> i16 {
+__sum__ :: (left:i16,right:i16) -> i16 {
return @add(left,right);
};
-__add__ :: (left:u32,right:u32) -> u32 {
+__sum__ :: (left:u32,right:u32) -> u32 {
return @add(left,right);
};
-__add__ :: (left:i32,right:i32) -> i32 {
+__sum__ :: (left:i32,right:i32) -> i32 {
return @add(left,right);
};
-__add__ :: (left:u64,right:u64) -> u64 {
+__sum__ :: (left:u64,right:u64) -> u64 {
return @add(left,right);
};
-__add__ :: (left:i64,right:i64) -> i64 {
+__sum__ :: (left:i64,right:i64) -> i64 {
return @add(left,right);
};
-__add__ :: (left:f16,right:f16) -> f16 {
+__sum__ :: (left:f16,right:f16) -> f16 {
return @add(left,right);
};
-__add__ :: (left:f32,right:f32) -> f32 {
+__sum__ :: (left:f32,right:f32) -> f32 {
return @add(left,right);
};
-__add__ :: (left:f64,right:f64) -> f64 {
+__sum__ :: (left:f64,right:f64) -> f64 {
return @add(left,right);
};
-__add__ :: (left:f128,right:f128) -> f128 {
+__sum__ :: (left:f128,right:f128) -> f128 {
return @add(left,right);
};
@@ -366,99 +366,99 @@ __smaller__ :: (left:f128,right:f128) -> bool {
return @smaller(left,right);
};
-__greaterOrEqual__ :: (left:u8,right:u8) -> bool {
+__greater_or_equal__ :: (left:u8,right:u8) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:i8,right:i8) -> bool {
+__greater_or_equal__ :: (left:i8,right:i8) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:u16,right:u16) -> bool {
+__greater_or_equal__ :: (left:u16,right:u16) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:i16,right:i16) -> bool {
+__greater_or_equal__ :: (left:i16,right:i16) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:u32,right:u32) -> bool {
+__greater_or_equal__ :: (left:u32,right:u32) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:i32,right:i32) -> bool {
+__greater_or_equal__ :: (left:i32,right:i32) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:u64,right:u64) -> bool {
+__greater_or_equal__ :: (left:u64,right:u64) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:i64,right:i64) -> bool {
+__greater_or_equal__ :: (left:i64,right:i64) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:f16,right:f16) -> bool {
+__greater_or_equal__ :: (left:f16,right:f16) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:f32,right:f32) -> bool {
+__greater_or_equal__ :: (left:f32,right:f32) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:f64,right:f64) -> bool {
+__greater_or_equal__ :: (left:f64,right:f64) -> bool {
return @greaterOrEqual(left,right);
};
-__greaterOrEqual__ :: (left:f128,right:f128) -> bool {
+__greater_or_equal__ :: (left:f128,right:f128) -> bool {
return @greaterOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:u8,right:u8) -> bool {
+__smaller_or_equal__ :: (left:u8,right:u8) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:i8,right:i8) -> bool {
+__smaller_or_equal__ :: (left:i8,right:i8) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:u16,right:u16) -> bool {
+__smaller_or_equal__ :: (left:u16,right:u16) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:i16,right:i16) -> bool {
+__smaller_or_equal__ :: (left:i16,right:i16) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:u32,right:u32) -> bool {
+__smaller_or_equal__ :: (left:u32,right:u32) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:i32,right:i32) -> bool {
+__smaller_or_equal__ :: (left:i32,right:i32) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:u64,right:u64) -> bool {
+__smaller_or_equal__ :: (left:u64,right:u64) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:i64,right:i64) -> bool {
+__smaller_or_equal__ :: (left:i64,right:i64) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:f16,right:f16) -> bool {
+__smaller_or_equal__ :: (left:f16,right:f16) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:f32,right:f32) -> bool {
+__smaller_or_equal__ :: (left:f32,right:f32) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:f64,right:f64) -> bool {
+__smaller_or_equal__ :: (left:f64,right:f64) -> bool {
return @smallerOrEqual(left,right);
};
-__smallerOrEqual__ :: (left:f128,right:f128) -> bool {
+__smaller_or_equal__ :: (left:f128,right:f128) -> bool {
return @smallerOrEqual(left,right);
};
@@ -514,55 +514,55 @@ __equal__ :: (left:bool,right:bool) -> bool {
return @equal(left,right);
};
-__notEqual__ :: (left:u8,right:u8) -> bool {
+__not_equal__ :: (left:u8,right:u8) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:i8,right:i8) -> bool {
+__not_equal__ :: (left:i8,right:i8) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:u16,right:u16) -> bool {
+__not_equal__ :: (left:u16,right:u16) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:i16,right:i16) -> bool {
+__not_equal__ :: (left:i16,right:i16) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:u32,right:u32) -> bool {
+__not_equal__ :: (left:u32,right:u32) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:i32,right:i32) -> bool {
+__not_equal__ :: (left:i32,right:i32) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:u64,right:u64) -> bool {
+__not_equal__ :: (left:u64,right:u64) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:i64,right:i64) -> bool {
+__not_equal__ :: (left:i64,right:i64) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:f16,right:f16) -> bool {
+__not_equal__ :: (left:f16,right:f16) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:f32,right:f32) -> bool {
+__not_equal__ :: (left:f32,right:f32) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:f64,right:f64) -> bool {
+__not_equal__ :: (left:f64,right:f64) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:f128,right:f128) -> bool {
+__not_equal__ :: (left:f128,right:f128) -> bool {
return @notEqual(left,right);
};
-__notEqual__ :: (left:bool,right:bool) -> bool {
+__not_equal__ :: (left:bool,right:bool) -> bool {
return @notEqual(left,right);
};
diff --git a/code/main.felan b/code/main.felan
index 5d321ed..715c8db 100644
--- a/code/main.felan
+++ b/code/main.felan
@@ -1,6 +1,133 @@
+/*
@import("basic.felan");
-file :: @import("file.felan");
+
+print :: (value:[]u8)->void{
+ i :u64 = 0;
+ while i < value.length {
+ putc value[i];
+ i += @cast(1,u64);
+ }
+};
+
+print :: (value:i64)->void{
+ value := value;
+ str : [20]u8 = undefined;
+ i :u64= 19;
+ while {
+ str[i] = @cast(value % 10,u8) + '0';
+ i -= @cast(1,u64);
+ value /= 10;
+ value != 0;
+ } {}
+
+ j :u64= i+@cast(1,u64);
+ while j < @cast(20,u64) {
+ putc str[j];
+ j += @cast(1,u64);
+ }
+};
+
+println :: (value:i64)->void{
+ print(value);
+ putc '\n';
+};
+
+println :: ()->void{
+ putc '\n';
+};
main :: () -> void {
- file.test();
+ stones := 6;
+ player := 1;
+ while stones != 0 {
+ c := choose_ai(stones);
+ print("player: ");
+ print(player);
+ print(" ---------\n");
+ println(stones);
+ println(c);
+
+ if (stones == 2 || stones == 1) && stones == c {
+ print("player: ");
+ print(player);
+ print(" won\n");
+ }
+
+ stones -= c;
+
+ player = (if (player == 1) 2 else 1);
+ }
};
+
+choose_ai :: (stones_left:i64) -> i64 {
+ return (if (stones_left % 3 == 0) 1 else stones_left % 3);
+};
+*/
+@import("basic.felan");
+
+main :: () -> void {
+ stones := 6;
+ player := 1;
+ while stones > 0 {
+ r :res =minimax(stones,true);
+ stones -= r.move;
+ putc @cast(stones,u8) + '0';
+ putc '\n';
+ putc 'p';
+ putc @cast(player,u8) + '0';
+ putc '\n';
+ player = (if player == 2 1 else 2);
+ }
+};
+
+res :: struct {
+ move : i64;
+ score : i64;
+};
+
+minimax :: (currentStones:i64, isMaximizing:bool) -> res {
+ if currentStones <= 0 {
+ r : res = undefined;
+ r.move = 0;
+ r.score = (if isMaximizing {-1;} else {1;});
+ return r;
+ }
+
+ found := false;
+ bestMove : res = undefined;
+ score := (if isMaximizing {-1000000;} else {1000000;});
+ move := 1;
+
+ while move <= 2 {
+ if currentStones - move >= 0 {
+ found = true;
+ result := minimax(currentStones - move, !isMaximizing);
+ if isMaximizing {
+ if result.score > score {
+ bestMove.move = move;
+ bestMove.score = result.score;
+ score = result.score;
+ }
+ }else{
+ if result.score < score {
+ bestMove.move = move;
+ bestMove.score = result.score;
+ score = result.score;
+ }
+ }
+ }
+ move += 1;
+ }
+
+ if !found {
+ r : res = undefined;
+ r.move = 0;
+ r.score = (if isMaximizing {-1;} else {1;});
+ return r;
+ }
+
+ return bestMove;
+};
+
+
+
diff --git a/src/compiler/ast-tree.c b/src/compiler/ast-tree.c
index 56daf7d..b343cc7 100644
--- a/src/compiler/ast-tree.c
+++ b/src/compiler/ast-tree.c
@@ -1477,7 +1477,7 @@ AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots
goto RETURN_ERROR;
}
AstTree *parameter = tree_metadata->parameters[0].value;
- if (!isConstByValue(parameter)) {
+ if (!isConst(parameter, true)) {
printError(parameter->str_begin, parameter->str_end,
"Is not constant");
goto RETURN_ERROR;
@@ -1552,7 +1552,7 @@ AstTreeRoot *getAstTreeRoot(char *filePath, AstTreeRoots *roots
goto RETURN_ERROR;
}
AstTree *parameter = tree_metadata->parameters[0].value;
- if (!isConstByValue(parameter)) {
+ if (!isConst(parameter, true)) {
printError(parameter->str_begin, parameter->str_end,
"Is not constant");
goto RETURN_ERROR;
@@ -3105,7 +3105,7 @@ bool isFunction(AstTree *value) {
return value->type->token == AST_TREE_TOKEN_TYPE_FUNCTION;
}
-bool isConst(AstTree *tree) {
+bool isConst(AstTree *tree, bool byValue) {
if (tree->type == NULL) {
UNREACHABLE;
}
@@ -3163,8 +3163,8 @@ bool isConst(AstTree *tree) {
AstTreeStruct *metadata = tree->metadata;
for (size_t i = 0; i < metadata->variables.size; ++i) {
AstTreeVariable *member = metadata->variables.data[i];
- if (!isConst(member->type) ||
- (member->value != NULL && !isConst(member->type))) {
+ if (!isConst(member->type, byValue) ||
+ (member->value != NULL && !isConst(member->type, byValue))) {
return false;
}
}
@@ -3172,17 +3172,18 @@ bool isConst(AstTree *tree) {
}
case AST_TREE_TOKEN_KEYWORD_IF: {
AstTreeIf *metadata = tree->metadata;
- return isConst(metadata->condition) && isConst(metadata->ifBody) &&
- (metadata->elseBody == NULL || isConst(metadata->elseBody));
+ return isConst(metadata->condition, byValue) &&
+ isConst(metadata->ifBody, byValue) &&
+ (metadata->elseBody == NULL || isConst(metadata->elseBody, byValue));
}
case AST_TREE_TOKEN_FUNCTION_CALL: {
AstTreeFunctionCall *metadata = tree->metadata;
for (size_t i = 0; i < metadata->parameters_size; ++i) {
- if (!isConst(metadata->parameters[i].value)) {
+ if (!isConst(metadata->parameters[i].value, byValue)) {
return false;
}
}
- return isConst(metadata->function);
+ return isConst(metadata->function, byValue);
}
case AST_TREE_TOKEN_FUNCTION: {
return true;
@@ -3211,27 +3212,27 @@ bool isConst(AstTree *tree) {
return false;
case AST_TREE_TOKEN_VARIABLE: {
AstTreeVariable *metadata = tree->metadata;
- return metadata->isConst;
+ return metadata->isConst && (!byValue || metadata->value != NULL);
}
case AST_TREE_TOKEN_OPERATOR_DEREFERENCE:
case AST_TREE_TOKEN_OPERATOR_ADDRESS:
case AST_TREE_TOKEN_OPERATOR_POINTER: {
AstTreeSingleChild *metadata = tree->metadata;
- return isConst(metadata);
+ return isConst(metadata, byValue);
}
case AST_TREE_TOKEN_OPERATOR_ACCESS: {
AstTreeAccess *metadata = tree->metadata;
- return isConst(metadata->object);
+ return isConst(metadata->object, byValue);
}
case AST_TREE_TOKEN_TYPE_ARRAY:
case AST_TREE_TOKEN_OPERATOR_ARRAY_ACCESS: {
AstTreeBracket *metadata = tree->metadata;
for (size_t i = 0; i < metadata->parameters.size; ++i) {
- if (!isConst(metadata->parameters.data[i])) {
+ if (!isConst(metadata->parameters.data[i], byValue)) {
return false;
}
}
- return isConst(metadata->operand);
+ return isConst(metadata->operand, byValue);
}
case AST_TREE_TOKEN_NONE:
}
@@ -3239,140 +3240,6 @@ bool isConst(AstTree *tree) {
UNREACHABLE;
}
-bool isConstByValue(AstTree *tree) {
- if (tree->type == NULL) {
- UNREACHABLE;
- }
- switch (tree->token) {
- case AST_TREE_TOKEN_BUILTIN_CAST:
- case AST_TREE_TOKEN_BUILTIN_TYPE_OF:
- case AST_TREE_TOKEN_BUILTIN_IMPORT:
- case AST_TREE_TOKEN_BUILTIN_IS_COMPTIME:
- case AST_TREE_TOKEN_BUILTIN_STACK_ALLOC:
- case AST_TREE_TOKEN_BUILTIN_HEAP_ALLOC:
- case AST_TREE_TOKEN_BUILTIN_NEG:
- case AST_TREE_TOKEN_BUILTIN_ADD:
- case AST_TREE_TOKEN_BUILTIN_SUB:
- case AST_TREE_TOKEN_BUILTIN_MUL:
- case AST_TREE_TOKEN_BUILTIN_DIV:
- case AST_TREE_TOKEN_BUILTIN_MOD:
- case AST_TREE_TOKEN_BUILTIN_EQUAL:
- case AST_TREE_TOKEN_BUILTIN_NOT_EQUAL:
- case AST_TREE_TOKEN_BUILTIN_GREATER:
- case AST_TREE_TOKEN_BUILTIN_SMALLER:
- case AST_TREE_TOKEN_BUILTIN_GREATER_OR_EQUAL:
- case AST_TREE_TOKEN_BUILTIN_SMALLER_OR_EQUAL:
- case AST_TREE_TOKEN_TYPE_TYPE:
- case AST_TREE_TOKEN_TYPE_FUNCTION:
- case AST_TREE_TOKEN_TYPE_VOID:
- case AST_TREE_TOKEN_TYPE_I8:
- case AST_TREE_TOKEN_TYPE_U8:
- case AST_TREE_TOKEN_TYPE_I16:
- case AST_TREE_TOKEN_TYPE_U16:
- case AST_TREE_TOKEN_TYPE_I32:
- case AST_TREE_TOKEN_TYPE_U32:
- case AST_TREE_TOKEN_TYPE_I64:
- case AST_TREE_TOKEN_TYPE_U64:
-#ifdef FLOAT_16_SUPPORT
- case AST_TREE_TOKEN_TYPE_F16:
-#endif
- case AST_TREE_TOKEN_TYPE_F32:
- case AST_TREE_TOKEN_TYPE_F64:
- case AST_TREE_TOKEN_TYPE_F128:
- case AST_TREE_TOKEN_TYPE_CODE:
- case AST_TREE_TOKEN_TYPE_NAMESPACE:
- case AST_TREE_TOKEN_TYPE_BOOL:
- case AST_TREE_TOKEN_VALUE_NULL:
- case AST_TREE_TOKEN_VALUE_UNDEFINED:
- case AST_TREE_TOKEN_VALUE_VOID:
- case AST_TREE_TOKEN_VALUE_NAMESPACE:
- case AST_TREE_TOKEN_VALUE_INT:
- case AST_TREE_TOKEN_VALUE_FLOAT:
- case AST_TREE_TOKEN_VALUE_BOOL:
- case AST_TREE_TOKEN_VALUE_OBJECT:
- case AST_TREE_TOKEN_KEYWORD_COMPTIME:
- case AST_TREE_TOKEN_SCOPE:
- return true;
- case AST_TREE_TOKEN_KEYWORD_STRUCT: {
- AstTreeStruct *metadata = tree->metadata;
- for (size_t i = 0; i < metadata->variables.size; ++i) {
- AstTreeVariable *member = metadata->variables.data[i];
- if (!isConstByValue(member->type) ||
- (member->value != NULL && !isConstByValue(member->type))) {
- return false;
- }
- }
- return true;
- }
- case AST_TREE_TOKEN_KEYWORD_IF: {
- AstTreeIf *metadata = tree->metadata;
- return isConstByValue(metadata->condition) &&
- isConstByValue(metadata->ifBody) &&
- (metadata->elseBody == NULL || isConstByValue(metadata->elseBody));
- }
- case AST_TREE_TOKEN_FUNCTION_CALL: {
- AstTreeFunctionCall *metadata = tree->metadata;
- for (size_t i = 0; i < metadata->parameters_size; ++i) {
- if (!isConstByValue(metadata->parameters[i].value)) {
- return false;
- }
- }
- return isConstByValue(metadata->function);
- }
- case AST_TREE_TOKEN_FUNCTION: {
- return true;
- }
- case AST_TREE_TOKEN_KEYWORD_WHILE:
- case AST_TREE_TOKEN_KEYWORD_PUTC:
- case AST_TREE_TOKEN_KEYWORD_RETURN:
- case AST_TREE_TOKEN_VARIABLE_DEFINE:
- case AST_TREE_TOKEN_OPERATOR_ASSIGN:
- case AST_TREE_TOKEN_OPERATOR_PLUS:
- case AST_TREE_TOKEN_OPERATOR_MINUS:
- case AST_TREE_TOKEN_OPERATOR_SUM:
- case AST_TREE_TOKEN_OPERATOR_SUB:
- case AST_TREE_TOKEN_OPERATOR_MULTIPLY:
- case AST_TREE_TOKEN_OPERATOR_DIVIDE:
- case AST_TREE_TOKEN_OPERATOR_MODULO:
- case AST_TREE_TOKEN_OPERATOR_EQUAL:
- case AST_TREE_TOKEN_OPERATOR_NOT_EQUAL:
- case AST_TREE_TOKEN_OPERATOR_GREATER:
- case AST_TREE_TOKEN_OPERATOR_SMALLER:
- case AST_TREE_TOKEN_OPERATOR_GREATER_OR_EQUAL:
- case AST_TREE_TOKEN_OPERATOR_SMALLER_OR_EQUAL:
- case AST_TREE_TOKEN_OPERATOR_LOGICAL_NOT:
- case AST_TREE_TOKEN_OPERATOR_LOGICAL_AND:
- case AST_TREE_TOKEN_OPERATOR_LOGICAL_OR:
- return false;
- case AST_TREE_TOKEN_VARIABLE: {
- AstTreeVariable *metadata = tree->metadata;
- return metadata->isConst && metadata->value != NULL;
- }
- case AST_TREE_TOKEN_OPERATOR_DEREFERENCE:
- case AST_TREE_TOKEN_OPERATOR_ADDRESS:
- case AST_TREE_TOKEN_OPERATOR_POINTER: {
- AstTreeSingleChild *metadata = tree->metadata;
- return isConstByValue(metadata);
- }
- case AST_TREE_TOKEN_OPERATOR_ACCESS: {
- AstTreeAccess *metadata = tree->metadata;
- return isConstByValue(metadata->object);
- }
- case AST_TREE_TOKEN_TYPE_ARRAY:
- case AST_TREE_TOKEN_OPERATOR_ARRAY_ACCESS: {
- AstTreeBracket *metadata = tree->metadata;
- for (size_t i = 0; i < metadata->parameters.size; ++i) {
- if (!isConstByValue(metadata->parameters.data[i])) {
- return false;
- }
- }
- return isConstByValue(metadata->operand);
- }
- case AST_TREE_TOKEN_NONE:
- }
- UNREACHABLE;
-}
-
AstTree *makeTypeOf(AstTree *value) {
switch (value->token) {
case AST_TREE_TOKEN_TYPE_TYPE:
@@ -3716,7 +3583,7 @@ bool typeIsEqualBack(const AstTree *type0, const AstTree *type1) {
}
AstTree *getValue(AstTree *tree) {
- if (!isConst(tree)) {
+ if (!isConst(tree, false)) {
printError(tree->str_begin, tree->str_end,
"Can't get value at compile time because it is not const");
return NULL;
@@ -4795,7 +4662,7 @@ bool setTypesOperatorAssign(AstTree *tree, AstTreeSetTypesHelper helper) {
} else if (!typeIsEqual(infix->left->type, infix->right->type)) {
printError(tree->str_begin, tree->str_end, "Type mismatch");
return false;
- } else if (isConst(infix->left)) {
+ } else if (isConst(infix->left, false)) {
printError(tree->str_begin, tree->str_end, "Constants can't be assigned");
return false;
} else {
@@ -4842,6 +4709,7 @@ bool setTypesOperatorInfix(AstTree *tree, AstTreeSetTypesHelper _helper,
AstTreeVariable *variable =
setTypesFindVariable(str, str + str_size, helper, &functionCall);
if (variable == NULL) {
+ printError(tree->str_begin, tree->str_end, "Can't find operator");
return false;
}
@@ -4902,6 +4770,7 @@ bool setTypesOperatorUnary(AstTree *tree, AstTreeSetTypesHelper helper,
AstTreeVariable *variable = setTypesFindVariable(
funcStr, funcStr + funcStr_size, helper, &functionCall);
if (variable == NULL) {
+ printError(tree->str_begin, tree->str_end, "Can't find operator");
return false;
}
@@ -5003,7 +4872,7 @@ bool setTypesAstVariable(AstTreeVariable *variable,
return false;
}
- if (isConstByValue(variable->type)) {
+ if (isConst(variable->type, true)) {
AstTree *type = variable->type;
variable->type = getValue(type);
if (variable->type == NULL) {
@@ -5046,7 +4915,7 @@ bool setTypesAstVariable(AstTreeVariable *variable,
AST_TREE_TOKEN_STRINGS[value->type->token],
AST_TREE_TOKEN_STRINGS[variable->type->token]);
return false;
- } else if (variable->isConst && !isConst(value)) {
+ } else if (variable->isConst && !isConst(value, false)) {
printError(value->str_begin, value->str_end,
"Can't initialize constant with non constant value");
return false;
diff --git a/src/compiler/ast-tree.h b/src/compiler/ast-tree.h
index 91beae3..bd357d2 100644
--- a/src/compiler/ast-tree.h
+++ b/src/compiler/ast-tree.h
@@ -375,8 +375,7 @@ AstTree *astTreeParseBracket(const ParserNode *parserNode,
AstTreeHelper *helper, AstTreeToken token);
bool isFunction(AstTree *value);
-bool isConst(AstTree *tree);
-bool isConstByValue(AstTree *tree);
+bool isConst(AstTree *tree,bool byValue);
AstTree *makeTypeOf(AstTree *value);
bool typeIsEqual(AstTree *type0, AstTree *type1);
bool typeIsEqualBack(const AstTree *type0, const AstTree *type1);