; ; @ANO_SCRIPT_NAME expression ; @ANO_SCRIPT_VERSION 0.0.1 ; @ANO_SCRIPT_DESCRIPTION LISP-style expression evaluation example ; ; @ANO_FLAGS_USE_PROTOS [ ] ; @ANO_FLAGS_VAR_NAME_SUBS [ ] ; @ANO_FLAGS_VAR_WARN_UNUSED [ ] ; ; Copyright (c) 2016-2026, Jani Salonen ; All rights reserved. ; ; An example of executing LISP-style expressions and examining their results. ; Individual expressions can be executed in the same environment, allowing the ; state from one expression to persist for the next. This enables, for example, ; the use of definitions such as 'define'. ; main [exit: 0] { print "\nExpression example 1:\n\n" ; Send greeting to expr_func function, let it add three parameters ; together, and add 10 to it. ; mov _exp ("\ (add 10 (expr_func [int] 20 (add 30 (sub 40 5)) 50 \ \"Hello, \\\"world!\\\"\")) \ ") ; We want new environment for expression, hence handle is invalid. ; expr_eval (\ expr_handle: INVALID, \ expr: _exp) ; Save expression handle needed to fetch the result. ; mov _env (rc) ; Fetch the result... ; expr_ret (expr_handle: _env) print " 1.1. Expression result should be 145: " . rc . ".\n" ; ...and close expression handle. ; expr_free (expr_handle: _env) ; Another round, reuse the environment. ; print "\nExpression example 2:\n\n" mov _exp_a ("(define pi 3.14159265358979323846)") mov _exp_b ("(define radius 123)") mov _exp_c ("(mul pi radius radius)") ; If x is between 30 and 50, say 'Yes'. ; mov _exp_d ("(define x 42)") mov _exp_e ("(if (> x 50) 100 0)") mov _exp_f ("(if (and (> x 30) (< x 50)) \"Yes\" \"No\")") ; Multiply 100 by two and then by x. Variable must be in global scope ; when used in expression. ; mov hundred (100) mov _exp_g ("(mul $hundred 2 x)") ; Define z based on y. ; mov _exp_h ("(define y (add 10 (sub 20 5)))") mov _exp_i ("(define z (if (and (> y 20) (< y 30)) \ \"y is between 20 and 30\" \"y is something else\")") ; We want new environment for first expression, hence handle is ; invalid. ; expr_eval (INVALID, _exp_a) mov _env (rc) ; Pass the environment handle to the following expressions. ; expr_eval (_env, _exp_b) expr_eval (_env, _exp_c) expr_ret (_env) print " 2.1. Circle area is: " . rc . "\n" ; expr_eval (_env, _exp_d) expr_eval (_env, _exp_e) expr_eval (_env, _exp_f) expr_ret (_env) print " 2.2. Is x between 30 and 50? " . rc . "\n" ; expr_eval (_env, _exp_g) expr_ret (_env) print " 2.3. Multiply 100 by two and then by x: " . rc . "\n" ; expr_eval (_env, _exp_h) expr_eval (_env, _exp_i) expr_ret (_env) print " 2.4. Is y between 20 and 30? " . rc . "\n\n" ; ...and close handle. ; expr_free (_env) } function expr_func (_first, _second, _third, _greeting) { print "Expression say: " . _greeting . "\n" print "We return the sum of " . _first . ", " . _second . \ " and " . _third . " to the caller.\n\n" ret _first + _second + _third }