Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ struct LexerResult runLexer(char* string) {
if(strcmp(buff, "func") == 0) {
pushToken(&result, FUNCTION);
}
else if(strcmp(buff, "asmf") == 0) {
pushToken(&result, ASM_FUNCTION);
}
else if(strcmp(buff, "true") == 0 || strcmp(buff, "false") == 0) {
pushToken(&result, BOOLEAN_VALUE);
result.tokens[result.size - 1].value = buff;
Expand Down
1 change: 1 addition & 0 deletions src/lexer/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define TOKENS_H

enum TokenType {
ASM_FUNCTION,
FUNCTION,
RETURN,
VAR,
Expand Down
2 changes: 2 additions & 0 deletions src/parser/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enum ASTNodeType {
AST_VARIABLE_DECLARATION,
AST_VARIABLE_REFERENCE,

AST_ASM_FUNCTION_DECLARATION,

AST_FUNCTION_DECLARATION,
AST_FUNCTION_HEADER,

Expand Down
54 changes: 54 additions & 0 deletions src/parser/asts/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Function-related AST parsing.
*/

#include <stdlib.h>

#include "./variables.h"

#include "../parser.h"
Expand All @@ -10,6 +12,8 @@
#include "../../lexer/tokens.h"
#include "../../lexer/lexer.h"

#include "../../utils/logging.c"

/**
* Parse the parameters from a function definition (for example).
* @param result the lexer result.
Expand Down Expand Up @@ -138,3 +142,53 @@ AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index) {

return node;
}

AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) {
AST_NODE* node = createASTNode(AST_ASM_FUNCTION_DECLARATION);

node->left = createASTNode(AST_FUNCTION_HEADER);

if(result.tokens[index].type != KEYWORD) {
return NULL;
}

AST_NODE* params = parseParameters(result, index + 1);

if(params == NULL) return NULL;

node->left->left = params;

index = params->endingIndex;

int buffSize = 32;
int buffIndex = 0;
char* buff = malloc(buffSize);

Comment on lines +165 to +166
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Check for malloc failure to ensure robust memory allocation

The malloc call for buff does not check if the allocation was successful. If malloc returns NULL, subsequent operations on buff will result in undefined behavior. Add a check to handle allocation failure.

Apply this diff to handle malloc failure:

     char* buff = malloc(buffSize);
+    if (buff == NULL) {
+        // Handle allocation failure
+        return NULL;
+    }

Committable suggestion skipped: line range outside the PR's diff.

for(; index <= result.size; ++index) {
struct Token t = result.tokens[index];
Comment on lines +167 to +168
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Correct loop boundary condition to prevent out-of-bounds access

The loop condition index <= result.size may cause an out-of-bounds access on result.tokens[index] when index == result.size. Since array indices start from 0, the last valid index is result.size - 1.

Apply this diff to fix the loop condition:

-    for(; index <= result.size; ++index) {
+    for(; index < result.size; ++index) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for(; index <= result.size; ++index) {
struct Token t = result.tokens[index];
for(; index < result.size; ++index) {
struct Token t = result.tokens[index];


if(t.type == BRACKETS_CLOSE) {
break;
}

if(t.type != STRING) {
return NULL;
}

char c;
while(c = *t.value++) {
buff[buffIndex] = c;
Comment on lines +178 to +180
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid modifying the original token value pointer

The line while(c = *t.value++) advances t.value, which might affect subsequent operations that rely on t.value. To prevent unintended side effects, use a separate pointer to iterate over the string.

Apply this diff to refactor the code:

-        while(c = *t.value++) {
+        char* valPtr = t.value;
+        while((c = *valPtr++)) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
char c;
while(c = *t.value++) {
buff[buffIndex] = c;
char c;
char* valPtr = t.value;
while((c = *valPtr++)) {
buff[buffIndex] = c;

buffIndex++;

if(buffIndex >= buffSize) {
buffSize *= 1.5;
buff = realloc(buff, buffSize);
}
Comment on lines +183 to +186
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix potential memory leak when reallocating buffer

If realloc fails, it returns NULL and the original memory is not freed, leading to a memory leak. Assigning the result of realloc directly to buff without checking for NULL could overwrite the original pointer. Use a temporary pointer to check the result before assigning it back to buff.

Apply this diff to fix the issue:

-        buff = realloc(buff, buffSize);
+        char* temp = realloc(buff, buffSize);
+        if (temp == NULL) {
+            free(buff);
+            return NULL;
+        }
+        buff = temp;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if(buffIndex >= buffSize) {
buffSize *= 1.5;
buff = realloc(buff, buffSize);
}
if(buffIndex >= buffSize) {
buffSize *= 1.5;
char* temp = realloc(buff, buffSize);
if (temp == NULL) {
free(buff);
return NULL;
}
buff = temp;
}
🧰 Tools
🪛 cppcheck (2.10-2)

[error] 185-185: Common realloc mistake

(memleakOnRealloc)

}
}

buff[buffIndex] = '\0';

node->value = buff;
return node;
}
7 changes: 7 additions & 0 deletions src/parser/asts/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ AST_NODE* parseArguments(struct LexerResult result, int index);
*/
AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index);

/**
* Parses an ASM function declaration.
* @param result the lexer result.
* @param index the starting index of the parsing.
*/
AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index);

#endif
10 changes: 9 additions & 1 deletion src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType type
index = node->endingIndex;
}
break;

case ASM_FUNCTION:
node = parseASMFunctionDeclaration(result, index);
if(node != NULL) {
current->next = node;
current = node;
index = node->endingIndex;
}
break;

}
}

Expand Down
Loading