-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpang-010.lua
184 lines (165 loc) · 5.6 KB
/
pang-010.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
-- pang: polish notation language
local pang_version="010"
print("pang version: "..pang_version)
local words={}
function print_function(arguments)
local value=evaluate_word(arguments[1])
print(value)
return value
end
function add_function(arguments)
return evaluate_word(arguments[1])+evaluate_word(arguments[2])
end
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
function if_function(arguments)
if evaluate_word(arguments[1]) then
return evaluate_word(arguments[2])
else
return evaluate_word(arguments[3])
end
end
function while_function(arguments)
while evaluate_word(arguments[1]) do
evaluate_word(arguments[2])
end
end
function not_function(arguments)
return not evaluate_word(arguments[1])
end
function equal_function(arguments)
local first=evaluate_word(arguments[1])
local second=evaluate_word(arguments[2])
return first==second
end
local variables={}
function get_function(arguments)
return variables[words[arguments[1]]]
end
function set_function(arguments)
local variable_name=words[arguments[1]]
local variable_value=evaluate_word(arguments[2])
variables[variable_name]=variable_value
end
function string_function(arguments)
return words[arguments[1]]
end
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
function greater_function(arguments)
return evaluate_word(arguments[1])>evaluate_word(arguments[2])
end
local word_definitions={["print"]={1,print_function},["add"]={2,add_function},["true"]={0,true_function},["false"]={0,false_function},["if"]={3,if_function},["while"]={2,while_function},["not"]={1,not_function},["equal"]={2,equal_function},["set"]={2,set_function},["get"]={1,get_function},["string"]={1,string_function},["modulus"]={2,modulus_function},["lesser_than_or_equal"]={2,lesser_than_or_equal_function},["greater"]={2,greater_function}}
word_definitions["multiply"]={2,multiply_function}
function phrase_length(word_index)
local word=words[word_index]
local length=1
if word=="do" then
while true do
if words[word_index+length]=="end" 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]=="string" 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=="do" then
local do_word_index=word_index+1
local evaluated
while words[do_word_index]~="end" 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("word:"..word.." 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 execute_program(pn_program)
local words_to_add=#words
for word in string.gmatch(pn_program, "%S+") do
table.insert(words,word)
end
evaluate_word(1+words_to_add)
end
function execute_words_file_function(arguments)
local file_name=words[arguments[1]]
local file=io.open(file_name,"r")
execute_program(file:read())
file:close()
end
word_definitions["execute_words_file"]={1,execute_words_file_function}
word_definitions["dont"]={1,function() end}
local call_stack={}
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["define_word"]={3,define_word_function}
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["argument"]={1,argument_function}
execute_program("define_word string square 1 multiply argument 1 argument 1")
execute_program("print square 4") --> 16
-- test for recursion
execute_program("define_word string 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()
execute_program(program)
end
end
read_execute_loop() -- start REPL
-- execute_words_file fizzbuzz.words
-- print factorial 3