Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added more code objects and responder hooks.

  • Loading branch information...
commit 2106f49d72230e8f7843eefec63f8aa5ec358fb5 1 parent fd4934e
Ben Samuel authored
Showing with 424 additions and 201 deletions.
  1. +300 −168 CodeObjects.py
  2. +30 −21 LolGrammer.py
  3. +94 −12 Responder.py
468 CodeObjects.py
View
@@ -18,7 +18,6 @@ class GenC(object):
""" Mixin class to represent something that can generate C. """
def genC(self):
pass
-
class Scope(object):
def __init__(self, **kw):
super(Scope, self).__init__(**kw)
@@ -28,39 +27,19 @@ def declare(self, var):
if var.name in self.declarations:
raise CompileException("Variable already declared")
self.declarations[var.name] = var
-
+class Declaration(object):
+ def __init__(self, var, typ):
+ assert isinstance(var, str) and typ isinstance(
class Function(Scope):
def __init__(self, **kw):
pass
-
class Block(object):
def __init__(self, **kw):
super(Block, self).__init__(**kw)
self.statements = list()
- def addStatement(self, stmnt):
- assert isinstance(stmnt, Statement)
+ def addStmnt(self, stmnt):
+ assert isinstance(stmnt, Stmnt)
self.statements.append(stmnt)
-
-class Signature(object):
- """ A signature maps variable names to types in, and then
- variable names to types out. """
- def __init__(self, **kw):
- super(Signature, self).__init__(**kw)
- self.vin = dict()
- self.vout = dict()
- def compatible(self, scope):
- """ Tests whether this signature can draw from the scope. """
- missing = set()
- incompatible = dict()
- for var, typ in self.vin.iterpairs():
- if var not in scope:
- missing.add(var)
- if typ != scope[var]:
- incompatible[var] = (typ, scope[var])
- return { "result": not (missing or incompatible),
- "missing": missing,
- "incompatible": incompatible }
-
class Contextual(object):
""" Takes a context. """
def __init__(self, **kw):
@@ -82,49 +61,108 @@ def contextTypes(self):
""" Override. """
pass
-class Statement(Contextual):
+class Stmnt(Contextual):
"""
-A Statement is an object of execution.
+A Stmnt is an object of execution.
To bind it, we need to know what variables of what types it is looking for
To execute it, we need to know what variables it is reading
from, and what variables it is providing to the next statement.
"""
def __init__(self, **kw):
- super(Statement, self).__init__(**kw)
-
- def signatures(self, **kw):
- """ Generates possible signatures """
- super(Statement, self).signatures(**kw)
+ super(Stmnt, self).__init__(**kw)
def contextTypes(self):
return (Block, Function)
-class ExpressionStatement(Statement, GenC):
+class InsertStmnt(Stmnt, GenC):
+ def __init__(self, agg, key, val, **kw):
+ super(InsertStmnt, self).__init__(**kw)
+ assert isinstance(key, Expr) and \
+ isinstance(val, (Expr, NoneType))
+ self.agg = VarName(agg)
+ self.key = key
+ self.val = val
+ def genC(self):
+ if self.val is None:
+ return (self.agg.genC(), '.insert(', self.key.genC(), ');')
+ else:
+ return (self.agg.genC(), '.insert(', self.key.genC(), ',',
+ self.val.genC(), ');')
+class RemoveStmnt(Stmnt, GenC):
+ def __init__(self, agg, key, **kw):
+ super(InsertStmnt, self).__init__(**kw)
+ assert isinstance(key, Expr)
+ self.agg = VarName(agg)
+ self.key = key
+ def genC(self):
+ return (self.agg.genC(), '.erase(', self.key.genC(), ');')
+class SetStmnt(Stmnt, GenC):
+ def __init__(self, lagg, ragg, **kw):
+ super(SetStmnt, self).__init__(**kw)
+ self.lagg = VarName(lagg)
+ self.ragg = VarName(ragg)
+ def genC(self):
+ return (self.head().genC(), '(', self.lagg.genC(), ',',
+ self.ragg.genC(), ');')
+class UnionStmnt(SetStmnt):
+ def head(self):
+ return Simple('unionAssign')
+class DiffStmnt(SetStmnt):
+ def head(self):
+ return Simple('diffAssign')
+class InterStmnt(SetStmnt):
+ def head(self):
+ return Simple('interAssign')
+
+class ExprStmnt(Stmnt, GenC):
def __init__(self, expr, **kw):
- super(ExpressionStatement, self).__init__(**kw)
- assert isinstance(expr, Expression)
+ super(ExprStmnt, self).__init__(**kw)
+ assert isinstance(expr, Expr)
self.expr = expr
def genC(self):
return (self.expr.genC(), ';')
-
-class Assignment(Statement, GenC):
+class Assignment(Stmnt, GenC):
def __init__(self, var, expr, **kw):
super(Assignment, self).__init__(**kw)
- assert isinstance(var, basestring)
- assert isinstance(expr, Expression)
- self.var = var
+ assert isinstance(expr, Expr)
+ self.var = VarName(var)
self.expr = expr
def genC(self):
- return ('(', self.var.genC(), ') = (', self.expr.genC(), ');')
+ return (self.var.genC(), '=', self.expr.genC(), ';')
+
+class InputStmnt(Stmnt, GenC):
+ def __init__(self, var, read, **kw):
+ super(InputStmnt, self).__init__(**kw)
+ self.var = VarName(var)
+ assert read in ('word', 'line', 'numbar')
+ self.read = read
+ def genC(self):
+ return (self.var.genC(), '= input_'+self.read+'();')
+class OutputStmnt(Stmnt, GenC):
+ def __init__(self, handle, terms, nl, **kw):
+ super(OutputStmnt, self).__init__(**kw)
+ assert handle in ('out', 'err')
+ assert all(isinstance(term, Expr) for term in terms)
+ assert isinstance(nl, bool)
+ self.handle = handle
+ self.terms = terms
+ self.nl = nl
+ def chandle(self):
+ return Simple('c' + self.handle)
+ def genC(self):
+ terms = [self.chandle()] + self.terms
+ if self.nl:
+ terms.append(Simple('endl'))
+ return tuple(iterJoin('<<', terms)) + (';',)
-class Conditional(Statement, GenC):
+class Conditional(Stmnt, GenC):
def __init__(self, cases, **kw):
super(Conditional, self).__init__(**kw)
self.cases = list()
for cond, block in cases:
self.addCase(cond, block)
def addCond(self, cond, block):
- assert isinstance(cond, Expression)
+ assert isinstance(cond, Expr)
assert isinstance(block, Block)
self.cases.append((cond, block))
def genC(self):
@@ -133,44 +171,34 @@ def genC(self):
l = []
for cond, block in self.cases:
l.extend(("else", "if(", cond.genC(), ") {", block.genC(), "}"))
+ l.append(";")
return tuple(l[1:])
-class Iteration(Statement, CGen):
+class Counter(Stmnt, CGen):
def __init__(self, label, preCondExpr, body, **kw):
- super(Iteration, self).__init__(**kw)
+ super(Counter, self).__init__(**kw)
assert isinstance(label, Label)
- assert isinstance(preCondExpr, Expression)
+ assert isinstance(preCondExpr, Expr)
assert isinstance(body, Block)
self.label = label
self.preCondExpr = preCondExpr
self.body = body
-class Expression(object):
- def __init__(self, **kw):
- super(Expression, self).__init__(**kw)
- self.args = kw["args"]
- for a in self.args:
- assert isinstance(a, Expression)
- def iterInputTypes(self):
- """ Returns a generator that steps through all the
- possible input types that can be fed to this operation. """
- return product(*[t.iterOutputTypes() for t in self.args])
+class Expr(object):
+ def retType(self):
+ pass
-class LiteralExpression(Expression):
- def usrType(self):
+class LitExpr(Expr):
+ def retType(self):
return { bool : BoolTyp,
int : IntTyp,
str : StrTyp }[type(self.val)]
def __init__(self, val, **kw):
- super(LiteralExpression, self).__init__(**kw)
+ super(LitExpr, self).__init__(**kw)
self.scope = scope
self.val = val
if isinstance(self.val, str):
self.subst = self.scope.allocateString(self.val)
- def signatures(self):
- yield { "in": [], "out": self.usrType() }
- def iterOutputTypes(self):
- yield self.lolType
def genC(self):
t = type(self.val)
if t == bool:
@@ -181,133 +209,221 @@ def genC(self):
return ('StrObj(',) + tuple(escapeCString(self.val)) + (')',)
else:
raise AssertionError("Unreachable, maybe val was modified?")
+class CastExpr(Expr, GenC):
+ def __init__(self, val, typ, **kw):
+ super(CastExpr, self).__init__(**kw)
+ assert isinstance(val, Expr)
+ assert isinstance(typ, LangTyp)
+ self.val = val
+ self.typ = typ
+ def genC(self):
+ i = self.val.retType()
+ o = self.typ
-def escapeCString(val):
- assert isinstance(val, str)
- escaperGen = ('\\' + c if c in ('"', '\\') else (c if 32 <= ord(c) <= 127 else '\\x%02x' % ord(c))
- for c in val)
- line = ''
- linelen = 0
- for c in escaperGen:
- if linelen + len(c) > 66:
- yield '"' + line + '"'
- line = ''
- linelen = 0
- line += c
- linelen += len(c)
- if line:
- yield '"' + line + '"'
+class Simple(object):
+ """ Represents a keyword or punctuation in production languages. """
+ def __init__(self, term, **others):
+ super(Simple, self).__init__(others)
+ self.term = term
+ self.__dict__.update(others)
+ def genC(self):
+ if self.cterm is None:
+ return self.term
+class VarName(Simple):
+ pass
+class LabelName(Simple):
+ pass
-class VariableExpression(Expression):
+class VariableExpr(Expr):
def __init__(self, var, **kw):
- super(VariableExpression, self).__init__(**kw)
+ super(VariableExpr, self).__init__(**kw)
assert isinstance(var, Variable)
self.var = var
def genC(self):
return self.var.name
-
-class ComparisonExpression(Expression):
- def signatures(self):
- for t in self.iterInputTypes():
- if not t or all(x == t[0] for x in t):
- yield { "in": t, "out": BoolTyp }
- def iterOutputTypes(self):
- yield BoolTyp
-class SetExpression(Expression):
- def __init__(self, **kw):
- super(SetExpression, self).__init__(**kw)
- self.setOp = kw["setOp"]
- assert isinstance(BoolOp, self.setOp)
-class BooleanExpression(Expression):
- def __init__(self, args, boolOp, **kw):
- super(BooleanExpression, self).__init__(**kw)
- assert isinstance(BoolOp, boolOp)
- self.boolOp = boolOp
-class BoolOp(object):
- """ Represents a binary boolean operation """
- def __init__(self, table):
- """ table represents all the a's and b's that
- produce True values """
- assert all(isinstance(a, bool) and isinstance(b, bool)
- for a, b in table)
- self.table = frozenset(table)
- def __equals__(self, other):
- return self.table == other.table
- tautology = frozenset((a, b) for a, b in
- product((False, True), repeat=2))
- def negated(self):
- """ convenience to generate the opposite operation """
- return BoolOp(tautology - self.table)
+class TermExpr(Expr):
+ def __init__(self, terms, **kw):
+ super(TermExpr, self).__init__(**kw)
+ assert all(isinstance(term, Expr) for term in terms)
+ self._terms = terms
+ @property
+ def terms(self):
+ return self._terms
+ def nullTerms(self):
+ raise AssertionError()
-AndOp = BoolOp([(True, True)])
-OrOp = BoolOp([(False, False)]).negated()
-XorOp = BoolOp([(True, False), (False, True)])
+def iterJoin(joiner, iterable):
+ yield iterable.next()
+ for term in iterable:
+ yield joiner
+ yield term
-class ConcatenationExpression(Expression):
- def iterOutputTypes(self):
- yield StrTyp
- def signatures(self):
- for a,b in self.iterInputTypes():
- if a == Yarn and b == Yarn:
- yield { "in": (a, b), "out": StrTyp }
-class SlicingExpression(Expression):
- def __init__(self, **kw):
- super(SlicingExpression, self).__init__(**kw)
- self.bukkit = kw["bukkit"]
+class BinaryOp(TermExpr, GenC):
+ def op(self):
+ pass
+ def genC(self):
+ terms = [term.genC() for term in self.terms()]
+ if len(terms) == 0:
+ return self.nullTerms().genC()
+ op = self.op().genC()
+ return ('(',) + tuple(iterJoin(op, terms)) + (')',)
+class FuncOp(TermExpr, GenC):
+ def func(self):
+ pass
+ def genC(self):
+ terms = [term.genC() for term in self.terms()]
+ func = self.func().genC()
+ return (func, '(',) + tuple(iterJoin(',', terms)) + (')',)
+
+class AddExpr(BinaryOp):
+ def op(self):
+ return Simple('+')
+class DiffExpr(BinaryOp):
+ def op(self):
+ return Simple('-')
+class DivExpr(BinaryOp):
+ def op(self):
+ return Simple('/')
+class MultExpr(BinaryOp):
+ def op(self):
+ return Simple('*')
+class ModExpr(BinaryOp):
+ def op(self):
+ return Simple('%')
+class GrtExpr(BinaryOp):
+ def op(self):
+ return Simple('>')
+class LessExpr(BinaryOp):
+ def op(self):
+ return Simple('<')
+class ConcatExpr(BinaryOp):
+ def op(self):
+ return Simple('+')
+class StrLenExpr(Expr, GenC):
+ def __init__(self, strExpr, **kw):
+ super(StrLenExpr, self).__init__(**kw)
+ self.strExpr = strExpr
+ def genC(self):
+ return (self.strExpr.genC(), '.size()')
+class SlicingExpr(Expr, GenC):
+ def __init__(self, strExpr, **kw):
+ super(SlicingExpr, self).__init__(**kw)
+ self.strExpr = strExpr
for a in ("from", "back", "to", "by"):
if a in kw:
- setattr(self, a, kw[a])
+ assert isinstance(kw[a], Expr)
+ setattr(self, 's' + a.capitalize(), kw[a])
else:
- setattr(self, a, None)
- assert issubclass(self.bukkit, Complex)
- def iterOutputTypes(self):
- return type(self.bukkit)
+ setattr(self, 's' + a.capitalize(), None)
+ def fromExpr(self):
+ if self.sFrom is not None:
+ return self.sFrom
+ elif self.sBack is not None:
+ return DiffExpr([StrLenExpr(self.strExpr), self.sBack])
+ else:
+ return LitExpr(val=0)
+ def byExpr(self):
+ if self.sTo is not None:
+ return DiffExpr([self.sTo, self.fromExpr()])
+ elif self.sBy is not None:
+ return self.sBy
+ else:
+ return DiffExpr([StrLenExpr(self.strExpr), self.fromExpr()])
+ def genC(self):
+ return ('sliceFromBy(', self.strExpr.genC(), ',',
+ self.fromExpr().genC(), ',',
+ self.byExpr().genC(), ')')
+class FirstExpr(Expr, GenC):
+ def __init__(self, agg, **kw):
+ super(FirstExpr, self).__init__(**kw)
+ assert isinstance(agg, Expr)
+ self.agg = agg
+ def genC(self):
+ return ('(*(', self.agg.genC(), '.begin()))')
+class LastExpr(Expr, GenC):
+ def __init__(self, agg, **kw):
+ super(LastExpr, self).__init__(**kw)
+ assert isinstance(agg, Expr)
+ self.agg = agg
+ def genC(self):
+ return ('(*(', self.agg.genC(), '.end()))')
+class MaxExpr(FuncOp):
+ def func(self):
+ return Simple('max')
+class MinExpr(FuncOp):
+ def func(self):
+ return Simple('min')
+class AndExpr(BinaryOp):
+ def op(self):
+ return Simple('&&')
+class OrExpr(BinaryOp):
+ def op(self):
+ return Simple('||')
+class NegateExpr(UnaryOp):
+ def op(self):
+ return Simple('!')
-class MathExpression(Expression):
- def iterOutputTypes(self):
- yield IntTyp
- def signatures(self):
- for a, b in self.iterInputTypes():
- if a == IntTyp and b == IntTyp:
- yield { "in": (a, b), "out": IntTyp }
# Types
-class Builtin(object):
+class LangType(object):
+ @property
+ def name(self):
+ pass
+class Builtin(LangType):
pass
-class Atomic(BuiltinType):
- def __init__(self, **kw):
- super(Atomic, self).__init__(**kw)
- assert self.python() == NoneType or type(kw[val]) == self.python()
-class VoidTyp(Atomic):
- def python(self):
- return NoneType
-class BoolTyp(Atomic):
- def python(self):
- return bool
-class IntTyp(Atomic):
- def python(self):
- return int
-class StrTyp(Atomic):
- def python(self):
- return str
+class Atomic(Builtin, GenC):
+ def __init__(self, name, pytyp, **kw):
+ super(Atomic, self).__init__(**kw)
+ self._name = name
+ self._pytyp = pytyp
+ @property
+ def name(self):
+ return self._name
+ @property
+ def pytyp(self):
+ return self._pytyp
+ def genC(self):
+ return self.name + 'T'
+ def __eq__(self, other):
+ return self == other or (
+ isinstance(other, Atomic) and
+ self.name == other.name and
+ self.pytyp == other.pytyp)
+voidTyp = Atomic('void', NoneType)
+boolTyp = Atomic('bool', bool)
+intTyp = Atomic('int', int)
+strTyp = Atomic('str', str)
class Complex(Builtin):
pass
-class MapTyp(Complex):
- def __init__(self, bigness, **kw):
+class MapTyp(Complex, GenC):
+ def __init__(self, bigness, keyTyp, valTyp, **kw):
super(MapTyp, self).__init__(**kw)
+ assert isinstance(bigness, int)
+ assert keyTyp in (intTyp, strTyp)
+ assert isinstance(valTyp, Atomic)
self.bigness = bigness
-def mapTypOf(key, val):
- assert issubclass(key, Atomic) and issubclass(val, Atomic)
- return type(name="BukkitOf%sAnd%s" % (key.__name__, val.__name__),
- bases=(MapTyp,),
- dict=dict([("keyType", key), ("valType", val)]))
+ self.keyType = keyTyp
+ self.valType = valTyp
+ containers = {
+ (int, False) : Simple('map'),
+ (int, True) : Simple('set'),
+ (str, False) : Simple('hash_map'),
+ (str, True): Simple('hash_set')
+ }
+ def genC(self):
+ k, v = (self.keyType.pytyp, self.valType.pytyp == NoneType)
+ if v:
+ return "%s<%s>" % (MapTyp.containers[k,v], self.keyType.genC())
+ else:
+ return "%s<%s, %s>" % (MapTyp.containers[k,v], self.keyType.genC(),
+ self.valType.genC())
def allAtomicTypes():
""" Iterator that yields all possible atomic types. """
- yield VoidTyp
- yield BoolTyp
- yield IntTyp
- yield StrTyp
+ yield voidTyp
+ yield boolTyp
+ yield intTyp
+ yield strTyp
def allBuiltinTypes():
""" Iterator that yields all possible types. """
for t in allAtomicTypes():
@@ -316,3 +432,19 @@ def allBuiltinTypes():
for k in allAtomicTypes():
for v in allAtomicTypes():
yield mapTypOf(k, v)
+
+def escapeCString(val):
+ assert isinstance(val, str)
+ escaperGen = ('\\' + c if c in ('"', '\\') else (c if 32 <= ord(c) <= 127 else '\\x%02x' % ord(c))
+ for c in val)
+ line = ''
+ linelen = 0
+ for c in escaperGen:
+ if linelen + len(c) > 66:
+ yield '"' + line + '"'
+ line = ''
+ linelen = 0
+ line += c
+ linelen += len(c)
+ if line:
+ yield '"' + line + '"'
51 LolGrammer.py
View
@@ -92,10 +92,10 @@ def build(r):
Concatenation = ~k.SMOOSH & AnList > r.concat
- SliceFrom = ~k.FRM & Expr > r.sliceFrom
- SliceBack = ~k.BAK & Expr > r.sliceBack
- SliceTo = ~k.TO & Expr > r.sliceTo
- SliceBy = ~k.BY & Expr > r.sliceBy
+ SliceFrom = k.FRM & Expr > r.sliceClause
+ SliceBack = k.BAK & Expr > r.sliceClause
+ SliceTo = k.TO & Expr > r.sliceClause
+ SliceBy = k.BY & Expr > r.sliceClause
Slicing = (~k.PIK & Expr & (SliceFrom | SliceBack)[0:1] &
(SliceTo | SliceBy)[0:1]) > r.slice_
@@ -126,27 +126,30 @@ def build(r):
Block = Delayed()
ExprStatement = Expr > r.exprSt
- Declaration = ~k.I & ~k.HAS & ~k.A[0:1] & Variable & (~k.IS & Type)[0:1] > r.declSt
+ Declaration = ~k.I & ~k.HAS & ~k.A[0:1] & Variable & ~k.IS & Type > r.decl
Assignment = Variable & ~k.R & Expr > r.assign
InsertStatement = ~k.MA & Variable & ~k.HAS & ~k.A[0:1] & Expr & \
(~k.BY & Expr)[0:1] > r.insertSt
RemoveStatement = ~k.MA & Variable & ~k.DON & ~k.HAS & ~k.A[0:1] & \
- Expr & (~k.BY & Expr)[0:1] > r.removeSt
- UnionStatement = ~k.MIX & ~k.MA & Variable & ~k.WIV & Expr \
+ Expr > r.removeSt
+ UnionStatement = ~k.MIX & ~k.MA & Variable & ~k.WIV & Variable \
> r.unionSt
- DiffStatement = ~k.MA & Varialbe & ~k.DON & ~k.HAS & ~k.FROM & \
- Expr > r.diffSt
- InterStatement = ~k.MA & Varialbe & ~k.HAS & ~k.SAEM & ~k.FROM & \
- Expr > r.interSt
+ DiffStatement = ~k.MA & Variable & ~k.DON & ~k.HAS & ~k.FROM & \
+ Variable > r.diffSt
+ InterStatement = ~k.MA & Variable & ~k.HAS & ~k.SAEM & ~k.FROM & \
+ Variable > r.interSt
CastStatement = Variable & ~k.IS & ~k.NOW & ~k.A[0:1] & Type > r.castSt
ReadType = k.WORD | k.LINE | k.NUMBAR
InputStatement = ~k.GIMMEH & ReadType[0:1] & Variable > r.inputSt
- OutputStatement = (~k.VISIBLE & Expr[1:] > r.output) \
- | (~k.VISIBLE & Expr[1:] & ~BANG > r.outputLn) \
- | (~k.VISIBLEZ & Expr[1:] > r.outputLn)
+ OutputStatement = (~k.VISIBLE & Expr[1:] > r.outputSt) \
+ | (~k.VISIBLE & Expr[1:] & ~BANG > r.outputLnSt) \
+ | (~k.VISIBLEZ & Expr[1:] > r.outputLnSt)
+ DebugStatement = (~k.INVISIBLE & Expr[1:] > r.debugSt) \
+ | (~k.INVISIBLE & Expr[1:] & ~BANG > r.debugLnSt) \
+ | (~k.INVISIBLEZ & Expr[1:] > r.debugLnSt)
CondBlock = ((((~k.YA & ~k.RLY) > r.posCon) | ((~k.MEBBE & Expr) > r.exprCon)
| ((~k.NO & ~k.WAI) > r.negCon)) & ~C & Block) > r.condBlock
@@ -158,15 +161,21 @@ def build(r):
CaseStatement = (Expr & ~C & ~k.WTF & ~C & CaseBlock[1:] & ~k.OIC) \
> r.caseSt
- LoopCounter = (~k.UPPIN > r.loopInc) | (~k.NERFIN > r.loopDec)
- LoopIterator = (~k.STEALIN & Variable & (~k.N & Variable)[0:1]) \
- > r.loopIter
TermClause = (~k.TILL & Expr > r.until) | (~k.WILE & Expr > r.while_)
LoopOpen = ~k.IM & ~k.IN & ~k.YR & Label > r.loopLabel
- LoopHead = (LoopOpen & (LoopCounter & k.YR & Variable)[0:1] &
- TermClause[0:1]) > r.loopCounter
LoopClose = ~k.IM & ~k.OUTTA & ~k.YR & Label > r.loopLabel
- LoopStatement = LoopHead & ~C & Block & LoopClose > r.loopSt
+
+ CounterDir = (~k.UPPIN > r.ctrInc) | (~k.NERFIN > r.ctrDec)
+ CounterHead = (CounterDir & k.YR & Variable)[0:1] & \
+ TermClause[0:1] > r.ctrHead
+ CounterStatement = LoopOpen & CounterHead & ~C & Block & LoopClose > r.ctrSt
+
+ IterVars = (~k.STEALIN & Variable & (~k.N & Variable)[0:1]) \
+ > r.iterVars
+ IterHead = (IterVars & k.YR & Variable)[0:1] & \
+ TermClause[0:1] > r.iterHead
+ IterStatement = LoopOpen & IterHead & ~C & Block & LoopClose > r.iterSt
+
BreakStatement = ~k.GTFO & Label[0:1] > r.breakSt
ContinueStatement = ~k.KEEP & ~k.TRYN & Label[0:1] > r.continueSt
@@ -181,7 +190,7 @@ def build(r):
SimpleStatement = (Declaration | Assignment | CastStatement |
InputStatement | OutputStatement |
ConditionStatement | CaseStatement |
- LoopStatement | ExprStatement |
+ CtrStatement | IterStatement | ExprStatement |
InsertStatement | RemoveStatement)
ControlStatement = (BreakStatement | ContinueStatement |
ReturnStatement)
106 Responder.py
View
@@ -52,15 +52,15 @@ def listT(s, x):
def mapT(s, x):
return co.mapTypOf(co.StrTyp, x[1])(bigness=x[0])
def boolTrue(s, x):
- return co.LiteralExpression(val=True)
+ return co.LitExpr(val=True)
def boolFalse(s, x):
- return co.LiteralExpression(val=False)
+ return co.LitExpr(val=False)
def intLit(s, x):
- return co.LiteralExpression(val=int(x[0]))
+ return co.LitExpr(val=int(x[0]))
def strLit(s, x):
d = { ':' : ':', ')' : '\n', '>' : '\t', 'o' : '\x07',
'"' : '"' }
- return co.LiteralExpression(
+ return co.LitExpr(
re.sub(':(.)', lambda m: d[m.group(1)]
if m.group(1) in d
else m.group(0), x[0][1:-1]))
@@ -69,19 +69,101 @@ def posCon(s, x):
def negCon(s, x):
return r.exprCon([r.boolFalse([])])
def notEqual(s, x):
- return co.NegateExpression(co.EqualityExpression(x[0:2]))
+ return co.NegateExpr(co.EqualityExpr(x[0:2]))
def greaterThan(s, x):
- return co.ComparisonExpression(x[0], x[1], op='>')
+ return co.GrtExpr(terms=x[0:2])
def lessThan(s, x):
- return co.ComparisonExpression(x[0], x[1], op='<')
+ return co.LessExpr(terms=x[0:2])
def equals(s, x):
- return co.EqualityExpression(x[0:2])
+ return co.EqualityExpr(terms=x[0:2])
def or_(s, x):
- return co.BooleanExpression(args=x, boolOp=co.OrOp)
+ return co.OrExpr(terms=x)
def and_(s, x):
- return co.BooleanExpression(args=x, boolOp=co.AndOp)
+ return co.AndExpr(terms=x)
def not_(s, x):
- return co.NegateExpression(x[0])
+ return co.NegateExpr(x[0])
def concat(s, x):
- return co.ConcatenationExpression(x)
+ return co.ConcatExpr(x)
+ def sliceClause(s, x):
+ return (x[0].lower(), x[1])
+ def slice_(s, x):
+ return co.SlicingExpr(strExpr=x[0], **dict(x[1:]))
+ def max_(s, x):
+ return co.MaxExpr(terms=x)
+ def min_(s, x):
+ return co.MinExpr(terms=x)
+ def add(s, x):
+ return co.AddExpr(terms=x)
+ def diff(s, x):
+ return co.DiffExpr(terms=x)
+ def divide(s, x):
+ return co.DivExpr(terms=x)
+ def mult(s, x):
+ return co.MultExpr(terms=x)
+ def modulus(s, x):
+ return co.ModExpr(terms=x)
+ def negative(s, x):
+ return co.DiffExpr(terms=[0, x[0]])
+ def first(s, x):
+ return co.FirstExpr(agg=x)
+ def last(s, x):
+ return co.LastExpr(agg=x)
+ def get(s, x):
+ return co.GetExpr(subj=x[1], index=x[0])
+ def cast(s, x):
+ return co.CastExpr(val=x[0], typ=x[1])
+ def call(s, x):
+ return co.CallExpr(head=x[0], args=x[1:])
+ def exprSt(s, x):
+ return co.ExprStmnt(expr=x[0])
+ def decl(s, x):
+ return co.Declaration(var=x[0], typ=x[1])
+ def assign(s, x):
+ return co.Assignment(var=x[0], expr=x[1])
+ def insertSt(s, x):
+ return co.InsertStmnt(agg=x[0], key=x[1],
+ val=x[2] if len(x)>2 else None)
+ def removeSt(s, x):
+ return co.RemoveStmnt(agg=x[0], key=x[1])
+ def unionSt(s, x):
+ return co.UnionStmnt(lagg=x[0], ragg=x[1])
+ def diffSt(s, x):
+ return co.DiffStmnt(lagg=x[0], ragg=x[1])
+ def interSt(s, x):
+ return co.InterStmnt(lagg=x[0], ragg=x[1])
+ def castSt(s, x):
+ return co.Assignment(var=x[0], expr=co.CastExpr(
+ val=co.VariableExpr(var=x[0]), typ=x[1]))
+ def inputSt(s, x):
+ readType, var = (['LINE'] + x)[-2:]
+ return co.InputStmnt(var=var, read=readType.lower())
+ def outputSt(s, x):
+ return co.OutputStmnt(handle='out', terms=x, nl=False)
+ def outputLnSt(s, x):
+ return co.OutputStmnt(handle='out', terms=x, nl=True)
+ def debugSt(s, x):
+ return co.OutputStmnt(handle='err', terms=x, nl=False)
+ def debugLnSt(s, x):
+ return co.OutputStmnt(handle='err', terms=x, nl=True)
+ def posCon(s, x):
+ return co.LitExpr(val=True)
+ def negCon(s, x):
+ return co.LitExpr(val=False)
+ def exprCon(s, x):
+ return x[0]
+ def condBlock(s, x):
+ e, b = x
+ return (e, b)
+ def condSt(s, x):
+ return co.Conditional(cases=x)
+ def until(s, x):
+ return ('until', x[0])
+ def while_(s, x):
+ return ('while', x[0])
+ def loopLabel(s, x):
+ return co.LabelName(x[0])
+ def ctrInc(s, x):
+ return Simple('++')
+ def ctrDec(s, x):
+ return Simple('--')
Please sign in to comment.
Something went wrong with that request. Please try again.