-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathouterInterpreter.lua
32 lines (32 loc) · 1.22 KB
/
outerInterpreter.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
require "innerInterpreter"
local dataStack,returnStack={},{}
local makePush=function(t) return function(v) table.insert(t,v) end end
local makePop =function(t) return function() return table.remove(t) end end
DS={Push=makePush(dataStack), Pop=makePop(dataStack)}
RS={Push=makePush(returnStack), Pop=makePop(returnStack)}
local execWord=function(inWordName) innerInterpreter({Dict[inWordName]}) end
local action={};
action.interpret={number=DS.Push, string=execWord}
action.compile ={
number=function(inValue) local ins=table.insert
ins(NewWord.param,Dict["lit"]); ins(NewWord.param,inValue)
end,
string=function(inWordName)
if Dict[inWordName].immediate then execWord(inWordName)
else table.insert(NewWord.param,Dict[inWordName]) end
end
}
function GetToken() local token
token,scanPos=string.match(Line,"^([ \t]*)()", scanPos)
token,scanPos=string.match(Line,"^([^ \t]*)()",scanPos)
return token~="" and token or nil -- token!="" ? token : nil
end
Mode="interpret"
outerInterpreter=function(inLine)
Line=inLine; scanPos=1; local token=GetToken()
while token do local tokVal = Dict[token] and token or tonumber(token)
if tokVal then action[Mode][type(tokVal)](tokVal); token=GetToken()
else return token end
end
return nil
end