home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / basic / fastp12.zip / EVAL.BAS < prev    next >
BASIC Source File  |  1993-04-21  |  2KB  |  53 lines

  1. '***********************************************************************
  2. '                           Fast Parser 1.2
  3. '              Copyright (C) 1992, 1993 by Daniel Corbier
  4. '                         All rights reserved.
  5. '
  6. ' This example features:
  7. '
  8. ' UserFunction()  Allows the user to define a function.
  9. '
  10. ' Eval##()  Evaluates the user expression.  It is a little bit easier
  11. '           to implement than Evaluate##(), and is appropriate where
  12. '           the speed of a loop is not important.
  13. '
  14. ' EqError%  Returns an error number if an invalid expression is entered.
  15. '***********************************************************************
  16. $Include"calc.inc"
  17.  
  18. Print "At the prompt, you can either enter:"
  19. Print "an expression like 'sin(3)+(5-6)^2)'"
  20. Print "a variable assignment like 'length = 20'"
  21. Print "or a function assignment like 'inv(x) = 1/x'"
  22.  
  23. Do
  24.     Print
  25.     Input "Expression:  ", Expr$
  26.  
  27.     If instr(Expr$,"(") > 0 and instr(Expr$,"(") < instr(Expr$,"=") then
  28.         Call UserFunction( Expr$ )
  29.  
  30.        ElseIf instr(Expr$,"=") > 0 then      ' Assign a variable value
  31.         EqualSign%            = instr(Expr$,"=")
  32.         VarName$(variable%)   = left$(Expr$,EqualSign%-1)
  33.         VarValue##(variable%) = Eval##(mid$(Expr$,EqualSign%+1))
  34.         Incr variable%
  35.  
  36.        Else
  37.         Answer## = Eval##( Expr$ )
  38.     End If
  39.  
  40.     Select Case EqError%
  41.         Case 0:     Print Answer##
  42.  
  43.         Case 1:  Print "Undefined function"
  44.         Case 2:  Print "Mismatched parenthesis"
  45.         Case 3:  Print "Undefined variable"
  46.         Case 4:  Print "Invalid binary number"
  47.         Case 5:  Print "Invalid octal number"
  48.         Case 6:  Print "Invalid hexadecimal number"
  49.         Case 7:  Print "Factorial overflow"
  50.         Case 9:  Print "Invalid expression"
  51.         Case 10: Print "Function definition space is full"
  52.     End Select
  53. Loop while Expr$ <> ""