From 7f03fe8cf0f8df53379252e6bf37bad1c1155d67 Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 28 Aug 2018 18:57:33 +0700 Subject: [PATCH] (#303) Introduce string and stream readers --- src/script/parser.c | 21 ++++++++++++++++++--- src/script/parser.h | 3 ++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/script/parser.c b/src/script/parser.c index 9f063a42..88b3451e 100644 --- a/src/script/parser.c +++ b/src/script/parser.c @@ -1,16 +1,19 @@ #include -#include #include +#include +#include #include "script/parser.h" +static struct ParseResult parse_expr(struct Token current_token); + static struct ParseResult parse_cdr(struct Token current_token) { if (*current_token.begin != '.') { return parse_failure("Expected .", current_token.begin); } - struct ParseResult cdr = parse_expr(next_token(current_token.end)); + struct ParseResult cdr = read_expr_from_string(current_token.end); if (cdr.is_error) { return cdr; } @@ -131,7 +134,7 @@ static struct ParseResult parse_symbol(struct Token current_token) current_token.end); } -struct ParseResult parse_expr(struct Token current_token) +static struct ParseResult parse_expr(struct Token current_token) { if (*current_token.begin == 0) { return parse_failure("EOF", current_token.begin); @@ -151,6 +154,18 @@ struct ParseResult parse_expr(struct Token current_token) return parse_symbol(current_token); } +struct ParseResult read_expr_from_string(const char *str) +{ + assert(str); + return parse_expr(next_token(str)); +} + +struct ParseResult read_expr_from_stream(FILE *stream) +{ + assert(stream); + return parse_failure("not implemented", NULL); +} + struct ParseResult parse_success(struct Expr expr, const char *end) { diff --git a/src/script/parser.h b/src/script/parser.h index 01b0b672..5f04fc56 100644 --- a/src/script/parser.h +++ b/src/script/parser.h @@ -21,7 +21,8 @@ struct ParseResult parse_success(struct Expr expr, struct ParseResult parse_failure(const char *error, const char *end); -struct ParseResult parse_expr(struct Token token); +struct ParseResult read_expr_from_string(const char *str); +struct ParseResult read_expr_from_stream(FILE *stream); void print_parse_error(FILE *stream, const char *str, -- 2.44.0