This repository was archived by the owner on Aug 22, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtest.gd
More file actions
205 lines (179 loc) · 5.53 KB
/
Copy pathtest.gd
File metadata and controls
205 lines (179 loc) · 5.53 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
extends Node
###
func func_1():
print("Function call test 1 - called a function without parameters")
func func_2(a, b, c := "optional param"):
prints("Function call test 2 -", a, b, c)
return "return value from func_2"
func func_3() -> Array:
return [ 1, 2, 3 ]
func print(p):
print("Script prints '", p, "'")
###
func _ready() -> void:
var g := Gompl.new(self)
var res
#g.debug_printing = true
# test calling GDScript functions
res = g.eval('
ifif = 2 + 2 * 3 // keywords can be part of the identifier names
func_1()
func_2("foo", ifif)
')
print("RESULT 1 (external func call): ", res, "\n") # "return value from func_2"
assert(res is String and res == "return value from func_2", "Result 1 wrong")
# test factorial code
res = g.eval('
n = 5 // no ; needed, or even line-breaks
p = 1
while n > 0 do
p = p * n
n = n - 1
end
p // the last expression is the result of the eval() call
')
print("RESULT 2 (factorial): ", res, "\n") # 120
assert((res is int or res is float) and res == 120, "Result 2 wrong")
# test custom env Dictionary, and some assignments
var env := { "test": "str" }
res = g.eval('
// test is defined already through the env Dictionary
t = if test != "str" then 100 else 50 end // if-then-elif-then-else and while-do can be used as expressions
r = s = -5 // assignments are expressions too
w = false
r = undefined // r will be removed from env!
t - 50 == 0
', env)
print("RESULT 3 (custom env): ", res) # true
print("-> WITH ENVIRONMENT: ", env, "\n")
assert((res is bool) and res == true and "r" not in env, "Result 3 wrong")
# test undefined (similar to null in GDScript)
res = g.eval('
y = if 1 + 1 == 3 then "y is undefined because this if-expression returns null" end
if y == undefined then print("y is not defined") end
y // will return undefined
')
print("RESULT 4 (undefined): ", res, "\n") # undefined
assert(res is Object and res is Gompl.Undefined, "Result 4 wrong")
# test conditions, skip and stop
res = g.eval('
x = -1
while x < 10 do
x = x + 1
if x == 3 then
print("no three for thee")
skip
elif x == 6 then
stop with x // the "with" part is optional
end
print(x)
end
')
print("RESULT 5 (condition, skip, stop): ", res, "\n") # 6
assert((res is int or res is float) and res == 6, "Result 5 wrong")
# test string stuff
res = g.eval('
print("hello world" - "lo ") // subtracting removes the word(s)
print("hello " * 3 + "world") // multiplying repeats the word
"number test: " + 3.141 + " " + 1000
')
print("RESULT 6: ", res, "\n") # number test: 3.141 1000
assert((res is String) and res == "number test: 3.141 1000", "Result 6 (string stuff) wrong")
# test function
res = g.eval('
x = 3 y = 5
sum()
sum()
function sum()
x = x + y
print("sum: " + x)
x
end
')
print("RESULT 7 (internal func): ", res, "\n") # 13
assert((res is int or res is float) and res == 13, "Result 7 wrong")
# test endless loop and max steps of code execution
var max_steps := 185
var state := {} # could also use g.cur_state for this
for i in 10:
# this compiles the code again on every step, which is wasteful - better use g.run() instead
res = g.eval('
x = 0
while true do
x = x + 1
if x > 100 then
interrupt with x // premature script exit
end
end', null, state, max_steps)
print("value of X on frame ", i, ": ", state.env["x"], " after ", state["step"], " steps")
await get_tree().process_frame
print("RESULT 8 (endless loop and interrupt): ", res, "\n")
assert((res is int or res is float) and res == 104, "Result 8 wrong")
# test calling Gompl functions
res = g.eval('
function test()
print("inside function test()")
if x == 0 then stop // use stop like "return"
elif x == 1 then 5
else 7 end
end
print("outside function test()")
x = 1
test()
', null, null, 200)
print("RESULT 9 (internal func): ", res, "\n")
assert((res is int or res is float) and res == 5, "Result 9 wrong")
# Attention: `g` keeps the `function test()` when the next call of `g.eval()` has the `clear_internal_funcs`
# parameter set to `false` (default is `true`) - this way you could reuse it
# test calling Gompl functions recursively
res = g.eval('
function a()
i = i + 1
if i < 1000 then a() end
i = i + 1
end
i = 0
a()
')
print("RESULT 10 (recursive func): ", res, "\n")
assert((res is int or res is float) and res == 2000, "Result 10 wrong")
# test array
res = g.eval('
a = array(3, 2, -100)
a.sort()
a[0] = "first"
a[a.find(3)] = "last"
a
')
print("RESULT 11 (array): ", res, "\n")
assert((res is Array) and res[0] == "first" and res[1] == 2 and res[2] == "last", "Result 11 wrong")
# test dictionary
res = g.eval('
d = dictionary("c": 3, "b": 2, "a": 1).set("d": 4).sort()
d["e"] = 5
print(d)
d.get_or_add("f", 6)
')
print("RESULT 12 (dictionary): ", res, "\n")
assert((res is int or res is float) and res == 6, "Result 12 wrong")
# test iterating
res = g.eval('
sum = 0
while i of 101 do
sum = sum + i
end
print("sum of 1 to 100: " + sum)
// "of" works outside of while loops too
print((s of "STR") + " -> " + s) // S
print((s of "STR") + " -> " + s) // T
print((s of "STR") + " -> " + s) // R
print((s of "STR") + " -> " + s) // undefined
while value of array(3, 5, 7, 9, 11) do
print(value + " * 2 = " + (value * 2))
if value == 7 then stop with value end
end
')
print("RESULT 13 (iterating): ", res, "\n")
assert((res is int or res is float) and res == 7, "Result 13 wrong")
# done, results in Output
print("done.")