Skip to content

Commit 6483486

Browse files
committed
G67
1 parent 585162b commit 6483486

File tree

12 files changed

+421
-252
lines changed

12 files changed

+421
-252
lines changed

cppcheck_report.txt

Lines changed: 365 additions & 0 deletions
Large diffs are not rendered by default.

src/bindings/nodejs.c

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,90 +13,59 @@
1313
// Maximum input size to prevent buffer overflows
1414
#define MAX_INPUT_SIZE (1024 * 1024) // 1MB limit
1515

16-
// Fixed function signature to match Node.js FFI binding
1716
const char* compile_ggcode_from_string(const char* source_code) {
1817
clock_t start_time = clock();
19-
20-
// Input validation
2118
if (!source_code) {
2219
return strdup("; ERROR: NULL input\n");
2320
}
24-
25-
// Check input length to prevent buffer overflows
2621
size_t input_len = strlen(source_code);
2722
if (input_len == 0) {
2823
return strdup("; EMPTY INPUT\n");
2924
}
30-
3125
if (input_len > MAX_INPUT_SIZE) {
3226
return strdup("; ERROR: Input too large (max 1MB)\n");
3327
}
34-
35-
// Initialize runtime state properly (no global contamination)
3628
init_runtime();
3729
Runtime* runtime = get_runtime();
38-
39-
runtime->statement_count = 0; // Use runtime state instead of global
40-
41-
// Initialize output buffer
30+
runtime->statement_count = 0;
4231
init_output_buffer();
43-
44-
// Parse and compile
4532
ASTNode* root = parse_script_from_string(source_code);
46-
4733
if (!root) {
4834
reset_runtime_state();
4935
free_output_buffer();
5036
return strdup("; ERROR: Parsing failed\n");
5137
}
52-
53-
// Set runtime state variables for note block parsing BEFORE emit_gcode
5438
char ggcode_file_name[64];
5539
snprintf(ggcode_file_name, sizeof(ggcode_file_name), "nodejs.ggcode");
56-
5740
Runtime *rt = get_runtime();
5841
strncpy(rt->RUNTIME_FILENAME, ggcode_file_name, sizeof(rt->RUNTIME_FILENAME) - 1);
5942
rt->RUNTIME_FILENAME[sizeof(rt->RUNTIME_FILENAME) - 1] = '\0';
60-
61-
// Set current time
6243
char time_line[64];
6344
time_t now = time(NULL);
6445
struct tm *t = localtime(&now);
6546
strftime(time_line, sizeof(time_line), "%Y-%m-%d %H:%M:%S", t);
6647
strncpy(rt->RUNTIME_TIME, time_line, sizeof(rt->RUNTIME_TIME) - 1);
6748
rt->RUNTIME_TIME[sizeof(rt->RUNTIME_TIME) - 1] = '\0';
68-
6949
emit_gcode(root);
70-
71-
// Generate G-code header
7250
emit_gcode_preamble(ggcode_file_name);
73-
74-
// Get output and check for errors
7551
const char* output = strdup(get_output_buffer());
7652
long output_size = get_output_length();
77-
7853
if (has_errors()) {
7954
report_error("[NodeJS] Compilation failed or errors detected");
8055
print_errors();
8156
clear_errors();
82-
free((void*)output); // Free the output we just allocated
57+
free((void*)output);
8358
reset_runtime_state();
8459
free_ast(root);
8560
free_output_buffer();
8661
return strdup("; ERROR: Compilation failed\n");
8762
}
88-
89-
// Print basic compilation report for successful compilation
9063
clock_t end_time = clock();
9164
double elapsed = ((double)(end_time - start_time)) / CLOCKS_PER_SEC * 1000.0;
92-
fprintf(stderr, "GGcode: Compiled %zu bytes → %ld bytes in %.2fms\n",
93-
input_len, output_size, elapsed);
94-
95-
// Cleanup
65+
fprintf(stderr, "GGcode: Compiled %zu bytes \u2192 %ld bytes in %.2fms\n", input_len, output_size, elapsed);
9666
reset_runtime_state();
9767
free_ast(root);
9868
free_output_buffer();
99-
10069
return output;
10170
}
10271

src/config/config.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ Runtime* get_runtime() {
3535
return &g_runtime;
3636
}
3737

38-
void set_input_file(const char* filename) {
39-
input_file = filename;
40-
}
41-
4238
const char* get_input_file() {
4339
return input_file;
4440
}

src/generator/emitter.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ Value *make_number_value(double num)
5656
return val;
5757
}
5858

59-
int get_statement_count()
60-
{
61-
Runtime *rt = get_runtime();
62-
return rt->statement_count;
63-
}
64-
6559
// Global flag to reset emitter state
6660
static int emitter_reset_flag = 0;
6761

