aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-06-10 00:39:11 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-06-10 00:40:03 +0330
commita839ff6229f4ae1415dddd5995781acb4bb599e1 (patch)
tree55015a667e09c974c38b689ae8245fa2696eca55
parent91214dfb533ac693880ef06b3e990d944009d2e4 (diff)
fix some return type issues
-rw-r--r--code/lib/io.felan8
-rw-r--r--code/lib/memory.felan9
-rw-r--r--code/lib/operator.felan8
-rw-r--r--code/lib/string.felan103
-rw-r--r--code/lib/types.felan2
-rw-r--r--code/lib/vector.felan2
-rw-r--r--code/main.felan39
-rw-r--r--src/compiler/ast-tree.c33
-rw-r--r--src/runner/runner.c19
9 files changed, 164 insertions, 59 deletions
diff --git a/code/lib/io.felan b/code/lib/io.felan
index c486902..413da8d 100644
--- a/code/lib/io.felan
+++ b/code/lib/io.felan
@@ -14,14 +14,6 @@ println :: (value:anytype) -> void {
print_char('\n');
};
-print :: (value:string) -> void {
- i := 0;
- while i < @cast(value.length,i64) {
- print_char(value[i]);
- i += 1;
- }
-};
-
print_char :: (value:u8) -> void {
putchar(@cast(value,i32));
};
diff --git a/code/lib/memory.felan b/code/lib/memory.felan
index 2710685..0082a1e 100644
--- a/code/lib/memory.felan
+++ b/code/lib/memory.felan
@@ -4,9 +4,10 @@ libc :: @c_library("/usr/lib/libc.so.6");
malloc :: @c_function(libc,"malloc",(i64)->(*void));
realloc :: @c_function(libc,"realloc",(*void,i64)->(*void));
free :: @c_function(libc,"free",(*void)->void);
+memcpy :: @c_function(libc,"memcpy",(*void,*void,i64)->(*void));
-malloc :: (size:i64,comptime t:type) -> (*t) {
- return @cast(malloc(size*@cast(@size_of(t),i64)),*t);
+malloc :: (size:u64,comptime t:type) -> (*t) {
+ return @cast(malloc(@cast(size*@size_of(t),i64)),*t);
};
realloc :: (ptr:*anytype,size:u64) -> (@type_of(ptr)) {
@@ -22,3 +23,7 @@ realloc :: (ptr:*anytype,size:u64) -> (@type_of(ptr)) {
free :: (ptr:*anytype) -> void {
free(@cast(ptr,*void));
};
+
+memcpy :: (dest:*anytype,src:@type_of(dest),n:u64) -> void {
+ memcpy(@cast(dest,*void),@cast(src,*void),@cast(n,i64));
+};
diff --git a/code/lib/operator.felan b/code/lib/operator.felan
index 52f4a3a..0c72971 100644
--- a/code/lib/operator.felan
+++ b/code/lib/operator.felan
@@ -800,6 +800,14 @@ __sub__ :: (left:*anytype,right:i64) -> (@type_of(left)) {
return @sub(left,right);
};
+__sum__ :: (left:*anytype,right:u64) -> (@type_of(left)) {
+ return @add(left,right);
+};
+
+__sub__ :: (left:*anytype,right:u64) -> (@type_of(left)) {
+ return @sub(left,right);
+};
+
__get_item__ :: (left:**anytype,index:i64) -> (@type_of(left.*.*)) {
return (left.* + index).*;
};
diff --git a/code/lib/string.felan b/code/lib/string.felan
index c7047b3..4b5452f 100644
--- a/code/lib/string.felan
+++ b/code/lib/string.felan
@@ -1,9 +1,104 @@
@import("operator.felan");
+@import("io.felan");
@import("types.felan");
+@import("memory.felan");
+
+String :: struct {
+ ptr : *u8;
+ size : u64;
+ capacity : u64;
+};
+
+string_new :: () -> String {
+ str : String = undefined;
+
+ str.ptr = malloc(0u64,u8);
+ str.size = 0u64;
+ str.capacity = 0u64;
+
+ return str;
+};
+
+string_from :: (str:[]u8) -> String {
+ result : String = undefined;
+
+ result.ptr = malloc(str.length,u8);
+ result.size = str.length;
+ result.capacity = str.length;
+
+ memcpy(result.ptr,str.ptr,str.length);
-sub_string :: (str:string, begin:i64, end:i64) -> string {
- result :[0]u8 = undefined;
- result.ptr = str.ptr + begin;
- result.length = @cast(end-begin,u64);
return result;
};
+
+append :: (this:*String, str:String) -> void {
+ _grow_if_needed(this, str.size);
+
+ memcpy(this.*.ptr+this.*.size,str.ptr,str.size);
+
+ this.*.size += str.size;
+};
+
+append :: (this:*String, str:[]u8) -> void {
+ _grow_if_needed(this, str.length);
+
+ memcpy(this.*.ptr+this.*.size,str.ptr,str.length);
+
+ this.*.size += str.length;
+};
+
+append :: (this:*String, char:u8) -> void {
+ _grow_if_needed(this, 1u64);
+
+ this.*[this.*.size] = char;
+
+ this.*.size += 1u64;
+};
+
+_grow_if_needed :: (this:*String, count:u64) -> void {
+ new_size := this.*.size + count;
+ if new_size >= this.*.capacity {
+ cap := this.*.capacity + this.*.capacity / 2u64 + 1u64;
+ if new_size >= cap {
+ cap = new_size;
+ }
+ this.*.capacity = cap;
+ this.*.ptr = realloc(this.*.ptr,this.*.capacity);
+ }
+};
+
+delete :: (this:String) -> void {
+ free(this.ptr);
+};
+
+print :: (this:String) -> void {
+ i := 0u64;
+ while i < this.size {
+ @putc(this[i]);
+ i += 1u64;
+ }
+};
+
+__get_item__ :: (left:*String,index:i64) -> u8 {
+ return (left.*.ptr + index).*;
+};
+
+__set_item__ :: (left:*String,index:i64,item:u8) -> u8 {
+ return (left.*.ptr + index).* = item;
+};
+
+__get_item_address__ :: (left:*String,index:i64) -> (*u8) {
+ return (left.*.ptr + index);
+};
+
+__get_item__ :: (left:*String,index:u64) -> u8 {
+ return (left.*.ptr + index).*;
+};
+
+__set_item__ :: (left:*String,index:u64,item:u8) -> u8 {
+ return (left.*.ptr + index).* = item;
+};
+
+__get_item_address__ :: (left:*String,index:u64) -> (*u8) {
+ return (left.*.ptr + index);
+};
diff --git a/code/lib/types.felan b/code/lib/types.felan
index df7f5c3..087f884 100644
--- a/code/lib/types.felan
+++ b/code/lib/types.felan
@@ -1,5 +1,3 @@
-string :: []u8;
-
array :: (comptime size:u64, comptime t:type) -> type {
return struct {
ptr : *t;
diff --git a/code/lib/vector.felan b/code/lib/vector.felan
index c4f2698..61fe77c 100644
--- a/code/lib/vector.felan
+++ b/code/lib/vector.felan
@@ -11,7 +11,7 @@ vector :: (comptime t:type)->type{
vector_new :: (comptime t:type) -> (vector(t)) {
v : vector(t) = undefined;
- v.ptr = malloc(0,@type_of(v.ptr.*));
+ v.ptr = malloc(0u64,@type_of(v.ptr.*));
v.size = 0u64;
v.capacity = 0u64;
return v;
diff --git a/code/main.felan b/code/main.felan
index 3af33e4..e365c37 100644
--- a/code/main.felan
+++ b/code/main.felan
@@ -1,39 +1,16 @@
@import("basic.felan");
-/*
-t :: (comptime formatter : string) macro -> void {
- i := 0;
- in := 0;
- opening := 0;
- while @cast(i,u64) < formatter.length {
- c := formatter[i];
- if c == '{' {
- if in == 0 {
- opening = i+1;
- }
- in += 1;
- } else if c == '}' {
- in -= 1;
- if in == 0 {
- return sub_string(formatter,opening,i);
- } else if in < 0 {
- in = 0;
- }
- }
- i += 1;
- }
- return "";
-};
-*/
-
main :: () -> void {
- v := vector_new(i64);
+ // print_formatted("hello");
+ str := string_from("Hey");
- push_back(&v,32);
+ print(str);
+ print_char('\n');
+ _grow_if_needed(&str,23u64);
+ // append(&str,"\nHello");
- println(v.capacity);
- println(v[0]);
+ print(str);
- delete(v);
+ delete(str);
};
diff --git a/src/compiler/ast-tree.c b/src/compiler/ast-tree.c
index e02a58c..f010067 100644
--- a/src/compiler/ast-tree.c
+++ b/src/compiler/ast-tree.c
@@ -421,7 +421,7 @@ void astTreePrint(const AstTree *tree, int indent) {
case AST_TREE_TOKEN_RAW_VALUE_NOT_OWNED:
goto RETURN_SUCCESS;
case AST_TREE_TOKEN_TYPE_C_FUNCTION:
- NOT_IMPLEMENTED;
+ goto RETURN_SUCCESS;
case AST_TREE_TOKEN_KEYWORD_BREAK:
case AST_TREE_TOKEN_KEYWORD_CONTINUE: {
AstTreeLoopControl *meatadata = tree->metadata;
@@ -611,7 +611,11 @@ void astTreePrint(const AstTree *tree, int indent) {
for (int i = 0; i < indent; ++i)
printf(" ");
printf("elseBody=\n");
- astTreePrint(metadata->elseBody, indent + 1);
+ if (metadata->elseBody != NULL) {
+ astTreePrint(metadata->elseBody, indent + 1);
+ } else {
+ printf("null");
+ }
printf("\n");
for (int i = 0; i < indent; ++i)
printf(" ");
@@ -5141,6 +5145,23 @@ bool isEqual(AstTree *left, AstTree *right, AstTreeScope *scope) {
return memcmp(left_metadata, right_metadata, getSizeOfType(left->type)) ==
0;
}
+ case AST_TREE_TOKEN_TYPE_ARRAY: {
+ AstTreeBracket *left_metadata = left->metadata;
+ AstTreeBracket *right_metadata = right->metadata;
+
+ if (left_metadata->parameters.size != right_metadata->parameters.size) {
+ return false;
+ }
+
+ for (size_t i = 0; i < left_metadata->parameters.size; ++i) {
+ if (!isEqual(left_metadata->parameters.data[i],
+ right_metadata->parameters.data[i], scope)) {
+ return false;
+ }
+ }
+
+ return isEqual(left_metadata->operand, right_metadata->operand, scope);
+ }
case AST_TREE_TOKEN_FUNCTION:
case AST_TREE_TOKEN_BUILTIN_CAST:
case AST_TREE_TOKEN_BUILTIN_TYPE_OF:
@@ -5179,7 +5200,6 @@ bool isEqual(AstTree *left, AstTree *right, AstTreeScope *scope) {
case AST_TREE_TOKEN_KEYWORD_COMPTIME:
case AST_TREE_TOKEN_KEYWORD_STRUCT:
case AST_TREE_TOKEN_TYPE_FUNCTION:
- case AST_TREE_TOKEN_TYPE_ARRAY:
case AST_TREE_TOKEN_FUNCTION_CALL:
case AST_TREE_TOKEN_VARIABLE_DEFINE:
case AST_TREE_TOKEN_VALUE_SHAPE_SHIFTER:
@@ -5902,6 +5922,7 @@ bool setTypesFunction(AstTree *tree, AstTreeSetTypesHelper _helper) {
if (!setAllTypes(metadata->returnType, helper, NULL, NULL)) {
return false;
}
+ metadata->returnType = getValue(metadata->returnType, false, helper.scope);
tree->type = makeTypeOf(tree);
@@ -6527,6 +6548,7 @@ bool setTypesIf(AstTree *tree, AstTreeSetTypesHelper helper,
if (!setAllTypes(metadata->ifBody, helper, function, NULL) ||
(metadata->elseBody != NULL &&
!setAllTypes(metadata->elseBody, helper, function, NULL))) {
+ return false;
}
if (metadata->elseBody != NULL &&
@@ -8012,10 +8034,7 @@ bool setTypesAstFunction(AstTreeFunction *metadata,
if (!setAllTypes(metadata->returnType, helper, NULL, NULL)) {
return false;
}
-
- if (isConst(metadata->returnType)) {
- metadata->returnType = getValue(metadata->returnType, false, helper.scope);
- }
+ metadata->returnType = getValue(metadata->returnType, false, helper.scope);
for (size_t i = 0; i < helper.dependencies.size; ++i) {
deps[deps_size] = helper.dependencies.data[i];
diff --git a/src/runner/runner.c b/src/runner/runner.c
index ab4e9b5..21ece80 100644
--- a/src/runner/runner.c
+++ b/src/runner/runner.c
@@ -208,10 +208,17 @@ AstTree *runAstTreeFunction(AstTree *tree, AstTree **arguments,
u32 breakCount = 0;
bool shouldContinue = false;
+ AstTreeScope *currentScope;
+ if (function->isMacro) {
+ currentScope = scope;
+ } else {
+ currentScope = &function->scope;
+ }
+
for (size_t i = 0; i < function->scope.expressions_size; ++i) {
- AstTree *ret = runExpression(function->scope.expressions[i],
- &function->scope, &shouldRet, false,
- isComptime, &breakCount, &shouldContinue);
+ AstTree *ret =
+ runExpression(function->scope.expressions[i], currentScope, &shouldRet,
+ false, isComptime, &breakCount, &shouldContinue);
if (shouldRet) {
return ret;
} else {
@@ -1700,9 +1707,11 @@ AstTree *runExpression(AstTree *expr, AstTreeScope *scope, bool *shouldRet,
}
AstTree *type =
copyAstTree((AstTreeSingleChild *)operand->type->metadata);
+ printLog("%s", AST_TREE_TOKEN_STRINGS[type->token]);
AstTreeRawValue *value = *(void **)operand->metadata;
astTreeDelete(operand);
- return newAstTree(AST_TREE_TOKEN_RAW_VALUE_NOT_OWNED, value, type, NULL, NULL);
+ return newAstTree(AST_TREE_TOKEN_RAW_VALUE_NOT_OWNED, value, type, NULL,
+ NULL);
} else if (operand->token == AST_TREE_TOKEN_VARIABLE) {
AstTree *ret;
if (isLeft) {
@@ -1794,6 +1803,8 @@ AstTree *runExpression(AstTree *expr, AstTreeScope *scope, bool *shouldRet,
}
size_t index = 0;
AstTreeStruct *type = tree->type->metadata;
+ printLog("%s %s", AST_TREE_TOKEN_STRINGS[tree->type->token],
+ AST_TREE_TOKEN_STRINGS[metadata->object->token]);
for (size_t i = 0; i < metadata->member.index; ++i) {
index += getSizeOfType(type->variables.data[i]->type);
}