Skip to content

cl: support var define#559

Merged
xushiwei merged 13 commits into
goplus:masterfrom
visualfc:def_var
Sep 5, 2020
Merged

cl: support var define#559
xushiwei merged 13 commits into
goplus:masterfrom
visualfc:def_var

Conversation

@visualfc

@visualfc visualfc commented Aug 19, 2020

Copy link
Copy Markdown
Member

cl: support var define

Go+ code

import "image"

type Point struct {
	X int
	Y int
}

var (
	i,j,k int = 100,200,i+j
)

func main() {
	var x = Point{10,20}
	var pt image.Point
	pt.X = 10
	pt.Y = -10
	println(i,j,k,x,pt)
}

generate Golang code error

package main

import (
	fmt "fmt"
	image "image"
)

type Point struct {
	X int
	Y int
}

var (
	i int
	j int
	k int
)

func init() { 
//line ./main.gop:9
	i = 100
//line ./main.gop:9
	j = 200
//line ./main.gop:9
	k = i + j
}
func main() {
	var (
		x  Point
		pt image.Point
	)
//line ./main.gop:14
	x = Point{X: 10, Y: 20}
//line ./main.gop:16
	pt.X = 10
//line ./main.gop:17
	pt.Y = -10
//line ./main.gop:18
	fmt.Println(i, j, k, x, pt)
}

@codecov-commenter

codecov-commenter commented Aug 19, 2020

Copy link
Copy Markdown

Codecov Report

Merging #559 into master will increase coverage by 0.14%.
The diff coverage is 90.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #559      +/-   ##
==========================================
+ Coverage   90.18%   90.32%   +0.14%     
==========================================
  Files          41       41              
  Lines       10461    10546      +85     
==========================================
+ Hits         9434     9526      +92     
+ Misses        737      732       -5     
+ Partials      290      288       -2     
Impacted Files Coverage Δ
cl/stmt.go 92.69% <71.42%> (-0.71%) ⬇️
cl/ctx_check.go 76.98% <87.50%> (+0.67%) ⬆️
cl/compile.go 84.75% <100.00%> (+6.74%) ⬆️
exec/golang/builder.go 91.20% <100.00%> (+0.24%) ⬆️
exec/golang/expr.go 94.18% <0.00%> (+0.02%) ⬆️
cl/expr.go 86.81% <0.00%> (+0.05%) ⬆️
exec/bytecode/struct.go 82.60% <0.00%> (+0.19%) ⬆️
cl/type_decl.go 85.95% <0.00%> (+0.23%) ⬆️
parser/parser.go 80.04% <0.00%> (+0.43%) ⬆️
exec/bytecode/var.go 93.82% <0.00%> (+0.61%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3109592...2b09907. Read the comment docs.

@xushiwei

Copy link
Copy Markdown
Member

please fix the conflicts.

Comment thread cl/ctx_check.go
return isNoExecCtxCallExpr(ctx, v.Call)
case *ast.GoStmt:
return isNoExecCtxCallExpr(ctx, v.Call)
case *ast.DeclStmt:

@xushiwei xushiwei Aug 24, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ast.DeclStmt is not a global declaration but a normal statement in a function, it should be same as ast.AssignStmt (isNoExecCtxAssignStmt).

a := 1 // is same as:
var a = 1

@visualfc visualfc Aug 29, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

关于 isNoExecCtxExpr ,因为是 isNoExecCtxXXX 函数都是循环调用。
发现只有在调用链中出现 ast.FuncLit 时才会返回 false 。

 func isNoExecCtxFuncLit(ctx *blockCtx, v *ast.FuncLit) bool {
 // TODO: log.Warn("isNoExecCtxFuncLit: to be optimized")
 return false
}

下面的测试可以通过
isNoExecCtxDeclStmt

	fns := make([]func() int, 3)
	for i, x <- [3, 15, 777] {
		var v = x
		var fn = func() int {
			return v
		}
		fns[i] = fn
	}
       println("values:", fns[0](), fns[1](), fns[2]())

@xushiwei xushiwei merged commit 21973e4 into goplus:master Sep 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants