/
bootstrap.sugar
67 lines (54 loc) · 2.3 KB
/
bootstrap.sugar
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
_-
Sugar Prelude Bootstrap File
(c) 2012 Breckin Loggins
The first character in this file is the "Null" character. Everytime the discriminator
sees it, it will push Null on the stack (in other words, it's short for the null op).
Everything below can be ignored by giving Sugar the null op instead of another character.
This will cause the thing Sugar wants to create to not be created or bound to a character.
For example, if you don't need quotation, you can give Sugar the null character when it
expects a character to define quotation.
Interestingly, this implies that the minimum sugar file is a single character (including
what you normally think of as whitespace) repeated until the interpreter tells you
that Sugar is bootstrapped. Of course, you can't do anything with that language but
repeatedly push null on the stack.
The second character is the "ignore" character. Every time
Sugar sees this character, it ignores all input until it sees it again. Thus, the
first syntactic construct we have is a comment.
The next two characters after the end of this comment define the command (op) trigger
character, and the quoting character, respectively.
After that, we teach Sugar about whitespace. Sugar will read a set of characters and
add each one as a whitespace reader. It will continue until it sees the Null characer.
Thus the literal characters after the quote are
SPC TAB NEWLINE_. The following hyphen starts the next comment...
-!`
_-
At this point the compiler has a notion of a "symbol". A symbol is a collection of
one or more contiguous non whitespace characters. The environment already has several
symbols bound to opcodes, types, and readers, so we now have enough syntax to execute
commands and create macros.
-
- dup: duplicates the top item on the stack -
`!pushdictionary
`tmp `!set
`tmp `!get
`tmp `!get
`!popdictionary
!createmacro
dup
!set
- swap: reverses the order of the top two items on the stack -
`!pushdictionary
`tmp1 `!set
`tmp2 `!set
`tmp1 `!get
`tmp2 `!get
`!popdictionary
!createmacro
swap
!set
- The reader macro. Invokes a reader by the given name -
`symbol `!reader `!read - Invoke the symbol reader -
`!reader `!read - Invoke the reader by the given symbol -
!createmacro - Create the macro with the quoted stack contents -
#
!set - Set the macro binding to the # character -