view doc/syntax.md @ 143:f30fbc9202c2

doc: Minor updates about if, function calls
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 03 Sep 2019 12:14:51 +0200
parents 7f331aa5abd1
children aa6847f62505
line wrap: on
line source

@page Syntax

## Syntax

### 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.

### 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.

#### `if`

```
(if cond (then-exprs) [(else-exprs)])
```

Where `cond` evaluates to a boolean value (numeric 1 or 0) and `then`, `else`
are expressions. If multiple expressions should be executed, `seq` can be used
to chain them.

`cond` should evaluate to either `'true`/`'false` or an integer (0 is false,
everything else is true).

#### `seq`

```
(seq expr expr ...)
```

Execute `expr`s and return the result of the last `expr`.

### Arithmetic operations

```
(+ a b)
(* a b)
(- a b)
(/ a b)
```

where `a` and `b` are numeric expressions.

### I/O

#### Output

```
(print a ...)
(println a ...)
```

Print the arguments formatted to stdout, separated by no spaces.