Mercurial > lbo > hg > ylisp
view doc/syntax.md @ 168:5697df53fe81
bin: Add interpreter binary src/bin/ylisp.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Tue, 03 Sep 2019 20:06:59 +0200 |
parents | aa6847f62505 |
children | 263256062455 |
line wrap: on
line source
@page Syntax Syntax ## Syntax Comments start with `--` and end at the end of a line. ### Definitions The following IDs are represented as `YBUILTIN_TYPE` in `yexpr_t` values. Each of them has a `ybuiltin_fn` associated with it that can be called with a pointer to the call stack, executing the defined logic. #### `defn`: Define function ``` (defn name (arg arg ...) (body body) (body) ...) ``` Result of the function is the last expression. The name is visible in the same and all lower scopes, where a scope is an s-expr. NOTE: This is only used during preprocessing. All expressions of this type shall be removed before the execution phase. #### `let`: Assign variable ``` (let var exprs.. expr) ``` The name is visible in the same and all lower scopes, where a scope is an s-expr. The value of the last `expr` will be assigned to the variable. Functions are also normal values that can be assigned. ### Control structures #### `for` ``` (for var (list-item list-item list-item) (expr ...) (expr ...) ...) ``` Run the given expressions once for every `list-item`. `var` is bound in the expressions to the respective item. The result of a for expression is a list of the value of the last expression in each iteration. ``` (defn fac (n) (if (== n 0) 1 ((* n (fac (- n 1)))))) (print (for i (1 2 3 4 5 6 7 8 9 10) (fac i))) -- prints (1 2 6 24 120 720 5040 40320 362880 3628800 ) ``` #### `if` ``` (if cond (then-exprs) [(else-exprs)]) ``` Where `cond` evaluates to a boolean value (`'true`/`'false` or `1`/`0`). `then-exprs` can be a single item (`(if (== 55 55) 1 2)`) or a list of expressions, of which the last will be returned (and all will be evaluated): (`(if (== 55 55) ((print 'hello 'world) (+ 22 3)))`) `cond` should evaluate to either `'true`/`'false` or an integer (0 is false, everything else is true). ### Arithmetic operations ``` (+ a b) (* a b) (- a b) (/ a b) ``` where `a` and `b` are numeric expressions. ### I/O #### Output ``` (print a ...) -- not yet implemented: (println a ...) ``` Print the arguments formatted to stdout, separated by spaces. Strings are printed literally, without quotes.