summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-02-09 04:33:05 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-02-09 04:33:05 +0330
commit6a38b0247cff3be5ce2d0f725d559253357b4dc6 (patch)
tree1f305316cdc50fdb45c28eb0722effbb2491183f
parentde52585a1b2736a6a788ebc57000d7496f259e64 (diff)
fix leaks
fix printError
-rw-r--r--src/compiler/ast-tree.c10
-rw-r--r--src/compiler/lexer.c2
-rw-r--r--src/compiler/parser.c74
-rw-r--r--src/runner/runner.c4
-rw-r--r--src/utils/log.c9
-rw-r--r--src/utils/log.h4
6 files changed, 51 insertions, 52 deletions
diff --git a/src/compiler/ast-tree.c b/src/compiler/ast-tree.c
index 8325b7f..0eea23d 100644
--- a/src/compiler/ast-tree.c
+++ b/src/compiler/ast-tree.c
@@ -343,19 +343,13 @@ AstTree *newAstTree(AstTreeToken token, void *metadata, AstTree *type) {
AstTree *copyAstTree(AstTree *tree) {
switch (tree->token) {
case AST_TREE_TOKEN_TYPE_TYPE:
- return &AST_TREE_TYPE_TYPE;
case AST_TREE_TOKEN_TYPE_VOID:
- return &AST_TREE_VOID_TYPE;
case AST_TREE_TOKEN_TYPE_U64:
- return &AST_TREE_U64_TYPE;
case AST_TREE_TOKEN_TYPE_BOOL:
- return &AST_TREE_BOOL_TYPE;
+ return tree;
case AST_TREE_TOKEN_VALUE_U64:
- return newAstTree(tree->token, (void *)(AstTreeU64)tree->metadata,
- &AST_TREE_U64_TYPE);
case AST_TREE_TOKEN_VALUE_BOOL:
- return newAstTree(tree->token, (void *)(AstTreeBool)tree->metadata,
- &AST_TREE_BOOL_TYPE);
+ return newAstTree(tree->token, tree->metadata, tree->type);
case AST_TREE_TOKEN_VARIABLE:
return newAstTree(tree->token, tree->metadata, copyAstTree(tree->type));
case AST_TREE_TOKEN_TYPE_FUNCTION: {
diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c
index 67db9c7..52702ba 100644
--- a/src/compiler/lexer.c
+++ b/src/compiler/lexer.c
@@ -143,7 +143,7 @@ LexerNodeArray lexer(char *str) {
} else {
RETURN_ERROR:
free(result.data);
- printError("Unexpected character '%c'", iter, iter + 1, c);
+ printError(iter, iter + 1, "Unexpected character '%c'", c);
return LEXER_NODE_ARRAY_ERROR;
}
}
diff --git a/src/compiler/parser.c b/src/compiler/parser.c
index 7085ccb..f61836e 100644
--- a/src/compiler/parser.c
+++ b/src/compiler/parser.c
@@ -415,7 +415,7 @@ bool parserNodeArray(LexerNode *begin, LexerNode *end, ParserNode *parent) {
if (pNode->parent != parent) {
continue;
} else if (pNode == NULL) {
- printError("Bad child", pNode->str_begin, pNode->str_end);
+ printError(pNode->str_begin, pNode->str_end, "Bad child");
goto RETURN_ERROR;
}
for (size_t i = 0; i < parsedNodes->size; ++i) {
@@ -536,16 +536,16 @@ ParserNode *parserPrintU64(LexerNode *node, LexerNode *end,
ParserNode *parent) {
LexerNode *afterNode = node + 1;
if (afterNode >= end) {
- printError("No param", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "No param");
return NULL;
} else if (afterNode->parserNode == NULL) {
- printError("Bad param", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Bad param");
return NULL;
}
ParserNode *operand = getUntilCommonParent(afterNode->parserNode, parent);
if (operand == NULL) {
- printError("Bad param", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Bad param");
return NULL;
}
@@ -562,7 +562,7 @@ ParserNode *parserReturn(LexerNode *node, LexerNode *end, ParserNode *parent) {
} else {
operand = getUntilCommonParent(afterNode->parserNode, parent);
if (operand == NULL) {
- printError("No param", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "No param");
return NULL;
}
}
@@ -584,14 +584,14 @@ ParserNode *parserNumber(LexerNode *node, ParserNode *parent) {
switch (*node->str_begin) {
case '0':
if (node->str_end - node->str_begin > 1) {
- printError("Not implemented", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Not implemented");
return NULL;
}
default: {
bool success;
uint64_t value = decimalToU64(node->str_begin, node->str_end, &success);
if (!success) {
- printError("Error in parsing number", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Error in parsing number");
return NULL;
}
parserNode =
@@ -616,13 +616,13 @@ ParserNode *parserEol(LexerNode *node, LexerNode *begin, ParserNode *parent) {
if (nodeBefore < begin) {
parserNodeBefore = NULL;
} else if (nodeBefore->parserNode == NULL) {
- printError("Bad EOL after %s", node->str_begin, node->str_end,
+ printError(node->str_begin, node->str_end, "Bad EOL after %s",
LEXER_TOKEN_STRINGS[nodeBefore->token]);
return NULL;
} else {
parserNodeBefore = getUntilCommonParent(nodeBefore->parserNode, parent);
if (parserNodeBefore == NULL || !isExpression(parserNodeBefore)) {
- printError("Bad EOL after %s", node->str_begin, node->str_end,
+ printError(node->str_begin, node->str_end, "Bad EOL after %s",
LEXER_TOKEN_STRINGS[nodeBefore->token]);
return NULL;
}
@@ -641,13 +641,13 @@ ParserNode *parserComma(LexerNode *node, LexerNode *begin, ParserNode *parent) {
LexerNode *nodeBefore = node - 1;
ParserNode *parserNodeBefore;
if (nodeBefore < begin || nodeBefore->parserNode == NULL) {
- printError("Bad Comma after %s", node->str_begin, node->str_end,
+ printError(node->str_begin, node->str_end, "Bad Comma after %s",
LEXER_TOKEN_STRINGS[nodeBefore->token]);
return NULL;
} else {
parserNodeBefore = getUntilCommonParent(nodeBefore->parserNode, parent);
if (parserNodeBefore == NULL || !isExpression(parserNodeBefore)) {
- printError("Bad Comma after %s", node->str_begin, node->str_end,
+ printError(node->str_begin, node->str_end, "Bad Comma after %s",
LEXER_TOKEN_STRINGS[nodeBefore->token]);
return NULL;
}
@@ -680,8 +680,8 @@ ParserNode *parserParenthesis(LexerNode *closing, LexerNode *begin,
}
if (opening == NULL) {
- printError("No opening for parenthesis", closing->str_begin,
- closing->str_end);
+ printError(closing->str_begin, closing->str_end,
+ "No opening for parenthesis");
return NULL;
}
opening->parserNode = parserNode;
@@ -749,8 +749,8 @@ ParserNode *parserCurlyBrackets(LexerNode *closing, LexerNode *begin,
}
if (opening == NULL) {
- printError("No opening for curly brackets", closing->str_begin,
- closing->str_end);
+ printError(closing->str_begin, closing->str_end,
+ "No opening for curly brackets");
return NULL;
}
@@ -774,11 +774,11 @@ ParserNode *parserFunction(LexerNode *node, LexerNode *begin, LexerNode *end,
LexerNode *bodyNode = node + 2;
if (paramsNode < begin || paramsNode->parserNode == NULL) {
NO_PARAMS:
- printError("No params", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "No params");
return NULL;
} else if (retTypeNode >= end || retTypeNode->parserNode == NULL) {
NO_RETURN_TYPE:
- printError("No return type", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "No return type");
return NULL;
}
ParserNode *params = getUntilCommonParent(paramsNode->parserNode, parent);
@@ -790,14 +790,14 @@ ParserNode *parserFunction(LexerNode *node, LexerNode *begin, LexerNode *end,
} else {
body = getUntilCommonParent(bodyNode->parserNode, parent);
if (body == NULL || body->token != PARSER_TOKEN_SYMBOL_CURLY_BRACKET) {
- printError("Bad body", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Bad body");
return NULL;
}
ParserNodeArray *bodyArray = body->metadata;
for (size_t i = 0; i < bodyArray->size; ++i) {
if (bodyArray->data[i]->token != PARSER_TOKEN_SYMBOL_EOL) {
- printError("Maybe forgot a ; with %s", bodyArray->data[i]->str_begin,
- bodyArray->data[i]->str_end,
+ printError(bodyArray->data[i]->str_begin, bodyArray->data[i]->str_end,
+ "Maybe forgot a ; with %s",
PARSER_TOKEN_STRINGS[bodyArray->data[i]->token]);
return NULL;
}
@@ -809,7 +809,7 @@ ParserNode *parserFunction(LexerNode *node, LexerNode *begin, LexerNode *end,
} else if (!isType(retType)) {
goto NO_RETURN_TYPE;
} else if (!isAllArguments(params->metadata)) {
- printError("Bad arguments", params->str_begin, params->str_end);
+ printError(params->str_begin, params->str_end, "Bad arguments");
return NULL;
}
@@ -844,19 +844,20 @@ ParserNode *parserVariable(LexerNode *node, LexerNode *begin, LexerNode *end,
LexerNode *nameNode = node - 1;
if (nameNode < begin || nameNode->parserNode == NULL) {
- printError("No name", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "No name");
goto RETURN_ERROR;
}
ParserNode *name = getUntilCommonParent(nameNode->parserNode, parent);
if (name == NULL) {
- printError("Name should be identifier but got nothing", node->str_begin,
- node->str_end);
+ printError(node->str_begin, node->str_end,
+ "Name should be identifier but got nothing");
return NULL;
} else if (name->token != PARSER_TOKEN_IDENTIFIER) {
- printError("Name should be identifier but got '%s'", name->str_begin,
- name->str_end, PARSER_TOKEN_STRINGS[name->token]);
+ printError(name->str_begin, name->str_end,
+ "Name should be identifier but got '%s'",
+ PARSER_TOKEN_STRINGS[name->token]);
return NULL;
} else {
name->parent = variableNode;
@@ -870,7 +871,7 @@ ParserNode *parserVariable(LexerNode *node, LexerNode *begin, LexerNode *end,
ParserToken token = PARSER_TOKEN_VARIABLE;
if (node1 >= end) {
- printError("Bad variable definition", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Bad variable definition");
return NULL;
} else if (node1->token == LEXER_TOKEN_SYMBOL_COLON) {
type = NULL;
@@ -880,13 +881,14 @@ ParserNode *parserVariable(LexerNode *node, LexerNode *begin, LexerNode *end,
type = NULL;
node1->parserNode = variableNode;
} else if (node1->parserNode == NULL) {
- printError("Bad variable type with token '%s' %d", node1->str_begin,
- node1->str_end, LEXER_TOKEN_STRINGS[node1->token], node1->token);
+ printError(node1->str_begin, node1->str_end,
+ "Bad variable type with token '%s' %d",
+ LEXER_TOKEN_STRINGS[node1->token], node1->token);
return NULL;
} else {
type = getUntilCommonParent(node1->parserNode, parent);
if (type == NULL || !isType(type)) {
- printError("Bad variable type", node1->str_begin, node1->str_end);
+ printError(node1->str_begin, node1->str_end, "Bad variable type");
return NULL;
}
type->parent = variableNode;
@@ -908,12 +910,12 @@ ParserNode *parserVariable(LexerNode *node, LexerNode *begin, LexerNode *end,
if (node1 != NULL) {
LexerNode *valueNode = node1 + 1;
if (valueNode >= end || valueNode->parserNode == NULL) {
- printError("No value ", name->str_begin, name->str_end);
+ printError(name->str_begin, name->str_end, "No value");
return NULL;
} else {
value = getUntilCommonParent(valueNode->parserNode, parent);
if (!isValue(value)) {
- printError("No value ", name->str_begin, name->str_end);
+ printError(name->str_begin, name->str_end, "No value");
return NULL;
}
value->parent = variableNode;
@@ -947,7 +949,7 @@ ParserNode *parserAssign(LexerNode *node, LexerNode *begin, LexerNode *end,
LexerNode *rightNode = node + 1;
if (leftNode < begin || rightNode >= end) {
- printError("Bad assign", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Bad assign");
return NULL;
}
@@ -955,7 +957,7 @@ ParserNode *parserAssign(LexerNode *node, LexerNode *begin, LexerNode *end,
ParserNode *right = getUntilCommonParent(rightNode->parserNode, parent);
if (left == NULL || right == NULL) {
- printError("Bad assign", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Bad assign");
return NULL;
}
@@ -974,7 +976,7 @@ ParserNode *parserPlus(LexerNode *node, LexerNode *begin, LexerNode *end,
LexerNode *rightNode = node + 1;
if (leftNode < begin || rightNode >= end) {
- printError("Bad plus", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Bad plus");
return NULL;
}
@@ -982,7 +984,7 @@ ParserNode *parserPlus(LexerNode *node, LexerNode *begin, LexerNode *end,
ParserNode *right = getUntilCommonParent(rightNode->parserNode, parent);
if (left == NULL || right == NULL) {
- printError("Bad plus", node->str_begin, node->str_end);
+ printError(node->str_begin, node->str_end, "Bad plus");
return NULL;
}
diff --git a/src/runner/runner.c b/src/runner/runner.c
index 606fe89..dde5307 100644
--- a/src/runner/runner.c
+++ b/src/runner/runner.c
@@ -69,7 +69,9 @@ AstTree *runAstTreeFunction(AstTreeFunction *function, AstTree **arguments,
continue;
case AST_TREE_TOKEN_VARIABLE_DEFINE: {
AstTreeVariable *variable = expr.metadata;
- variable->value = calcAstTreeValue(variable->value);
+ AstTree *value = calcAstTreeValue(variable->value);
+ astTreeDelete(variable->value);
+ variable->value = value;
}
continue;
case AST_TREE_TOKEN_OPERATOR_SUM:
diff --git a/src/utils/log.c b/src/utils/log.c
index e1822c0..3f73729 100644
--- a/src/utils/log.c
+++ b/src/utils/log.c
@@ -17,8 +17,8 @@ void _printLogBack(const char *format, const char *file, int line, ...) {
free(errorStr);
}
-void _printErrorBack(const char *format, const char *file, int line,
- char *begin, char *end, ...) {
+void _printErrorBack(const char *file, int line, char *begin, char *end,
+ const char *format, ...) {
va_list args;
va_start(args, end);
char *errorStr;
@@ -51,7 +51,8 @@ void _printErrorBack(const char *format, const char *file, int line,
if (*iter == '\n') {
if (iter <= begin) {
file_line_begin = iter + 1;
- } else if (iter >= end) {
+ }
+ if (iter >= end && file_line_end == fileCodes[file_index]) {
file_line_end = iter;
}
if (iter <= end) {
@@ -71,5 +72,5 @@ void _printErrorBack(const char *format, const char *file, int line,
}
fprintf(stderr, "%c", *iter);
}
- fprintf(stderr, "\e[0m");
+ fprintf(stderr, "\e[0m\n");
}
diff --git a/src/utils/log.h b/src/utils/log.h
index 4057394..6cede24 100644
--- a/src/utils/log.h
+++ b/src/utils/log.h
@@ -1,10 +1,10 @@
#pragma once
#define printLog(format,...) _printLogBack(format, __FILE_NAME__, __LINE__, ## __VA_ARGS__)
-#define printError(format,begin,end,...) _printErrorBack(format, __FILE_NAME__, __LINE__, begin, end, ## __VA_ARGS__)
+#define printError(begin,end,format,...) _printErrorBack(__FILE_NAME__, __LINE__, begin, end, format, ## __VA_ARGS__)
#define UNREACHABLE printLog("Unreachable");exit(1)
extern void _printLogBack(const char *format, const char *file, int line, ...);
-extern void _printErrorBack(const char *format, const char *file, int line, char *begin, char *end, ...);
+extern void _printErrorBack(const char *file, int line, char *begin, char *end,const char *format, ...);