add list support to parser
This commit is contained in:
@@ -50,6 +50,8 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
|
|||||||
token = darray_get(tokens, *index);
|
token = darray_get(tokens, *index);
|
||||||
} else {
|
} else {
|
||||||
while ((*index) < tokens->size) {
|
while ((*index) < tokens->size) {
|
||||||
|
skip_newlines_and_indents(tokens, index);
|
||||||
|
error_if_finished(file, tokens, index);
|
||||||
token = darray_get(tokens, *index);
|
token = darray_get(tokens, *index);
|
||||||
if (token->type != TOKEN_IDENTIFIER) {
|
if (token->type != TOKEN_IDENTIFIER) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
@@ -104,7 +106,7 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
|
|||||||
break;
|
break;
|
||||||
token = darray_get(tokens, *index);
|
token = darray_get(tokens, *index);
|
||||||
if (token->type != TOKEN_COMMA) {
|
if (token->type != TOKEN_COMMA) {
|
||||||
(*index)-=count;
|
(*index) -= count;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
(*index)++;
|
(*index)++;
|
||||||
|
|||||||
47
src/parser/list/list.c
Normal file
47
src/parser/list/list.c
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "../../lexer/token.h"
|
||||||
|
#include "../../memory.h"
|
||||||
|
#include "../parser.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
ParsedValue *parse_list(char *file, DArray *tokens, size_t *index) {
|
||||||
|
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||||
|
parsedValue->type = AST_LIST;
|
||||||
|
DArray *list = checked_malloc(sizeof(DArray));
|
||||||
|
parsedValue->data = list;
|
||||||
|
darray_init(list, sizeof(ParsedValue));
|
||||||
|
(*index)++;
|
||||||
|
error_if_finished(file, tokens, index);
|
||||||
|
Token *token = darray_get(tokens, *index);
|
||||||
|
if (token->type != TOKEN_RBRACKET) {
|
||||||
|
while (true) {
|
||||||
|
skip_newlines_and_indents(tokens, index);
|
||||||
|
ParsedValue *parsedValue = parse_token(file, tokens, index, true);
|
||||||
|
darray_push(list, parsedValue);
|
||||||
|
free(parsedValue);
|
||||||
|
error_if_finished(file, tokens, index);
|
||||||
|
skip_newlines_and_indents(tokens, index);
|
||||||
|
error_if_finished(file, tokens, index);
|
||||||
|
token = darray_get(tokens, *index);
|
||||||
|
if (token->type == TOKEN_RBRACKET) {
|
||||||
|
break;
|
||||||
|
} else if (token->type != TOKEN_COMMA) {
|
||||||
|
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
|
||||||
|
token->column);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
(*index)++;
|
||||||
|
error_if_finished(file, tokens, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(*index)++;
|
||||||
|
return parsedValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_parsed_list(void *ptr) {
|
||||||
|
ParsedValue *parsedValue = ptr;
|
||||||
|
DArray *parsed_list = parsedValue->data;
|
||||||
|
darray_free(parsed_list, free_parsed);
|
||||||
|
free(parsedValue->data);
|
||||||
|
}
|
||||||
11
src/parser/list/list.h
Normal file
11
src/parser/list/list.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef LIST_H
|
||||||
|
#define LIST_H
|
||||||
|
#include "../../lexer/token.h" // for Token
|
||||||
|
#include "../parser.h"
|
||||||
|
|
||||||
|
ParsedValue *parse_list(char *file, DArray *tokens,
|
||||||
|
size_t *index);
|
||||||
|
|
||||||
|
void free_parsed_list(void *ptr);
|
||||||
|
|
||||||
|
#endif // LIST_H
|
||||||
@@ -8,9 +8,7 @@
|
|||||||
|
|
||||||
ParsedValue *convert_to_operation(DArray * to_operate_on, DArray * operations) {
|
ParsedValue *convert_to_operation(DArray * to_operate_on, DArray * operations) {
|
||||||
if (to_operate_on->size == 1) {
|
if (to_operate_on->size == 1) {
|
||||||
ParsedValue * parsedValue = malloc(sizeof(ParsedValue));
|
return darray_get(to_operate_on, 0);
|
||||||
memcpy(parsedValue, darray_get(to_operate_on, 0), sizeof(ParsedValue));
|
|
||||||
return parsedValue;
|
|
||||||
}
|
}
|
||||||
TokenType operation = 0;
|
TokenType operation = 0;
|
||||||
DArray positions;
|
DArray positions;
|
||||||
|
|||||||
@@ -6,22 +6,24 @@
|
|||||||
#include "assignable/call/call.h"
|
#include "assignable/call/call.h"
|
||||||
#include "assignable/identifier/identifier.h"
|
#include "assignable/identifier/identifier.h"
|
||||||
#include "declaration/declaration.h"
|
#include "declaration/declaration.h"
|
||||||
#include "operations/operations.h"
|
|
||||||
#include "dowrap/dowrap.h"
|
#include "dowrap/dowrap.h"
|
||||||
#include "if/if.h"
|
#include "if/if.h"
|
||||||
|
#include "list/list.h"
|
||||||
#include "literals/literals.h"
|
#include "literals/literals.h"
|
||||||
#include "number/number.h"
|
#include "number/number.h"
|
||||||
|
#include "operations/operations.h"
|
||||||
#include "string/string.h"
|
#include "string/string.h"
|
||||||
|
#include <gmp.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <gmp.h>
|
|
||||||
|
|
||||||
const char *ValueTypeNames[] = {
|
const char *ValueTypeNames[] = {
|
||||||
"string", "assign", "identifier", "number", "if statement", "access",
|
"string", "assign", "identifier", "number", "if statement",
|
||||||
"call", "declaration", "null", "boolean", "do wrap", "operations"};
|
"access", "call", "declaration", "null", "boolean",
|
||||||
|
"do wrap", "operations", "list"};
|
||||||
|
|
||||||
void error_if_finished(char *file, DArray *tokens, size_t *index) {
|
void error_if_finished(char *file, DArray *tokens, size_t *index) {
|
||||||
if ((*index) >= tokens->size) {
|
if ((*index) >= tokens->size) {
|
||||||
@@ -109,6 +111,9 @@ ParsedValue *parse_token_full(char *file, DArray *tokens, size_t *index,
|
|||||||
case TOKEN_DO:
|
case TOKEN_DO:
|
||||||
output = parse_dowrap(file, tokens, index);
|
output = parse_dowrap(file, tokens, index);
|
||||||
break;
|
break;
|
||||||
|
case TOKEN_LBRACKET:
|
||||||
|
output = parse_list(file, tokens, index);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
|
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
|
||||||
token->column);
|
token->column);
|
||||||
@@ -136,7 +141,7 @@ ParsedValue *parse_token_full(char *file, DArray *tokens, size_t *index,
|
|||||||
case TOKEN_DOT:
|
case TOKEN_DOT:
|
||||||
output = parse_access(file, tokens, index, output);
|
output = parse_access(file, tokens, index, output);
|
||||||
break;
|
break;
|
||||||
SWITCH_OPERATIONS
|
SWITCH_OPERATIONS
|
||||||
if (process_operations) {
|
if (process_operations) {
|
||||||
output = parse_operations(file, tokens, index, output);
|
output = parse_operations(file, tokens, index, output);
|
||||||
break;
|
break;
|
||||||
@@ -211,5 +216,9 @@ void free_parsed(void *ptr) {
|
|||||||
case AST_DOWRAP:
|
case AST_DOWRAP:
|
||||||
free_dowrap(parsed);
|
free_dowrap(parsed);
|
||||||
break;
|
break;
|
||||||
|
case AST_LIST:
|
||||||
|
free_parsed_list(parsed);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
printf("%d\n",parsed->type);
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,8 @@ typedef enum {
|
|||||||
AST_NULL,
|
AST_NULL,
|
||||||
AST_BOOLEAN,
|
AST_BOOLEAN,
|
||||||
AST_DOWRAP,
|
AST_DOWRAP,
|
||||||
AST_OPERATION
|
AST_OPERATION,
|
||||||
|
AST_LIST
|
||||||
} ValueType;
|
} ValueType;
|
||||||
|
|
||||||
extern const char* ValueTypeNames[];
|
extern const char* ValueTypeNames[];
|
||||||
|
|||||||
8
test.ar
8
test.ar
@@ -41,4 +41,10 @@ else term.log("bruh")
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
mm=1/2/4/2/4354/534/534//534//3422*404203420234+3432423324&&430234230||4320423040230423^384239423043024923%4432042304920.3432423423
|
mm=1/2/4/2/4354/534/534//534//3422*404203420234+3432423324&&430234230||4320423040230423^384239423043024923%4432042304920.3432423423
|
||||||
|
|
||||||
|
x = [
|
||||||
|
'hello world',
|
||||||
|
'wow',
|
||||||
|
10
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user