I'd like to make a simple infix expression evaluator. It should be able to support simple functions, such as *, +, -,/ as well as simpl recursion. That is, if I define G <- 19 H <- G + 6 evaluate G shoulr return 25 Anyone know a good webpage for some info? cheers, Micha __________________________________________ Micha Pringle Systems Development TISCOM Systems Albisriederstrasse 365 CH-8047 Zurich Switzerland Tel: +41 (0)1 405 60 80 Fax: +41 (0)1 405 60 99 e-mail: micha.pringle@tiscom.com __________________________________________
Hello! On Wed, Nov 15, 2000 at 04:25:35PM +0100, Micha Pringle wrote:
I'd like to make a simple infix expression evaluator. It should be able to support simple functions, such as *, +, -,/ as well as simpl recursion. That is, if I define G <- 19 H <- G + 6 evaluate G shoulr return 25 Anyone know a good webpage for some info?
I don't know about a good web page. However nearly every book on compiler design will handle examples like yours, usually using parser and/or scanner generators. You can do the same in Haskell, as there are parser generators (happy) and scanner generators (alex) available. For simple expressions as in your example you could evaluate the commands immediately (if a command is either a variable assignment or an expression), for example by having the parser for command *list* return something like type CommandListResult = [CommandResult] type CommandResult = VariableState -> IO VariableState type Variablestate = [(String, Value)] type Value = Int -- or, if you have multiple, dynamic, value types, -- then it's data Value = ValInt Int | ... However, if you want to define functions, do loop constructs, etc., you'll have a much easier way to build some syntax tree, perhaps even transform it a bit, and then write an evaluator for the tree. In interactive mode, you'll have to do it even a bit different. Then, you must have a state containing function definitions variable contents each command can define a function (altering the "function definitions" part of the state), evaluate an expression (altering nothing except if it calls a side-effecting function) or set a variable (altering the "variable contents" part of the state). Probably, the function definitions is an alist of name -> (list of formal parameters, some representation of the function body) Kind regards, Hannah.
participants (2)
-
Hannah Schroeter -
Micha Pringle