From eaa6e6d7f57e879b421102ff5eb24de9f06cfc1d Mon Sep 17 00:00:00 2001 From: Thunder422 Date: Sun, 18 Jan 2015 11:37:04 -0500 Subject: [PATCH] table: replaced internal function type use with function flag added new function table flag changed to use function flag instead added function flag to all function entries - the type initializer was changed to the default type added is function table entry and token pass-thru access functions updated function type checks in tester and translator classes --- table.cpp | 156 ++++++++++++++++++++++++------------------------- table.h | 8 ++- test_ibcp.cpp | 18 +++--- token.h | 4 ++ translator.cpp | 72 ++++++++++++----------- 5 files changed, 136 insertions(+), 122 deletions(-) diff --git a/table.cpp b/table.cpp index 9f6274a..4e400b0 100644 --- a/table.cpp +++ b/table.cpp @@ -330,9 +330,9 @@ static TableEntry tableEntries[] = // Internal Functions (No Parentheses) //----------------------------------------- { // Rnd_Code - Type::IntFunc, + Type{}, "RND", "", "", - TableFlag{}, 2, &Dbl_None_ExprInfo, + Function_Flag, 2, &Dbl_None_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, //-------------------- @@ -384,190 +384,190 @@ static TableEntry tableEntries[] = // INTERNAL FUNCTIONS (Parentheses) //-------------------------------------- { // Abs_Code - Type::IntFunc, + Type{}, "ABS(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Fix_Code - Type::IntFunc, + Type{}, "FIX(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Frac_Code - Type::IntFunc, + Type{}, "FRAC(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Int_Code - Type::IntFunc, + Type{}, "INT(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // RndArg_Code - Type::IntFunc, + Type{}, "RND(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Sgn_Code - Type::IntFunc, + Type{}, "SGN(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Cint_Code - Type::IntFunc, + Type{}, "CINT(", "", "", - TableFlag{}, 2, &Int_Dbl_ExprInfo, + Function_Flag, 2, &Int_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Cdbl_Code - Type::IntFunc, + Type{}, "CDBL(", "", "", - TableFlag{}, 2, &Dbl_Int_ExprInfo, + Function_Flag, 2, &Dbl_Int_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Sqr_Code - Type::IntFunc, + Type{}, "SQR(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Atn_Code - Type::IntFunc, + Type{}, "ATN(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Cos_Code - Type::IntFunc, + Type{}, "COS(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Sin_Code - Type::IntFunc, + Type{}, "SIN(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Tan_Code - Type::IntFunc, + Type{}, "TAN(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Exp_Code - Type::IntFunc, + Type{}, "EXP(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Log_Code - Type::IntFunc, + Type{}, "LOG(", "", "", - TableFlag{}, 2, &Dbl_Dbl_ExprInfo, + Function_Flag, 2, &Dbl_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Tab_Code - Type::IntFunc, + Type{}, "TAB(", "", "", - Print_Flag, 2, &None_Int_ExprInfo, + Function_Flag | Print_Flag, 2, &None_Int_ExprInfo, NULL, NULL, NULL, NULL, printFunctionRecreate }, { // Spc_Code - Type::IntFunc, + Type{}, "SPC(", "", "", - Print_Flag, 2, &None_Int_ExprInfo, + Function_Flag | Print_Flag, 2, &None_Int_ExprInfo, NULL, NULL, NULL, NULL, printFunctionRecreate }, { // Asc_Code - Type::IntFunc, + Type{}, "ASC(", "", "", - TableFlag{}, 2, &Int_Str_ExprInfo, + Function_Flag, 2, &Int_Str_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Asc2_Code - Type::IntFunc, + Type{}, "ASC(", "2", "", - TableFlag{}, 2, &Int_StrInt_ExprInfo, + Function_Flag, 2, &Int_StrInt_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Chr_Code - Type::IntFunc, + Type{}, "CHR$(", "", "", - TableFlag{}, 2, &Str_Int_ExprInfo, + Function_Flag, 2, &Str_Int_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Instr2_Code - Type::IntFunc, + Type{}, "INSTR(", "2", "", - TableFlag{}, 2, &Int_StrStr_ExprInfo, + Function_Flag, 2, &Int_StrStr_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Instr3_Code - Type::IntFunc, + Type{}, "INSTR(", "3", "", - TableFlag{}, 2, &Int_StrStrInt_ExprInfo, + Function_Flag, 2, &Int_StrStrInt_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Left_Code - Type::IntFunc, + Type{}, "LEFT$(", "", "", - SubStr_Flag, 2, &Str_StrInt_ExprInfo, + Function_Flag | SubStr_Flag, 2, &Str_StrInt_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Len_Code - Type::IntFunc, + Type{}, "LEN(", "", "", - TableFlag{}, 2, &Int_Str_ExprInfo, + Function_Flag, 2, &Int_Str_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Mid2_Code - Type::IntFunc, + Type{}, "MID$(", "2", "", - SubStr_Flag, 2, &Str_StrInt_ExprInfo, + Function_Flag | SubStr_Flag, 2, &Str_StrInt_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Mid3_Code - Type::IntFunc, + Type{}, "MID$(", "3", "", - SubStr_Flag, 2, &Str_StrIntInt_ExprInfo, + Function_Flag | SubStr_Flag, 2, &Str_StrIntInt_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Repeat_Code - Type::IntFunc, + Type{}, "REPEAT$(", "", "", - TableFlag{}, 2, &Str_StrInt_ExprInfo, + Function_Flag, 2, &Str_StrInt_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Right_Code - Type::IntFunc, + Type{}, "RIGHT$(", "", "", - SubStr_Flag, 2, &Str_StrInt_ExprInfo, + Function_Flag | SubStr_Flag, 2, &Str_StrInt_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Space_Code - Type::IntFunc, + Type{}, "SPACE$(", "", "", - TableFlag{}, 2, &Str_Int_ExprInfo, + Function_Flag, 2, &Str_Int_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Str_Code - Type::IntFunc, + Type{}, "STR$(", "", "", - TableFlag{}, 2, &Str_Dbl_ExprInfo, + Function_Flag, 2, &Str_Dbl_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // Val_Code - Type::IntFunc, + Type{}, "VAL(", "", "", - TableFlag{}, 2, &Dbl_Str_ExprInfo, + Function_Flag, 2, &Dbl_Str_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, //---------------------- @@ -1053,39 +1053,39 @@ static TableEntry tableEntries[] = NULL, NULL, NULL, NULL, binaryOperatorRecreate }, { // AbsInt_Code - Type::IntFunc, + Type{}, "ABS(", "%", "", - TableFlag{}, 2, &Int_Int_ExprInfo, + Function_Flag, 2, &Int_Int_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // RndArgInt_Code - Type::IntFunc, + Type{}, "RND(", "%", "", - TableFlag{}, 2, &Int_Int_ExprInfo, + Function_Flag, 2, &Int_Int_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // SgnInt_Code - Type::IntFunc, + Type{}, "SGN(", "%", "", - TableFlag{}, 2, &Int_Int_ExprInfo, + Function_Flag, 2, &Int_Int_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // CvtInt_Code - Type::IntFunc, + Type{}, "", "CvtInt", "", - Hidden_Flag, 2, &Null_ExprInfo, + Function_Flag | Hidden_Flag, 2, &Null_ExprInfo, NULL, NULL, NULL, NULL, blankRecreate }, { // CvtDbl_Code - Type::IntFunc, + Type{}, "", "CvtDbl", "", - Hidden_Flag, 2, &Null_ExprInfo, + Function_Flag | Hidden_Flag, 2, &Null_ExprInfo, NULL, NULL, NULL, NULL, blankRecreate }, { // StrInt_Code - Type::IntFunc, + Type{}, "STR$(", "%", "", - TableFlag{}, 2, &Str_Int_ExprInfo, + Function_Flag, 2, &Str_Int_ExprInfo, NULL, NULL, NULL, NULL, internalFunctionRecreate }, { // PrintDbl_Code @@ -1388,7 +1388,7 @@ void Table::add(TableEntry &entry) iterator->second = &entry; s_alternate[&entry][alternate->m_exprInfo->m_operandCount - 1] .push_back(alternate); - if (entry.m_type == Type::IntFunc) + if (entry.isFunction()) { // multiple codes; set multiple flag on primary code entry.m_flags |= Multiple_Flag; @@ -1469,7 +1469,7 @@ void Table::add(TableEntry &entry) break; // no more operands, all operands match } } - if (entry.m_type == Type::IntFunc && exprInfo->m_operandCount + if (entry.isFunction() && exprInfo->m_operandCount > primary->m_exprInfo->m_operandCount) { // multiple codes; set multiple flag on primary code diff --git a/table.h b/table.h index b8ff633..fdedd87 100644 --- a/table.h +++ b/table.h @@ -38,8 +38,7 @@ using TokenPtr = std::shared_ptr; // TODO temporary until code type enumeration implemented enum class Type { - IntFunc = 1, - Constant, + Constant = 1, DefFunc, DefFuncNoArgs, NoParen, @@ -61,6 +60,7 @@ enum TableFlag : unsigned Two_Flag = 1u << 7, // code can have two words or characters Command_Flag = 1u << 8, // code is a command Operator_Flag = 1u << 9, // code is an operator + Function_Flag = 1u << 10, // code is a BASIC function EndStmt_Flag = 1u << 31 // end statement }; @@ -158,6 +158,10 @@ class TableEntry return hasFlag(Command_Flag) && !hasFlag(Reference_Flag) && !isOperator(); } + bool isFunction() const + { + return hasFlag(Function_Flag); + } int precedence() const { return m_precedence; diff --git a/test_ibcp.cpp b/test_ibcp.cpp index f8a10a2..01b0c34 100644 --- a/test_ibcp.cpp +++ b/test_ibcp.cpp @@ -657,12 +657,14 @@ static const char *tokenTypeName(const TokenPtr &token) { return "Operator"; } + else if (token->isFunction()) + { + return "IntFunc"; + } else { switch (token->type()) { - case Type::IntFunc: - return "IntFunc"; case Type::Constant: return "Constant"; case Type::DefFunc: @@ -701,13 +703,16 @@ void Tester::printToken(const TokenPtr &token) m_cout << " |" << token->string() << '|'; } } + else if (token->isFunction()) + { + m_cout << (token->operandCount() == 0 ? " " : "()") << ' ' + << std::setw(7) << dataTypeName(token->dataType()) << " " + << token->debugName(); + } else { switch (token->type()) { - case Type::IntFunc: - m_cout << (token->operandCount() == 0 ? " " : "()"); - break; case Type::Constant: case Type::NoParen: case Type::DefFuncNoArgs: @@ -748,9 +753,6 @@ void Tester::printToken(const TokenPtr &token) } m_cout << " |" << token->string() << '|'; break; - case Type::IntFunc: - m_cout << " " << token->debugName(); - break; } } m_cout << std::endl; diff --git a/token.h b/token.h index 4db4808..9c27593 100644 --- a/token.h +++ b/token.h @@ -216,6 +216,10 @@ class Token { return m_entry->isCommand(); } + bool isFunction() const + { + return m_entry->isFunction(); + } int precedence() const { return m_entry->precedence(); diff --git a/translator.cpp b/translator.cpp index 82e259f..0a1b9c3 100644 --- a/translator.cpp +++ b/translator.cpp @@ -342,15 +342,8 @@ bool Translator::getOperand(DataType dataType, Reference reference) throw TokenError {expectedErrorStatus(dataType, reference), std::move(m_token)}; } - switch (m_token->type()) + if (m_token->isFunction()) { - case Type::Constant: - break; // go add token to output and push to done stack - - case Type::NoParen: - break; // go add token to output and push to done stack - - case Type::IntFunc: if (reference != Reference::None) { if (reference != Reference::All || !m_token->hasFlag(SubStr_Flag)) @@ -370,38 +363,49 @@ bool Translator::getOperand(DataType dataType, Reference reference) processInternalFunction(reference); doneAppend = false; // already appended } - break; - - case Type::DefFunc: - case Type::DefFuncNoArgs: - if (reference == Reference::Variable) - { - throw TokenError {expectedErrorStatus(dataType, reference), - std::move(m_token)}; - } - if (m_parse->getParen()) + } + else + { + switch (m_token->type()) { - if (reference != Reference::None) + case Type::Constant: + break; // go add token to output and push to done stack + + case Type::NoParen: + break; // go add token to output and push to done stack + + case Type::DefFunc: + case Type::DefFuncNoArgs: + if (reference == Reference::Variable) { - // NOTE these are allowed in the DEF command - // just point to the open parentheses of the token - TokenPtr token = std::move(m_token); - throw TokenError {Status::ExpEqualOrComma, token->column() - + token->length(), 1}; + throw TokenError {expectedErrorStatus(dataType, reference), + std::move(m_token)}; + } + if (m_parse->getParen()) + { + if (reference != Reference::None) + { + // NOTE these are allowed in the DEF command + // just point to the open parentheses of the token + TokenPtr token = std::move(m_token); + throw TokenError {Status::ExpEqualOrComma, token->column() + + token->length(), 1}; + } + processParenToken(); + doneAppend = false; // already appended } + break; // go add token to output and push to done stack + + case Type::Paren: + m_parse->getParen(); processParenToken(); doneAppend = false; // already appended - } - break; // go add token to output and push to done stack - - case Type::Paren: - m_parse->getParen(); - processParenToken(); - doneAppend = false; // already appended - break; + break; - default: - throw TokenError {Status::BUG_NotYetImplemented, std::move(m_token)}; + default: + throw TokenError {Status::BUG_NotYetImplemented, + std::move(m_token)}; + } } if (doneAppend)