add numbers to parser
This commit is contained in:
2
Makefile
2
Makefile
@@ -3,7 +3,7 @@ LEXER_C = src/lexer/lex.yy.c
|
||||
LEXER_H = src/lexer/lex.yy.h
|
||||
|
||||
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
|
||||
|
||||
all: $(BINARY)
|
||||
|
||||
21
src/memory.c
21
src/memory.c
@@ -1,9 +1,28 @@
|
||||
#include "memory.h"
|
||||
#include <gc.h>
|
||||
#include <gc/gc.h>
|
||||
#include <gmp.h>
|
||||
#include <stdlib.h> // for malloc/free (temp arena fallback)
|
||||
#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); }
|
||||
|
||||
|
||||
14
src/parser/number/number.c
Normal file
14
src/parser/number/number.c
Normal 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;
|
||||
}
|
||||
11
src/parser/number/number.h
Normal file
11
src/parser/number/number.h
Normal 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
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "../dynamic_array/darray.h"
|
||||
#include "../lexer/token.h"
|
||||
#include "assign/assign.h"
|
||||
#include "number/number.h"
|
||||
#include "identifier/identifier.h"
|
||||
#include "string/string.h"
|
||||
#include <stdbool.h>
|
||||
@@ -10,7 +11,7 @@
|
||||
#include <stdlib.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,
|
||||
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_SLASH:
|
||||
case TOKEN_ASSIGN_STAR:
|
||||
if (parsed->size == 0) {
|
||||
fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line,
|
||||
token->column);
|
||||
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:
|
||||
fprintf(stderr, "Panic: unreachable\n");
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -96,5 +106,7 @@ void free_parsed(void *ptr) {
|
||||
case AST_ASSIGN:
|
||||
free_parse_assign(parsed);
|
||||
break;
|
||||
case AST_NUMBER:
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,8 @@ typedef struct LinkedList LinkedList;
|
||||
typedef enum {
|
||||
AST_STRING,
|
||||
AST_ASSIGN,
|
||||
AST_IDENTIFIER
|
||||
AST_IDENTIFIER,
|
||||
AST_NUMBER,
|
||||
} ValueType;
|
||||
|
||||
extern const char* ValueTypeNames[];
|
||||
|
||||
Reference in New Issue
Block a user