add if statements
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../../memory.h"
|
||||
|
||||
ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens,
|
||||
ParsedValue *assign_to, size_t *index) {
|
||||
@@ -17,13 +18,14 @@ ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens,
|
||||
token->column, ValueTypeNames[assign_to->type]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ParsedAssign *assign = malloc(sizeof(ParsedAssign));
|
||||
ParsedAssign *assign = checked_malloc(sizeof(ParsedAssign));
|
||||
assign->to = assign_to;
|
||||
assign->type = token->type;
|
||||
(*index)++;
|
||||
error_if_finished(file,tokens,index);
|
||||
token = darray_get(tokens, *index);
|
||||
assign->from = parse_token(file, parsed, tokens, index, true);
|
||||
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_ASSIGN;
|
||||
parsedValue->data = assign;
|
||||
return parsedValue;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#include "identifier.h"
|
||||
#include "../../lexer/token.h"
|
||||
#include "../parser.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../../memory.h"
|
||||
|
||||
ParsedValue *parse_identifier(Token *token) {
|
||||
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_IDENTIFIER;
|
||||
parsedValue->data = strcpy(malloc(sizeof(token->value)), token->value);
|
||||
parsedValue->data = strcpy(checked_malloc(sizeof(token->value)), token->value);
|
||||
return parsedValue;
|
||||
}
|
||||
68
src/parser/if/if.c
Normal file
68
src/parser/if/if.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "if.h"
|
||||
#include "../../lexer/token.h"
|
||||
#include "../parser.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../../memory.h"
|
||||
|
||||
ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens,
|
||||
size_t *index) {
|
||||
(*index)++;
|
||||
error_if_finished(file, tokens, index);
|
||||
Token *token = darray_get(tokens, *index);
|
||||
if (token->type != TOKEN_LPAREN) {
|
||||
fprintf(stderr,
|
||||
"%s:%u:%u error: if statement requires paren for the condition\n",
|
||||
file, token->line, token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
(*index)++;
|
||||
error_if_finished(file, tokens, index);
|
||||
DArray *parsed_if = checked_malloc(sizeof(DArray));
|
||||
darray_init(parsed_if, sizeof(ParsedConditional));
|
||||
DArray *condition = checked_malloc(sizeof(DArray));
|
||||
darray_init(condition, sizeof(ParsedValue));
|
||||
while ((*index) < tokens->size) {
|
||||
ParsedValue *parsed_code = parse_token(file, parsed, tokens, index, true);
|
||||
if (parsed_code) {
|
||||
darray_push(condition, parsed_code);
|
||||
free(parsed_code);
|
||||
}
|
||||
token = darray_get(tokens, *index);
|
||||
if (token->type == TOKEN_RPAREN)
|
||||
break;
|
||||
}
|
||||
if (token->type != TOKEN_RPAREN) {
|
||||
fprintf(stderr,
|
||||
"%s:%u:%u error: missing closing parenthesis in if condition\n",
|
||||
file, token->line, token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
(*index)++;
|
||||
error_if_finished(file, tokens, index);
|
||||
ParsedValue *parsed_content = parse_token(file, parsed, tokens, index, true);
|
||||
if (!parsed_content) {
|
||||
fprintf(stderr, "%s:%u:%u error: expected body after if condition\n", file,
|
||||
token->line, token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ParsedConditional output_conditional = {condition, parsed_content};
|
||||
darray_push(parsed_if, &output_conditional);
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_IF;
|
||||
parsedValue->data = parsed_if;
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
void free_conditional(void *ptr) {
|
||||
ParsedConditional *conditional = ptr;
|
||||
darray_free(conditional->condition, free_parsed);
|
||||
free_parsed(conditional->content);
|
||||
}
|
||||
|
||||
void free_parsed_if(void *ptr) {
|
||||
ParsedValue *parsedValue = ptr;
|
||||
DArray *parsed_if = parsedValue->data;
|
||||
darray_free(parsed_if, free_conditional);
|
||||
}
|
||||
18
src/parser/if/if.h
Normal file
18
src/parser/if/if.h
Normal file
@@ -0,0 +1,18 @@
|
||||
// parser.h
|
||||
|
||||
#ifndef iF_H
|
||||
#define iF_H
|
||||
#include "../parser.h"
|
||||
#include "../../lexer/token.h" // for Token
|
||||
|
||||
typedef struct {
|
||||
DArray * condition;
|
||||
ParsedValue * content;
|
||||
} ParsedConditional;
|
||||
|
||||
ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens, size_t *index);
|
||||
|
||||
void free_parsed_if(void *ptr);
|
||||
|
||||
|
||||
#endif // iF_H
|
||||
@@ -1,12 +1,12 @@
|
||||
#include "number.h"
|
||||
#include "../../lexer/token.h"
|
||||
#include "../parser.h"
|
||||
#include <stdlib.h>
|
||||
#include "../../memory.h"
|
||||
#include <gmp.h>
|
||||
|
||||
ParsedValue *parse_number(Token *token) {
|
||||
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
|
||||
mpz_t *number = malloc(sizeof(mpz_t));
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
mpz_t *number = checked_malloc(sizeof(mpz_t));
|
||||
mpz_init_set_str(*number, token->value, 10);
|
||||
parsedValue->type = AST_NUMBER;
|
||||
parsedValue->data = number;
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
#include "../dynamic_array/darray.h"
|
||||
#include "../lexer/token.h"
|
||||
#include "assign/assign.h"
|
||||
#include "number/number.h"
|
||||
#include "identifier/identifier.h"
|
||||
#include "if/if.h"
|
||||
#include "number/number.h"
|
||||
#include "string/string.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
@@ -11,13 +12,24 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char *ValueTypeNames[] = {"string", "assign", "identifier", "number"};
|
||||
const char *ValueTypeNames[] = {"string", "assign", "identifier", "number", "if statement"};
|
||||
|
||||
void error_if_finished(char *file, DArray *tokens, size_t *index) {
|
||||
if ((*index) >= tokens->size) {
|
||||
Token *token = darray_get(tokens, tokens->size - 1);
|
||||
fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line,
|
||||
token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
||||
size_t *index, bool inline_flag) {
|
||||
Token *token = darray_get(tokens, *index);
|
||||
if (!inline_flag) {
|
||||
switch (token->type) {
|
||||
case TOKEN_IF:
|
||||
return parse_if(file, parsed, tokens, index);
|
||||
default:
|
||||
break;
|
||||
};
|
||||
@@ -70,9 +82,9 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
||||
token->column);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ParsedValue *assigning_to = darray_get(parsed, parsed->size-1);
|
||||
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]);
|
||||
token->column, ValueTypeNames[assigning_to->type]);
|
||||
exit(EXIT_FAILURE);
|
||||
case TOKEN_NUMBER:
|
||||
(*index)++;
|
||||
@@ -85,8 +97,7 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
||||
|
||||
void parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag) {
|
||||
size_t index = 0;
|
||||
size_t length = tokens->size;
|
||||
while (index < length) {
|
||||
while (index < tokens->size) {
|
||||
ParsedValue *parsed_code =
|
||||
parse_token(file, parsed, tokens, &index, inline_flag);
|
||||
if (parsed_code) {
|
||||
@@ -108,5 +119,8 @@ void free_parsed(void *ptr) {
|
||||
break;
|
||||
case AST_NUMBER:
|
||||
break;
|
||||
case AST_IF:
|
||||
free_parsed_if(parsed);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ typedef enum {
|
||||
AST_ASSIGN,
|
||||
AST_IDENTIFIER,
|
||||
AST_NUMBER,
|
||||
AST_IF,
|
||||
} ValueType;
|
||||
|
||||
extern const char* ValueTypeNames[];
|
||||
@@ -28,4 +29,6 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
||||
|
||||
void free_parsed(void *ptr);
|
||||
|
||||
void error_if_finished(char *file,DArray *tokens, size_t *index);
|
||||
|
||||
#endif // PARSER_H
|
||||
@@ -6,10 +6,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../../memory.h"
|
||||
|
||||
char *swap_quotes(char *input, char quote) {
|
||||
size_t len = strlen(input);
|
||||
char *result = malloc(len + 1);
|
||||
char *result = checked_malloc(len + 1);
|
||||
if (!result)
|
||||
return NULL;
|
||||
|
||||
@@ -73,7 +74,7 @@ char *unquote(char *str) {
|
||||
}
|
||||
|
||||
ParsedValue *parse_string(Token token) {
|
||||
ParsedValue *parsedValue = malloc(sizeof(ParsedValue));
|
||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||
parsedValue->type = AST_STRING;
|
||||
parsedValue->data = unquote(token.value);
|
||||
return parsedValue;
|
||||
|
||||
Reference in New Issue
Block a user