From f139e6457c62b0e43c63bc94ff600eb0cf48cc2f Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 23 Jan 2008 23:45:42 +0000 Subject: [PATCH] added: a short form of multiplication (without the '*' character) e.g. '5y', (it's used only if the second parameter is a variable or function) changed: variables and functions are case-sensitive now added: variables and functions can have underline characters in their names git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@61 e52654a7-88a9-db11-a3e9-0013d4bc506e --- ttmath/ttmathobjects.h | 2 +- ttmath/ttmathparser.h | 53 ++++++++++++++++++++++++++++++------------ ttmath/ttmathtypes.h | 3 ++- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/ttmath/ttmathobjects.h b/ttmath/ttmathobjects.h index 0d76a10..7cfdd28 100644 --- a/ttmath/ttmathobjects.h +++ b/ttmath/ttmathobjects.h @@ -102,7 +102,7 @@ public: if( (c>='a' && c<='z') || (c>='A' && c<='Z') ) return true; - if( can_be_digit && (c>='0' && c<='9') ) + if( can_be_digit && ((c>='0' && c<='9') || c=='_') ) return true; return false; diff --git a/ttmath/ttmathparser.h b/ttmath/ttmathparser.h index dfacefa..9a557ec 100644 --- a/ttmath/ttmathparser.h +++ b/ttmath/ttmathparser.h @@ -66,7 +66,7 @@ namespace ttmath x = [+|-]Value[operator[+|-]Value][operator[+|-]Value]... where: an operator can be: - ^ (pow) (the highest priority) + ^ (pow) (almost the heighest priority, look below at 'short mul') * (mul) / (div) (* and / have the same priority) @@ -85,6 +85,12 @@ namespace ttmath || (logical or) (the lowest priority) + short mul: + or if the second Value (Var below) is either a variable or function there cannot be + an operator between them, e.g. + [+|-]ValueVar is treated as [+|-]Value * Var and the multiplication + has the greatest priority: 2^3m equals 2^(3*m) + and Value can be: constant e.g. 100 @@ -106,6 +112,7 @@ namespace ttmath for separating parameters "1 < 2" (the result will be: 1) "4 < 3" (the result will be: 0) + "2+x" (of course if the variable 'x' is defined) etc. we can also use a semicolon for separating any 'x' input strings @@ -122,13 +129,15 @@ class Parser private: /*! - there are 5 mathematical operators as follows: + there are 5 mathematical operators as follows (with their standard priorities): add (+) sub (-) mul (*) div (/) pow (^) - with their standard priorities + and 'shortmul' used when there is no any operators between + a first parameter and a variable or function + (the 'shortmul' has the greatest priority e.g. '5^3m' equals '5^(3*m)' ) */ class MatOperator { @@ -136,7 +145,7 @@ private: enum Type { - none,add,sub,mul,div,pow,lt,gt,let,get,eq,neq,lor,land + none,add,sub,mul,div,pow,lt,gt,let,get,eq,neq,lor,land,shortmul }; @@ -181,6 +190,10 @@ private: priority = 14; break; + case shortmul: + priority = 20; + break; + default: Error( err_internal_error ); break; @@ -1378,8 +1391,6 @@ return c; } - - /*! this method read the name of a variable or a function @@ -1396,29 +1407,31 @@ int character; result.erase(); - character = ToLowerCase(*pstring); + character = *pstring; /* - the first letter must be from range 'a' - 'z' + the first letter must be from range 'a' - 'z' or 'A' - 'Z' */ - if( character<'a' || character>'z' ) + if( ! (( character>='a' && character<='z' ) || ( character>='A' && character<='Z' )) ) Error( err_unknown_character ); do { result += character; - character = ToLowerCase( * ++pstring ); + character = * ++pstring; } while( (character>='a' && character<='z') || - (character>='0' && character<='9') ); + (character>='A' && character<='Z') || + (character>='0' && character<='9') || + character=='_' ); SkipWhiteCharacters(); /* - if there's character '(' that means this name is a name of a function + if there's a character '(' that means this name is a name of a function */ if( *pstring == '(' ) { @@ -1499,7 +1512,6 @@ const char * new_stack_pointer; } - /*! this method converts the character ascii c into the value in range <0;base-1> @@ -1592,7 +1604,7 @@ int character; /* warning: if we're using for example the base equal 16 - we can find a first character like 'e' that is there is not e=2.71.. + we can find a first character like 'e' that is not e=2.71.. but the value 14, for this case we must use something like var::e for variables (not implemented yet) */ @@ -1632,6 +1644,7 @@ void InsertOperatorToTable(const std::string & name, typename MatOperator::Type operators_table.insert( std::make_pair(name, type) ); } + /*! this method creates the table of operators */ @@ -1732,6 +1745,14 @@ int ReadOperator(Item & result) result.type = Item::semicolon; ++pstring; } + else + if( (*pstring>='a' && *pstring<='z') || (*pstring>='A' && *pstring<='Z') ) + { + // short mul (without any operators) + + result.type = Item::mat_operator; + result.moperator.SetType( MatOperator::shortmul ); + } else ReadMathematicalOperator(result); @@ -1794,6 +1815,7 @@ int res; break; case MatOperator::mul: + case MatOperator::shortmul: if( value1.Mul(value2) ) Error( err_overflow ); break; @@ -1811,6 +1833,7 @@ int res; break; + default: /* on the stack left an unknown operator but we had to recognize its before @@ -1913,7 +1936,7 @@ return kod; /*! this method calculate how many parameters there are on the stack - and index of the first parameter + and the index of the first parameter if there aren't any parameters on the stack this method returns 'size' equals zero and 'index' pointing after the first bracket diff --git a/ttmath/ttmathtypes.h b/ttmath/ttmathtypes.h index dc96e2b..22e6b9c 100644 --- a/ttmath/ttmathtypes.h +++ b/ttmath/ttmathtypes.h @@ -248,7 +248,8 @@ namespace ttmath err_object_exists, err_unknown_object, err_still_calculating, - err_too_big_factorial + err_too_big_factorial, + err_in_short_form_used_function };