2020-06-28 15:27:56 +00:00
|
|
|
# This file is part of MarMOTS.
|
|
|
|
#
|
|
|
|
# MarMOTS is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
|
|
|
|
# General Public License as published by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# MarMOTS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
|
|
|
|
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
|
|
|
|
# Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along with MarMOTS. If not,
|
|
|
|
# see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# Copyright 2009, 2010, 2011, 2020 Jeremy Penner
|
|
|
|
|
2011-09-14 22:30:10 +00:00
|
|
|
from tpers import *
|
2011-09-15 22:33:50 +00:00
|
|
|
from scripting import Fail
|
2011-09-14 22:30:10 +00:00
|
|
|
|
|
|
|
class Vars(TPrs):
|
|
|
|
def InitPersistent(self):
|
|
|
|
self.rgmpsynt_sobj = [{}] # [0] is for globals?
|
|
|
|
|
2011-09-15 22:19:20 +00:00
|
|
|
def Get(self, syntVar, synt):
|
2011-09-14 22:30:10 +00:00
|
|
|
for mpsynt_sobj in reversed(self.rgmpsynt_sobj):
|
|
|
|
try:
|
2011-09-15 22:19:20 +00:00
|
|
|
return mpsynt_sobj[syntVar]
|
2011-09-14 22:30:10 +00:00
|
|
|
except:
|
|
|
|
pass
|
2011-09-15 22:19:20 +00:00
|
|
|
return Fail("Variable " + synt.name.St() + " not defined", synt)
|
2011-09-14 22:30:10 +00:00
|
|
|
|
|
|
|
def Define(self, synt, sobj):
|
|
|
|
self.rgmpsynt_sobj[-1][synt] = sobj
|
|
|
|
|
2011-09-15 22:19:20 +00:00
|
|
|
def Set(self, syntVar, sobj, synt):
|
|
|
|
if syntVar == None:
|
|
|
|
return Fail("No variable specified to set", None, synt)
|
2011-09-14 22:30:10 +00:00
|
|
|
for mpsynt_sobj in reversed(self.rgmpsynt_sobj):
|
2011-09-15 22:19:20 +00:00
|
|
|
if syntVar in mpsynt_sobj:
|
|
|
|
mpsynt_sobj[syntVar] = sobj
|
2011-09-14 22:30:10 +00:00
|
|
|
return
|
2011-09-15 22:19:20 +00:00
|
|
|
return Fail("Variable " + syntVar.name.St() + " not defined", synt)
|
2011-09-14 22:30:10 +00:00
|
|
|
|
|
|
|
def Push(self):
|
|
|
|
self.rgmpsynt_sobj.append({})
|
|
|
|
|
|
|
|
def Pop(self):
|
|
|
|
self.rgmpsynt_sobj.pop()
|
2011-09-15 22:19:20 +00:00
|
|
|
|
|
|
|
# instr: can be a synt or a callable. If instr is a synt, the VM will push the current list onto the rstack with its associated
|
|
|
|
# ip (instruction pointer), compile the synt, and begin executing it (Vm.CallI). If instr is a callable, the vm will call it; if it
|
|
|
|
# returns a synt, the synt is compiled and called.
|
2011-09-14 22:30:10 +00:00
|
|
|
class Vm(TPrs):
|
|
|
|
def InitPersistent(self):
|
|
|
|
self.vstack = []
|
|
|
|
self.rstack = []
|
|
|
|
self.vars = Vars()
|
2011-09-15 22:19:20 +00:00
|
|
|
self.rgfail = []
|
2011-09-14 22:30:10 +00:00
|
|
|
|
|
|
|
def Push(self, sobj):
|
|
|
|
self.vstack.append(sobj)
|
|
|
|
def Pop(self):
|
|
|
|
return self.vstack.pop()
|
|
|
|
|
2011-09-15 22:19:20 +00:00
|
|
|
def Log(self, fail):
|
|
|
|
if fail != None:
|
|
|
|
self.rgfail.append(fail)
|
|
|
|
|
2011-09-14 22:30:10 +00:00
|
|
|
def CallI(self, synt, r, ip):
|
2011-09-15 22:19:20 +00:00
|
|
|
rNew = synt.Compile()
|
|
|
|
if len(rNew) == 0: # no-op optimization
|
|
|
|
return (r, ip)
|
2011-09-14 22:30:10 +00:00
|
|
|
if ip < len(r): # tail call optimization
|
2011-09-15 22:33:50 +00:00
|
|
|
self.rstack.append((r, ip))
|
2011-09-15 22:19:20 +00:00
|
|
|
return (rNew, 0)
|
2011-09-14 22:30:10 +00:00
|
|
|
|
|
|
|
def RunSynt(self, synt):
|
|
|
|
r = synt.Compile()
|
|
|
|
ip = 0
|
|
|
|
while True:
|
|
|
|
if ip < len(r):
|
|
|
|
instr = r[ip]
|
|
|
|
ip += 1
|
2011-09-15 22:33:50 +00:00
|
|
|
if hasattr(instr, "Compile"):
|
2011-09-14 22:30:10 +00:00
|
|
|
(r, ip) = self.CallI(instr, r, ip)
|
|
|
|
elif callable(instr):
|
|
|
|
synt = instr(self)
|
|
|
|
if synt != None:
|
2011-09-16 23:10:15 +00:00
|
|
|
(r, ip) = self.CallI(synt, r, ip)
|
2011-09-14 22:30:10 +00:00
|
|
|
else:
|
2011-09-15 22:33:50 +00:00
|
|
|
assert False, "invalid instr " + str(instr)
|
2011-09-14 22:30:10 +00:00
|
|
|
else:
|
2011-09-15 22:33:50 +00:00
|
|
|
if len(self.rstack) == 0:
|
2011-09-14 22:30:10 +00:00
|
|
|
break
|
|
|
|
(r, ip) = self.rstack.pop()
|