Skip to content

Commit

Permalink
argv.c array.c comments.c fib.c now working
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchance committed Mar 25, 2017
1 parent ca3c856 commit a33b4eb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 61 deletions.
9 changes: 6 additions & 3 deletions ast2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

regex = {
'AlwaysInlineAttr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> always_inline",
'ArraySubscriptExpr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*?)' (?P<tags>.*)",
'AsmLabelAttr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> \"(?P<function>.+)\"",
'AvailabilityAttr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> (?P<os>\w+) (?P<version>[\d.]+) (?P<unknown1>[\d.]+) (?P<unknown2>[\d.]+) (?P<unknown3>\".*?\"|\w+) (?P<unknown4>\".*?\"|\w+)",
'BinaryOperator': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*?)' '(?P<operator>.*?)'",
Expand All @@ -38,15 +39,16 @@
'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>.+)',
'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>.*)>",
'FunctionDecl': r"^ (?P<address>[0-9a-fx]+) (?P<prev>prev [0-9a-fx]+)? ?<(?P<position1>.*)> (?P<position2>[^ ]+)(?P<tags1> implicit)?(?P<tags2> used)? (?P<name>\w+) '(?P<type>.*)'(?P<tags3> extern)?",
'IfStmt': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)>',
'ImplicitCastExpr': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> \'(?P<type>.*)\' <(?P<kind>.*)>',
'IntegerLiteral': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> \'(?P<type>.*)\' (?P<value>\d+)',
'MemberExpr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*?)' lvalue ->(?P<name>\w+) (?P<address2>[0-9a-fx]+)",
'IntegerLiteral': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> \'(?P<type>.*)\' (?P<value>.+)',
'MemberExpr': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*?)' (?P<tags>.*?)(?P<name>\w+) (?P<address2>[0-9a-fx]+)",
'ParenExpr': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> \'(?P<type>.*?)\'',
'ParmVarDecl': r'^ (?P<address>[0-9a-fx]+) <(?P<position>.*)> (?P<position2>.+) \'(?P<type>.*?)\'(?P<type2>:\'.*?\')?',
'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>.+)',
Expand All @@ -59,6 +61,7 @@
'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>.*)",
'WhileStmt': r"^ (?P<address>[0-9a-fx]+) <(?P<position>.*)>",
}

def build_tree(nodes, depth):
Expand Down
118 changes: 60 additions & 58 deletions c2go.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def resolve_type(s):
if s == 'int' or s == '__darwin_ct_rune_t':
return s

if s == 'long':
if s == 'long' or s == '__mbstate_t' or s == '__builtin_va_list':
return 'int64'

if s == 'long long':
Expand Down Expand Up @@ -86,6 +86,9 @@ def resolve_type(s):
if s == 'long int':
return 'int32'

if s == '__int128':
return 'int64'

if re.match('unsigned char \\[\\d+\\]', s):
return s[14:] + 'byte'

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

# return s
return s

raise Exception('Cannot resolve type "%s"' % s)
# raise Exception('Cannot resolve type "%s"' % s)

def cast(expr, from_type, to_type):
from_type = resolve_type(from_type)
Expand All @@ -128,15 +131,9 @@ def print_line(out, line, indent):
out.write('%s%s\n' % ('\t' * indent, line))

def render_expression(node):
if node['node'] == 'BINARY_OPERATOR':
end_of_left = list(node.get_children())[0].extent.end.column
operator = None
for t in node.get_tokens():
if t.extent.start.column >= end_of_left:
operator = t.spelling
break

left, right = [render_expression(t)[0] for t in list(node.get_children())]
if node['node'] == 'BinaryOperator':
operator = node['operator']
left, right = [render_expression(t)[0] for t in node['children']]

return_type = 'bool'
if operator == '|' or operator == '&':
Expand All @@ -151,19 +148,9 @@ def render_expression(node):
except TypeError:
return '// CONDITIONAL_OPERATOR: %s' % ''.join([t.spelling for t in node.get_tokens()]), 'unknown'

if node['node'] == 'UNARY_OPERATOR':
expr_start = list(node.get_children())[0].extent.start.column
operator = None
for t in node.get_tokens():
if t.extent.start.column >= expr_start:
break

