Skip to content

Commit

Permalink
Merge pull request #1491 from xushiwei/q
Browse files Browse the repository at this point in the history
gox: newScope support start..end
  • Loading branch information
xushiwei authored Oct 24, 2023
2 parents c53cbfa + e7edc29 commit 09e2200
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ var unaryGopNames = map[string]string{
}

func loadFuncBody(ctx *blockCtx, fn *gox.Func, body *ast.BlockStmt, src ast.Node) {
cb := fn.BodyStart(ctx.pkg)
cb := fn.BodyStart(ctx.pkg, body)
compileStmts(ctx, body.List)
cb.End(src)
}
Expand Down
48 changes: 24 additions & 24 deletions cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,10 @@ func compileRangeStmt(ctx *blockCtx, v *ast.RangeStmt) {
if v.Value != nil {
names = append(names, v.Value.(*ast.Ident).Name)
}
cb.ForRange(names...)
cb.ForRangeEx(names, v)
compileExpr(ctx, v.X)
} else {
cb.ForRange()
cb.ForRangeEx(nil, v)
n := 0
if v.Key == nil {
if v.Value != nil {
Expand All @@ -321,11 +321,11 @@ func compileRangeStmt(ctx *blockCtx, v *ast.RangeStmt) {
if pos == 0 {
pos = v.For
}
cb.RangeAssignThen(pos)
cb.RangeAssignThen(pos) // TODO: need NewScope for body
compileStmts(ctx, v.Body.List)
cb.SetComments(comments, once)
setBodyHandler(ctx)
cb.End()
cb.End(v)
}

func compileForPhraseStmt(ctx *blockCtx, v *ast.ForPhraseStmt) {
Expand Down Expand Up @@ -459,7 +459,7 @@ func toForStmt(forPos token.Pos, value ast.Expr, body *ast.BlockStmt, re *ast.Ra
func compileForStmt(ctx *blockCtx, v *ast.ForStmt) {
cb := ctx.cb
comments, once := cb.BackupComments()
cb.For()
cb.For(v)
if v.Init != nil {
compileStmt(ctx, v.Init)
}
Expand All @@ -468,15 +468,15 @@ func compileForStmt(ctx *blockCtx, v *ast.ForStmt) {
} else {
cb.None()
}
cb.Then()
cb.Then(v.Body)
compileStmts(ctx, v.Body.List)
if v.Post != nil {
cb.Post()
compileStmt(ctx, v.Post)
}
cb.SetComments(comments, once)
setBodyHandler(ctx)
cb.End()
cb.End(v)
}

// if init; cond then
Expand All @@ -487,23 +487,23 @@ func compileForStmt(ctx *blockCtx, v *ast.ForStmt) {
func compileIfStmt(ctx *blockCtx, v *ast.IfStmt) {
cb := ctx.cb
comments, once := cb.BackupComments()
cb.If()
cb.If(v)
if v.Init != nil {
compileStmt(ctx, v.Init)
}
compileExpr(ctx, v.Cond)
cb.Then()
cb.Then(v.Body)
compileStmts(ctx, v.Body.List)
if e := v.Else; e != nil {
cb.Else()
cb.Else(e)
if stmts, ok := e.(*ast.BlockStmt); ok {
compileStmts(ctx, stmts.List)
} else {
compileStmt(ctx, e)
}
}
cb.SetComments(comments, once)
cb.End()
cb.End(v)
}

// typeSwitch(name) init; expr typeAssertThen()
Expand Down Expand Up @@ -536,7 +536,7 @@ func compileTypeSwitchStmt(ctx *blockCtx, v *ast.TypeSwitchStmt) {
if ta.Type != nil {
panic("TODO: type switch syntax error, please use x.(type)")
}
cb.TypeSwitch(name)
cb.TypeSwitch(name, v)
if v.Init != nil {
compileStmt(ctx, v.Init)
}
Expand Down Expand Up @@ -582,13 +582,13 @@ func compileTypeSwitchStmt(ctx *blockCtx, v *ast.TypeSwitchStmt) {
firstDefault = c
}
}
cb.TypeCase(len(c.List)) // TypeCase(0) means default case
cb.TypeCase(len(c.List), c) // TypeCase(0) means default case
compileStmts(ctx, c.Body)
commentStmt(ctx, stmt)
cb.End()
cb.End(c)
}
cb.SetComments(comments, once)
cb.End()
cb.End(v)
}

// switch init; tag then
Expand All @@ -606,7 +606,7 @@ func compileTypeSwitchStmt(ctx *blockCtx, v *ast.TypeSwitchStmt) {
func compileSwitchStmt(ctx *blockCtx, v *ast.SwitchStmt) {
cb := ctx.cb
comments, once := cb.BackupComments()
cb.Switch()
cb.Switch(v)
if v.Init != nil {
compileStmt(ctx, v.Init)
}
Expand All @@ -615,7 +615,7 @@ func compileSwitchStmt(ctx *blockCtx, v *ast.SwitchStmt) {
} else {
cb.None() // switch {...}
}
cb.Then()
cb.Then(v.Body)
seen := make(valueMap)
var firstDefault ast.Stmt
for _, stmt := range v.Body.List {
Expand Down Expand Up @@ -656,17 +656,17 @@ func compileSwitchStmt(ctx *blockCtx, v *ast.SwitchStmt) {
firstDefault = c
}
}
cb.Case(len(c.List)) // Case(0) means default case
cb.Case(len(c.List), c) // Case(0) means default case
body, has := hasFallthrough(c.Body)
compileStmts(ctx, body)
if has {
cb.Fallthrough()
}
commentStmt(ctx, stmt)
cb.End()
cb.End(c)
}
cb.SetComments(comments, once)
cb.End()
cb.End(v)
}

func hasFallthrough(body []ast.Stmt) ([]ast.Stmt, bool) {
Expand Down Expand Up @@ -699,7 +699,7 @@ func hasFallthrough(body []ast.Stmt) ([]ast.Stmt, bool) {
func compileSelectStmt(ctx *blockCtx, v *ast.SelectStmt) {
cb := ctx.cb
comments, once := cb.BackupComments()
cb.Select()
cb.Select(v)
for _, stmt := range v.Body.List {
c, ok := stmt.(*ast.CommClause)
if !ok {
Expand All @@ -710,13 +710,13 @@ func compileSelectStmt(ctx *blockCtx, v *ast.SelectStmt) {
compileStmt(ctx, c.Comm)
n = 1
}
cb.CommCase(n) // CommCase(0) means default case
cb.CommCase(n, c) // CommCase(0) means default case
compileStmts(ctx, c.Body)
commentStmt(ctx, stmt)
cb.End()
cb.End(c)
}
cb.SetComments(comments, once)
cb.End()
cb.End(v)
}

func compileBranchStmt(ctx *blockCtx, v *ast.BranchStmt) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/goplus/c2go v0.7.16
github.com/goplus/gox v1.12.2-0.20231023222557-3d1b2e0a7dbf
github.com/goplus/gox v1.12.2-0.20231024165412-08fb2524d4a0
github.com/goplus/mod v0.11.8-0.20231019172744-da5848421263
github.com/qiniu/x v1.13.1
golang.org/x/tools v0.14.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/goplus/c2go v0.7.16 h1:B9jRNNytoq7yDBQOZSm2qENQhRQMfNhgOea5XHWNUnI=
github.com/goplus/c2go v0.7.16/go.mod h1:XODEFX2PeEEJXNLLdykWeZgMSaKJ51fVm+C9IM3FxNQ=
github.com/goplus/gox v1.12.1/go.mod h1:wymoQJ7ydd42cTlaXb4wNbvn4LlKjR+j8PZehI7v1zQ=
github.com/goplus/gox v1.12.2-0.20231023222557-3d1b2e0a7dbf h1:gQNKxSMyYDdOFNpfqZfWjVZE5q/MsZmqn2rJ3U+OvA8=
github.com/goplus/gox v1.12.2-0.20231023222557-3d1b2e0a7dbf/go.mod h1:Ek1YIy3wRaZ1i0DD2XG29i3r5AFdhcOradK0/GGs1YQ=
github.com/goplus/gox v1.12.2-0.20231024165412-08fb2524d4a0 h1:60n144rwLnIapa5Vs9LIhh6QeaKrI26eWh0eJLVL4eM=
github.com/goplus/gox v1.12.2-0.20231024165412-08fb2524d4a0/go.mod h1:Ek1YIy3wRaZ1i0DD2XG29i3r5AFdhcOradK0/GGs1YQ=
github.com/goplus/mod v0.11.5/go.mod h1:NDC5E+XOT8vcJCMjqKhLDJHTHX7lyVN4Vbfi2U7dBhs=
github.com/goplus/mod v0.11.8-0.20231019172744-da5848421263 h1:PE0HveOss5mai9pa52L4/ZvVqZtltogJ9rIUIsdlG/I=
github.com/goplus/mod v0.11.8-0.20231019172744-da5848421263/go.mod h1:yl2QncBKTdXk+8UaNsdo4u2zSpGEJYA5JKjgD3K2h00=
Expand Down

0 comments on commit 09e2200

Please sign in to comment.