Skip to content

Instantly share code, notes, and snippets.

@stowell
Last active September 20, 2017 01:14
Show Gist options
  • Save stowell/fb14f33ec612391c5ff59a6de68b696d to your computer and use it in GitHub Desktop.
Save stowell/fb14f33ec612391c5ff59a6de68b696d to your computer and use it in GitHub Desktop.
Rigid parser for sigcode
import fileinput
import pprint
import re
blockstartre = re.compile('^(?P<blockname>\S+)\s+is$')
defre = re.compile('^(?P<varname>\S+)\s+is\s+(?P<expr>.+)$')
funcallre = re.compile('(?P<funname>\S+)\s+(?P<funvalue>\S+)')
opcallre = re.compile('(?P<left>\S+)\s+(?P<opname>\S+)\s+(?P<right>\S+)')
funs = {
'negative': 'def negative(v): return -v',
'exp': 'def exp(v): return numpy.exp(v)',
}
ops = {
'plus': '+',
'minus': '-',
'over': '/',
}
class Block(object):
def __init__(self, blockname):
self.blockname = blockname
self.values = []
def addvalue(self, value):
self.values.append(value)
class Line(object):
pass
class Function(object):
def __init__(self, funname, funvalue):
self.funname = funname
self.funvalue = funvalue
class Operator(object):
def __init__(self, left, opname, right):
self.left = left
self.opname = opname
self.right = right
class Value(object):
def __init__(self, value):
self.value = value
class Definition(object):
def __init__(self, varname, exprvalue):
self.varname = varname
self.exprvalue = exprvalue
def main():
blocks = []
currentblock = None
for line in fileinput.input():
blockstartmatch = blockstartre.match(line)
defmatch = defre.match(line)
funcallmatch = funcallre.match(line)
opcallmatch = opcallre.match(line)
isemptyline = 0 == len(line)
if isemptyline:
currentblock = None
elif not currentblock and blockstartmatch:
blockname = blockstartmatch.groupdict()['blockname']
currentblock = Block(blockname=blockname)
blocks.append(currentblock)
elif defmatch:
varname = defmatch.groupdict()['varname']
expr = defmatch.groupdict()['expr']
exprfuncallmatch = funcallre.match(expr)
expropcallmatch = opcallre.match(expr)
if exprfuncallmatch:
exprfunname = exprfuncallmatch.groupdict()['funname']
exprfunvalue = exprfuncallmatch.groupdict()['funvalue']
exprvalue = Function(funname=exprfunname, funvalue=exprfunvalue)
elif expropcallmatch:
exprleft = expropcallmatch.groupdict()['left']
expropname = expropcallmatch.groupdict()['opname']
exprright = expropcallmatch.groupdict()['right']
exprvalue = Operator(left=exprleft, opname=expropname, right=exprright)
else:
exprvalue = Value(value=expr)
value = Definition(varname=varname, exprvalue=exprvalue)
currentblock.addvalue(value)
elif funcallmatch:
funname = funcallmatch.groupdict()['funname']
funvalue = funcallmatch.groupdict()['funvalue']
value = Function(funname=funname, funvalue=funvalue)
currentblock.addvalue(value)
elif opcallmatch:
left = opcallmatch.groupdict()['left']
opname = opcallmatch.groupdict()['opname']
right = expropcallmatch.groupdict()['right']
value = Operator(left=left, opname=opname, right=right)
currentblock.addvalue(value)
else:
value = Value(value=line.strip())
currentblock.addvalue(value)
for block in blocks:
pprint.pprint(block.values)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment