-
Notifications
You must be signed in to change notification settings - Fork 0
/
insomniac_as
174 lines (130 loc) · 6.86 KB
/
insomniac_as
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
() ( -- () ) Puts an empty pair on the stack
[0-9]+ ( -- num ) Puts a numeric literal on the stack
#\<C> ( -- c ) Puts a character literal on the stack
#t #f ( -- b ) Puts true or false on the stack
"Hi" ( -- s ) Puts a string literal on the stack
s"Hi" ( -- sym ) Put a symbol literal on the stack
out ( obj -- ) Outputs the object on top of the stack to stdout
printf. This instruction should only be used for
debugging purposes.
open ( path write -- fd ) Open the file at path in read (wrtie false) or write (write true)
and leave a file descriptor the stack.
write ( u8 num fd -- written ) Write num bytes from u8 to the file represented by fd leaving the number
of bytes actually written on the stack.
read ( num fd -- u8 ) Read num bytes from the fd leaving a byte buffer on the stack of all the bytes
actually read. u8 is sized to the number of bytes actually read.
close ( fd -- ) Close the fd on the top of the stack.
nop ( -- ) Does nothing
dup ( a -- a a)
swap ( a b -- b a)
rot (c b a -- a c b)
depth ( -- depth ) How deep is the stack?
sym ( str -- sym ) Takes a string and makes it into a symbol
cons ( cdr car -- (car . cdr) ) Takes the top two stack entries and
makes them into a pair
car ( pair -- car ) Return the car of the pair
cdr ( pair -- cdr ) Return the cdr of the pair
set-car ( pair val -- pair ) Returns the pair with it's car modified
set-cdr ( pair val -- pair ) Returns the pair with it's cdr modified
vector ( size -- vector ) Makes a vector of the given size
vector-u8 ( size -- byte-vector ) Makes a byte vector of the given size
record ( size -- record ) Makes a record of the given size -- effectively a special vector
idx@ ( index vector -- obj ) Takes an index and vector/record/byte vector and
retrieves the object at the given
index
idx! ( index obj vector -- ) Takes an index, object, and vector/record/byte vector and
stores the object at the given index
vec-len ( vector -- size) Takes a vector as an argument and returns
the vectors length.
str->slice ( string -- slice ) Convert a string into bytevector slice.
u8->str ( u8 -- string ) Convert a bytevector into a string.
slice ( start end vector -- slice ) Given a vector or bytevector, return a vector of the same
type that is backed by the same data store as the original.
End is constrained by the length of the original vector.
jmp ( -label- ) Jumps to given label
jnf ( obj -label- ) Jumps to the given label if obj is not false
continue ( -label- ) Sets the location of an exception handling routine
restore ( -- ) Turn exception handling on from within an exception
handler, there is no checking on this so make sure you
only call it if you are in an exception handler that
has been called
call ( -label- closure ) Converts the location following this
instruction to closure and leaves it on the
stack then jumps to the label. Note: not tail
call safe!
proc ( -label- closure ) Creates a closure that begins execution at
the given label.
adopt ( child parent -- adopted ) Creates a new closure that has the bindings
of the parent and the coderef of the child.
jin ( closure -- ) Replace the executing code-ref with the one in
the closure object on the stack. This does not
modify bindings in the current environment and
does not leave a return closure on the stack.
NOTE: This instruction does lose the current
exception handler. This is a difference between
jin and jmp and is related to the how coderefs
are dealt with interanlly.
ret ( closure -- ) Return to the location left on the stack and
replace the current environment with the one
associated with that location.
call_in ( closure1 -- closure2 ) Converts the location following this
instruction to closure and leaves it
on the stack and enters the initial
closure.
NOTE: If the vm sees "call_in swap ret"
in the instruction stream, call_in is
treated as a tail call and does not return
as normal.
tail_call_in (ret args closure1 -- ) pops closure 1 off the stack, swaps ret and
args and then does a call to closure 1 (i.e.
new env ) but does not leave a return on the
stack.
slurp ( file -- string ) A crude routine to load a file into a string
and leave the entire thing on the stack. This
should not be used outside of the bootstrap
code.
import ( file -- bindings library ) Load an external dll and return an
alist of name to function id bindings
as well as a library object that can
be used with call_ext.
call_ext (<arguments> library func -- <returns>) Call a given function
from the given library
asm ( string -- proc ) Take a string and convert it into an assembled
proc in the current environment.
bind ( value sym -- ) Binds a symbol in the current environemnt
! ( value sym -- ) Stores a value at the given symbol, must already
exist
@ ( sym -- value ) Load the value stored in a symbol
+ ( a b -- a+b )
- ( a b -- a-b )
* ( a b -- a*b )
/ ( a b -- a/b )
% ( a b -- a%b )
Numberic comparisons
= ( a b -- a==b )
< ( a b -- a<b )
> ( a b -- a>b )
not ( bool -- !bool) if false is on top of the stack, returns true,
otherwise false
eq ( a b -- a eq b) Simple equivalence operations works for most
primitive types and checks to see if complex
object references are the same object
Type testing predicates
All of these match (obj -- bool) and return true if the object on the top of the stack is of the given type.
fixnum?
bool?
char?
string?
vector?
vector-u8?
slice?
record?
pair?
null?
proc?
symbol?
self? ( a -- bool ) returns true if a is something that can be self evaluated and
false in other cases.
Internals
These calls are used to examine/dump internal vm state
gc-stats