Advertisement
Guest User

Expression Evaluator (very bare bones)

a guest
Sep 5th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. //For right now this doesn't check variable names
  2. //And will only work on real expressions once parsed
  3. //IE 'IF 5+1==6 Then Print Hi'
  4. //A step to extract the values from variable names
  5. //And replace the tokens with their real values
  6. //Will solve this, but only with math and variables. There's still commands to parse within expressions and functions
  7.  
  8.  
  9. //This is mostly for handling an expression of the type if expression
  10. function evaluate_expression(tokenslist$() )
  11. state=false //Assume false
  12. comparison = false
  13. //The comparison operator wasn't found yet
  14.  
  15. tokenamount = getarrayelements(tokenslist$() )
  16.  
  17. //Begin the iteration over the tokens to attempt to make sense of the input
  18. //We expect equals ==
  19. //We need to find the equals sign
  20.  
  21. //Find first occurence of equals sign
  22. for t=1 to tokenamount
  23. if tokenslist$(t) ="="
  24. firstsign = t
  25. exitfor
  26. endif
  27.  
  28. next t
  29.  
  30. //We have found the first equal sign.
  31. //We now need to make sure we are dealing with a comparison
  32. if tokenslist$(firstsign+1)="=" then comparison=true
  33. if tokenslist$(firstsign+1)<>"="
  34. print "Expected == operator but found = instead in expression. "
  35.  
  36. endif
  37.  
  38. //Generate the left string
  39. for a=1 to firstsign-1
  40. leftside$ = leftside$ + tokenslist$(a)
  41. next a
  42.  
  43. //Generate the right string
  44. for b=firstsign+1 to tokenamount
  45. rightside$ = rightside$ + tokenslist$(b)
  46. next b
  47.  
  48. //Run them both through simple math
  49. result_a = simplemath(leftside$)
  50. result_b = simplemath(rightside$)
  51.  
  52. //Evaluate the situation
  53. if result_a = result_b then state=true
  54. if result_a <> result_b then state=false
  55.  
  56. //Return the states
  57. endfunction state, comparison
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement