add identifier to parser

This commit is contained in:
2025-06-01 02:28:27 +01:00
parent b7e9493171
commit d2518afb8e
20 changed files with 733 additions and 251 deletions

View File

@@ -1,5 +1,5 @@
#include "lex.yy.h"
#include "lexer.h"
#include "lex.yy.h"
void lexer(LexerState state) {
yyscan_t scanner;
@@ -12,12 +12,9 @@ void lexer(LexerState state) {
int token;
while ((token = yylex(scanner)) != 0) {
Token * token_struct = create_token(
token,
yyget_lineno(scanner),
state.current_column+1,
yyget_text(scanner)
);
Token *token_struct =
create_token(token, yyget_lineno(scanner), state.current_column + 1,
yyget_text(scanner));
darray_push(state.tokens, token_struct);
free(token_struct);
if (token == TOKEN_NEW_LINE) {

View File

@@ -1,8 +1,8 @@
#ifndef LEXER_H
#define LEXER_H
#include "token.h"
#include "../dynamic_array/darray.h"
#include "token.h"
#include <stdio.h>
typedef struct {
@@ -15,5 +15,4 @@ typedef struct {
void lexer(LexerState state);
#endif // LEXER_H

View File

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

View File

@@ -1,7 +1,7 @@
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
LinkedList *create_list(size_t data_size) {
LinkedList *list = malloc(sizeof(LinkedList));
@@ -21,14 +21,16 @@ void append(LinkedList *list, void *element) {
list->head = new_node;
} else {
Node *temp = list->head;
while (temp->next) temp = temp->next;
while (temp->next)
temp = temp->next;
temp->next = new_node;
}
list->length++;
}
void *get_element_at(LinkedList *list, size_t index) {
if (index >= list->length) return NULL;
if (index >= list->length)
return NULL;
Node *current = list->head;
for (size_t i = 0; i < index; ++i)
@@ -38,7 +40,8 @@ void *get_element_at(LinkedList *list, size_t index) {
}
int set_element_at(LinkedList *list, size_t index, void *element) {
if (index >= list->length) return 0;
if (index >= list->length)
return 0;
Node *current = list->head;
for (size_t i = 0; i < index; ++i)
@@ -49,7 +52,8 @@ int set_element_at(LinkedList *list, size_t index, void *element) {
}
int remove_at(LinkedList *list, size_t index) {
if (index >= list->length) return 0;
if (index >= list->length)
return 0;
Node *temp = list->head;
Node *prev = NULL;
@@ -70,9 +74,7 @@ int remove_at(LinkedList *list, size_t index) {
return 1;
}
size_t list_length(LinkedList *list) {
return list->length;
}
size_t list_length(LinkedList *list) { return list->length; }
void print_list(LinkedList *list, void (*print_func)(void *)) {
Node *current = list->head;

View File

@@ -1,26 +1,23 @@
#include "dynamic_array/darray.h"
#include "lexer/lexer.h"
#include "lexer/token.h"
#include "parser/parser.h"
#include "memory.h"
#include "dynamic_array/darray.h"
#include "parser/parser.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
int main() {
int main(int argc, char *argv[]) {
if (argc <= 1)
return -1;
ar_memory_init();
char * path = "test.ar";
char *path = argv[1];
DArray tokens;
darray_init(&tokens, sizeof(Token));
LexerState state = {
path,
fopen(path, "r"),
0,
&tokens
};
LexerState state = {path, fopen(path, "r"), 0, &tokens};
lexer(state);
fclose(state.file);
@@ -28,7 +25,6 @@ int main() {
darray_init(&ast, sizeof(ParsedValue));
parser(path, &ast, &tokens, false);
darray_free(&tokens, free_token);

View File

@@ -1,19 +1,13 @@
#include "memory.h"
#include <gc.h>
#include <string.h>
#include <stdlib.h> // for malloc/free (temp arena fallback)
#include <string.h>
void ar_memory_init() {
GC_INIT();
}
void ar_memory_init() { GC_INIT(); }
void* ar_alloc(size_t size) {
return GC_MALLOC(size);
}
void *ar_alloc(size_t size) { return GC_MALLOC(size); }
void* ar_alloc_atomic(size_t size) {
return GC_MALLOC_ATOMIC(size);
}
void *ar_alloc_atomic(size_t size) { return GC_MALLOC_ATOMIC(size); }
char *ar_strdup(const char *str) {
size_t len = strlen(str) + 1;

View File

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

View File

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

View File

@@ -2,6 +2,7 @@
#include "../dynamic_array/darray.h"
#include "../lexer/token.h"
#include "assign/assign.h"
#include "identifier/identifier.h"
#include "string/string.h"
#include <stdbool.h>
#include <stddef.h>
@@ -23,11 +24,11 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
(*index)++;
return parse_string(*token);
case TOKEN_NEW_LINE:
while (token->type == TOKEN_NEW_LINE && *index+1 <= tokens->size) {
(*index)++;
while (token->type == TOKEN_NEW_LINE && ++(*index) < tokens->size) {
token = darray_get(tokens, *index);
}
if (token->type == TOKEN_NEW_LINE) return NULL;
if (token->type == TOKEN_NEW_LINE)
return NULL;
return parse_token(file, parsed, tokens, index, inline_flag);
case TOKEN_INDENT:
fprintf(stderr, "%s:%u:%u error: invalid indentation\n", file, token->line,
@@ -47,9 +48,13 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
exit(EXIT_FAILURE);
}
ParsedValue *assign_to = malloc(sizeof(ParsedValue));
memcpy(assign_to, darray_get(parsed, parsed->size-1), sizeof(ParsedValue));
memcpy(assign_to, darray_get(parsed, parsed->size - 1),
sizeof(ParsedValue));
darray_resize(parsed, parsed->size - 1);
return parse_assign(file, parsed, tokens, assign_to, index);
case TOKEN_IDENTIFIER:
(*index)++;
return parse_identifier(token);
default:
fprintf(stderr, "Panic: unreachable\n");
exit(EXIT_FAILURE);
@@ -72,6 +77,7 @@ void parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag) {
void free_parsed(void *ptr) {
ParsedValue *parsed = ptr;
switch (parsed->type) {
case AST_IDENTIFIER:
case AST_STRING:
free(parsed->data);
break;

View File

@@ -1,29 +1,28 @@
#ifndef PARSER_H
#define PARSER_H
#include "../dynamic_array/darray.h"
#include <stdbool.h>
#include <stddef.h>
#include "../dynamic_array/darray.h"
typedef struct LinkedList LinkedList;
typedef enum {
AST_STRING,
AST_ASSIGN,
AST_IDENTIFIER,
} ValueType;
typedef struct {
ValueType type;
void *data;
} ParsedValue;
void parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag);
ParsedValue *parse_token(char*file,DArray *parsed, DArray *tokens, size_t *index, bool inline_flag);
ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
size_t *index, bool inline_flag);
void free_parsed(void *ptr);
#endif // PARSER_H

View File

@@ -42,6 +42,7 @@ char *unquote(char *str) {
cJSON *json = cJSON_Parse(str);
if (!json || !cJSON_IsString(json)) {
cJSON_Delete(json);
if (swapped)
free(swapped);
return NULL;
@@ -73,7 +74,6 @@ char *unquote(char *str) {
ParsedValue *parse_string(Token token) {
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
parsedValue->type = AST_STRING;
parsedValue->data = unquote(token.value);
return parsedValue;

View File

@@ -6,18 +6,15 @@
const char *WHITE_SPACE = " \t\n\r\f\v";
char *cloneString(char *str)
{
if (str == NULL)
{
char *cloneString(char *str) {
if (str == NULL) {
return NULL;
}
size_t len = strlen(str);
char *clone = malloc((len + 1) * sizeof(char));
if (clone == NULL)
{
if (clone == NULL) {
return NULL;
}
@@ -25,46 +22,37 @@ char *cloneString(char *str)
return clone;
}
void stripString(char *str, const char *chars)
{
if (str == NULL || chars == NULL)
{
void stripString(char *str, const char *chars) {
if (str == NULL || chars == NULL) {
return;
}
size_t len = strlen(str);
size_t charsLen = strlen(chars);
if (len == 0 || charsLen == 0)
{
if (len == 0 || charsLen == 0) {
return;
}
size_t i = 0;
while (i < len)
{
if (strchr(chars, str[i]) == NULL)
{
while (i < len) {
if (strchr(chars, str[i]) == NULL) {
break;
}
i++;
}
if (i > 0)
{
if (i > 0) {
memmove(str, str + i, len - i + 1);
}
size_t j = len - i - 1;
while (j > 0)
{
if (strchr(chars, str[j]) == NULL)
{
while (j > 0) {
if (strchr(chars, str[j]) == NULL) {
break;
}
j--;
}
if (j < len)
{
if (j < len) {
str[j + 1] = '\0';
}

View File

@@ -7,5 +7,4 @@ char* cloneString(char* str);
void stripString(char *str, const char *chars);
#endif // CLONESTRING_H

View File

@@ -1,11 +1,12 @@
#include "translator.h"
#include "../dynamic_array/darray.h"
#include <stdlib.h>
#include <stdint.h>
#include <stdlib.h>
Translated *init_translator() {
Translated *translated = malloc(sizeof(Translated));
if (!translated) return NULL;
if (!translated)
return NULL;
darray_init(&translated->bytecode, sizeof(uint8_t));
return translated;

View File

@@ -1,14 +1,10 @@
#ifndef TRANSLATOR_H
#define TRANSLATOR_H
#include <stddef.h>
#include "../dynamic_array/darray.h"
#include <stddef.h>
typedef enum {
OP_INIT_STRING
} OperationType;
typedef enum { OP_INIT_STRING } OperationType;
typedef struct {
size_t registerCount;

482
test.ar
View File

@@ -1 +1,483 @@
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
x="hello world"
"hello world"