From 1a94c2b69b12424a9a7cce3ea4ab0c220bf7755c Mon Sep 17 00:00:00 2001 From: Steven Schronk Date: Tue, 30 Nov 2010 16:00:14 -0600 Subject: [PATCH] If statements now support else clauses. Implementation uses pointer to token to allow parser to rewind last request for next token. All example code now compiles without error. --- parse.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/parse.c b/parse.c index 3143784..b8cf17f 100644 --- a/parse.c +++ b/parse.c @@ -736,6 +736,8 @@ void parse_return() void parse_if() { + char *pTemp = NULL; + if(settings.tokens) { token_print("ifStatement", OPEN); @@ -802,11 +804,59 @@ void parse_if() if(*pT == '}') { + pTemp = pC; /* store pointer to where we are - in case no 'else' clause exists */ if(settings.tokens) { token_print("symbol", BOTH); } } else { compiler_error(46, "Could Not Find '}' Symbol At This Location", pS, pC, pT); } + /* Look forward and see if 'else' clause is there*/ + if(has_more_tokens(pC) == true) + { + pC = advance(pC, pT); + tk = token_type(pT); + } else { + compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT); + } + + if(strcmp(pT, "else") == 0) + { + if(settings.tokens) { token_print("keyword", BOTH); }; + + if(has_more_tokens(pC) == true) + { + pC = advance(pC, pT); + tk = token_type(pT); + } else { + compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT); + } + + if(*pT == '{') + { + if(settings.tokens) { token_print("symbol", BOTH); } + } else { + compiler_error(45, "Could Not Find '{' Symbol At This Location", pS, pC, pT); + } + + parse_statements(); + + if(*pT == '}') + { + if(settings.tokens) { token_print("symbol", BOTH); } + } else { + compiler_error(46, "Could Not Find '}' Symbol At This Location", pS, pC, pT); + } + } else { + pC = pTemp-1; /* rewind back to end of 'if' statement */ + if(has_more_tokens(pC) == true) + { + pC = advance(pC, pT); + tk = token_type(pT); + } else { + compiler_error(47, "Could Not Complete If Statement. Incomplete Program", pS, pC, pT); + } + } + if(settings.tokens) { space_count--; -- 2.11.4.GIT