binary operators!

This commit is contained in:
Jeremy Penner 2011-09-11 21:43:53 -04:00
parent 734b38f66c
commit f65c877a91

View file

@ -166,38 +166,32 @@ class Synt(Typeable):
class SyntDesc(Synt):
@classmethod
def StForTypein(cls):
for desce in cls.desc:
for desce in cls.Desc():
if isinstance(desce, list) and len(desce) == 1:
return desce[0]
@staticmethod
def DefProp(isynt):
def get(self):
def getI(self):
return self.rgsynt[isynt]
def set(self, syntNew):
def setI(self, syntNew):
self.Replace(isynt, syntNew)
return property(get, set)
return property(getI, setI)
@classmethod
def Desc(cls):
return cls.desc
def Populate(self):
for desce in self.desc:
for desce in self.Desc():
if isinstance(desce, tuple):
synt = desce[0].SyntDefault(self)
self.rgsynt.append(synt)
# for synt in self.rgsynt:
# synt.Populate()
def SyntProperty(self, isynt):
def get():
return self.rgsynt[isynt]
def set(syntNew):
self.Replace(isynt, syntNew)
def delete():
self.Replace(isynt, None)
return property(get, set, delete)
def DesceChild(self, isyntChild):
isyntDesce = 0;
for desce in self.desc:
for desce in self.Desc():
if isinstance(desce, tuple):
if isyntDesce == isyntChild:
return desce
@ -223,7 +217,7 @@ class SyntDesc(Synt):
def Project(self, pcur):
"adds more horizontal pws to a horizontal pw"
isynt = 0
for desce in self.desc:
for desce in self.Desc():
if isinstance(desce, str):
PwStatic(pcur.PwHoriz(self), desce)
elif isinstance(desce, list):
@ -241,9 +235,15 @@ class SyntName(Synt):
return self.GetStTypein()
class SyntExpr(Synt):
rgclsSyntOp = []
@classmethod
def RgsyntReplace(cls, syntChild, st, rtype):
return RtypeAny().RgsyntFromSt(syntChild.syntParent, st)
for synt in RtypeAny().RgsyntFromSt(syntChild.syntParent, st):
yield synt
for clsSyntOp in cls.rgclsSyntOp:
if clsSyntOp.StForTypein().lower().startswith(st.lower()):
yield clsSyntOp.SyntDefault(None)
def Project(self, pcur):
self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, " ")
@ -301,6 +301,8 @@ class SyntLit(Synt):
return self.rtype
def Project(self, pcur):
self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, self.StForTypein())
def Exec(self, ectx):
return self.value
@RegStmt
class SyntVar(SyntDesc):
@ -326,6 +328,36 @@ class SyntVarRef(Synt):
def Project(self, pcur):
self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, self.StForTypein())
class SyntBinOp(SyntDesc):
@classmethod
def Desc(cls):
return [(SyntExpr, None), " ", [cls.op], " ", (SyntExpr, None)]
left = SyntDesc.DefProp(0)
right = SyntDesc.DefProp(1)
@classmethod
def Create(cls, op, rtype, dgEval = None, stNameI = None):
def Decorate(dgEval):
stName = stNameI or dgEval.__name__
def Eval(self, ectx):
return dgEval(self.left.Eval(ectx), self.right.Eval(ectx))
clsNew = type(stName, (cls,), {"Eval": Eval, "op": op})
SyntExpr.rgclsSyntOp.append(clsNew)
return clsNew
if dgEval == None: # used as a decorator
return Decorate
clsNew = Decorate(dgEval)
if stNameI != None:
globals()[stNameI] = clsNew
return clsNew
SyntBinOp.Create("+", RtypeNum(), lambda l,r: l + r, "SyntPlus")
SyntBinOp.Create("-", RtypeNum(), lambda l,r: l - r, "SyntMinus")
SyntBinOp.Create("*", RtypeNum(), lambda l,r: l * r, "SyntMult")
SyntBinOp.Create("/", RtypeNum(), lambda l,r: l / r, "SyntDiv")
SyntBinOp.Create("=", RtypeBool(), lambda l,r: l == r, "SyntEq")
SyntBinOp.Create(">", RtypeBool(), lambda l,r: l > r, "SyntGt")
@RegStmt
class SyntIf(SyntDesc):
desc = [["If"], " ", (SyntExpr, (1, RtypeBool)), ", then:", (SyntBlock, None), "or else:", (SyntBlock, None)]
@ -634,12 +666,12 @@ class PwDropdown(PwBlock):
PwBlock.Draw(self, ascr, x, y + 1, w, 0, mpksel, False)
class PwList(PwContainer):
def __init__(self, pwParent, scril, dxIndent):
def __init__(self, pwParent, synt, dxIndent):
PwContainer.__init__(self, pwParent)
self.scril = scril
self.synt = synt
self.dxIndent = dxIndent
def Value(self):
return self.scril
return self.synt
def DxDyNew(self, w, dxStart, mpksel):
dx = dxStart
dy = 0