File size: 4,762 Bytes
d086025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Facsimile

A simple demonstrative Lisp-like language. Facsimile provides an interpreter
for a small language based on Lisp's paradigms and syntax, usable through a
simple command-line interface.

## Command-line usage

```
facsimile <entrypoint-source-file> [arg ...]
```

The first command-line argument is the path to the Facsimile source file to
interpret. Any additional arguments are passed to the program. The program
exits with status 1 (and prints an error to stderr) if the source file cannot
be opened or if evaluation fails.

### The `args` variable

The CLI automatically defines a special variable, `args`, available to the
program as a list of strings. Its first element is the path of the entrypoint
source file, followed by any other command-line arguments. `args` therefore
always contains at least one element.

### Examples

```
facsimile examples/factorial.fax 5
facsimile examples/fizzbuzz.fax
facsimile examples/lambda.fax
```

See the example programs in the `assets/examples` directory.

## Language overview

### Syntax

The elementary part of the syntax is the *atom*: a basic data value.

- **Numbers** are single-precision IEEE-754 floats, given in standard decimal
  representation, optionally with exponents.
- **Strings** are Unicode strings, beginning and ending with either `"` or `'`,
  and may contain escapes formed of two characters whose first is a backslash.
- **Booleans** are the atoms `true` and `false`.
- **Symbols** are alphanumeric identifiers (underscores allowed). They can
  reference other data or functions, but can also be treated as data.

Program structure is formed by **lists**: whitespace-separated atoms enclosed in
regular brackets (`(` and `)`). The special atom `nil` is equivalent to an
empty list (`()`).

When a list represents code, its first element is a symbol identifying the
function to call (or a lambda function), and the remaining elements are the
arguments. Any argument that is a list is evaluated as code before being passed
to the function; any argument that is a symbol is resolved as a variable when
possible.

Comments are available either as single-line (preceded by `//`) or continuous
(commencing with `/*` and ending with `*/`).

### Special functions

- `if` — control flow. Takes one or more condition/block pairs, each governed by
  a condition, plus an optional final (`else`) block. Conditions are checked in
  order; the first that returns a truthy value has its following block
  evaluated, and `if` returns that block's result. If no condition is truthy,
  the final block is evaluated if provided, otherwise `nil` is returned.

  ```
  (if
    a           // if a {
    (print "a") //   println("a");
    b           // } else if b {
    (print "b") //   println("b");
    c           // } else if c {
    (print "c") //   println("c");
    (print "x") // } else {
  )             // }
  ```

- `and` (aliased as `all`) and `or` (aliased as `any`) — short-circuiting
  logical operators. `and` requires all of its arguments to be truthy and
  returns the first non-truthy argument, or `nil` if all are truthy. `or`
  requires any argument to be truthy and returns the first truthy argument, or
  `nil` if none are truthy.

- `def` and `fun` — define user functions. Both use the same syntax except that
  `def` takes the intended function name as its first argument. The first
  (`fun`) / second (`def`) argument is a list of symbols naming the parameters.
  The remaining arguments form the body and are evaluated in order; the result
  of the last body expression is returned to the caller.

  ```
  (def myfunction (a b c) // fn myfunction(a, b, c) {
    (print a)             //   println(a);
    (add a b c)           //   return a + b + c;
  )                       // }
  ```

  Functions can be called by their symbol, which delays dereferencing:

  ```
  (def callwith1 (f n) (call f n 1))
  (print 1 2 (callwith1 (quote add) 2))
  ```

- `quote` — returns its first argument without evaluating it, e.g. to pass a
  symbol or list to a function as data.

- `list` — constructs a list without evaluating its elements.

  ```
  (somefunction ("hello" "world"))         // error: "hello" isn't callable
  (somefunction (quote ("hello" "world"))) // works
  (somefunction (list "hello" "world"))    // works
  ```

- `block` — evaluates a sequence of expressions, returning the result of the
  last one.

  ```
  (if a
    (block
      (print "abc")
      (print "def")
    )
  )
  ```

### Standard library

The standard library is automatically available in all programs and provides:

- `not`
- `eq`
- `ne`
- `lt`
- `gt`
- `lte`
- `gte`
- `add`
- `sub`
- `mul`
- `div`
- `rem`
- `get`
- `num`
- `fmt`
- `cat`
- `print`
- `input`
- `srand`
- `rand`