From bbeaec98458bf0a1d58d6e3f3b55ef2ba8b783a8 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 30 Apr 2014 08:24:54 -0400 Subject: [PATCH] CP: Parser rewrite. Builds but not functional yet. --- source/slvm/main.c | 14 +++++++++++--- source/slvm/parser.c | 21 ++++++++++++--------- source/slvm/parser.h | 5 +++-- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/source/slvm/main.c b/source/slvm/main.c index 708cf66..1af77d9 100644 --- a/source/slvm/main.c +++ b/source/slvm/main.c @@ -5,6 +5,7 @@ #include #include #include "slvm.h" +#include "parser.h" /* Wish List: @@ -147,13 +148,20 @@ defcode("fpeekc", _fpeekc, 0, &_fputc){ /* Interpreter Words *****************************************************************************/ -// defcode("fetch", , 0, &){} -// defcode("parse", , 0, &){} +defcode("fetch", _fetch, 0, &_fpeekc){ + ArgStackPtr++; + *(ArgStackPtr) = (val_t)fetch_token((FILE*)_stdin_val); +} + +defcode("parse", _parse, 0, &_fetch){ + ArgStackPtr++; + *(ArgStackPtr-1) = (val_t)parse( (char*)*(ArgStackPtr-1), ArgStackPtr ); +} // defcode("interp", , 0, &){} /* Input Words *****************************************************************************/ -defcode("getc", get_char, 0, &_fpeekc){ +defcode("getc", get_char, 0, &_parse){ ArgStackPtr++; *(ArgStackPtr) = getc(stdin); } diff --git a/source/slvm/parser.c b/source/slvm/parser.c index e6dce38..e98577c 100644 --- a/source/slvm/parser.c +++ b/source/slvm/parser.c @@ -5,25 +5,28 @@ $HeadURL$ */ #include "parser.h" +#include +#include +#include /* Fetching Tokens *****************************************************************************/ static void skip_whitespace(FILE* input); static void skip_comment(FILE* input); -static void parse_string(FILE* input); -static void parse_token(FILE* input); +static char* parse_string(FILE* input); +static char* parse_token(FILE* input); static void grow_token(size_t* p_size, size_t* p_index, char** p_p_str, char ch); static bool is_whitespace(FILE* input); static char fpeekc(FILE* input); -char* fetch(FILE* input) +char* fetch_token(FILE* input) { char* result = NULL; - skip_whitespace(); + skip_whitespace(input); switch(fpeekc(input)) { - case '#': skip_comment(); - result = fetch(input); + case '#': skip_comment(input); + result = fetch_token(input); break; case '"': result = parse_string(input); break; @@ -100,7 +103,7 @@ static void skip_comment(FILE* input) static bool is_whitespace(FILE* input) { - char ch = peekc(input); + char ch = fpeekc(input); return ((ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n')); } @@ -119,9 +122,9 @@ static bool is_integer(val_t* p_val); static bool is_string(val_t* p_val); static bool is_char(val_t* p_val); -ValueType_T parse(char* str, val_t* p_val) +TokenType_T parse(char* str, val_t* p_val) { - ValueType_T type = ERROR; + TokenType_T type = ERROR; if(str != NULL) { if(!is_float(p_val) && diff --git a/source/slvm/parser.h b/source/slvm/parser.h index 099df11..08beeb2 100644 --- a/source/slvm/parser.h +++ b/source/slvm/parser.h @@ -8,6 +8,7 @@ #define PARSER_H #include "slvm.h" +#include typedef enum { WORD = 0, @@ -18,8 +19,8 @@ typedef enum { ERROR } TokenType_T; -char* fetch(FILE* input); +char* fetch_token(FILE* input); -ValueType_T parse(char* str, val_t* p_val); +TokenType_T parse(char* str, val_t* p_val); #endif /* PARSER_H */ -- 2.52.0