Skip to content

Commit

Permalink
All tests now parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchance committed Mar 25, 2017
1 parent a33b4eb commit d041597
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 71 deletions.
10 changes: 5 additions & 5 deletions ast2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
'CompoundStmt': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)>',
'ConstantArrayType': r'^ (?P<address>[0-9a-fx]+) \'(?P<type>.*)\' (?P<size>\d+)',
'CStyleCastExpr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*?)' <(?P<kind>.*)>",
'DeclRefExpr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*?)' (lvalue (?P<kind>\w+)|Function) (?P<address2>[0-9a-fx]+) '(?P<name>.*?)' '(?P<type2>.*?)'",
'DeclRefExpr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*?)'.*? (lvalue (?P<kind>\w+)|Function) (?P<address2>[0-9a-fx]+) '(?P<name>.*?)' '(?P<type2>.*?)'",
'DeclStmt': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)>",
'DeprecatedAttr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> \"(?P<message1>.*?)\" \"(?P<message2>.*?)\"",
'ElaboratedType': r'^ (?P<address>[0-9a-fx]+) \'(?P<type>.*)\' (?P<tags>.+)',
'FieldDecl': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> (?P<position2>[^ ]+) (?P<type>.+)',
'FieldDecl': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> (?P<position2>[^ ]+) (?P<tags>.*?)(?P<name>\w+?) '(?P<type>.+?)'",
'FloatingLiteral': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*)' (?P<value>.+)",
'FormatAttr': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)>(?P<tags> Implicit)? (?P<function>\w+) (?P<unknown1>\d+) (?P<unknown2>\d+)',
'ForStmt': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)>",
Expand All @@ -51,7 +51,7 @@
'ParmVarDecl': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> (?P<position2>.+?)(?P<name> \w+)? '(?P<type>.*?)'(?P<type2>:'.*?')?",
'PointerType': r'^ (?P<address>[0-9a-fx]+) \'(?P<type>.*)\'',
'Record': r'^ (?P<address>[0-9a-fx]+) \'(?P<type>.*)\'',
'RecordDecl': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> (?P<position2>[^ ]+) (?P<type>.+)',
'RecordDecl': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> (?P<position2>[^ ]+) (?P<kind>struct|union) (?P<name>\w+)",
'RecordType': r'^ (?P<address>[0-9a-fx]+) \'(?P<type>.*)\'',
'ReturnStmt': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)>',
'StringLiteral': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> \'(?P<type>.*)\'(?P<tags> lvalue)? (?P<value>.*)',
Expand All @@ -60,7 +60,7 @@
'TypedefDecl': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.+?)> (?P<position2><invalid sloc>|[^ ]+)(?P<tags>.*?) (?P<name>\w+) \'(?P<type>.*?)\'(?P<type2>:\'.*?\')?',
'TypedefType': r'^ (?P<address>[0-9a-fx]+) \'(?P<type>.*)\' (?P<tags>.+)',
'UnaryOperator': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*?)'(?P<tags1> lvalue)?(?P<tags2> prefix)?(?P<tags3> postfix)? '(?P<operator>.*?)'",
'VarDecl': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> (?P<position2>[^ ]+) (?P<name>.+) '(?P<type>.+)'(?P<tags>.*)",
'VarDecl': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> (?P<position2>[^ ]+) (?P<name>.+) '(?P<type>.+?)'.*?(?P<tags>.*)",
'WhileStmt': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)>",
}

Expand Down Expand Up @@ -112,7 +112,7 @@ def convert_lines_to_nodes(lines):
sys.exit(1)

node_type = indent_and_type.group(2)
# if node_type == 'DeclRefExpr':
# if node_type == 'FieldDecl':
# print(line[offset:])

offset = len(indent_and_type.group(0))
Expand Down
111 changes: 45 additions & 66 deletions c2go.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def resolve_type(s):
if s == 'float':
return 'float32'

if s == 'void *':
if s == 'void *' or s == '__darwin_pthread_handler_rec *':
return 'interface{}'

if s == 'char':
Expand Down Expand Up @@ -104,6 +104,9 @@ def resolve_type(s):
if '(*)' in s or s == '__sFILEX *' or s == 'fpos_t':
return "interface{}"

if '(' in s:
return 'interface{}'

return s

# raise Exception('Cannot resolve type "%s"' % s)
Expand Down Expand Up @@ -169,27 +172,7 @@ def render_expression(node):

return '%s%s' % (operator, expr[0]), expr[1]

if node['node'] == 'UNEXPOSED_EXPR':
children = list(node.get_children())
if len(children) < 1:
return '// UNEXPOSED_EXPR: %s' % ''.join([t.spelling for t in node.get_tokens()]), 'unknown'

# if len(children) > 1:
# raise Exception('To many children!')

e = render_expression(children[0])
name = e[0]

# if name == 'argc':
# name = 'len(os.Args)'
# add_import("os")
# elif name == 'argv':
# name = 'os.Args'
# add_import("os")

return name, e[1]

if node['node'] in ('CHARACTER_LITERAL', 'StringLiteral', 'FLOATING_LITERAL'):
if node['node'] in ('CHARACTER_LITERAL', 'StringLiteral', 'FloatingLiteral'):
return node['value'], 'const char*'

