add numbers to parser

This commit is contained in:
2025-06-03 02:52:15 +01:00
parent e4c2af3cc7
commit ef61c391a1
7 changed files with 64 additions and 7 deletions

View File

@@ -3,7 +3,7 @@ LEXER_C = src/lexer/lex.yy.c
LEXER_H = src/lexer/lex.yy.h LEXER_H = src/lexer/lex.yy.h
CFILES = $(shell find src -name '*.c') CFILES = $(shell find src -name '*.c')
CFLAGS = -lm -lcjson -lgc -Wall -Wextra -Wno-unused-function -s CFLAGS = -lm -lcjson -lgc -lgmp -Wall -Wextra -Wno-unused-function -s
BINARY = bin/argon BINARY = bin/argon
all: $(BINARY) all: $(BINARY)

View File

@@ -1,9 +1,28 @@
#include "memory.h" #include "memory.h"
#include <gc.h> #include <gc.h>
#include <gc/gc.h>
#include <gmp.h>
#include <stdlib.h> // for malloc/free (temp arena fallback) #include <stdlib.h> // for malloc/free (temp arena fallback)
#include <string.h> #include <string.h>
void ar_memory_init() { GC_INIT(); }
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
return GC_realloc(ptr, new_size);
}
void gmp_gc_free(void *ptr, size_t size) {
(void)size; // Boehm GC manages this itself
// No-op — memory will be collected automatically
GC_FREE(ptr);
}
void ar_memory_init() {
GC_INIT();
mp_set_memory_functions(GC_malloc, gmp_gc_realloc, gmp_gc_free);
}
void *ar_alloc(size_t size) { return GC_MALLOC(size); } void *ar_alloc(size_t size) { return GC_MALLOC(size); }

View File

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

View File

@@ -0,0 +1,11 @@
// parser.h
#ifndef NUMBER_H
#define NUMBER_H
#include "../parser.h"
#include "../../lexer/token.h" // for Token
// Function declaration for parsing an identifier
ParsedValue * parse_number(Token * token);
#endif // NUMBER_H

View File

@@ -2,6 +2,7 @@
#include "../dynamic_array/darray.h" #include "../dynamic_array/darray.h"
#include "../lexer/token.h" #include "../lexer/token.h"
#include "assign/assign.h" #include "assign/assign.h"
#include "number/number.h"
#include "identifier/identifier.h" #include "identifier/identifier.h"
#include "string/string.h" #include "string/string.h"
#include <stdbool.h> #include <stdbool.h>
@@ -10,7 +11,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
const char *ValueTypeNames[] = {"string", "assign", "identifier"}; const char *ValueTypeNames[] = {"string", "assign", "identifier", "number"};
ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens, ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
size_t *index, bool inline_flag) { size_t *index, bool inline_flag) {
@@ -64,9 +65,18 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
case TOKEN_ASSIGN_PLUS: case TOKEN_ASSIGN_PLUS:
case TOKEN_ASSIGN_SLASH: case TOKEN_ASSIGN_SLASH:
case TOKEN_ASSIGN_STAR: case TOKEN_ASSIGN_STAR:
if (parsed->size == 0) {
fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line, fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line,
token->column); token->column);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
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]);
exit(EXIT_FAILURE);
case TOKEN_NUMBER:
(*index)++;
return parse_number(token);
default: default:
fprintf(stderr, "Panic: unreachable\n"); fprintf(stderr, "Panic: unreachable\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -96,5 +106,7 @@ void free_parsed(void *ptr) {
case AST_ASSIGN: case AST_ASSIGN:
free_parse_assign(parsed); free_parse_assign(parsed);
break; break;
case AST_NUMBER:
break;
} }
} }

View File

@@ -10,7 +10,8 @@ typedef struct LinkedList LinkedList;
typedef enum { typedef enum {
AST_STRING, AST_STRING,
AST_ASSIGN, AST_ASSIGN,
AST_IDENTIFIER AST_IDENTIFIER,
AST_NUMBER,
} ValueType; } ValueType;
extern const char* ValueTypeNames[]; extern const char* ValueTypeNames[];

View File

@@ -1 +1 @@
"hello world"=10 x=10