add do wraps
This commit is contained in:
@@ -15,6 +15,7 @@ ParsedValue *parse_access(char*file,DArray *tokens, size_t * index, ParsedValue
|
||||
parsedAccess->access = strcpy(checked_malloc(strlen(token->value) + 1), token->value);
|
||||
parsedValue->type = AST_ACCESS;
|
||||
parsedValue->data = parsedAccess;
|
||||
(*index)++;
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,12 +18,7 @@ ParsedValue *parse_call(char *file, DArray *tokens, size_t *index,
|
||||
(*index)++;
|
||||
error_if_finished(file, tokens, index);
|
||||
Token *token = darray_get(tokens, *index);
|
||||
if (token->type == TOKEN_RPAREN) {
|
||||
(*index)++;
|
||||
if ((*index) >= tokens->size)
|
||||
return parsedValue;
|
||||
token = darray_get(tokens, *index);
|
||||
} else {
|
||||
if (token->type != TOKEN_RPAREN) {
|
||||
while ((*index) < tokens->size) {
|
||||
skip_newlines_and_indents(tokens, index);
|
||||
error_if_finished(file, tokens, index);
|
||||
@@ -35,10 +30,6 @@ ParsedValue *parse_call(char *file, DArray *tokens, size_t *index,
|
||||
error_if_finished(file, tokens, index);
|
||||
token = darray_get(tokens, *index);
|
||||
if (token->type == TOKEN_RPAREN) {
|
||||
(*index)++;
|
||||
if ((*index) >= tokens->size)
|
||||
break;
|
||||
token = darray_get(tokens, *index);
|
||||
break;
|
||||
} else if (token->type != TOKEN_COMMA) {
|
||||
fprintf(stderr, "%s:%zu:%zu error: expected comma\n", file, token->line,
|
||||
@@ -49,6 +40,7 @@ ParsedValue *parse_call(char *file, DArray *tokens, size_t *index,
|
||||
error_if_finished(file, tokens, index);
|
||||
}
|
||||
}
|
||||
(*index)++;
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
|
||||
error_if_finished(file, tokens, index);
|
||||
Token *token = darray_get(tokens, *index);
|
||||
|
||||
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_DECLARATION;
|
||||
DArray *declarations = malloc(sizeof(DArray));
|
||||
DArray *declarations = checked_malloc(sizeof(DArray));
|
||||
darray_init(declarations, sizeof(ParsedSingleDeclaration));
|
||||
parsedValue->data = declarations;
|
||||
while (true) {
|
||||
@@ -100,6 +100,10 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
|
||||
break;
|
||||
token = darray_get(tokens, *index);
|
||||
}
|
||||
skip_newlines_and_indents(tokens, index);
|
||||
if ((*index) >= tokens->size)
|
||||
break;
|
||||
token = darray_get(tokens, *index);
|
||||
if (token->type != TOKEN_COMMA)
|
||||
break;
|
||||
(*index)++;
|
||||
|
||||
104
src/parser/dowrap/dowrap.c
Normal file
104
src/parser/dowrap/dowrap.c
Normal file
@@ -0,0 +1,104 @@
|
||||
#include "../../lexer/token.h"
|
||||
#include "../../memory.h"
|
||||
#include "../parser.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *repeat_space(size_t x) {
|
||||
|
||||
char *str = checked_malloc(x + 1); // +1 for the null terminator
|
||||
|
||||
memset(str, ' ', x);
|
||||
str[x] = '\0';
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
void free_string_dowrap(void *ptr) {
|
||||
// `ptr` is a pointer to a char*
|
||||
char *str = *(char **)ptr;
|
||||
free(str);
|
||||
}
|
||||
|
||||
ParsedValue *parse_dowrap(char *file, DArray *tokens, size_t *index) {
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_DOWRAP;
|
||||
DArray *dowrap_parsed = checked_malloc(sizeof(DArray));
|
||||
darray_init(dowrap_parsed, sizeof(ParsedValue));
|
||||
parsedValue->data = dowrap_parsed;
|
||||
(*index)++;
|
||||
if ((*index) >= tokens->size)
|
||||
return parsedValue;
|
||||
Token *token = darray_get(tokens, *index);
|
||||
if (token->type != TOKEN_NEW_LINE) {
|
||||
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
|
||||
token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
size_t indent_depth = 0;
|
||||
bool temp_indent_depth_toggle = false;
|
||||
size_t temp_indent_depth = 0;
|
||||
bool pass = false;
|
||||
DArray dowrap_tokens;
|
||||
darray_init(&dowrap_tokens, sizeof(Token));
|
||||
DArray to_free;
|
||||
darray_init(&to_free, sizeof(char *));
|
||||
|
||||
size_t starting_index = *index;
|
||||
|
||||
size_t temp_index_count = 0;
|
||||
|
||||
while (!pass && ++(*index) < tokens->size) {
|
||||
token = darray_get(tokens, *index);
|
||||
switch (token->type) {
|
||||
case TOKEN_INDENT:
|
||||
temp_indent_depth_toggle = true;
|
||||
if (dowrap_tokens.size == 0) {
|
||||
indent_depth = strlen(token->value);
|
||||
temp_indent_depth = indent_depth;
|
||||
} else {
|
||||
temp_indent_depth = strlen(token->value);
|
||||
}
|
||||
break;
|
||||
case TOKEN_NEW_LINE:
|
||||
temp_indent_depth = 0;
|
||||
temp_indent_depth_toggle = true;
|
||||
darray_push(&dowrap_tokens, token);
|
||||
temp_index_count++;
|
||||
break;
|
||||
default:
|
||||
if (temp_indent_depth < indent_depth && temp_indent_depth_toggle) {
|
||||
pass = true;
|
||||
break;
|
||||
}
|
||||
if (temp_indent_depth >= indent_depth) {
|
||||
size_t indent_amount = temp_indent_depth-indent_depth;
|
||||
Token indent_token;
|
||||
indent_token.line = token->line;
|
||||
indent_token.column = token->column;
|
||||
indent_token.type = TOKEN_INDENT;
|
||||
indent_token.value = repeat_space(indent_amount);
|
||||
darray_push(&dowrap_tokens, &indent_token);
|
||||
darray_push(&to_free, &indent_token.value);
|
||||
}
|
||||
temp_indent_depth_toggle = false;
|
||||
temp_indent_depth = 0;
|
||||
temp_index_count=0;
|
||||
darray_push(&dowrap_tokens, token);
|
||||
}
|
||||
}
|
||||
(*index)-=temp_index_count;
|
||||
for (size_t i = 0; i<temp_index_count;i++) {
|
||||
darray_pop(&dowrap_tokens, NULL);
|
||||
}
|
||||
parser(file, dowrap_parsed, &dowrap_tokens, false);
|
||||
|
||||
darray_free(&dowrap_tokens, NULL);
|
||||
|
||||
darray_free(&to_free, free_string_dowrap);
|
||||
|
||||
return parsedValue;
|
||||
}
|
||||
12
src/parser/dowrap/dowrap.h
Normal file
12
src/parser/dowrap/dowrap.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef DOWRAP_H
|
||||
#define DOWRAP_H
|
||||
#include "../parser.h"
|
||||
#include "../../lexer/token.h" // for Token
|
||||
|
||||
// Function declaration for parsing an identifier
|
||||
ParsedValue *parse_dowrap(char *file, DArray *tokens,
|
||||
size_t *index);
|
||||
|
||||
void free_dowrap(void *ptr);
|
||||
|
||||
#endif // DOWRAP_H
|
||||
@@ -1,24 +1,25 @@
|
||||
#include "parser.h"
|
||||
#include "../dynamic_array/darray.h"
|
||||
#include "../lexer/token.h"
|
||||
#include "assignable/access/access.h"
|
||||
#include "assignable/assign/assign.h"
|
||||
#include "assignable/call/call.h"
|
||||
#include "assignable/identifier/identifier.h"
|
||||
#include "declaration/declaration.h"
|
||||
#include "dowrap/dowrap.h"
|
||||
#include "if/if.h"
|
||||
#include "literals/literals.h"
|
||||
#include "number/number.h"
|
||||
#include "string/string.h"
|
||||
#include "literals/literals.h"
|
||||
#include "assignable/call/call.h"
|
||||
#include "assignable/access/access.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char *ValueTypeNames[] = {"string", "assign", "identifier",
|
||||
"number", "if statement", "access",
|
||||
"call", "declaration", "null", "boolean"};
|
||||
const char *ValueTypeNames[] = {
|
||||
"string", "assign", "identifier", "number", "if statement", "access",
|
||||
"call", "declaration", "null", "boolean", "do wrap"};
|
||||
|
||||
void error_if_finished(char *file, DArray *tokens, size_t *index) {
|
||||
if ((*index) >= tokens->size) {
|
||||
@@ -78,11 +79,14 @@ ParsedValue *parse_token(char *file, DArray *tokens, size_t *index,
|
||||
case TOKEN_NEW_LINE:
|
||||
(*index)++;
|
||||
return NULL;
|
||||
break;
|
||||
case TOKEN_INDENT:
|
||||
fprintf(stderr, "%s:%zu:%zu error: invalid indentation\n", file, token->line,
|
||||
token->column);
|
||||
if (strlen(token->value) > 0) {
|
||||
fprintf(stderr, "%s:%zu:%zu error: invalid indentation\n", file,
|
||||
token->line, token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
(*index)++;
|
||||
return NULL;
|
||||
case TOKEN_IDENTIFIER:
|
||||
(*index)++;
|
||||
output = parse_identifier(token);
|
||||
@@ -94,6 +98,9 @@ ParsedValue *parse_token(char *file, DArray *tokens, size_t *index,
|
||||
case TOKEN_LET:
|
||||
output = parse_declaration(file, tokens, index);
|
||||
break;
|
||||
case TOKEN_DO:
|
||||
output = parse_dowrap(file, tokens, index);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
|
||||
token->column);
|
||||
@@ -131,11 +138,21 @@ ParsedValue *parse_token(char *file, DArray *tokens, size_t *index,
|
||||
|
||||
void parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag) {
|
||||
size_t index = 0;
|
||||
bool expecting_new_line = false;
|
||||
while (index < tokens->size) {
|
||||
ParsedValue *parsed_code = parse_token(file, tokens, &index, inline_flag);
|
||||
if (parsed_code) {
|
||||
if (expecting_new_line) {
|
||||
Token *token = darray_get(tokens, index-1);
|
||||
fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line,
|
||||
token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
expecting_new_line = true;
|
||||
darray_push(parsed, parsed_code);
|
||||
free(parsed_code);
|
||||
} else {
|
||||
expecting_new_line = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ typedef enum {
|
||||
AST_CALL,
|
||||
AST_DECLARATION,
|
||||
AST_NULL,
|
||||
AST_BOOLEAN
|
||||
AST_BOOLEAN,
|
||||
AST_DOWRAP
|
||||
} ValueType;
|
||||
|
||||
extern const char* ValueTypeNames[];
|
||||
|
||||
Reference in New Issue
Block a user