Skip to content

Commit d07864b

Browse files
committed
Add GoVarStmt, reduce GoRawStmt escape hatches
Add GoVarStmt type for in-body variable declarations. Convert 16 GoRawStmt instances to structured nodes: var declarations to GoVarStmt, simple assignments to GoAssignStmt, defer to GoDeferStmt, comment to GoComment. GoRawStmt drops from 64 to 48.
1 parent b5c5426 commit d07864b

5 files changed

Lines changed: 32 additions & 17 deletions

File tree

compiler/codegen_build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ func (g *codeGen) buildIf(i *ast.IfStmt) ([]GoStmt, error) {
260260
if !g.isDeclared(name) {
261261
varType := g.varType(name)
262262
if varType.IsTyped() {
263-
preDecls = append(preDecls, GoRawStmt{Code: fmt.Sprintf("var %s %s", name, varType.GoType())})
263+
preDecls = append(preDecls, GoVarStmt{Name: name, Type: varType.GoType()})
264264
} else {
265-
preDecls = append(preDecls, GoRawStmt{Code: fmt.Sprintf("var %s interface{}", name)})
265+
preDecls = append(preDecls, GoVarStmt{Name: name, Type: "interface{}"})
266266
}
267267
g.declareVar(name)
268268
}
@@ -508,7 +508,7 @@ func (g *codeGen) buildFunc(f *ast.FuncDef) (GoFuncDecl, error) {
508508

509509
// Recursion depth guard
510510
body = append(body, GoExprStmt{Expr: GoRawExpr{Code: fmt.Sprintf("rugo_check_depth(%q)", f.Name)}})
511-
body = append(body, GoRawStmt{Code: "defer func() { rugo_call_depth-- }()"})
511+
body = append(body, GoDeferStmt{Body: []GoStmt{GoRawStmt{Code: "rugo_call_depth--"}}})
512512

513513
if hasDefaults {
514514
// Arity range check
@@ -525,13 +525,13 @@ func (g *codeGen) buildFunc(f *ast.FuncDef) (GoFuncDecl, error) {
525525
for i, p := range f.Params {
526526
g.declareVar(p.Name)
527527
if p.Default == nil {
528-
body = append(body, GoRawStmt{Code: fmt.Sprintf("var %s interface{} = _args[%d]", p.Name, i)})
528+
body = append(body, GoVarStmt{Name: p.Name, Type: "interface{}", Value: GoRawExpr{Code: fmt.Sprintf("_args[%d]", i)}})
529529
} else {
530530
defaultExpr, err := g.exprString(p.Default)
531531
if err != nil {
532532
return GoFuncDecl{}, err
533533
}
534-
body = append(body, GoRawStmt{Code: fmt.Sprintf("var %s interface{}", p.Name)})
534+
body = append(body, GoVarStmt{Name: p.Name, Type: "interface{}"})
535535
body = append(body, GoRawStmt{Code: fmt.Sprintf("if len(_args) > %d { %s = _args[%d] } else { %s = %s }", i, p.Name, i, p.Name, defaultExpr)})
536536
}
537537
body = append(body, GoExprStmt{Expr: GoRawExpr{Code: fmt.Sprintf("_ = %s", p.Name)}})

compiler/codegen_expr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ func (g *codeGen) loweredSpawnExpr(e *ast.LoweredSpawnExpr) (string, error) {
718718
}},
719719
GoRawStmt{Code: "close(t.done)"},
720720
}},
721-
GoRawStmt{Code: "// spawn body"},
721+
GoComment{Text: "spawn body"},
722722
}},
723723
},
724724
Result: GoRawExpr{Code: "interface{}(t)"},
@@ -789,10 +789,10 @@ func (g *codeGen) fnExpr(e *ast.FnExpr) (string, error) {
789789
if derr != nil {
790790
return "", derr
791791
}
792-
preamble = append(preamble, GoRawStmt{Code: fmt.Sprintf("var %s interface{}", p.Name)})
792+
preamble = append(preamble, GoVarStmt{Name: p.Name, Type: "interface{}"})
793793
preamble = append(preamble, GoRawStmt{Code: fmt.Sprintf("if len(_args) > %d { %s = _args[%d] } else { %s = %s }", i, p.Name, i, p.Name, defaultExpr)})
794794
} else {
795-
preamble = append(preamble, GoRawStmt{Code: fmt.Sprintf("var %s interface{}", p.Name)})
795+
preamble = append(preamble, GoVarStmt{Name: p.Name, Type: "interface{}"})
796796
preamble = append(preamble, GoRawStmt{Code: fmt.Sprintf("if len(_args) > %d { %s = _args[%d] }", i, p.Name, i)})
797797
}
798798
preamble = append(preamble, GoExprStmt{Expr: GoRawExpr{Code: fmt.Sprintf("_ = %s", p.Name)}})

