@@ -358,11 +358,28 @@ func TestCompile(t *testing.T) {
358
358
` PushLocal 0 # v` ,
359
359
` ReturnTop` ,
360
360
},
361
+
362
+ `return add1(10)` : {
363
+ ` PushIntConst 0 # value=10` ,
364
+ ` IntCall 0 # testpkg.add1` ,
365
+ ` ReturnIntTop` ,
366
+ },
367
+
368
+ `return concat(concat("x", "y"), "z")` : {
369
+ ` PushConst 0 # value="x"` ,
370
+ ` PushConst 1 # value="y"` ,
371
+ ` Call 1 # testpkg.concat` ,
372
+ ` PushConst 2 # value="z"` ,
373
+ ` Call 1 # testpkg.concat` ,
374
+ ` ReturnTop` ,
375
+ },
361
376
}
362
377
363
378
makePackageSource := func (body string ) string {
364
379
return `
365
- package testpkg
380
+ package ` + testPackage + `
381
+ func add1(x int) int { return x + 1 }
382
+ func concat(s1, s2 string) string { return s1 + s2 }
366
383
func f(i int, s string, b bool, eface interface{}) interface{} {
367
384
` + body + `
368
385
}
@@ -373,37 +390,37 @@ func TestCompile(t *testing.T) {
373
390
`
374
391
}
375
392
376
- env := quasigo .NewEnv ()
377
- env .AddNativeFunc (testPackage , "imul" , func (stack * quasigo.ValueStack ) {
378
- panic ("should not be called" )
379
- })
380
- env .AddNativeFunc (testPackage , "idiv" , func (stack * quasigo.ValueStack ) {
381
- panic ("should not be called" )
382
- })
383
- env .AddNativeFunc (testPackage , "atoi" , func (stack * quasigo.ValueStack ) {
384
- panic ("should not be called" )
385
- })
386
- env .AddNativeFunc (testPackage , "sprintf" , func (stack * quasigo.ValueStack ) {
387
- panic ("should not be called" )
388
- })
389
- env .AddNativeFunc ("builtin" , "PrintInt" , func (stack * quasigo.ValueStack ) {
390
- panic ("should not be called" )
391
- })
392
- env .AddNativeFunc ("builtin" , "Print" , func (stack * quasigo.ValueStack ) {
393
- panic ("should not be called" )
394
- })
395
-
396
393
for testSrc , disasmLines := range tests {
394
+ env := quasigo .NewEnv ()
395
+ env .AddNativeFunc (testPackage , "imul" , func (stack * quasigo.ValueStack ) {
396
+ panic ("should not be called" )
397
+ })
398
+ env .AddNativeFunc (testPackage , "idiv" , func (stack * quasigo.ValueStack ) {
399
+ panic ("should not be called" )
400
+ })
401
+ env .AddNativeFunc (testPackage , "atoi" , func (stack * quasigo.ValueStack ) {
402
+ panic ("should not be called" )
403
+ })
404
+ env .AddNativeFunc (testPackage , "sprintf" , func (stack * quasigo.ValueStack ) {
405
+ panic ("should not be called" )
406
+ })
407
+ env .AddNativeFunc ("builtin" , "PrintInt" , func (stack * quasigo.ValueStack ) {
408
+ panic ("should not be called" )
409
+ })
410
+ env .AddNativeFunc ("builtin" , "Print" , func (stack * quasigo.ValueStack ) {
411
+ panic ("should not be called" )
412
+ })
397
413
src := makePackageSource (testSrc )
398
- parsed , err := parseGoFile (src )
414
+ parsed , err := parseGoFile (testPackage , src )
399
415
if err != nil {
400
- t .Errorf ("parse %s: %v" , testSrc , err )
401
- continue
416
+ t .Fatalf ("parse %s: %v" , testSrc , err )
402
417
}
403
- compiled , err := compileTestFunc (env , "f" , parsed )
418
+ compiled , err := compileTestFile (env , "f" , testPackage , parsed )
404
419
if err != nil {
405
- t .Errorf ("compile %s: %v" , testSrc , err )
406
- continue
420
+ t .Fatal (err )
421
+ }
422
+ if compiled == nil {
423
+ t .Fatal ("can't find f function" )
407
424
}
408
425
want := disasmLines
409
426
have := strings .Split (quasigo .Disasm (env , compiled ), "\n " )
0 commit comments