src/lexer/token_utils.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,3 @@ Token make_token(Token_Type type, const char *value, int line, int column) {
2424

2525

2626

27-
/// @brief Free memory allocated for a token's value
28-
void token_free(Token token) {
29-
if (token.value) {
30-
free(token.value);
31-
}
32-
}
33-
34-

src/parser/ast_helpers.c

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,4 @@
33
#include <string.h>
44
#include "../error/error.h"
55

6-
// Helper functions for common AST node creation patterns
7-
8-
ASTNode* create_number_node(double value) {
9-
ASTNode *ast_node = malloc(sizeof(ASTNode));
10-
if (!ast_node) {
11-
report_error("[AST] malloc failed for ASTNode");
12-
return NULL;
13-
}
14-
ast_node->type = AST_NUMBER;
15-
ast_node->parent = NULL;
16-
ast_node->number.value = value;
17-
return ast_node;
18-
}
19-
20-
ASTNode* create_var_node(const char* name) {
21-
ASTNode *ast_node = malloc(sizeof(ASTNode));
22-
if (!ast_node) {
23-
report_error("[AST] malloc failed for ASTNode");
24-
return NULL;
25-
}
26-
ast_node->type = AST_VAR;
27-
ast_node->parent = NULL;
28-
ast_node->var.name = strdup(name);
29-
if (!ast_node->var.name) {
30-
report_error("[AST] strdup failed for variable name");
31-
free(ast_node);
32-
return NULL;
33-
}
34-
return ast_node;
35-
}
36-
37-
ASTNode* create_binary_node(Token_Type op, ASTNode* left, ASTNode* right) {
38-
ASTNode *ast_node = malloc(sizeof(ASTNode));
39-
if (!ast_node) {
40-
report_error("[AST] malloc failed for ASTNode");
41-
return NULL;
42-
}
43-
ast_node->type = AST_BINARY;
44-
ast_node->parent = NULL;
45-
ast_node->binary_expr.op = op;
46-
ast_node->binary_expr.left = left;
47-
ast_node->binary_expr.right = right;
48-
return ast_node;
49-
}
50-
51-
ASTNode* create_unary_node(Token_Type op, ASTNode* operand) {
52-
ASTNode *ast_node = malloc(sizeof(ASTNode));
53-
if (!ast_node) {
54-
report_error("[AST] malloc failed for ASTNode");
55-
return NULL;
56-
}
57-
ast_node->type = AST_UNARY;
58-
ast_node->parent = NULL;
59-
ast_node->unary_expr.op = op;
60-
ast_node->unary_expr.operand = operand;
61-
return ast_node;
62-
}
63-
64-
ASTNode* create_call_node(const char* name, ASTNode** args, int arg_count) {
65-
ASTNode *ast_node = malloc(sizeof(ASTNode));
66-
if (!ast_node) {
67-
report_error("[AST] malloc failed for ASTNode");
68-
return NULL;
69-
}
70-
ast_node->type = AST_CALL;
71-
ast_node->parent = NULL;
72-
ast_node->call_expr.name = strdup(name);
73-
if (!ast_node->call_expr.name) {
74-
report_error("[AST] strdup failed for function name");
75-
free(ast_node);
76-
return NULL;
77-
}
78-
ast_node->call_expr.args = args;
79-
ast_node->call_expr.arg_count = arg_count;
80-
return ast_node;
81-
}
82-
83-
ASTNode* create_array_literal_node(ASTNode** elements, int count) {
84-
ASTNode *ast_node = malloc(sizeof(ASTNode));
85-
if (!ast_node) {
86-
report_error("[AST] malloc failed for ASTNode");
87-
return NULL;
88-
}
89-
ast_node->type = AST_ARRAY_LITERAL;
90-
ast_node->parent = NULL;
91-
ast_node->array_literal.elements = elements;
92-
ast_node->array_literal.count = count;
93-
return ast_node;
94-
}
95-
96-
ASTNode* create_block_node(ASTNode** statements, int count) {
97-
ASTNode *ast_node = malloc(sizeof(ASTNode));
98-
if (!ast_node) {
99-
report_error("[AST] malloc failed for ASTNode");
100-
return NULL;
101-
}
102-
ast_node->type = AST_BLOCK;
103-
ast_node->parent = NULL;
104-
ast_node->block.statements = statements;
105-
ast_node->block.count = count;
106-
return ast_node;
107-
}
6+
// Helper functions for common AST node creation patterns

src/parser/parser.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313
#define PARSE_ERROR(msg, ...) \
1414
fatal_error(get_runtime()->parser.lexer->source, get_runtime()->parser.current.line, get_runtime()->parser.current.column, msg, ##__VA_ARGS__)
1515

16-
static int gcode_mode_active = 0;
1716
// Parser moved to runtime state - no more global parser
1817

19-
// Function to reset parser static variables
20-
void reset_parser_static_vars(void) {
21-
gcode_mode_active = 0;
22-
}
23-
2418
static ASTNode *parse_binary_expression();
2519
static ASTNode *parse_block();
2620
static ASTNode *parse_while();
@@ -38,8 +32,9 @@ static ASTNode *parse_return();
3832
static ASTNode *parse_postfix_expression(); // <-- add this
3933

4034
// Forward declaration
41-
Token lexer_peek_token(Lexer *lexer);
4235

36+
// Restore static variable needed by parse_gcode
37+
static int gcode_mode_active = 0;
4338

4439
/// @brief step 2
4540
/// @return
@@ -1149,15 +1144,3 @@ case AST_EXPR_STMT:
11491144

11501145
free(node);
11511146
}
1152-
1153-
Token lexer_peek_token(Lexer *lexer)
1154-
{
1155-
int saved_pos = lexer->pos;
1156-
int saved_column = lexer->column;
1157-
1158-
Token next = lexer_next_token(lexer);
1159-
1160-
lexer->pos = saved_pos;
1161-
lexer->column = saved_column;
1162-
return next;
1163-
}

0 commit comments

Comments
 (0)