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): class SyntDesc(Synt):
@classmethod @classmethod
def StForTypein(cls): def StForTypein(cls):
for desce in cls.desc: for desce in cls.Desc():
if isinstance(desce, list) and len(desce) == 1: if isinstance(desce, list) and len(desce) == 1:
return desce[0] return desce[0]
@staticmethod @staticmethod
def DefProp(isynt): def DefProp(isynt):
def get(self): def getI(self):
return self.rgsynt[isynt] return self.rgsynt[isynt]
def set(self, syntNew): def setI(self, syntNew):
self.Replace(isynt, syntNew) self.Replace(isynt, syntNew)
return property(get, set) return property(getI, setI)
@classmethod
def Desc(cls):
return cls.desc
def Populate(self): def Populate(self):
for desce in self.desc: for desce in self.Desc():
if isinstance(desce, tuple): if isinstance(desce, tuple):
synt = desce[0].SyntDefault(self) synt = desce[0].SyntDefault(self)
self.rgsynt.append(synt) self.rgsynt.append(synt)
# for synt in self.rgsynt: # for synt in self.rgsynt:
# synt.Populate() # 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): def DesceChild(self, isyntChild):
isyntDesce = 0; isyntDesce = 0;
for desce in self.desc: for desce in self.Desc():
if isinstance(desce, tuple): if isinstance(desce, tuple):
if isyntDesce == isyntChild: if isyntDesce == isyntChild:
return desce return desce
@ -223,7 +217,7 @@ class SyntDesc(Synt):
def Project(self, pcur): def Project(self, pcur):
"adds more horizontal pws to a horizontal pw" "adds more horizontal pws to a horizontal pw"
isynt = 0 isynt = 0
for desce in self.desc: for desce in self.Desc():
if isinstance(desce, str): if isinstance(desce, str):
PwStatic(pcur.PwHoriz(self), desce) PwStatic(pcur.PwHoriz(self), desce)
elif isinstance(desce, list): elif isinstance(desce, list):
@ -241,9 +235,15 @@ class SyntName(Synt):
return self.GetStTypein() return self.GetStTypein()
class SyntExpr(Synt): class SyntExpr(Synt):
rgclsSyntOp = []
@classmethod @classmethod
def RgsyntReplace(cls, syntChild, st, rtype): 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): def Project(self, pcur):
self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, " ") self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, " ")
@ -301,6 +301,8 @@ class SyntLit(Synt):
return self.rtype return self.rtype
def Project(self, pcur): def Project(self, pcur):
self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, self.StForTypein()) self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, self.StForTypein())
def Exec(self, ectx):
return self.value
@RegStmt @RegStmt
class SyntVar(SyntDesc): class SyntVar(SyntDesc):
@ -325,7 +327,37 @@ class SyntVarRef(Synt):
return self.syntVar.name.St() return self.syntVar.name.St()
def Project(self, pcur): def Project(self, pcur):
self.syntParent.ProjectTypeinForChild(pcur.PwHoriz(self), self, self.StForTypein()) 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 @RegStmt
class SyntIf(SyntDesc): class SyntIf(SyntDesc):
desc = [["If"], " ", (SyntExpr, (1, RtypeBool)), ", then:", (SyntBlock, None), "or else:", (SyntBlock, None)] 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) PwBlock.Draw(self, ascr, x, y + 1, w, 0, mpksel, False)
class PwList(PwContainer): class PwList(PwContainer):
def __init__(self, pwParent, scril, dxIndent): def __init__(self, pwParent, synt, dxIndent):
PwContainer.__init__(self, pwParent) PwContainer.__init__(self, pwParent)
self.scril = scril self.synt = synt
self.dxIndent = dxIndent self.dxIndent = dxIndent
def Value(self): def Value(self):
return self.scril return self.synt
def DxDyNew(self, w, dxStart, mpksel): def DxDyNew(self, w, dxStart, mpksel):
dx = dxStart dx = dxStart
dy = 0 dy = 0