add else if, else, and comments
This commit is contained in:
@@ -47,6 +47,7 @@ int yywrap(void *) {
|
|||||||
|
|
||||||
|
|
||||||
"if" { return TOKEN_IF; }
|
"if" { return TOKEN_IF; }
|
||||||
|
"else if" { return TOKEN_ELSE_IF; }
|
||||||
"else" { return TOKEN_ELSE; }
|
"else" { return TOKEN_ELSE; }
|
||||||
"while" { return TOKEN_WHILE; }
|
"while" { return TOKEN_WHILE; }
|
||||||
"forever" { return TOKEN_FOREVER; }
|
"forever" { return TOKEN_FOREVER; }
|
||||||
@@ -102,6 +103,8 @@ int yywrap(void *) {
|
|||||||
COLUMN_NO += yyleng;
|
COLUMN_NO += yyleng;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[^\n]* {}
|
||||||
|
|
||||||
. {
|
. {
|
||||||
GET_STATE
|
GET_STATE
|
||||||
fprintf(stderr, "%s:%u:%u error: unexpected character '%s'\n", state->path, yylineno, COLUMN_NO+1, yytext);
|
fprintf(stderr, "%s:%u:%u error: unexpected character '%s'\n", state->path, yylineno, COLUMN_NO+1, yytext);
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ typedef enum {
|
|||||||
|
|
||||||
// Keywords
|
// Keywords
|
||||||
TOKEN_IF,
|
TOKEN_IF,
|
||||||
|
TOKEN_ELSE_IF,
|
||||||
TOKEN_ELSE,
|
TOKEN_ELSE,
|
||||||
TOKEN_WHILE,
|
TOKEN_WHILE,
|
||||||
TOKEN_FOREVER,
|
TOKEN_FOREVER,
|
||||||
|
|||||||
@@ -8,7 +8,14 @@
|
|||||||
|
|
||||||
ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens,
|
ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens,
|
||||||
ParsedValue *assign_to, size_t *index) {
|
ParsedValue *assign_to, size_t *index) {
|
||||||
|
bool islet = false;
|
||||||
Token *token = darray_get(tokens, *index);
|
Token *token = darray_get(tokens, *index);
|
||||||
|
if (token->type == TOKEN_LET) {
|
||||||
|
islet = true;
|
||||||
|
(*index)++;
|
||||||
|
error_if_finished(file,tokens,index);
|
||||||
|
token = darray_get(tokens, *index);
|
||||||
|
}
|
||||||
switch (assign_to->type) {
|
switch (assign_to->type) {
|
||||||
case AST_IDENTIFIER:
|
case AST_IDENTIFIER:
|
||||||
case AST_ASSIGN:
|
case AST_ASSIGN:
|
||||||
@@ -25,6 +32,7 @@ ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens,
|
|||||||
error_if_finished(file,tokens,index);
|
error_if_finished(file,tokens,index);
|
||||||
token = darray_get(tokens, *index);
|
token = darray_get(tokens, *index);
|
||||||
assign->from = parse_token(file, parsed, tokens, index, true);
|
assign->from = parse_token(file, parsed, tokens, index, true);
|
||||||
|
assign->let = islet;
|
||||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||||
parsedValue->type = AST_ASSIGN;
|
parsedValue->type = AST_ASSIGN;
|
||||||
parsedValue->data = assign;
|
parsedValue->data = assign;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "../../lexer/token.h"
|
#include "../../lexer/token.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
bool let;
|
||||||
ParsedValue * to;
|
ParsedValue * to;
|
||||||
TokenType type;
|
TokenType type;
|
||||||
ParsedValue * from;
|
ParsedValue * from;
|
||||||
|
|||||||
@@ -10,53 +10,114 @@ ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens,
|
|||||||
size_t *index) {
|
size_t *index) {
|
||||||
(*index)++;
|
(*index)++;
|
||||||
error_if_finished(file, tokens, index);
|
error_if_finished(file, tokens, index);
|
||||||
|
|
||||||
|
DArray *parsed_if = checked_malloc(sizeof(DArray));
|
||||||
|
darray_init(parsed_if, sizeof(ParsedConditional));
|
||||||
|
|
||||||
|
bool expect_conditional = true;
|
||||||
|
|
||||||
|
while (*index < tokens->size) {
|
||||||
Token *token = darray_get(tokens, *index);
|
Token *token = darray_get(tokens, *index);
|
||||||
|
|
||||||
|
// Handle TOKEN_ELSE or TOKEN_ELSE_IF for subsequent branches
|
||||||
|
if (!expect_conditional) {
|
||||||
|
if (token->type != TOKEN_NEW_LINE)
|
||||||
|
break; // no more branches
|
||||||
|
(*index)++;
|
||||||
|
error_if_finished(file, tokens, index);
|
||||||
|
token = darray_get(tokens, *index);
|
||||||
|
|
||||||
|
if (token->type == TOKEN_ELSE || token->type == TOKEN_ELSE_IF) {
|
||||||
|
(*index)++;
|
||||||
|
error_if_finished(file, tokens, index);
|
||||||
|
} else {
|
||||||
|
break; // no more branches
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DArray *condition = NULL;
|
||||||
|
|
||||||
|
if (token->type != TOKEN_ELSE) {
|
||||||
|
// Parse ( condition )
|
||||||
|
token = darray_get(tokens, *index);
|
||||||
if (token->type != TOKEN_LPAREN) {
|
if (token->type != TOKEN_LPAREN) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"%s:%u:%u error: if statement requires paren for the condition\n",
|
"%s:%u:%u error: expected '(' after if\n",
|
||||||
file, token->line, token->column);
|
file, token->line, token->column);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
(*index)++;
|
(*index)++;
|
||||||
error_if_finished(file, tokens, index);
|
error_if_finished(file, tokens, index);
|
||||||
DArray *parsed_if = checked_malloc(sizeof(DArray));
|
|
||||||
darray_init(parsed_if, sizeof(ParsedConditional));
|
condition = checked_malloc(sizeof(DArray));
|
||||||
DArray *condition = checked_malloc(sizeof(DArray));
|
|
||||||
darray_init(condition, sizeof(ParsedValue));
|
darray_init(condition, sizeof(ParsedValue));
|
||||||
while ((*index) < tokens->size) {
|
|
||||||
|
while (*index < tokens->size) {
|
||||||
ParsedValue *parsed_code = parse_token(file, parsed, tokens, index, true);
|
ParsedValue *parsed_code = parse_token(file, parsed, tokens, index, true);
|
||||||
if (parsed_code) {
|
if (parsed_code) {
|
||||||
darray_push(condition, parsed_code);
|
darray_push(condition, parsed_code);
|
||||||
free(parsed_code);
|
free(parsed_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
token = darray_get(tokens, *index);
|
token = darray_get(tokens, *index);
|
||||||
if (token->type == TOKEN_RPAREN)
|
if (token->type == TOKEN_RPAREN)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (token->type != TOKEN_RPAREN) {
|
if (token->type != TOKEN_RPAREN) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"%s:%u:%u error: missing closing parenthesis in if condition\n",
|
"%s:%u:%u error: missing closing ')' in condition\n",
|
||||||
file, token->line, token->column);
|
file, token->line, token->column);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
(*index)++;
|
(*index)++;
|
||||||
error_if_finished(file, tokens, index);
|
error_if_finished(file, tokens, index);
|
||||||
ParsedValue *parsed_content = parse_token(file, parsed, tokens, index, true);
|
}
|
||||||
|
|
||||||
|
// Parse the body
|
||||||
|
ParsedValue *parsed_content =
|
||||||
|
parse_token(file, parsed, tokens, index, false);
|
||||||
|
|
||||||
if (!parsed_content) {
|
if (!parsed_content) {
|
||||||
fprintf(stderr, "%s:%u:%u error: expected body after if condition\n", file,
|
fprintf(stderr,
|
||||||
token->line, token->column);
|
"%s:%u:%u error: expected body after condition\n",
|
||||||
|
file, token->line, token->column);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
ParsedConditional output_conditional = {condition, parsed_content};
|
|
||||||
darray_push(parsed_if, &output_conditional);
|
ParsedConditional conditional = {condition, parsed_content};
|
||||||
|
darray_push(parsed_if, &conditional);
|
||||||
|
|
||||||
|
expect_conditional = false; // After first iteration, expect newline + else/else if
|
||||||
|
}
|
||||||
|
|
||||||
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
|
||||||
parsedValue->type = AST_IF;
|
parsedValue->type = AST_IF;
|
||||||
parsedValue->data = parsed_if;
|
parsedValue->data = parsed_if;
|
||||||
|
|
||||||
|
// printf("Parsed if chain:\n");
|
||||||
|
// for (size_t i = 0; i < parsed_if->size; i++) {
|
||||||
|
// ParsedConditional *cond = darray_get(parsed_if, i);
|
||||||
|
// if (cond->condition) {
|
||||||
|
// printf(" if/else if condition:\n");
|
||||||
|
// for (size_t j = 0; j < cond->condition->size; j++) {
|
||||||
|
// ParsedValue *v = darray_get(cond->condition, j);
|
||||||
|
// printf(" - condition value type: %d\n", v->type);
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// printf(" else:\n");
|
||||||
|
// }
|
||||||
|
// printf(" - content value type: %d\n", cond->content->type);
|
||||||
|
// }
|
||||||
|
|
||||||
return parsedValue;
|
return parsedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_conditional(void *ptr) {
|
void free_conditional(void *ptr) {
|
||||||
ParsedConditional *conditional = ptr;
|
ParsedConditional *conditional = ptr;
|
||||||
|
if (conditional->condition)
|
||||||
darray_free(conditional->condition, free_parsed);
|
darray_free(conditional->condition, free_parsed);
|
||||||
free_parsed(conditional->content);
|
free_parsed(conditional->content);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,17 +2,17 @@
|
|||||||
|
|
||||||
#ifndef iF_H
|
#ifndef iF_H
|
||||||
#define iF_H
|
#define iF_H
|
||||||
#include "../parser.h"
|
|
||||||
#include "../../lexer/token.h" // for Token
|
#include "../../lexer/token.h" // for Token
|
||||||
|
#include "../parser.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
DArray * condition;
|
DArray *condition; // NULL for 'else'
|
||||||
ParsedValue *content;
|
ParsedValue *content;
|
||||||
} ParsedConditional;
|
} ParsedConditional;
|
||||||
|
|
||||||
ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens, size_t *index);
|
ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens,
|
||||||
|
size_t *index);
|
||||||
|
|
||||||
void free_parsed_if(void *ptr);
|
void free_parsed_if(void *ptr);
|
||||||
|
|
||||||
|
|
||||||
#endif // iF_H
|
#endif // iF_H
|
||||||
@@ -12,7 +12,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
const char *ValueTypeNames[] = {"string", "assign", "identifier", "number", "if statement"};
|
const char *ValueTypeNames[] = {"string", "assign", "identifier", "number",
|
||||||
|
"if statement"};
|
||||||
|
|
||||||
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) {
|
||||||
@@ -49,6 +50,7 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
|||||||
fprintf(stderr, "%s:%u:%u error: invalid indentation\n", file, token->line,
|
fprintf(stderr, "%s:%u:%u error: invalid indentation\n", file, token->line,
|
||||||
token->column);
|
token->column);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
case TOKEN_LET:
|
||||||
case TOKEN_IDENTIFIER:;
|
case TOKEN_IDENTIFIER:;
|
||||||
ParsedValue *assign_to = parse_identifier(token);
|
ParsedValue *assign_to = parse_identifier(token);
|
||||||
(*index)++;
|
(*index)++;
|
||||||
@@ -90,7 +92,8 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens,
|
|||||||
(*index)++;
|
(*index)++;
|
||||||
return parse_number(token);
|
return parse_number(token);
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Panic: unreachable\n");
|
fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line,
|
||||||
|
token->column);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user