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
This commit is contained in:
Tomasz Sowa 2008-01-23 23:45:42 +00:00
parent 29bb4fb3f7
commit f139e6457c
3 changed files with 41 additions and 17 deletions

View File

@ -102,7 +102,7 @@ public:
if( (c>='a' && c<='z') || (c>='A' && c<='Z') ) if( (c>='a' && c<='z') || (c>='A' && c<='Z') )
return true; return true;
if( can_be_digit && (c>='0' && c<='9') ) if( can_be_digit && ((c>='0' && c<='9') || c=='_') )
return true; return true;
return false; return false;

View File

@ -66,7 +66,7 @@ namespace ttmath
x = [+|-]Value[operator[+|-]Value][operator[+|-]Value]... x = [+|-]Value[operator[+|-]Value][operator[+|-]Value]...
where: where:
an operator can be: an operator can be:
^ (pow) (the highest priority) ^ (pow) (almost the heighest priority, look below at 'short mul')
* (mul) * (mul)
/ (div) (* and / have the same priority) / (div) (* and / have the same priority)
@ -85,6 +85,12 @@ namespace ttmath
|| (logical or) (the lowest priority) || (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: and Value can be:
constant e.g. 100 constant e.g. 100
@ -106,6 +112,7 @@ namespace ttmath
for separating parameters for separating parameters
"1 < 2" (the result will be: 1) "1 < 2" (the result will be: 1)
"4 < 3" (the result will be: 0) "4 < 3" (the result will be: 0)
"2+x" (of course if the variable 'x' is defined)
etc. etc.
we can also use a semicolon for separating any 'x' input strings we can also use a semicolon for separating any 'x' input strings
@ -122,13 +129,15 @@ class Parser
private: private:
/*! /*!
there are 5 mathematical operators as follows: there are 5 mathematical operators as follows (with their standard priorities):
add (+) add (+)
sub (-) sub (-)
mul (*) mul (*)
div (/) div (/)
pow (^) 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 class MatOperator
{ {
@ -136,7 +145,7 @@ private:
enum Type 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; priority = 14;
break; break;
case shortmul:
priority = 20;
break;
default: default:
Error( err_internal_error ); Error( err_internal_error );
break; break;
@ -1378,8 +1391,6 @@ return c;
} }
/*! /*!
this method read the name of a variable or a function this method read the name of a variable or a function
@ -1396,29 +1407,31 @@ int character;
result.erase(); 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 ); Error( err_unknown_character );
do do
{ {
result += character; result += character;
character = ToLowerCase( * ++pstring ); character = * ++pstring;
} }
while( (character>='a' && character<='z') || while( (character>='a' && character<='z') ||
(character>='0' && character<='9') ); (character>='A' && character<='Z') ||
(character>='0' && character<='9') ||
character=='_' );
SkipWhiteCharacters(); 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 == '(' ) 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> this method converts the character ascii c into the value in range <0;base-1>
@ -1592,7 +1604,7 @@ int character;
/* /*
warning: warning:
if we're using for example the base equal 16 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 but the value 14, for this case we must use something like var::e for variables
(not implemented yet) (not implemented yet)
*/ */
@ -1632,6 +1644,7 @@ void InsertOperatorToTable(const std::string & name, typename MatOperator::Type
operators_table.insert( std::make_pair(name, type) ); operators_table.insert( std::make_pair(name, type) );
} }
/*! /*!
this method creates the table of operators this method creates the table of operators
*/ */
@ -1732,6 +1745,14 @@ int ReadOperator(Item & result)
result.type = Item::semicolon; result.type = Item::semicolon;
++pstring; ++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 else
ReadMathematicalOperator(result); ReadMathematicalOperator(result);
@ -1794,6 +1815,7 @@ int res;
break; break;
case MatOperator::mul: case MatOperator::mul:
case MatOperator::shortmul:
if( value1.Mul(value2) ) Error( err_overflow ); if( value1.Mul(value2) ) Error( err_overflow );
break; break;
@ -1811,6 +1833,7 @@ int res;
break; break;
default: default:
/* /*
on the stack left an unknown operator but we had to recognize its before 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 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 if there aren't any parameters on the stack this method returns
'size' equals zero and 'index' pointing after the first bracket 'size' equals zero and 'index' pointing after the first bracket

View File

@ -248,7 +248,8 @@ namespace ttmath
err_object_exists, err_object_exists,
err_unknown_object, err_unknown_object,
err_still_calculating, err_still_calculating,
err_too_big_factorial err_too_big_factorial,
err_in_short_form_used_function
}; };