-
Notifications
You must be signed in to change notification settings - Fork 5
/
expression.go
350 lines (320 loc) · 11.2 KB
/
expression.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"fmt"
"go/ast"
"go/token"
"github.com/NickyBoy89/java2go/astutil"
"github.com/NickyBoy89/java2go/nodeutil"
"github.com/NickyBoy89/java2go/symbol"
log "github.com/sirupsen/logrus"
sitter "github.com/smacker/go-tree-sitter"
)
// ParseExpr parses an expression type
func ParseExpr(node *sitter.Node, source []byte, ctx Ctx) ast.Expr {
switch node.Type() {
case "ERROR":
log.WithFields(log.Fields{
"parsed": node.Content(source),
"className": ctx.className,
}).Warn("Expression parse error")
return &ast.BadExpr{}
case "comment":
return &ast.BadExpr{}
case "update_expression":
// This can either be a pre or post expression
// a pre expression has the identifier second, while the post expression
// has the identifier first
// Post-update expression, e.g. `i++`
if node.Child(0).IsNamed() {
return &ast.CallExpr{
Fun: &ast.Ident{Name: "PostUpdate"},
Args: []ast.Expr{ParseExpr(node.Child(0), source, ctx)},
}
}
// Otherwise, pre-update expression
return &ast.CallExpr{
Fun: &ast.Ident{Name: "PreUpdate"},
Args: []ast.Expr{ParseExpr(node.Child(1), source, ctx)},
}
case "class_literal":
// Class literals refer to the class directly, such as
// Object.class
return &ast.BadExpr{}
case "assignment_expression":
return &ast.CallExpr{
Fun: &ast.Ident{Name: "AssignmentExpression"},
Args: []ast.Expr{
ParseExpr(node.Child(0), source, ctx),
&ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf("\"%s\"", node.Child(1).Content(source))},
ParseExpr(node.Child(2), source, ctx),
},
}
case "super":
return &ast.BadExpr{}
case "lambda_expression":
// Lambdas can either be called with a list of expressions
// (ex: (n1, n1) -> {}), or with a single expression
// (ex: n1 -> {})
var lambdaBody *ast.BlockStmt
var lambdaParameters *ast.FieldList
bodyNode := node.ChildByFieldName("body")
switch bodyNode.Type() {
case "block":
lambdaBody = ParseStmt(bodyNode, source, ctx).(*ast.BlockStmt)
default:
// Lambdas can be called inline without a block expression
lambdaBody = &ast.BlockStmt{
List: []ast.Stmt{
&ast.ExprStmt{
X: ParseExpr(bodyNode, source, ctx),
},
},
}
}
paramNode := node.ChildByFieldName("parameters")
switch paramNode.Type() {
case "inferred_parameters", "formal_parameters":
lambdaParameters = ParseNode(paramNode, source, ctx).(*ast.FieldList)
default:
// If we can't identify the types of the parameters, then just set their
// types to any
lambdaParameters = &ast.FieldList{
List: []*ast.Field{
&ast.Field{
Names: []*ast.Ident{ParseExpr(paramNode, source, ctx).(*ast.Ident)},
Type: &ast.Ident{Name: "any"},
},
},
}
}
return &ast.FuncLit{
Type: &ast.FuncType{
Params: lambdaParameters,
},
Body: lambdaBody,
}
case "method_reference":
// This refers to manually selecting a function from a specific class and
// passing it in as an argument in the `func(className::methodName)` style
// For class constructors such as `Class::new`, you only get one node
if node.NamedChildCount() < 2 {
return &ast.SelectorExpr{
X: ParseExpr(node.NamedChild(0), source, ctx),
Sel: &ast.Ident{Name: "new"},
}
}
return &ast.SelectorExpr{
X: ParseExpr(node.NamedChild(0), source, ctx),
Sel: ParseExpr(node.NamedChild(1), source, ctx).(*ast.Ident),
}
case "array_initializer":
// A literal that initilzes an array, such as `{1, 2, 3}`
items := []ast.Expr{}
for _, c := range nodeutil.NamedChildrenOf(node) {
items = append(items, ParseExpr(c, source, ctx))
}
// If there wasn't a type for the array specified, then use the one that has been defined
if _, ok := ctx.lastType.(*ast.ArrayType); ctx.lastType != nil && ok {
return &ast.CompositeLit{
Type: ctx.lastType.(*ast.ArrayType),
Elts: items,
}
}
return &ast.CompositeLit{
Elts: items,
}
case "method_invocation":
// Methods with a selector are called as X.Sel(Args)
// Otherwise, they are called as Fun(Args)
if node.ChildByFieldName("object") != nil {
return &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ParseExpr(node.ChildByFieldName("object"), source, ctx),
Sel: ParseExpr(node.ChildByFieldName("name"), source, ctx).(*ast.Ident),
},
Args: ParseNode(node.ChildByFieldName("arguments"), source, ctx).([]ast.Expr),
}
}
return &ast.CallExpr{
Fun: ParseExpr(node.ChildByFieldName("name"), source, ctx),
Args: ParseNode(node.ChildByFieldName("arguments"), source, ctx).([]ast.Expr),
}
case "object_creation_expression":
// This is called when anything is created with a constructor
objectType := node.ChildByFieldName("type")
// A object can also be created with this format:
// parentClass.new NestedClass()
if !node.NamedChild(0).Equal(objectType) {
}
// Get all the arguments, and look up their types
objectArguments := node.ChildByFieldName("arguments")
arguments := make([]ast.Expr, objectArguments.NamedChildCount())
argumentTypes := make([]string, objectArguments.NamedChildCount())
for ind, argument := range nodeutil.NamedChildrenOf(objectArguments) {
arguments[ind] = ParseExpr(argument, source, ctx)
// Look up each argument and find its type
if argument.Type() != "identifier" {
argumentTypes[ind] = symbol.TypeOfLiteral(argument, source)
} else {
if localDef := ctx.localScope.FindVariable(argument.Content(source)); localDef != nil {
argumentTypes[ind] = localDef.OriginalType
// Otherwise, a variable may exist as a global variable
} else if def := ctx.currentFile.FindField().ByOriginalName(argument.Content(source)); len(def) > 0 {
argumentTypes[ind] = def[0].OriginalType
}
}
}
var constructor *symbol.Definition
// Find the respective constructor, and call it
if objectType.Type() == "generic_type" {
constructor = ctx.currentClass.FindMethodByName(objectType.NamedChild(0).Content(source), argumentTypes)
} else {
constructor = ctx.currentClass.FindMethodByName(objectType.Content(source), argumentTypes)
}
if constructor != nil {
return &ast.CallExpr{
Fun: &ast.Ident{Name: constructor.Name},
Args: arguments,
}
}
// It is also possible that a constructor could be unresolved, so we handle
// this by calling the type of the type + "Construct" at the beginning
return &ast.CallExpr{
Fun: &ast.Ident{Name: "Construct" + objectType.Content(source)},
Args: arguments,
}
case "array_creation_expression":
dimensions := []ast.Expr{}
arrayType := astutil.ParseType(node.ChildByFieldName("type"), source)
for _, child := range nodeutil.NamedChildrenOf(node) {
if child.Type() == "dimensions_expr" {
dimensions = append(dimensions, ParseExpr(child, source, ctx))
}
}
// TODO: Fix this to handle arrays that are declared with types,
// i.e `new int[] {1, 2, 3};`
if len(dimensions) == 0 {
panic("Array had zero dimensions")
}
return GenMultiDimArray(symbol.NodeToStr(arrayType), dimensions)
case "instanceof_expression":
return &ast.BadExpr{}
case "dimensions_expr":
return ParseExpr(node.NamedChild(0), source, ctx)
case "binary_expression":
if node.Child(1).Content(source) == ">>>" {
return &ast.CallExpr{
Fun: &ast.Ident{Name: "UnsignedRightShift"},
Args: []ast.Expr{ParseExpr(node.Child(0), source, ctx), ParseExpr(node.Child(2), source, ctx)},
}
}
return &ast.BinaryExpr{
X: ParseExpr(node.Child(0), source, ctx),
Op: StrToToken(node.Child(1).Content(source)),
Y: ParseExpr(node.Child(2), source, ctx),
}
case "unary_expression":
return &ast.UnaryExpr{
Op: StrToToken(node.Child(0).Content(source)),
X: ParseExpr(node.Child(1), source, ctx),
}
case "parenthesized_expression":
return &ast.ParenExpr{
X: ParseExpr(node.NamedChild(0), source, ctx),
}
case "ternary_expression":
// Ternary expressions are replaced with a function that takes in the
// condition, and returns one of the two values, depending on the condition
args := []ast.Expr{}
for _, c := range nodeutil.NamedChildrenOf(node) {
args = append(args, ParseExpr(c, source, ctx))
}
return &ast.CallExpr{
Fun: &ast.Ident{Name: "ternary"},
Args: args,
}
case "cast_expression":
// TODO: This probably should be a cast function, instead of an assertion
return &ast.TypeAssertExpr{
X: ParseExpr(node.NamedChild(1), source, ctx),
Type: astutil.ParseType(node.NamedChild(0), source),
}
case "field_access":
// X.Sel
obj := node.ChildByFieldName("object")
if obj.Type() == "this" {
def := ctx.currentClass.FindField().ByOriginalName(node.ChildByFieldName("field").Content(source))
if len(def) == 0 {
// TODO: This field could not be found in the current class, because it exists in the superclass
// definition for the class
def = []*symbol.Definition{&symbol.Definition{
Name: node.ChildByFieldName("field").Content(source),
}}
}
return &ast.SelectorExpr{
X: ParseExpr(node.ChildByFieldName("object"), source, ctx),
Sel: &ast.Ident{Name: def[0].Name},
}
}
return &ast.SelectorExpr{
X: ParseExpr(obj, source, ctx),
Sel: ParseExpr(node.ChildByFieldName("field"), source, ctx).(*ast.Ident),
}
case "array_access":
return &ast.IndexExpr{
X: ParseExpr(node.NamedChild(0), source, ctx),
Index: ParseExpr(node.NamedChild(1), source, ctx),
}
case "scoped_identifier":
return ParseExpr(node.NamedChild(0), source, ctx)
case "this":
return &ast.Ident{Name: ShortName(ctx.className)}
case "identifier":
return &ast.Ident{Name: node.Content(source)}
case "type_identifier": // Any reference type
switch node.Content(source) {
// Special case for strings, because in Go, these are primitive types
case "String":
return &ast.Ident{Name: "string"}
}
if ctx.currentFile != nil {
// Look for the class locally first
if localClass := ctx.currentFile.FindClass(node.Content(source)); localClass != nil {
return &ast.StarExpr{
X: &ast.Ident{Name: localClass.Name},
}
}
}
return &ast.StarExpr{
X: &ast.Ident{Name: node.Content(source)},
}
case "null_literal":
return &ast.Ident{Name: "nil"}
case "decimal_integer_literal":
literal := node.Content(source)
switch literal[len(literal)-1] {
case 'L':
return &ast.CallExpr{Fun: &ast.Ident{Name: "int64"}, Args: []ast.Expr{&ast.BasicLit{Kind: token.INT, Value: literal[:len(literal)-1]}}}
}
return &ast.Ident{Name: literal}
case "hex_integer_literal":
return &ast.Ident{Name: node.Content(source)}
case "decimal_floating_point_literal":
// This is something like 1.3D or 1.3F
literal := node.Content(source)
switch literal[len(literal)-1] {
case 'D':
return &ast.CallExpr{Fun: &ast.Ident{Name: "float64"}, Args: []ast.Expr{&ast.BasicLit{Kind: token.FLOAT, Value: literal[:len(literal)-1]}}}
case 'F':
return &ast.CallExpr{Fun: &ast.Ident{Name: "float32"}, Args: []ast.Expr{&ast.BasicLit{Kind: token.FLOAT, Value: literal[:len(literal)-1]}}}
}
return &ast.Ident{Name: literal}
case "string_literal":
return &ast.Ident{Name: node.Content(source)}
case "character_literal":
return &ast.Ident{Name: node.Content(source)}
case "true", "false":
return &ast.Ident{Name: node.Content(source)}
}
panic("Unhandled expression: " + node.Type())
}