aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-06-09 18:56:19 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-06-09 18:56:19 +0330
commit8dc246166a007c2815f93ff6db535a660b05431c (patch)
treea3747464019316434103fe757a83fbdd990f16ce
parent03e9e1708eada3985529949302f214a223a297c2 (diff)
fix assigning to dereference access in a chain
-rw-r--r--code/lib/memory.felan11
-rw-r--r--code/lib/vector.felan18
-rw-r--r--code/main.felan2
-rw-r--r--src/compiler/lexer.c6
-rw-r--r--src/compiler/lexer.h4
-rw-r--r--src/compiler/parser.c12
-rw-r--r--src/runner/runner.c15
7 files changed, 49 insertions, 19 deletions
diff --git a/code/lib/memory.felan b/code/lib/memory.felan
index 55451c5..2710685 100644
--- a/code/lib/memory.felan
+++ b/code/lib/memory.felan
@@ -2,12 +2,23 @@
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);
malloc :: (size:i64,comptime t:type) -> (*t) {
return @cast(malloc(size*@cast(@size_of(t),i64)),*t);
};
+realloc :: (ptr:*anytype,size:u64) -> (@type_of(ptr)) {
+ return @cast(
+ realloc(
+ @cast(ptr,*void),
+ @cast(size*@size_of(@type_of(ptr.*)),i64),
+ ),
+ @type_of(ptr)
+ );
+};
+
free :: (ptr:*anytype) -> void {
free(@cast(ptr,*void));
};
diff --git a/code/lib/vector.felan b/code/lib/vector.felan
index a33d386..d6faece 100644
--- a/code/lib/vector.felan
+++ b/code/lib/vector.felan
@@ -21,16 +21,26 @@ __get_item__ :: (left:vector(anytype),index:i64) -> (@type_of(left.ptr.*)) {
return (left.ptr + index).*;
};
-__set_item__ :: (left:vector(anytype),index:i64,item:@type_of(left.ptr.*)) -> (@type_of(left.ptr.*)) {
+__set_item__ :: (left:*vector(anytype),index:i64,item:@type_of(left.*.ptr.*)) -> (@type_of(left.ptr.*)) {
return (left.ptr + index).* = item;
};
-__get_item_address__ :: (left:vector(anytype),index:i64) -> (@type_of(left.ptr)) {
+__get_item_address__ :: (left:*vector(anytype),index:i64) -> (@type_of(left.*.ptr)) {
return (left.ptr + index);
};
-push_back :: (vec:vector(anytype),value:@type_of(vec.ptr.*)) -> void {
- @putc('h');
+push_back :: (vec:*vector(anytype),value:@type_of(vec.*[0])) -> void {
+ _grow_if_needed(vec);
+};
+
+_grow_if_needed :: (vec:*vector(anytype)) -> void {
+ if vec.*.size == vec.*.capacity {
+ @putc('a');
+ vec.*.capacity = vec.*.capacity + vec.*.capacity/2u64 + 1u64;
+ vec.*.ptr = realloc(vec.*.ptr,vec.*.capacity);
+ }else{
+ @putc('b');
+ }
};
delete :: (vec:vector(anytype)) -> void {
diff --git a/code/main.felan b/code/main.felan
index 20c815a..63f98a2 100644
--- a/code/main.felan
+++ b/code/main.felan
@@ -30,7 +30,7 @@ t :: (comptime formatter : string) macro -> void {
main :: () -> void {
v := vector_new(u8);
- push_back(v,2u8);
+ push_back(&v,'2');
delete(v);
};
diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c
index 43a0850..869f62f 100644
--- a/src/compiler/lexer.c
+++ b/src/compiler/lexer.c
@@ -9,7 +9,6 @@ const char *LEXER_TOKEN_STRINGS[] = {
"LEXER_TOKEN_SYMBOL_CLOSE_CURLY_BRACKET",
"LEXER_TOKEN_SYMBOL_CLOSE_PARENTHESIS",
- "LEXER_TOKEN_SYMBOL_CLOSE_BRACKET",
"LEXER_TOKEN_IDENTIFIER",
"LEXER_TOKEN_BUILTIN",
"LEXER_TOKEN_BUILTIN_CAST",
@@ -73,15 +72,16 @@ const char *LEXER_TOKEN_STRINGS[] = {
"LEXER_TOKEN_KEYWORD_UNDEFINED",
"LEXER_TOKEN_SYMBOL_FUNCTION_ARROW",
- "LEXER_TOKEN_SYMBOL_POINTER",
"LEXER_TOKEN_KEYWORD_STRUCT",
- "LEXER_TOKEN_SYMBOL_CLOSE_BRACKET_LEFT",
"LEXER_TOKEN_SYMBOL_DEREFERENCE",
+ "LEXER_TOKEN_SYMBOL_CLOSE_BRACKET",
"LEXER_TOKEN_SYMBOL_ACCESS",
"LEXER_TOKEN_SYMBOL_FUNCTION_CALL",
"LEXER_TOKEN_SYMBOL_PLUS",
+ "LEXER_TOKEN_SYMBOL_CLOSE_BRACKET_LEFT",
+ "LEXER_TOKEN_SYMBOL_POINTER",
"LEXER_TOKEN_SYMBOL_MINUS",
"LEXER_TOKEN_SYMBOL_ADDRESS",
"LEXER_TOKEN_SYMBOL_LOGICAL_NOT",
diff --git a/src/compiler/lexer.h b/src/compiler/lexer.h
index e868900..1802e41 100644
--- a/src/compiler/lexer.h
+++ b/src/compiler/lexer.h
@@ -9,7 +9,6 @@ typedef enum LexerToken {
LEXER_TOKEN_SYMBOL_CLOSE_PARENTHESIS,
LEXER_TOKEN_ORDER1 = LEXER_TOKEN_SYMBOL_CLOSE_PARENTHESIS,
- LEXER_TOKEN_SYMBOL_CLOSE_BRACKET,
LEXER_TOKEN_IDENTIFIER,
LEXER_TOKEN_BUILTIN,
LEXER_TOKEN_BUILTIN_CAST,
@@ -75,15 +74,16 @@ typedef enum LexerToken {
LEXER_TOKEN_SYMBOL_FUNCTION_ARROW,
LEXER_TOKEN_ORDER2 = LEXER_TOKEN_SYMBOL_FUNCTION_ARROW,
LEXER_TOKEN_KEYWORD_STRUCT,
- LEXER_TOKEN_SYMBOL_CLOSE_BRACKET_LEFT,
LEXER_TOKEN_SYMBOL_DEREFERENCE,
LEXER_TOKEN_ORDER3 = LEXER_TOKEN_SYMBOL_DEREFERENCE,
+ LEXER_TOKEN_SYMBOL_CLOSE_BRACKET,
LEXER_TOKEN_SYMBOL_ACCESS,
LEXER_TOKEN_SYMBOL_FUNCTION_CALL,
LEXER_TOKEN_SYMBOL_PLUS,
LEXER_TOKEN_ORDER4 = LEXER_TOKEN_SYMBOL_PLUS,
+ LEXER_TOKEN_SYMBOL_CLOSE_BRACKET_LEFT,
LEXER_TOKEN_SYMBOL_POINTER,
LEXER_TOKEN_SYMBOL_MINUS,
LEXER_TOKEN_SYMBOL_ADDRESS,
diff --git a/src/compiler/parser.c b/src/compiler/parser.c
index a35800c..1becd88 100644
--- a/src/compiler/parser.c
+++ b/src/compiler/parser.c
@@ -1522,7 +1522,7 @@ ParserNode *parserParenthesis(LexerNode *closing, LexerNode *begin,
ParserNode *pNode =
getUntilCommonParents(iter->parserNode, parent, parserNode);
if (pNode == NULL) {
- printLog(pNode->str_begin, pNode->str_end, "Bad node");
+ printError(pNode->str_begin, pNode->str_end, "Bad node");
return NULL;
} else {
pNode->parent = parserNode;
@@ -1557,7 +1557,7 @@ ParserNode *parserFunctionCall(LexerNode *closing, LexerNode *begin,
ParserNode *pNode =
getUntilCommonParents(iter->parserNode, parent, parserNode);
if (pNode == NULL) {
- printLog(pNode->str_begin, pNode->str_end, "Bad node");
+ printError(pNode->str_begin, pNode->str_end, "Bad node");
return NULL;
} else {
pNode->parent = parserNode;
@@ -1640,7 +1640,7 @@ ParserNode *parserBracketsRight(LexerNode *closing, LexerNode *begin,
ParserNode *pNode =
getUntilCommonParents(iter->parserNode, parent, parserNode);
if (pNode == NULL) {
- printLog(pNode->str_begin, pNode->str_end, "Bad node");
+ printError(pNode->str_begin, pNode->str_end, "Bad node");
return NULL;
} else {
pNode->parent = parserNode;
@@ -1676,8 +1676,8 @@ ParserNode *parserBracketsLeft(LexerNode *closing, LexerNode *begin,
if (afterNode >= end || afterNode->parserNode == NULL ||
(after = getUntilCommonParent(afterNode->parserNode, parent)) == NULL ||
!isExpression(after)) {
- printLog(closing->str_begin, closing->str_end,
- "Bad bracket can't be parsed");
+ printError(closing->str_begin, closing->str_end,
+ "Bad bracket can't be parsed");
return NULL;
}
@@ -1694,7 +1694,7 @@ ParserNode *parserBracketsLeft(LexerNode *closing, LexerNode *begin,
ParserNode *pNode =
getUntilCommonParents(iter->parserNode, parent, parserNode);
if (pNode == NULL) {
- printLog(pNode->str_begin, pNode->str_end, "Bad node");
+ printError(pNode->str_begin, pNode->str_end, "Bad node");
return NULL;
} else {
pNode->parent = parserNode;
diff --git a/src/runner/runner.c b/src/runner/runner.c
index ef363fe..ab4e9b5 100644
--- a/src/runner/runner.c
+++ b/src/runner/runner.c
@@ -1676,13 +1676,12 @@ AstTree *runExpression(AstTree *expr, AstTreeScope *scope, bool *shouldRet,
}
case AST_TREE_TOKEN_OPERATOR_DEREFERENCE: {
AstTreeSingleChild *metadata = expr->metadata;
- AstTree *operand = runExpression(metadata, scope, shouldRet, false,
+ AstTree *operand = runExpression(metadata, scope, shouldRet, true,
isComptime, breakCount, shouldContinue);
if (discontinue(*shouldRet, *breakCount)) {
return operand;
}
- if (operand->token == AST_TREE_TOKEN_RAW_VALUE ||
- operand->token == AST_TREE_TOKEN_RAW_VALUE_NOT_OWNED) {
+ if (operand->token == AST_TREE_TOKEN_RAW_VALUE) {
if (operand->type->token != AST_TREE_TOKEN_OPERATOR_POINTER) {
printLog("%s", AST_TREE_TOKEN_STRINGS[operand->type->token]);
UNREACHABLE;
@@ -1694,6 +1693,16 @@ AstTree *runExpression(AstTree *expr, AstTreeScope *scope, bool *shouldRet,
memcpy(value, *(void **)operand->metadata, size);
astTreeDelete(operand);
return newAstTree(AST_TREE_TOKEN_RAW_VALUE, value, type, NULL, NULL);
+ } else if (operand->token == AST_TREE_TOKEN_RAW_VALUE_NOT_OWNED) {
+ if (operand->type->token != AST_TREE_TOKEN_OPERATOR_POINTER) {
+ printLog("%s", AST_TREE_TOKEN_STRINGS[operand->type->token]);
+ UNREACHABLE;
+ }
+ AstTree *type =
+ copyAstTree((AstTreeSingleChild *)operand->type->metadata);
+ AstTreeRawValue *value = *(void **)operand->metadata;
+ astTreeDelete(operand);
+ 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) {