-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIo.txt
executable file
·189 lines (132 loc) · 3.61 KB
/
Io.txt
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
Keywords
---------
; // separate
= // update existing
:= // assign new and update
Io> """Hello world!""" // triple quoted strings can have newlines
Io> "There are " .. (8*10)+19 .. " bottles of beer on that very wall, over there."
everything is returned. Return nil when that is what you mean.
Io> 2 sqrt
==> 1.414214
Io> b := 2 * 3
==> 6
CONDITIONS
Io> if(a == 1) then(writeln("a is one")) else(writeln("a is not one"))
LISTS
Io> d := List clone append(30, 10, 5, 20)
==> list(30, 10, 5, 20)
Io> d print
==> list(30, 10, 5, 20)
Io> d := d sort
==> list(5, 10, 20, 30)
Io> d first
==> 5
Io> d last
==> 30
Io> d at(2)
==> 20
Io> d remove(30)
==> list(5, 10, 20)
Io> d atPut(1, 123)
==> list(5, 123, 20)
Io> list(30, 10, 5, 20) select(>10)
==> list(30, 20)
Io> list(30, 10, 5, 20) detect(>10)
==> 30
Io> list(30, 10, 5, 20) map(*2)
==> list(60, 20, 10, 40)
Io> list(30, 10, 5, 20) map(v, v*2)
==> list(60, 20, 10, 40)
Loops
Io> for(i, 1, 10, write(i, " "))
1 2 3 4 5 6 7 8 9 10
Io> d foreach(i, v, writeln(i, ": ", v))
0: 5
1: 123
3: 20
Io> list("abc", "def", "ghi") foreach(println)
abc
def
ghi
Strings
Io> a := "foo"
==> "foo"
Io> b := "bar"
==> "bar"
Io> c := a .. b
==> "foobar"
Io> c at(0)
==> 102
Io> c at(0) asCharacter
==> "f"
Io> s := "this is a test"
==> "this is a test"
Io> words := s split(" ", "\t") print
"this", "is", "a", "test"
Io> s findSeq("is")
==> 2
Io> s findSeq("test")
==> 10
Io> s slice(10)
==> "test"
Io> s slice(2, 10)
==> "is is a "
Io> someObject slotNames
Io> someObject slotNames sort
Io> someObject slotSummary
Inspecting a method will print a decompiled version of it:
Io> Lobby getSlot("forward")
==> # io/Z_Importer.io:65
method(
Importer import(call)
)
doFile and doString
A script can be run from the interactive mode using the doFile method:
doFile("scriptName.io")
The evaluation context of doFile is the receiver, which in this case would be the lobby. To evaluate the script in the context of some other object, simply send the doFile message to it:
someObject doFile("scriptName.io")
The doString method can be used to evaluate a string:
Io> doString("1+1")
==> 2
And to evaluate a string in the context of a particular object:
someObject doString("1 + 1")
Syntax
Io> plus := block(a, b, a + b)
==> method(a, b,
a + b
)
Io> plus call(2, 3)
==> 5
Io> plus argumentNames
==> list("a", "b")
Io> plus code
==> block(a, b, a +(b))
Io> plus message name
==> a
Io> plus message next setName("-")
==> -(b)
Io> plus
==> method(a, b,
a - b
)
Io> plus call(2, 3)
==> -1
Io> plus message next
==> +(b)
Io> plus message next name
==> +
Builder := Object clone
Builder forward := method(
tag(call message name, call message argAt(0))
)
This forward method intercepts all messages. So the html message is going to hit that slot
One vital bit here: the method lists no arguments. Its argument list is empty. However, it really does take arguments. Normally, in Io, you’ll use html := method(arg1, arg2, ...) to tell the method to receive two arguments. This would unpause The Matrix, though, and run all the messages like languages normally do.
If you send arguments to a method (or a block) without naming them, then Io will go lazy on us. It’ll attach those arguments to our Call object, but it’ll skip actually running them.
So, the code call message name and call message argAt(0) gets the method name and the arguments from the Call object.
Io> call message name
==> html
Io> call message argAt(0)
==> head(title("Lazy Bricks, Lazy Mortar")) ;
body(div(p("Here's a bit more Io...") ;
p("Previously on Hackety Org...")) ;
div(p("Adieu, friends and uncles.")))