-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpang-024.lua
385 lines (331 loc) · 10.6 KB
/
pang-024.lua
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
-- pang: polish notation language
-- pang: linguaggio a notazione polacca
local pang_version="024" -- versione
local language="italian" -- lingua -- nil
local translate_italian={
["pang version: "]="pang versione: ",
["exit"]="esci",
["print"]="stampa",
["define_word"]="definisci_parola",
["multiply"]="moltiplica",
["argument"]="argomento",
["do"]="fai",["end"]="fine",
["set"]="metti",
["get"]="prendi",
["variable_set"]="metti_variabile",
["variable_get"]="prendi_variabile",
["while"]="mentre",
["not"]="non",
["greater"]="maggiore",
["if"]="se",
["equal"]="uguale",
["modulus"]="modulo",
["string"]="stringa",
["add"]="somma",
["true"]="vero",
["false"]="falso",
["dont"]="non_fare",
["word:"]="parola:",
[" definition not found"]=" definizione non trovata",
["command_prompt"]="richiesta_comandi",
}
function tr(string) -- translate / traduci
if language=="italian" then -- italiano supportato
local traduced=translate_italian[string]
if traduced==nil then print("can't translate: "..string) return string end
return traduced
end
return string
end
print(tr("pang version: ")..pang_version)
local words={}
-- print <printable>
function print_function(arguments)
local value=evaluate_word(arguments[1])
print(value)
return value
end
-- add <number> <number>
function add_function(arguments)
return evaluate_word(arguments[1])+evaluate_word(arguments[2])
end
-- multiply <number> <number>
function multiply_function(arguments)
return evaluate_word(arguments[1])*evaluate_word(arguments[2])
end
function true_function(arguments)
return true
end
function false_function(arguments)
return false
end
-- if <condition> <if true> <if false>
function if_function(arguments)
if evaluate_word(arguments[1]) then
return evaluate_word(arguments[2])
else
return evaluate_word(arguments[3])
end
end
-- while <condition> <do while true>
function while_function(arguments)
while evaluate_word(arguments[1]) do
evaluate_word(arguments[2])
end
end
-- not <boolean>
function not_function(arguments)
return not evaluate_word(arguments[1])
end
-- equal <first to compare> <second to compare>
function equal_function(arguments)
local first=evaluate_word(arguments[1])
local second=evaluate_word(arguments[2])
return first==second
end
--local variables={}
local call_stack={{}}
-- get <variable name>
function get_function(arguments)
local variables=call_stack[#call_stack]
return variables[words[arguments[1]]]
end
-- set <variable name> <value to set>
function set_function(arguments)
local variables=call_stack[#call_stack]
local variable_name=words[arguments[1]]
local variable_value=evaluate_word(arguments[2])
variables[variable_name]=variable_value
end
---------------------------------------
-- variable_get <variable name>
function variable_get_function(arguments)
local variables=call_stack[#call_stack]
return variables[evaluate_word(arguments[1])]
end
-- variable_set <variable name> <value to set>
function variable_set_function(arguments)
local variables=call_stack[#call_stack]
local variable_name=evaluate_word(arguments[1])
local variable_value=evaluate_word(arguments[2])
variables[variable_name]=variable_value
end
---------------------------------------
-- string <word as string>
function string_function(arguments)
return words[arguments[1]]
end
-- modulus <dividend> <divisor>
function modulus_function(arguments)
return evaluate_word(arguments[1])%evaluate_word(arguments[2])
end
function lesser_than_or_equal_function(arguments)
return evaluate_word(arguments[1])<=evaluate_word(arguments[2])
end
-- greater <lesser> <greater>
function greater_function(arguments)
return evaluate_word(arguments[1])>evaluate_word(arguments[2])
end
local word_definitions={[tr("print")]={1,print_function},[tr("add")]={2,add_function},[tr("true")]={0,true_function},[tr("false")]={0,false_function},[tr("if")]={3,if_function},[tr("while")]={2,while_function},[tr("not")]={1,not_function},[tr("equal")]={2,equal_function},[tr("set")]={2,set_function},[tr("get")]={1,get_function},[tr("string")]={1,string_function},[tr("modulus")]={2,modulus_function},[tr("greater")]={2,greater_function}}
word_definitions[tr("multiply")]={2,multiply_function}
--variable_
word_definitions[tr("variable_set")]={2,variable_set_function}
word_definitions[tr("variable_get")]={1,variable_get_function}
function list_word_definitions_function(arguments)
for word,word_definition in pairs(word_definitions) do
io.write(word..":"..word_definition[1].." ")
end
io.write("\n")
end
word_definitions["?"]={0,list_word_definitions_function}
function phrase_length(word_index)
local word=words[word_index]
local length=1
if word==tr("do") then
while true do
if words[word_index+length]==tr("end") or
words[word_index+length]==nil
then return length+1 end
length=length+phrase_length(word_index+length)
end
end
local number=tonumber(word)
if number~=nil then return 1 end
local word_definition=word_definitions[word]
if word_definition==nil or word_index>1 and (words[word_index-1]==tr("string") or words[word_index-1]==tr("define_word") or words[word_index-1]==tr("set") or words[word_index-1]==tr("increment")) then return 1 end
local argument_length=word_definition[1]
for argument_index=1,argument_length do
length=length+phrase_length(word_index+length)
end
return length
end
function evaluate_word(word_index)
local returned_value
local word=words[word_index]
returned_value=tonumber(word)
if returned_value~=nil then return returned_value end
local word_definition
word_definition=word_definitions[word]
if word==tr("do") then
local do_word_index=word_index+1
local evaluated
while words[do_word_index]~=tr("end") and words[do_word_index]~=nil do
evaluated=evaluate_word(do_word_index)
local current_phrase_length=phrase_length(do_word_index)
do_word_index=do_word_index+current_phrase_length
end
return evaluated
end
if nil==word_definition then print(tr("word:")..word..tr(" definition not found")) return end
local arguments={}, arity, argument_word_index
arity=word_definition[1]
argument_word_index=word_index+1
for argument_index=1,arity do
table.insert(arguments,argument_word_index)
argument_word_index=argument_word_index+phrase_length(argument_word_index)
end
returned_value=word_definition[2](arguments)
return returned_value
end
function program_words(pn_program)
local quoted=""
local opened=false
local quote='"'
for word in string.gmatch(pn_program, "%S+") do
if word==quote then
if opened then
--print(quote..quoted..quote)
table.insert(words,quoted)
quoted=""
end
opened=not opened
elseif opened then
if quoted~="" then quoted=quoted.." " end
quoted=quoted..word
else
--print(word)
table.insert(words,word)
end
end
end
function execute_program(pn_program)
pn_program=tr("do").." "..pn_program.." "..tr("end")
local words_to_add=#words
--[[
for word in string.gmatch(pn_program, "%S+") do
table.insert(words,word)
end
--]]
--words=program_words(pn_program)
program_words(pn_program)
if #words==words_to_add then
--print("empty program")
return
end
if false then
for word_index=words_to_add+1,#words do
io.write("["..words[word_index].."]"..phrase_length(word_index).." ")
end
print()
end
evaluate_word(1+words_to_add)
end
function execute_words_file(file_name)
--local file_name=words[arguments[1]]
local file=io.open(file_name,"r")
local program=""
while true do
local program_line=file:read()
if program_line==nil then break end
program=program..program_line.."\n"
end
file:close()
--print(program)
execute_program(program)
end
-- execute_words_file <filename>
function execute_words_file_function(arguments)
local file_name=words[arguments[1]]
execute_words_file(file_name)
end
word_definitions["!"]={1,execute_words_file_function}
-- dont <skip this>
word_definitions[tr("dont")]={1,function() end}
--local call_stack={{}}
-- define_word <name> <arity> <action>
function define_word_function(arguments)
local arity=evaluate_word(arguments[2])
local word_function=function(word_arguments)
local value_arguments={}
for argument_index,word_argument in pairs(word_arguments) do
value_arguments[argument_index]=evaluate_word(word_argument)
end
local returned
table.insert(call_stack,value_arguments)
returned=evaluate_word(arguments[3])
table.remove(call_stack)
return returned
end
word_definitions[words[arguments[1]]]={arity,word_function}
end
word_definitions[tr("define_word")]={3,define_word_function}
-- argument <argument index>
function argument_function(arguments)
local last=call_stack[#call_stack]
local argument_index=evaluate_word(arguments[1])
local returned=last[argument_index]
return returned
end
word_definitions[tr("argument")]={1,argument_function}
-- TEST for word definition and use with arguments
-- porting to Italian added
--execute_program("define_word square 1 multiply argument 1 argument 1") --> English
--execute_program("definisci_parola quadrato 1 moltiplica argomento 1 argomento 1") --> Italian
--execute_program("print square 4") --> 16 --> English
--execute_program("stampa quadrato 4") --> 16 --> Italian
-- TEST for recursion
--execute_program("define_word factorial 1 if equal 0 argument 1 1 multiply argument 1 factorial add -1 argument 1")
--execute_program("print factorial 0") --> 1
--execute_program("print factorial 4") --> 24
function read_execute_loop()
while true do
local program=io.read()
if program==nil or program==tr("exit") then break end
execute_program(program)
end
end
word_definitions[tr("command_prompt")]={0,read_execute_loop}
-- EXAMPLES
--- metti i somma 1 prendi i
function increment_function(arguments)
local variables=call_stack[#call_stack]
local variable_name=words[arguments[1]] --evaluate_word(arguments[1])
variables[variable_name]=variables[variable_name]+1
local return_value=variables[variable_name]
return return_value
end
translate_italian["increment"]="incrementa"
word_definitions[tr("increment")]={1,increment_function}
--- TO DO incrementa stringa i
test_string=[[
metti i 1
stampa prendi i
incrementa i
incrementa i
stampa prendi i
]]
--execute_program(test_string)
function main()
print("? for help")
local filename=arg[1]
if filename~=nil then
if filename=="-" then read_execute_loop() else
execute_words_file(filename)
if arg[2]=="-" then read_execute_loop() end
end
else
read_execute_loop()
end
print("bye")
end
main()