add declaring and assignment with parser LHS support
This commit is contained in:
30
src/parser/declaration/declaration.c
Normal file
30
src/parser/declaration/declaration.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "declaration.h"
|
||||
#include "../../lexer/token.h"
|
||||
#include "../../memory.h"
|
||||
#include "../parser.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
ParsedValue *parse_declaration(char *file, DArray *parsed, DArray *tokens,
|
||||
size_t *index) {
|
||||
(*index)++;
|
||||
error_if_finished(file, tokens, index);
|
||||
Token *token = darray_get(tokens, *index);
|
||||
|
||||
ParsedValue * parsedValue = malloc(sizeof(ParsedValue));
|
||||
ParsedDeclaration * declaration = malloc(sizeof(ParsedDeclaration));
|
||||
parsedValue->data = declaration;
|
||||
parsedValue->type = AST_DECLARATION;
|
||||
|
||||
if (token->type != TOKEN_IDENTIFIER) {
|
||||
fprintf(stderr, "%s:%u:%u error: declaration requires an identifier\n",
|
||||
file, token->line, token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
declaration->name = strcpy(checked_malloc(sizeof(token->value)), token->value);
|
||||
(*index)++;
|
||||
if ((*index) >= tokens->size) return parsedValue;
|
||||
token = darray_get(tokens, *index);
|
||||
return parsedValue;
|
||||
}
|
||||
16
src/parser/declaration/declaration.h
Normal file
16
src/parser/declaration/declaration.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef DECLARATION_H
|
||||
#define DECLARATION_H
|
||||
#include "../parser.h"
|
||||
#include "../../lexer/token.h" // for Token
|
||||
|
||||
typedef struct {
|
||||
char * name;
|
||||
bool is_function;
|
||||
DArray args; // string[]
|
||||
ParsedValue * from;
|
||||
} ParsedDeclaration;
|
||||
|
||||
// Function declaration for parsing an identifier
|
||||
ParsedValue *parse_declaration(char *file, DArray *parsed, DArray *tokens, size_t *index);
|
||||
|
||||
#endif // DECLARATION_H
|
||||
Reference in New Issue
Block a user