if node['node'] == 'IntegerLiteral':
Expand Down Expand Up @@ -248,15 +231,15 @@ def render_expression(node):
return '%s[%s]' % (render_expression(children[0])[0],
render_expression(children[1])[0]), 'unknown'

if node['node'] == 'MEMBER_REF_EXPR':
children = list(node.get_children())
return '%s.%s' % (render_expression(children[0])[0], list(node.get_tokens())[-2].spelling), 'unknown'
if node['node'] == 'MemberExpr':
children = node['children']
return '%s.%s' % (render_expression(children[0])[0], node['name']), children[0]['type']

if node['node'] == 'CSTYLE_CAST_EXPR':
children = list(node.get_children())
return render_expression(children[0]), 'unknown'

if node['node'] == 'FIELD_DECL' or node['node'] == 'VarDecl':
if node['node'] == 'FieldDecl' or node['node'] == 'VarDecl':
type = resolve_type(node['type'])
name = node['name'].replace('used', '')

Expand All @@ -265,13 +248,9 @@ def render_expression(node):
prefix = 'var '

suffix = ''
# children = node['children']

# We must check the position of the child is at the end. Otherwise a
# child can refer to another expression like the size of the data type.
# if len(children) > 0 and children[0].extent.end.column == node.extent.end.column:
# e = render_expression(children[0])
# suffix = ' = %s' % cast(e[0], e[1], type)
if 'children' in node:
children = node['children']
suffix = ' = %s' % render_expression(children[0])[0]

return '%s%s %s%s' % (prefix, name, type, suffix), 'unknown'

Expand Down Expand Up @@ -317,7 +296,7 @@ def render(out, node, indent=0, return_type=None):

if has_body:
return_type = ' ' + node['type']
if return_type == ' void':
if return_type == ' void ()':
return_type = ''

if function_name == 'main':
Expand Down Expand Up @@ -345,33 +324,33 @@ def render(out, node, indent=0, return_type=None):
render(out, c, indent, return_type)
return

# if node['node'] == 'IF_STMT':
# children = list(node.get_children())
if node['node'] == 'IfStmt':
children = node['children']

# e = render_expression(children[0])
# print_line(out, 'if %s {' % cast(e[0], e[1], 'bool'), indent)
e = render_expression(children[0])
print_line(out, 'if %s {' % cast(e[0], e[1], 'bool'), indent)

# render(out, children[1], indent + 1, return_type)
render(out, children[1], indent + 1, return_type)

# if len(children) > 2:
# print_line(out, '} else {', indent)
# render(out, children[2], indent + 1, return_type)
if len(children) > 2:
print_line(out, '} else {', indent)
render(out, children[2], indent + 1, return_type)

# print_line(out, '}', indent)
print_line(out, '}', indent)

# return
return

# if node['node'] == 'WHILE_STMT':
# children = list(node.get_children())
if node['node'] == 'WhileStmt':
children = node['children']

# e = render_expression(children[0])
# print_line(out, 'for %s {' % cast(e[0], e[1], 'bool'), indent)
e = render_expression(children[0])
print_line(out, 'for %s {' % cast(e[0], e[1], 'bool'), indent)

# render(out, children[1], indent + 1, return_type)
render(out, children[1], indent + 1, return_type)

# print_line(out, '}', indent)
print_line(out, '}', indent)

# return
return

if node['node'] == 'ForStmt':
children = node['children']
Expand All @@ -385,21 +364,13 @@ def render(out, node, indent=0, return_type=None):

return

# if node['node'] == 'BREAK_STMT':
# print_line(out, 'break', indent)
# return

# if node['node'] == 'UNARY_OPERATOR':
# variable, operator = [t.spelling for t in list(node.get_tokens())[0:2]]
# if operator == '++':
# print_line(out, '%s += 1' % variable, indent)
# #print_line(out, '%s = string(%s[1:])' % (variable, variable), indent)
# return

# print_line(out, '%s%s' % (operator, variable), indent)
# return
if node['node'] == 'BreakStmt':
print_line(out, 'break', indent)
return

# #raise Exception('UNARY_OPERATOR: %s' % operator)
if node['node'] == 'UnaryOperator':
print_line(out, render_expression(node)[0], indent)
return

if node['node'] == 'ReturnStmt':
# try:
Expand All @@ -410,7 +381,7 @@ def render(out, node, indent=0, return_type=None):

return

if node['node'] in ('BINARY_OPERATOR', 'INTEGER_LITERAL', 'CallExpr'):
if node['node'] in ('BinaryOperator', 'INTEGER_LITERAL', 'CallExpr'):
print_line(out, render_expression(node)[0], indent)
return

Expand All @@ -434,6 +405,14 @@ def render(out, node, indent=0, return_type=None):
return

if node['node'] == 'RecordDecl':
if node['kind'] == 'union':
return

print_line(out, "type %s %s {" % (node['name'], node['kind']), indent)
if 'children' in node:
for c in node['children']:
print_line(out, render_expression(c)[0], indent + 1)
print_line(out, "}\n", indent)
return

#if node['node'] == 'UNION_DECL' or node['node'] == 'STRUCT_DECL':
Expand Down

0 comments on commit d041597

Please sign in to comment.