Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Last active August 29, 2015 14:26
Show Gist options
  • Save cocodrips/610cf5688ca430eaf722 to your computer and use it in GitHub Desktop.
Save cocodrips/610cf5688ca430eaf722 to your computer and use it in GitHub Desktop.
icfpc2014のgcc
import ast
class PythonToGCC(ast.NodeVisitor):
def visit_Num(self, node):
print "LDC {}".format(node.n)
def visit_BinOp(self, node):
self.visit(node.left)
self.visit(node.right)
self.visit(node.op)
def visit_Add(self, node):
print "ADD"
def visit_Sub(self, node):
print "SUB"
def visit_Mult(self, node):
print "MUL"
def visit_Div(self, node):
print "DIV"
def visit_Compare(self, node):
self.visit(node.left)
self.visit(node.comparators[0])
print "CEQ"
def visit_Print(self, node):
for v in node.values:
self.visit(v)
print "DBUG"
src = '''
print 3 + 5 == 8
print 3 * 5 == 8
'''
tree = ast.parse(src)
visiter = PythonToGCC()
visiter.visit(tree)
print "RTN"
LDC 3
LDC 5
ADD
LDC 8
CEQ
DBUG
LDC 3
LDC 5
LDC 8
CEQ
DBUG
RTN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment