|
16 | 16 |
|
17 | 17 | <script type="text/python"> |
18 | 18 |
|
19 | | -class Name: |
| 19 | +def tokenize(src): |
| 20 | + module = 'test' |
| 21 | + return JS('$tokenize(src, module)') ## Brython tokenizer |
| 22 | + |
| 23 | + |
| 24 | +class Assign: |
| 25 | + def __init__(self, ctx): |
| 26 | + self.targets = [] |
| 27 | + self.targets.append( to_ast_node(ctx.tree[0]) ) ## should be: expr.name==id |
| 28 | + self.value = to_ast_node( ctx.tree[1] ) ## should be an: expr.name==operand |
| 29 | + |
| 30 | +class Num: |
20 | 31 | def __init__(self, ctx): |
21 | | - print 'Name:', ctx.name |
22 | | - self.id = ctx.name |
| 32 | + self.n = ctx.value |
| 33 | + |
| 34 | +class Name: |
| 35 | + def __init__(self, ctx=None, name=None): |
| 36 | + if name: |
| 37 | + self.id = name |
| 38 | + elif ctx.type == 'id': |
| 39 | + self.id = ctx.value |
| 40 | + else: |
| 41 | + raise TypeError |
| 42 | + |
| 43 | + print 'Name:', self.id |
23 | 44 |
|
24 | 45 | class _arguments: |
25 | 46 | def __init__(self, ctx): |
|
32 | 53 | if ctx.type != 'func_args': |
33 | 54 | raise TypeError |
34 | 55 | for c in ctx.tree: |
35 | | - if c.type != 'func_arg_id': |
| 56 | + if c.type == 'func_arg_id': |
| 57 | + self.args.append( Name(name=c.name) ) |
| 58 | + elif c.type == 'func_star_arg' and c.op=='*': |
| 59 | + self.vararg = c.name |
| 60 | + elif c.type == 'func_star_arg' and c.op=='**': |
| 61 | + self.kwarg = c.name |
| 62 | + else: |
36 | 63 | raise TypeError |
37 | | - self.args.append( Name(c) ) |
38 | 64 |
|
39 | 65 |
|
40 | 66 |
|
41 | | -class FunctionDef: |
| 67 | +class BinOp: |
42 | 68 | def __init__(self, ctx): |
| 69 | + self.left = to_ast_node( ctx.tree[0] ) |
| 70 | + self.right = to_ast_node( ctx.tree[1] ) |
| 71 | + self.op = ctx.op ## should be: +,-,*, etc... |
| 72 | + |
| 73 | +class Return: |
| 74 | + def __init__(self, ctx): |
| 75 | + self.value = to_ast_node( ctx.tree[0] ) |
| 76 | + |
| 77 | +class FunctionDef: |
| 78 | + def __init__(self, ctx, node): |
43 | 79 | self.name = ctx.name |
44 | 80 | self.args = _arguments( ctx.tree[0] ) |
45 | 81 | self.body = [] |
46 | 82 | self.decorator_list = [] |
47 | 83 | self.returns = None ## python3 returns annotation |
| 84 | + for child in node.children: |
| 85 | + anode = to_ast_node( child.get_ctx() ) |
| 86 | + self.body.append( anode ) |
| 87 | + |
| 88 | + |
| 89 | +class Call: |
| 90 | + def __init__(self, ctx, node): |
| 91 | + self.func = None |
| 92 | + self.args = [] |
| 93 | + self.keywords = [] |
| 94 | + self.starargs = None |
| 95 | + self.kwargs = None |
| 96 | + |
| 97 | + for c in ctx.tree: |
| 98 | + if c.type == 'call_arg': |
| 99 | + self.args.append( to_ast_node(c.tree[0]) ) |
48 | 100 |
|
49 | | -def tokenize(src): |
50 | | - module = 'test' |
51 | | - return JS('$tokenize(src, module)') |
52 | 101 |
|
53 | | -def walk_tree( ctx ): |
54 | | - anode = None |
55 | | - print 'ctx.type:', ctx.type |
| 102 | +def to_ast_node( ctx, node=None ): |
| 103 | + print 'to-ast-node', ctx |
56 | 104 | if ctx.type == 'def': |
57 | | - print ' def ', ctx.name |
58 | | - anode = FunctionDef( ctx ) |
| 105 | + return FunctionDef( ctx, node ) |
| 106 | + elif ctx.type == 'assign': |
| 107 | + return Assign(ctx) |
| 108 | + elif ctx.type == 'return': |
| 109 | + return Return(ctx) |
| 110 | + elif ctx.type == 'node': |
| 111 | + return to_ast_node( ctx.tree[0], node=node ) |
| 112 | + elif ctx.type == 'expr' and ctx.name == 'id': |
| 113 | + if ctx.tree[0].type == 'call': |
| 114 | + return Call( ctx.tree[0], node) |
| 115 | + else: |
| 116 | + return Name( ctx.tree[0] ) |
| 117 | + elif ctx.type == 'expr' and ctx.name == 'operand': |
| 118 | + return BinOp( ctx.tree[0] ) |
| 119 | + |
| 120 | + elif ctx.type == 'expr' and ctx.name == 'int': |
| 121 | + return Num( ctx.tree[0] ) |
59 | 122 |
|
60 | | - if anode: |
61 | | - return anode |
62 | 123 | else: |
63 | | - for c in ctx.tree: |
64 | | - walk_tree( c ) |
| 124 | + print ctx |
| 125 | + raise TypeError |
| 126 | + |
65 | 127 |
|
66 | | -def convert_to_ast( node ): |
| 128 | +def walk_nodes( node, module ): |
67 | 129 | print 'node.type:', node.type |
68 | | - ctx = node.get_ctx() |
69 | | - if ctx: |
70 | | - walk_tree( ctx ) |
71 | 130 |
|
72 | | - for child in node.children: |
73 | | - convert_to_ast( child ) |
| 131 | + if node.type == 'expression': |
| 132 | + anode = to_ast_node( node.get_ctx(), node=node ) |
| 133 | + module.append( anode ) |
74 | 134 |
|
75 | | -source = """ |
76 | | -def xxx(a,b): |
77 | | - return a+b |
| 135 | + else: |
| 136 | + for child in node.children: |
| 137 | + walk_nodes( child, module ) |
78 | 138 |
|
79 | | -print( xxx(1, 2) ) |
80 | 139 |
|
| 140 | +source = """ |
| 141 | +def func(a,b): |
| 142 | + c = a+b |
| 143 | + return c |
| 144 | + |
| 145 | +func(100, 200) |
81 | 146 | """ |
82 | 147 |
|
83 | 148 | def test(): |
84 | | - global a |
| 149 | + global a, module |
85 | 150 | a = tokenize( source ) |
86 | 151 | print a |
87 | | - convert_to_ast( a ) |
| 152 | + module = list() |
| 153 | + walk_nodes( a, module ) |
| 154 | + |
88 | 155 |
|
89 | 156 | </script> |
90 | 157 |
|
|
0 commit comments