infix.lsp

Module index

source download

Module: infix.lsp

Infix expression parser

Version: 2.1 - comments redone for automatic documentation
Version: 2.2 - fixed bug for trailing lower priority ops
Version: 2.3 - doc changes
Author: Lutz Mueller 2006-2010

Infix expression parser

Parses infix, prefix or postfix expressions given in strings and returns a newLISP expressions, which can be evaluated; captures syntax errors.

At the beginning od the program using this module include the following statement:
 (load "/usr/share/newlisp/modules/infix.lsp")
 ; or shorter
 (module "infix.lsp")
 


§

INFIX:xlate

syntax: (INFIX:xlate str-expression [context-target])
parameter: str-expression - The infix expression in a string
parameter: context-target - An optional context as compile taret.

return: A newLISP expression or nil on failure.

When nil is returned then the error message is in result. As an optional second parameter a target context can be passed, if not used, MAIN is assumed.

Note that the parser requires operators, variables and constants surrounded by spaces except where parenthesis are used.

Example:
 (INFIX:xlate "3 + 4") => (add 3 4)  parses infix
 (INFIX:xlate "+ 3 4") => (add 3 4)  parses prefix s-expressions
 (INFIX:xlate "3 4 +") => (add 2 4)  parses postfix

 (INFIX:xlate "3 + * 4") => "ERR: missing argument for +"

 (eval (INFIX:xlate "3 + 4")) => 7

 (INFIX:xlate "(3 + 4) * (5 - 2)")  => (mul (add 3 4) (sub 5 2))

 (INFIX:xlate "(a + b) ^ 2 + (a - b) ^ 2") => (add (pow (add a b) 2) (pow (sub a b) 2))

 (INFIX:xlate "x = (3 + sin(20)) * (5 - 2)")  => (setq x (mul (add 3 (sin 20)) (sub 5 2)))

 (INFIX:xlate "x = (3 + sin(10 - 2)) * (5 - 2)")  
         => (setq x (mul (add 3 (sin (sub 10 2))) (sub 5 2)))


- ∂ -

generated with newLISP  and newLISPdoc