compiler/codegen_func.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ func (g *codeGen) buildTestHarness(tests []*ast.TestDef, topStmts []ast.Statemen
2222
GoIfStmt{
2323
Cond: GoRawExpr{Code: "reason, ok := r.(rugoTestSkip); ok"},
2424
Body: []GoStmt{
25-
GoRawStmt{Code: "skipped = true"},
26-
GoRawStmt{Code: "skipReason = string(reason)"},
25+
GoAssignStmt{Target: "skipped", Op: "=", Value: GoRawExpr{Code: "true"}},
26+
GoAssignStmt{Target: "skipReason", Op: "=", Value: GoRawExpr{Code: "string(reason)"}},
2727
GoReturnStmt{},
2828
},
2929
},
30-
GoRawStmt{Code: `failColor := "\033[31m"`},
31-
GoRawStmt{Code: `failReset := "\033[0m"`},
30+
GoAssignStmt{Target: "failColor", Op: ":=", Value: GoRawExpr{Code: `"\033[31m"`}},
31+
GoAssignStmt{Target: "failReset", Op: ":=", Value: GoRawExpr{Code: `"\033[0m"`}},
3232
GoIfStmt{
3333
Cond: GoRawExpr{Code: `os.Getenv("NO_COLOR") != ""`},
3434
Body: []GoStmt{
35-
GoRawStmt{Code: `failColor = ""`},
36-
GoRawStmt{Code: `failReset = ""`},
35+
GoAssignStmt{Target: "failColor", Op: "=", Value: GoRawExpr{Code: `""`}},
36+
GoAssignStmt{Target: "failReset", Op: "=", Value: GoRawExpr{Code: `""`}},
3737
},
3838
},
39-
GoRawStmt{Code: `failReason = fmt.Sprintf("%v", r)`},
39+
GoAssignStmt{Target: "failReason", Op: "=", Value: GoRawExpr{Code: `fmt.Sprintf("%v", r)`}},
4040
GoRawStmt{Code: `fmt.Fprintf(os.Stderr, " %sFAIL%s: %v\n", failColor, failReset, r)`},
41-
GoRawStmt{Code: "passed = false"},
41+
GoAssignStmt{Target: "passed", Op: "=", Value: GoRawExpr{Code: "false"}},
4242
},
4343
},
4444
}}
@@ -56,7 +56,7 @@ func (g *codeGen) buildTestHarness(tests []*ast.TestDef, topStmts []ast.Statemen
5656
g.popScope()
5757

5858
body = append(body, bodyStmts...)
59-
body = append(body, GoRawStmt{Code: "passed = true"})
59+
body = append(body, GoAssignStmt{Target: "passed", Op: "=", Value: GoRawExpr{Code: "true"}})
6060
body = append(body, GoReturnStmt{})
6161

6262
decls = append(decls, GoFuncDecl{

compiler/goast.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ type GoReturnStmt struct {
9999

100100
func (GoReturnStmt) goStmt() {}
101101

102+
// GoVarStmt represents: var name type [= value]
103+
type GoVarStmt struct {
104+
Name string
105+
Type string
106+
Value GoExpr // nil for uninitialized
107+
}
108+
109+
func (GoVarStmt) goStmt() {}
110+
102111
// GoIfStmt represents: if cond { body } [else if ...] [else { body }]
103112
type GoIfStmt struct {
104113
Cond GoExpr

compiler/goprint.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ func (p *goPrinter) printStmt(s GoStmt) {
123123
} else {
124124
p.line("return")
125125
}
126+
case GoVarStmt:
127+
if st.Value != nil {
128+
p.line("var %s %s = %s", st.Name, st.Type, p.exprStr(st.Value))
129+
} else {
130+
p.line("var %s %s", st.Name, st.Type)
131+
}
126132
case GoIfStmt:
127133
p.printIf(st)
128134
case GoForStmt:

0 commit comments

Comments
 (0)