add if statements

This commit is contained in:
2025-06-03 13:24:12 +01:00
parent ef61c391a1
commit 18993a5d7e
16 changed files with 148 additions and 26 deletions

View File

@@ -1,12 +1,13 @@
#include "hashmap.h"
#include <stdlib.h>
#include "../memory.h"
struct table *createTable(int size)
{
struct table *t = (struct table *)malloc(sizeof(struct table));
struct table *t = (struct table *)checked_malloc(sizeof(struct table));
t->size = size;
t->list = (struct node **)malloc(sizeof(struct node *) * size);
t->list = (struct node **)checked_malloc(sizeof(struct node *) * size);
int i;
for (i = 0; i < size; i++)
t->list[i] = NULL;
@@ -48,7 +49,7 @@ void insert(struct table *t, int key, void* val)
{
int pos = hashCode(t, key);
struct node *list = t->list[pos];
struct node *newNode = (struct node *)malloc(sizeof(struct node));
struct node *newNode = (struct node *)checked_malloc(sizeof(struct node));
struct node *temp = list;
while (temp)
{

View File

@@ -1,9 +1,10 @@
#include "token.h"
#include "../string/string.h"
#include <stdlib.h>
#include "../memory.h"
Token *create_token(TokenType type, int line, int column, char *value) {
Token *token = malloc(sizeof(Token));
Token *token = checked_malloc(sizeof(Token));
token->type = type;
token->line = line;
token->column = column;

View File

@@ -2,9 +2,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../memory.h"
LinkedList *create_list(size_t data_size) {
LinkedList *list = malloc(sizeof(LinkedList));
LinkedList *list = checked_malloc(sizeof(LinkedList));
list->head = NULL;
list->data_size = data_size;
list->length = 0;
@@ -12,8 +13,8 @@ LinkedList *create_list(size_t data_size) {
}
void append(LinkedList *list, void *element) {
Node *new_node = malloc(sizeof(Node));
new_node->data = malloc(list->data_size);
Node *new_node = checked_malloc(sizeof(Node));
new_node->data = checked_malloc(list->data_size);
memcpy(new_node->data, element, list->data_size);
new_node->next = NULL;

View File

@@ -4,7 +4,16 @@
#include <gmp.h>
#include <stdlib.h> // for malloc/free (temp arena fallback)
#include <string.h>
#include <stdio.h>
void *checked_malloc(size_t size) {
void *ptr = malloc(size);
if (!ptr) {
fprintf(stderr, "fatal error: failed to allocate %zu bytes\n", size);
exit(EXIT_FAILURE);
}
return ptr;
}
void *gmp_gc_realloc(void *ptr, size_t old_size, size_t new_size) {
(void)old_size; // Ignore old_size, Boehm doesn't need it

View File

@@ -11,4 +11,6 @@ char *ar_strdup(const char *str);
// Memory init/shutdown
void ar_memory_init();
void *checked_malloc(size_t size);
#endif // ARGON_MEMORY_H

View File

@@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../memory.h"
ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens,
ParsedValue *assign_to, size_t *index) {
@@ -17,13 +18,14 @@ ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens,
token->column, ValueTypeNames[assign_to->type]);
exit(EXIT_FAILURE);
}
ParsedAssign *assign = malloc(sizeof(ParsedAssign));
ParsedAssign *assign = checked_malloc(sizeof(ParsedAssign));
assign->to = assign_to;
assign->type = token->type;
(*index)++;
error_if_finished(file,tokens,index);
token = darray_get(tokens, *index);
assign->from = parse_token(file, parsed, tokens, index, true);
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
parsedValue->type = AST_ASSIGN;
parsedValue->data = assign;
return parsedValue;

View File

@@ -1,12 +1,12 @@
#include "identifier.h"
#include "../../lexer/token.h"
#include "../parser.h"
#include <stdlib.h>
#include <string.h>
#include "../../memory.h"
ParsedValue *parse_identifier(Token *token) {
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
parsedValue->type = AST_IDENTIFIER;
parsedValue->data = strcpy(malloc(sizeof(token->value)), token->value);
parsedValue->data = strcpy(checked_malloc(sizeof(token->value)), token->value);
return parsedValue;
}

68
src/parser/if/if.c Normal file
View File

@@ -0,0 +1,68 @@
#include "if.h"
#include "../../lexer/token.h"
#include "../parser.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../memory.h"
ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens,
size_t *index) {
(*index)++;
error_if_finished(file, tokens, index);
Token *token = darray_get(tokens, *index);
if (token->type != TOKEN_LPAREN) {
fprintf(stderr,
"%s:%u:%u error: if statement requires paren for the condition\n",
file, token->line, token->column);
exit(EXIT_FAILURE);
}
(*index)++;
error_if_finished(file, tokens, index);
DArray *parsed_if = checked_malloc(sizeof(DArray));
darray_init(parsed_if, sizeof(ParsedConditional));
DArray *condition = checked_malloc(sizeof(DArray));
darray_init(condition, sizeof(ParsedValue));
while ((*index) < tokens->size) {
ParsedValue *parsed_code = parse_token(file, parsed, tokens, index, true);
if (parsed_code) {
darray_push(condition, parsed_code);
free(parsed_code);
}
token = darray_get(tokens, *index);
if (token->type == TOKEN_RPAREN)
break;
}
if (token->type != TOKEN_RPAREN) {
fprintf(stderr,
"%s:%u:%u error: missing closing parenthesis in if condition\n",
file, token->line, token->column);
exit(EXIT_FAILURE);
}
(*index)++;
error_if_finished(file, tokens, index);
ParsedValue *parsed_content = parse_token(file, parsed, tokens, index, true);
if (!parsed_content) {
fprintf(stderr, "%s:%u:%u error: expected body after if condition\n", file,
token->line, token->column);
exit(EXIT_FAILURE);
}
ParsedConditional output_conditional = {condition, parsed_content};
darray_push(parsed_if, &output_conditional);
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
parsedValue->type = AST_IF;
parsedValue->data = parsed_if;
return parsedValue;
}
void free_conditional(void *ptr) {
ParsedConditional *conditional = ptr;
darray_free(conditional->condition, free_parsed);
free_parsed(conditional->content);
}
void free_parsed_if(void *ptr) {
ParsedValue *parsedValue = ptr;
DArray *parsed_if = parsedValue->data;
darray_free(parsed_if, free_conditional);
}

18
src/parser/if/if.h Normal file
View File

@@ -0,0 +1,18 @@
// parser.h
#ifndef iF_H
#define iF_H
#include "../parser.h"
#include "../../lexer/token.h" // for Token
typedef struct {
DArray * condition;
ParsedValue * content;
} ParsedConditional;
ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens, size_t *index);
void free_parsed_if(void *ptr);
#endif // iF_H

View File

@@ -1,12 +1,12 @@
#include "number.h"
#include "../../lexer/token.h"
#include "../parser.h"
#include <stdlib.h>
#include "../../memory.h"
#include <gmp.h>
ParsedValue *parse_number(Token *token) {
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
mpz_t *number = malloc(sizeof(mpz_t));
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
mpz_t *number = checked_malloc(sizeof(mpz_t));
mpz_init_set_str(*number, token->value, 10);
parsedValue->type = AST_NUMBER;
parsedValue->data = number;

View File

@@ -2,8 +2,9 @@
#include "../dynamic_array/darray.h"
#include "../lexer/token.h"
#include "assign/assign.h"
#include "number/number.h"
#include "identifier/identifier.h"
#include "if/if.h"
#include "number/number.h"
#include "string/string.h"
#include <stdbool.h>
#include <stddef.h>
@@ -11,13 +12,24 @@
#include <stdlib.h>
#include <string.h>
const char *ValueTypeNames[] = {"string", "assign", "identifier", "number"};
const char *ValueTypeNames[] = {"string", "assign", "identifier", "number", "if statement"};
void error_if_finished(char *file, DArray *tokens, size_t *index) {
if ((*index) >= tokens->size) {
Token *token = darray_get(tokens, tokens->size - 1);
fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line,
token->column);
exit(EXIT_FAILURE);
}
}
ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
size_t *index, bool inline_flag) {
Token *token = darray_get(tokens, *index);
if (!inline_flag) {
switch (token->type) {
case TOKEN_IF:
return parse_if(file, parsed, tokens, index);
default:
break;
};
@@ -70,9 +82,9 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
token->column);
exit(EXIT_FAILURE);
}
ParsedValue *assigning_to = darray_get(parsed, parsed->size-1);
ParsedValue *assigning_to = darray_get(parsed, parsed->size - 1);
fprintf(stderr, "%s:%u:%u error: cannot assign to %s\n", file, token->line,
token->column, ValueTypeNames[assigning_to->type]);
token->column, ValueTypeNames[assigning_to->type]);
exit(EXIT_FAILURE);
case TOKEN_NUMBER:
(*index)++;
@@ -85,8 +97,7 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
void parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag) {
size_t index = 0;
size_t length = tokens->size;
while (index < length) {
while (index < tokens->size) {
ParsedValue *parsed_code =
parse_token(file, parsed, tokens, &index, inline_flag);
if (parsed_code) {
@@ -108,5 +119,8 @@ void free_parsed(void *ptr) {
break;
case AST_NUMBER:
break;
case AST_IF:
free_parsed_if(parsed);
break;
}
}

View File

@@ -12,6 +12,7 @@ typedef enum {
AST_ASSIGN,
AST_IDENTIFIER,
AST_NUMBER,
AST_IF,
} ValueType;
extern const char* ValueTypeNames[];
@@ -28,4 +29,6 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
void free_parsed(void *ptr);
void error_if_finished(char *file,DArray *tokens, size_t *index);
#endif // PARSER_H

View File

@@ -6,10 +6,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../memory.h"
char *swap_quotes(char *input, char quote) {
size_t len = strlen(input);
char *result = malloc(len + 1);
char *result = checked_malloc(len + 1);
if (!result)
return NULL;
@@ -73,7 +74,7 @@ char *unquote(char *str) {
}
ParsedValue *parse_string(Token token) {
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
parsedValue->type = AST_STRING;
parsedValue->data = unquote(token.value);
return parsedValue;

View File

@@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../memory.h"
const char *WHITE_SPACE = " \t\n\r\f\v";
@@ -12,7 +13,7 @@ char *cloneString(char *str) {
}
size_t len = strlen(str);
char *clone = malloc((len + 1) * sizeof(char));
char *clone = checked_malloc((len + 1) * sizeof(char));
if (clone == NULL) {
return NULL;

View File

@@ -2,9 +2,10 @@
#include "../dynamic_array/darray.h"
#include <stdint.h>
#include <stdlib.h>
#include "../memory.h"
Translated *init_translator() {
Translated *translated = malloc(sizeof(Translated));
Translated *translated = checked_malloc(sizeof(Translated));
if (!translated)
return NULL;

View File

@@ -1 +1 @@
x=10
if (x=10) "hello world"