-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.pn
153 lines (128 loc) · 3.08 KB
/
test.pn
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
# Functions.
show = (value):
value string print
"\n" print
.
test = (value, name):
name print, ": " print
value string print
"\n" print
.
# Tests.
show("== Basics ==")
show( (1, "a", "foo") )
show( ("a"
"b"
"c") )
test( (x): x + 1., "(x): x + 1.")
π = "3.14159"
test(π, "π")
show("\n== Blocks ==")
show( : "test" print, "\n" print. )
show(
: "test" print
"\n" print
.)
flurgh = : "test" print, "\n" print.
show(flurgh())
show("\n== function invocation ==")
value = 3
fn = (x): x + 1.
show(value)
show(fn)
show(fn(value))
show(fn(value, 7))
# show(3(fn)) # == 3, and stops the script cold.
# show("baz"(fn)) # == "baz", and stops the script cold.
# show(value(fn)) # Crashes.
show("extra args:")
fn2 = (first, second): second.
show( fn2("foo"): "bar". )
bar = fn2("foo"): "bar".
show( bar("baz") )
show("block args:")
show( fn2("a"): "b". )
seconded = fn2("a"): "b".
show( seconded() )
show("\n== Control ==")
show( if (3 < 4): "Yes". )
show("xxxxx")
show( if (3 > 4): "No". ) # Returns a random stale value, actually "xxxxx".
show( if (3 < 4): "Yes". else: "No". )
# show(if) # Crashes.
x = 3
if (x < 3):
"Less\n" print.
elsif (x == 3):
"Equal\n" print.
else:
"Greater\n" print.
i = 0
while (i < 5):
i string print, "\n" print
i = i + 1
.
threeer = (x):
if (x == 3): return("Three").
x
.
test(threeer(2), "threeer(2)")
test(threeer(3), "threeer(3)")
# "break" and "continue" are broken, so this loop will never terminate.
# i = 0
# loop:
# i = i + 1
# if (i == 3): continue.
# if (i >= 5): break.
# i string print, "\n" print
# .
# Does work, printing "#<closure>".
# Number def("times", (block): block string print, "\n" print.)
# 3 times(): "x".
Number def("times", (block):
i = 0
while (i < self):
block()
i = i + 1
.
.
)
3 times(): "Hello\n" print.
# 3 times: "Hello\n" print. # Crashes.
# Objects.
show("\n== Objects ==")
selfless = (x): self.
test(selfless(), "\"self\" in non-method fn")
# test(Object kind, "Object kind") # "lookup failed 0 string"
# but: test(0 string, "0 string") works.
Document = Object delegated
doc = Document allocate
test(doc, "doc")
Document def("string", (): return "#<Document>".)
test(doc, "doc (after defining 'string')")
obj = Object allocate
test(obj string, "obj string")
# test(Object string, "Object string") # Lookup fails: "0 string"
bytes_per_word = 4
Mixin def("num_fields", (): return 0.)
Mixin def("new", ():
num_slots = self num_fields + 1
# num_slots == 1, but this crashes the JIT:
# test(num_slots, "num_slots")
# I think it kills "self", 'cause it's a lookup failed: "0 allocate".
# Anyway, num_slots == 1, because 'num_fields' defined in such a way that the
# vtable itself won't respond to it; only objects allocated from that vtable
# will. But we can get around that.
num_field_fn = self lookup("num_fields")
num_slots = num_field_fn() + 1
test(num_slots, "num_slots")
new_obj = self allocate(num_slots * bytes_per_word)
return(new_obj)
.)
Document def("num_fields", (): return 3.)
doc = Document new
test(doc, "doc (made with 'new')")
show("\n=====")
# show [b, c]
# Object foom = "foo"
# Object foom