Last active
August 29, 2015 14:26
-
-
Save cocodrips/610cf5688ca430eaf722 to your computer and use it in GitHub Desktop.
icfpc2014のgcc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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