From 73a6b6d4fcbd5bd7e3bb6d59db247b7267c7fe7e Mon Sep 17 00:00:00 2001 From: llsth Date: Wed, 8 Apr 2015 12:59:08 +0200 Subject: [PATCH] Accept newline as delimiter --- app/localize/lex.h | 5 +++-- app/main/lexer.cpp | 38 +++++++++++++++++++++++++++++++++++++- app/main/lexer.h | 2 ++ app/main/parser.cpp | 14 +++++++------- app/system/preferences.cpp | 26 ++++++++++++-------------- 5 files changed, 61 insertions(+), 24 deletions(-) diff --git a/app/localize/lex.h b/app/localize/lex.h index dad860a2..5ccb70f5 100644 --- a/app/localize/lex.h +++ b/app/localize/lex.h @@ -45,7 +45,7 @@ typedef enum { symhelp, symeval, symdelete, symall, symclear, symdef, symexit, symoperator, symstatement, symfunction, symvariable, symtrigon, symhyper, symcomplex, - syme, sympi, symi,symins, symsemicolon, symend, + syme, sympi, symi,symins, symdelimiter, symend, symshow, symlist, symload, symsave, symexecute, syminput, symoutput, symdigits, symdec, symhex, symbin, symoct, @@ -72,7 +72,8 @@ static const operatordef operators[] = { { ')', symrparen}, { '|', symabsolute}, { '=', symassign}, - { ';', symsemicolon} + { '\n', symdelimiter}, + { ';', symdelimiter} }; /** diff --git a/app/main/lexer.cpp b/app/main/lexer.cpp index e00e15ee..8e2282e5 100644 --- a/app/main/lexer.cpp +++ b/app/main/lexer.cpp @@ -78,7 +78,7 @@ void Lexer::Tokenize() void Lexer::GetNextToken() { // Skip spaces and non visible characters - while (*str != 0 && (Program->Language->CharIsCntrl(*str) || Program->Language->CharIsSpace(*str))) { + while (*str != 0 && ShouldSkip(*str)) { str++; if (Program->Language->CharIsSpace(*str)) { pos++; @@ -223,3 +223,39 @@ Symbol Lexer::FindKeyword(const char *ident) return (Symbol)0; } + +char* Lexer::FindKeyword(Symbol symbol) +{ + static const unsigned int kwcount = sizeof(keywords) / sizeof(keyworddef); + for (unsigned int i = 0; i < kwcount; i++) { + if (keywords[i].symbol == symbol) { + return (char*)keywords[i].name; + } + } + + static const unsigned int ocount = sizeof(operators) / sizeof(operatordef); + for (unsigned int i = 0; i < ocount; i++) { + if (operators[i].symbol == symbol) { + return (char*)&(operators[i].chr); + } + } + + return NOMEM; +} + +bool Lexer::ShouldSkip(char character) +{ + if (character == '\n') { + return false; + } + + if (Program->Language->CharIsCntrl(character)) { + return true; + } + + if (Program->Language->CharIsSpace(character)) { + return true; + } + + return false; +} diff --git a/app/main/lexer.h b/app/main/lexer.h index 5147f8af..dcc8b766 100644 --- a/app/main/lexer.h +++ b/app/main/lexer.h @@ -50,6 +50,7 @@ public: void Tokenize(); Token* GetFirstToken(); char* GetInput(); + static char* FindKeyword(Symbol symbol); private: char *input; // Input to process by lexer @@ -63,6 +64,7 @@ private: bool GetQuotedIdent(); bool GetLitteral(); bool GetDigitValue(); + bool ShouldSkip(char character); Symbol FindKeyword(const char *ident); }; diff --git a/app/main/parser.cpp b/app/main/parser.cpp index 7f5b6289..bfe636d5 100644 --- a/app/main/parser.cpp +++ b/app/main/parser.cpp @@ -59,7 +59,7 @@ SyntaxNode* Parser::Parse() result = TryParseStatement(); GetToken(); - if (token->symbol == symsemicolon || (token->symbol == symend && block != NOMEM)) { + if (token->symbol == symdelimiter || (token->symbol == symend && block != NOMEM)) { if (block == NOMEM) { block = new StatementBlockNode(); } @@ -68,7 +68,7 @@ SyntaxNode* Parser::Parse() block->Add(result); } - while (token->symbol == symsemicolon) { + while (token->symbol == symdelimiter) { GetToken(); } } else if (token->symbol != symend) { @@ -95,7 +95,7 @@ SyntaxNode* Parser::Parse() SyntaxNode* Parser::TryParseStatement() { GetToken(); - if(token->symbol == symend || token->symbol == symsemicolon) { + if(token->symbol == symend || token->symbol == symdelimiter) { PutToken(); return NOMEM; } @@ -457,7 +457,7 @@ SyntaxNode* Parser::ParseHelpStatement() { GetToken(); - if (token->symbol == symsemicolon || token->symbol == symend) { + if (token->symbol == symdelimiter || token->symbol == symend) { PutToken(); return new HelpStatement(); } else if (token->symbol == symident) { @@ -496,7 +496,7 @@ SyntaxNode* Parser::ParseListStatement() GetToken(); if (token->symbol == symqident) return new ListStatement(token->GetText()); - else if (token->symbol == symend || symsemicolon) { + else if (token->symbol == symend || symdelimiter) { PutToken(); return new ListStatement(); } else { @@ -536,7 +536,7 @@ SyntaxNode* Parser::ParseNumeralStatement() GetToken(); switch (token->symbol) { case symend: - case symsemicolon: + case symdelimiter: PutToken(); return (statement->symbol == syminput) ? (SyntaxNode*)new InputStatement() : @@ -588,7 +588,7 @@ SyntaxNode* Parser::ParseNumeralStatement() SyntaxNode* Parser::ParseDigistStatement() { GetToken(); - if (token->symbol == symsemicolon || token->symbol == symend) { + if (token->symbol == symdelimiter || token->symbol == symend) { PutToken(); return new DigitsStatement(); } else if (token->symbol != symnumber) { diff --git a/app/system/preferences.cpp b/app/system/preferences.cpp index 99b9fdaf..bb105730 100644 --- a/app/system/preferences.cpp +++ b/app/system/preferences.cpp @@ -29,6 +29,7 @@ #include "lib/real.h" #include "lib/ntext.h" #include "lib/charbuf.h" +#include "main/lexer.h" #include "main/nodes.h" #include "main/parser.h" #include "system/preferences.h" @@ -53,10 +54,10 @@ void PreferencesBase::SetDefaults() char* PreferencesBase::GetDescription() { - static const char promptq = '"'; - static const char *sprompt = "prompt"; - static const char *sdigits = "digits"; - static const char delimitor = ';'; + static char *promptkw = Lexer::FindKeyword(symprompt); + static char *digitkw = Lexer::FindKeyword(symdigits); + static char delimiter = *(Lexer::FindKeyword(symdelimiter)); + static char promptq = '"'; Number *d = new RealNumber(GetDigits()); NumeralSystem *ns = new DecimalSystem(2); @@ -65,23 +66,20 @@ char* PreferencesBase::GetDescription() buf->Empty(); buf->EnsureSize( - sizeof(sprompt) + sizeof(SPACE) + sizeof(promptq) + StrLen(prompt) + - sizeof(promptq) + sizeof(delimitor) + sizeof(NEWLINE) + - sizeof(sdigits) + sizeof(SPACE) + StrLen(dtext) + sizeof(delimitor) + - sizeof(NEWLINE) + 1); + StrLen(promptkw) + sizeof(SPACE) + sizeof(promptq) + + StrLen(prompt) + sizeof(promptq) + sizeof(delimiter) + + StrLen(digitkw) + sizeof(SPACE) + StrLen(dtext) + sizeof(delimiter) + 1); - buf->Append(sprompt); + buf->Append(promptkw); buf->Append(SPACE); buf->Append(promptq); buf->Append(prompt); buf->Append(promptq); - buf->Append(delimitor); - buf->Append(NEWLINE); - buf->Append(sdigits); + buf->Append(delimiter); + buf->Append(digitkw); buf->Append(SPACE); buf->Append(dtext); - buf->Append(delimitor); - buf->Append(NEWLINE); + buf->Append(delimiter); delete ns; return buf->GetString();