82Fermer84
PpHdLe 07/10/2009 à 21:46
Réécriture de l'algo utilisé:

  /* We can have:
     + an operator: + - * / ^
     + a variable: starts with a letter
     + a function: starts with a lettre, finished by a '('
     + an int: starts with a number.
     + a float: starts with a number. Contains 'E' and '.'.
     + a list: starts with '{'
     + a parenthesis: starts with '('

     We use:
       + a stack of operande
       + a stack of operator.

     Read a char:
     Phase 1: Get the new operand.
      Skip SPACE
      Read negate operator, and skip it.
      If it is a letter, scans for variable/function.
         If it is a variable, push it in the operand stack.
	 If it is a function, parse the arguments as a list - allow list-
	 and push it in the operand stack.
      If it is a number, scans for 'E' or '.' while there are digits, and then:
         Build Int and push in the operand stack.
	 Build Float and push in the operand stack.
      If it is a parenthesis, parse the sub-express recursively, check for and ending ')'   and push in the operand stack.
      Else stop parsing (Pop last Operator before finishing).
      If there was a negate operator, negate the pushed operator
       (Well in fact, it is a little big more complicated due to -x^-2 ;) )

     Phase 2: Evaluate the old operators if needed and push the new one.
      Skip SPACE.
      If it is an operator, add it in the operator stack so that
         all pushed operators have bigger priority than this one.
	 If an already pushed operator has higher or equal priority
	 Then eval the operator (pop it) over the two pushed operands (pop them),
	 and push the new evaluated operand.
      Else stop parsing.

      Go to Phase 1 to read the new operand.

      Phase Finish:
        Finish by evaluating the pushed operators:
            While OpcRef>=1
               func = Opc[--OpcRef]
               op   = func( Op[--OpRef], Op[--OpRef])
               OpRef[OpRef++] = op