operator = t.spelling

if operator is None:
operator = '++'

expr = render_expression(list(node.get_children())[0])
if node['node'] == 'UnaryOperator':
operator = node['operator']
expr = render_expression(node['children'][0])

if operator == '!':
return '%s(%s)' % ('__not_%s' % expr[1], expr[0]), expr[1]
Expand Down Expand Up @@ -193,20 +180,20 @@ def render_expression(node):
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")
# 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'):
return node['value'], 'const char*'

if node['node'] == 'INTEGER_LITERAL':
literal = list(node.get_tokens())[0].spelling
if node['node'] == 'IntegerLiteral':
literal = node['value']
if literal[-1] == 'L':
literal = '%s(%s)' % (resolve_type('long'), literal[:-1])

Expand All @@ -217,7 +204,16 @@ def render_expression(node):
return '(%s)' % e[0], e[1]

if node['node'] == 'DeclRefExpr':
return node['name'], node['type']
name = node['name']

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

return name, node['type']

if node['node'] == 'ImplicitCastExpr':
return render_expression(node['children'][0])
Expand Down Expand Up @@ -247,8 +243,8 @@ def render_expression(node):

return '%s(%s)' % (func_name, ', '.join(args)), func_def[0]

if node['node'] == 'ARRAY_SUBSCRIPT_EXPR':
children = list(node.get_children())
if node['node'] == 'ArraySubscriptExpr':
children = node['children']
return '%s[%s]' % (render_expression(children[0])[0],
render_expression(children[1])[0]), 'unknown'

Expand All @@ -260,22 +256,22 @@ def render_expression(node):
children = list(node.get_children())
return render_expression(children[0]), 'unknown'

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

prefix = ''
if node['node'] == 'VAR_DECL':
if node['node'] == 'VarDecl':
prefix = 'var '

suffix = ''
children = list(node.get_children())
# 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 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)

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

Expand Down Expand Up @@ -316,8 +312,8 @@ def render(out, node, indent=0, return_type=None):
has_body = True

args = []
# for a in get_function_params(node):
# args.append('%s %s' % (a['name'], resolve_type(a['type'])))
for a in get_function_params(node):
args.append('%s %s' % (a['name'], resolve_type(a['type'])))

if has_body:
return_type = ' ' + node['type']
Expand Down Expand Up @@ -377,17 +373,17 @@ def render(out, node, indent=0, return_type=None):

# return

# if node['node'] == 'FOR_STMT':
# children = list(node.get_children())
if node['node'] == 'ForStmt':
children = node['children']

# a, b, c = [render_expression(e)[0] for e in children[:3]]
# print_line(out, 'for %s; %s; %s {' % (a, b, c), indent)
a, b, c = [render_expression(e)[0] for e in children[:3]]
print_line(out, 'for %s; %s; %s {' % (a, b, c), indent)

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

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

# return
return

# if node['node'] == 'BREAK_STMT':
# print_line(out, 'break', indent)
Expand Down Expand Up @@ -419,7 +415,13 @@ def render(out, node, indent=0, return_type=None):
return

if node['node'] == 'TypedefDecl':
print_line(out, "type %s %s\n" % (node['type'], node['name']), indent)
# FIXME: All of the logic here is just to avoid errors, it needs to be
# fixed up.
if 'struct' in node['type'] or 'union' in node['type']:
return
node['type'] = node['type'].replace('unsigned', '')

print_line(out, "type %s %s\n" % (node['name'], resolve_type(node['type'])), indent)
# print(node)
return

Expand Down Expand Up @@ -472,10 +474,10 @@ def render(out, node, indent=0, return_type=None):
# print_line(out, '// ' + ' '.join(tokens[1:-2]), indent)
# return

# if node['node'] == 'DECL_STMT':
# for child in node.get_children():
# print_line(out, render_expression(child)[0], indent)
# return
if node['node'] == 'DeclStmt':
for child in node['children']:
print_line(out, render_expression(child)[0], indent)
return

if node['node'] == 'VarDecl':
# tokens = [t.spelling for t in node.get_tokens()]
Expand Down

0 comments on commit a33b4eb

Please sign in to comment.