mirror of
https://github.com/pragma-/pbot.git
synced 2025-01-03 08:32:42 +01:00
Improve paren/prec module
This commit is contained in:
parent
9c1a077d2e
commit
8adc766dc4
@ -13,8 +13,8 @@ use warnings;
|
||||
# These are set automatically by the build/commit script
|
||||
use constant {
|
||||
BUILD_NAME => "PBot",
|
||||
BUILD_REVISION => 475,
|
||||
BUILD_DATE => "2013-11-27",
|
||||
BUILD_REVISION => 476,
|
||||
BUILD_DATE => "2013-12-01",
|
||||
};
|
||||
|
||||
1;
|
||||
|
@ -12,10 +12,9 @@ with open("paren/stddef") as f:
|
||||
class CParser(c_parser.CParser):
|
||||
def __init__(self, *a, **kw):
|
||||
super(CParser, self).__init__(*a, **kw)
|
||||
self.p_expression_hack = self._expression_hack
|
||||
self.cparser = yacc.yacc(
|
||||
module=self,
|
||||
start='expression_hack',
|
||||
start='expression',
|
||||
debug=kw.get('yacc_debug', False),
|
||||
errorlog=yacc.NullLogger(),
|
||||
optimize=kw.get('yacc_optimize', True),
|
||||
@ -34,12 +33,6 @@ class CParser(c_parser.CParser):
|
||||
lexer=self.clex,
|
||||
debug=debuglevel)
|
||||
|
||||
def _expression_hack(self, p):
|
||||
""" expression_hack : translation_unit expression
|
||||
| expression
|
||||
"""
|
||||
p[0] = c_ast.ExprList([p[2] if len(p) == 3 else p[1]])
|
||||
|
||||
|
||||
class CGenerator(c_generator.CGenerator):
|
||||
def visit_UnaryOp(self, n):
|
||||
@ -49,25 +42,35 @@ class CGenerator(c_generator.CGenerator):
|
||||
return 'sizeof (%s)' % self.visit(n.expr)
|
||||
else:
|
||||
return 'sizeof %s' % self._parenthesize_unless_simple(n.expr)
|
||||
return super(CGenerator, self).visit_UnaryOp(n)
|
||||
else:
|
||||
operand = self.visit(n.expr)
|
||||
if isinstance(n.expr, c_ast.ArrayRef) or not self._is_simple_node(n.expr):
|
||||
operand = '(%s)' % operand
|
||||
if n.op in ('p++', 'p--'):
|
||||
return operand + n.op[1:]
|
||||
else:
|
||||
return n.op + operand
|
||||
|
||||
def visit_Assignment(self, n):
|
||||
return '%s %s %s' % (self.visit(n.lvalue), n.op, self._parenthesize_unless_simple(n.rvalue))
|
||||
|
||||
def translate_to_c(input):
|
||||
|
||||
def parenthesize(source):
|
||||
parser = CParser(yacc_optimize=False)
|
||||
try:
|
||||
ast = parser.parse(input, '<input>')
|
||||
ast = parser.parse(source, '<input>')
|
||||
except plyparser.ParseError as e:
|
||||
print(sys.argv[1] + ': ' + "Error: {0}".format(e.args[0]))
|
||||
print("{0}: Error: {1}".format(sys.argv[1], e.args[0]))
|
||||
return
|
||||
generator = CGenerator()
|
||||
print(sys.argv[1] + ': ' + generator.visit(ast))
|
||||
print("{0}: {1}".format(sys.argv[1], generator.visit(ast)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) > 2:
|
||||
translate_to_c(' '.join(sys.argv[2:]))
|
||||
parenthesize(' '.join(sys.argv[2:]))
|
||||
elif len(sys.argv) == 2:
|
||||
print(sys.argv[1] + ': ' + "Usage: paren <expression>")
|
||||
else:
|
||||
print(sys.argv[1] + ': ' + "Usage: paren <expression>")
|
||||
print('error')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user