Accept newline as delimiter

This commit is contained in:
llsth 2015-04-08 12:59:08 +02:00
parent cffa6011cb
commit 73a6b6d4fc
5 changed files with 61 additions and 24 deletions

View File

@ -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}
};
/**

View File

@ -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;
}

View File

@ -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);
};

View File

@ -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) {

View File

@ -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();