From 96f9475b7f063f3902edacee427280624aeef3b6 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Sat, 1 Aug 2020 23:39:19 -0400 Subject: [PATCH] Create new flags! --- scripting.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/scripting.py b/scripting.py index ee7c218..24392cc 100644 --- a/scripting.py +++ b/scripting.py @@ -39,7 +39,7 @@ class Botdef(TPrs): class Flagdef(TPrs): def InitPersistent(self, stName, value = True): self.stName = stName - self.value = True + self.value = value class Rbot(TPrs): def InitPersistent(self, botdef): @@ -74,7 +74,7 @@ class Vm(TPrs): self.mpflagdef[flagdef] = fValue def FFlagSet(self, flagdef): - return self.mpflagdef[flagdef] + return self.mpflagdef[flagdef] if flagdef in self.mpflagdef else flagdef.value def AddPlayer(self): rgbotdefPlayer = [botdef for botdef in self.defs.rgbotdef if botdef.fPlayer] @@ -116,6 +116,15 @@ class StypeEnum(Stype): class StypeStmt(StypeEnum): 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): "Runtime type" def StForSobj(self, sobj): @@ -179,6 +188,11 @@ class Synt(Typeable): def SyntDefault(cls): return cls() + def WithParent(self, syntParent): + assert self.syntParent == None + self.syntParent = syntParent + return self + def Populate(self): "Populate rgsynt with useful default values." pass @@ -220,9 +234,7 @@ class Synt(Typeable): pcur.pwTooltip.AddTip(pwTypein, Tooltip) def Replace(self, syntOld, syntNew): - assert syntNew.syntParent == None - syntNew.syntParent = self - self.rgsynt[self.Isynt(syntOld)] = syntNew + self.rgsynt[self.Isynt(syntOld)] = syntNew.WithParent(self) def Append(self, syntNew): assert syntNew.syntParent == None @@ -404,6 +416,21 @@ class SyntFlagRef(Synt): except: 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): desc = [["set"]] def Eval(self, env, val): @@ -441,7 +468,7 @@ class SyntIf(SyntDesc): @RegStmt 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) expr = SyntDesc.DefProp(1) def Eval(self, env):