-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpang-029.lua
566 lines (466 loc) · 15.3 KB
/
pang-029.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
-- pang: polish notation language
-- pang: linguaggio a notazione polacca
pang_version="029-prerelease" -- versione
language=nil --"italian" -- lingua -- nil
translate_italian={
-- for printing messages to the user
["pang version: "]="pang versione: ",
-- translations for defined words
["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",
---metti_chiamante
["caller_set"]="metti_chiamante",
["caller_get"]="prendi_chiamante",
["while"]="mentre",
["not"]="non",
["greater"]="maggiore",
["if"]="se",
["equal"]="uguale",
["modulus"]="modulo",
["string"]="stringa",
["add"]="somma",
["true"]="vero",
["false"]="falso",
["dont"]="non_fare",
-- for printing messages to the user
["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)
words={} -- program as words array
----> Definitions
word_definitions={} -- definitions of words, of behaviors of sub-programs
-- print <printable>
function print_function(arguments)
local value=evaluate_word(arguments[1])
print(value)
return value
end
-- add <number> <number>
function add_function(arguments)
local first_number=evaluate_word(arguments[1])
local second_number=evaluate_word(arguments[2])
return first_number+second_number
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
local result=evaluate_word(arguments[2])
-- https://www.lua.org/pil/8.4.html
local status,err=pcall(function()
return result._reserved end)
if status and result._reserved=="break_loop" then break end
end
end
-- break
word_definitions[tr("break")]={0,function()
return {_reserved="break_loop"}
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
---------------------------------------
----> Variables set and get
--local variables={}
call_stack={{}}
-- get <variable name>
function get_function(arguments)
local variables=call_stack[#call_stack]
local variable_name=evaluate_word(arguments[1])
local returned_value=variables[variable_name]
if returned_value==nil then print("nil returning from get_function()") end
return returned_value
end
-- set <variable name> <value to set>
function 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
word_definitions[tr("set")]={2,set_function}
word_definitions[tr("get")]={1,get_function}
---------------------------------------
-- variable_get <namespace> <variable name>
function variable_get_function(arguments)
local namespace=evaluate_word(arguments[1])
local variable_name=evaluate_word(arguments[2])
return namespace[variable_name]
end
-- variable_set <namespace> <variable name> <value to set>
function variable_set_function(arguments)
local namespace=evaluate_word(arguments[1])
local variable_name=evaluate_word(arguments[2])
local variable_value=evaluate_word(arguments[3])
namespace[variable_name]=variable_value
end
--variable_
word_definitions[tr("variable_set")]={3,variable_set_function}
word_definitions[tr("variable_get")]={2,variable_get_function}
--
word_definitions["namespace"]={0,function() return call_stack[#call_stack] end }
---------------------------------------
----> Labels and "Go-To"s
labels={} -- global not call-stack based
-- label <label_identifier>
function label_function(arguments)
local label_identifier=evaluate_word(arguments[1])
local word_index=arguments[0] -- first word of the phrase
-- word index after
local next_word_index=word_index+phrase_length(word_index)
local namespace=labels --call_stack[#call_stack]
local variable_name=label_identifier
local variable_value=next_word_index
namespace[variable_name]=variable_value
end
word_definitions[tr("label")]={1,label_function}
-- goto <label_identifier>
function goto_function(arguments)
local namespace=labels --call_stack[#call_stack]
local label_identifier=evaluate_word(arguments[1])
return namespace[label_identifier]
end
word_definitions[tr("goto")]={1,goto_function}
---------------------------------------
-- string <word as string>
function string_function(arguments)
return words[arguments[1]]
end
-- word <word (not processed)>
function word_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
--word_definitions={
word_definitions[tr("print")]={1,print_function}
word_definitions[tr("add")]={2,add_function}
word_definitions[tr("true")]={0,true_function}
word_definitions[tr("false")]={0,false_function}
word_definitions[tr("if")]={3,if_function}
word_definitions[tr("while")]={2,while_function}
word_definitions[tr("not")]={1,not_function}
word_definitions[tr("equal")]={2,equal_function}
word_definitions[tr("string")]={1,string_function}
word_definitions[tr("modulus")]={2,modulus_function}
word_definitions[tr("greater")]={2,greater_function}
word_definitions[":"]={1,word_function}
--}
word_definitions[tr("multiply")]={2,multiply_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}
-------------> end of words' definitions
function phrase_length(word_index)
local word=words[word_index]
local length=1
if word_index>1 and words[word_index-1]==":" then return 1 end
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
----if word_definition==nil or word_index>1 and words[word_index-1]==":" then return 1 end
--if word_index>1 and words[word_index-1]==":" then return 1 end
if word_definition==nil 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
local current_word=words[do_word_index] -- current word index
--[[
if current_word==tr("goto") then
do_word_index=evaluate_word(do_word_index)
else
--]]
-- more usual condition
evaluated=evaluate_word(do_word_index)
local current_phrase_length=phrase_length(do_word_index)
do_word_index=do_word_index+current_phrase_length
if tr("goto")==current_word then
do_word_index=evaluated
end
--end
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
arguments[0]=word_index -- word_index as argument
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
-- from PolishNotation program to "words" array
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
-- execute PolishNotation program
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 -- DEBUG
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=evaluate_word(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[evaluate_word(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 --- may it be "end"? TODO
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=evaluate_word(arguments[1]) --evaluate_word(arguments[1])
variables[variable_name]=variables[variable_name]+1
local return_value=variables[variable_name]
return return_value
end
-- definition
translate_italian["increment"]="incrementa"
word_definitions[tr("increment")]={1,increment_function}
---]] block WIP considered harmful getting the caller's namespace without caller passing it
-- definition
translate_italian["get_caller"]="prendi_chiamante"
word_definitions[tr("get_caller")]={1,get_caller_function}
-- definition
translate_italian["set_caller"]="metti_chiamante"
word_definitions[tr("set_caller")]={2,set_caller_function}
--]]
local test_string_bug_hunt=[[
stampa 1
: non_fare
: fai
stampa 2
]]
-- : non_fare
-- : fai
--- TO DO incrementa stringa i | incrementa : 1
test_string=[[
: " 'incrementa' is a Lua function "
: non_fare
: fai
: inizio
metti : i 1
stampa prendi : i
incrementa : i
incrementa : i
stampa prendi : i
: fine
non_fare
fai
stampa : " ('conta1') namespaces issues (previous way, not working, security issues) "
definisci_parola : prendi_chiamante 1 : " not implemented here (chiamante means caller function/namespace) "
definisci_parola : metti_chiamante 2 : " not implemented here "
definisci_parola : conta1 1 non_fare metti_chiamante argomento 1 somma 3 prendi_chiamante argomento 1
metti : n 1
stampa prendi : n
conta1 : n
stampa prendi : n
conta1 : n
stampa prendi : n
stampa : " ('conta2') namespaces issues (current way, working). security issue namespace can be accessed for other variables beyond the one specified by name "
definisci_parola : conta2 2 metti_variabile argomento 2 argomento 1 somma prendi_variabile argomento 2 argomento 1 3
metti : n 1
stampa prendi : n
conta2 : n namespace
stampa prendi : n
conta2 : n namespace
stampa prendi : n
stampa : " 'conta0' previous work... much started from here "
metti : numero 10
definisci_parola : conta0 1 metti argomento 1 somma 1 prendi argomento 1
non_fare conta0 : numero
stampa prendi : numero
]]
--execute_program(test_string) -- test_string_bug_hunt
--execute_words_file("principale-001.parole")
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()