Create new flags!

This commit is contained in:
Jeremy Penner 2020-08-01 23:39:19 -04:00
parent 0d83da70ae
commit 96f9475b7f

View file

@ -39,7 +39,7 @@ class Botdef(TPrs):
class Flagdef(TPrs): class Flagdef(TPrs):
def InitPersistent(self, stName, value = True): def InitPersistent(self, stName, value = True):
self.stName = stName self.stName = stName
self.value = True self.value = value
class Rbot(TPrs): class Rbot(TPrs):
def InitPersistent(self, botdef): def InitPersistent(self, botdef):
@ -74,7 +74,7 @@ class Vm(TPrs):
self.mpflagdef[flagdef] = fValue self.mpflagdef[flagdef] = fValue
def FFlagSet(self, flagdef): def FFlagSet(self, flagdef):
return self.mpflagdef[flagdef] return self.mpflagdef[flagdef] if flagdef in self.mpflagdef else flagdef.value
def AddPlayer(self): def AddPlayer(self):
rgbotdefPlayer = [botdef for botdef in self.defs.rgbotdef if botdef.fPlayer] rgbotdefPlayer = [botdef for botdef in self.defs.rgbotdef if botdef.fPlayer]
@ -116,6 +116,15 @@ class StypeEnum(Stype):
class StypeStmt(StypeEnum): class StypeStmt(StypeEnum):
rgclsSynt = [] rgclsSynt = []
class StypeNewFlag(Stype):
def RgsyntFromSt(self, defs, st):
if len(st) > 0:
for flagdef in defs.rgflagdef:
if flagdef.stName == st:
return
yield SyntMakeFlagDef(defs, st, False)
yield SyntMakeFlagDef(defs, st, True)
class Rtype(Stype): class Rtype(Stype):
"Runtime type" "Runtime type"
def StForSobj(self, sobj): def StForSobj(self, sobj):
@ -179,6 +188,11 @@ class Synt(Typeable):
def SyntDefault(cls): def SyntDefault(cls):
return cls() return cls()
def WithParent(self, syntParent):
assert self.syntParent == None
self.syntParent = syntParent
return self
def Populate(self): def Populate(self):
"Populate rgsynt with useful default values." "Populate rgsynt with useful default values."
pass pass
@ -220,9 +234,7 @@ class Synt(Typeable):
pcur.pwTooltip.AddTip(pwTypein, Tooltip) pcur.pwTooltip.AddTip(pwTypein, Tooltip)
def Replace(self, syntOld, syntNew): def Replace(self, syntOld, syntNew):
assert syntNew.syntParent == None self.rgsynt[self.Isynt(syntOld)] = syntNew.WithParent(self)
syntNew.syntParent = self
self.rgsynt[self.Isynt(syntOld)] = syntNew
def Append(self, syntNew): def Append(self, syntNew):
assert syntNew.syntParent == None assert syntNew.syntParent == None
@ -404,6 +416,21 @@ class SyntFlagRef(Synt):
except: except:
return env.vm.Fail("No such flag", self) return env.vm.Fail("No such flag", self)
class SyntMakeFlagDef(Synt):
def InitPersistent(self, defs, stName, fInit):
Synt.InitPersistent(self)
self.defs = defs
self.stName = stName
self.fInit = fInit
def StForTypein(self):
return "New flag: " + self.stName + " (initially " + str(self.fInit) + ")"
def WithParent(self, syntParent):
flagdef = Flagdef(self.stName, self.fInit)
self.defs.rgflagdef.append(flagdef)
return SyntFlagRef(flagdef).WithParent(syntParent)
class SyntIsFlagSet(SyntDesc): class SyntIsFlagSet(SyntDesc):
desc = [["set"]] desc = [["set"]]
def Eval(self, env, val): def Eval(self, env, val):
@ -441,7 +468,7 @@ class SyntIf(SyntDesc):
@RegStmt @RegStmt
class SyntSet(SyntDesc): class SyntSet(SyntDesc):
desc = [["Set flag"], " ", RtypeFlag(), " to ", RtypeUnion(RtypeBool(), RtypeFlag())] desc = [["Set flag"], " ", RtypeUnion(RtypeFlag(), StypeNewFlag()), " to ", RtypeUnion(RtypeBool(), RtypeFlag())]
lvalue = SyntDesc.DefProp(0) lvalue = SyntDesc.DefProp(0)
expr = SyntDesc.DefProp(1) expr = SyntDesc.DefProp(1)
def Eval(self, env): def Eval(self, env):