Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cb.ValWithUnit #443

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ func getConf() *Config {
return &Config{Fset: fset, Importer: imp}
}

func TestValWithUnit(t *testing.T) {
pkg := NewPackage("", "foo", nil)
cb := pkg.CB()
testValWithUnitPanic(t, "no unit for int", cb, types.Typ[types.Int], "m")
testValWithUnitPanic(t, "y is not unit of time.Duration", cb, namedType("time", "Duration"), "y")
testValWithUnitPanic(t, "user defined type: not impl", cb, namedType("foo", "Bar"), "m")
cb.ValWithUnit(&ast.BasicLit{Value: "1", Kind: token.INT}, namedType("time", "Duration"), "m")
}

func namedType(pkgName, tName string) types.Type {
pkg := types.NewPackage(pkgName, "")
return types.NewNamed(types.NewTypeName(0, pkg, tName, nil), types.Typ[types.Int64], nil)
}

func testValWithUnitPanic(t *testing.T, name string, cb *CodeBuilder, typ types.Type, unit string) {
t.Helper()
t.Run(name, func(t *testing.T) {
defer func() {
if e := recover(); e == nil {
t.Fatal("TestErrValWithUnit: no panic?")
}
}()
cb.ValWithUnit(&ast.BasicLit{Value: "1", Kind: token.INT}, typ, unit)
})
}

func TestSwitchStmtThen(t *testing.T) {
pkg := NewPackage("", "foo", nil)
cb := pkg.CB()
Expand Down
39 changes: 39 additions & 0 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/goplus/gogen/internal"
xtoken "github.com/goplus/gogen/token"
Expand Down Expand Up @@ -1382,6 +1383,44 @@ func (p *CodeBuilder) pushVal(v interface{}, src ast.Node) *CodeBuilder {
return p
}

// ValWithUnit func
func (p *CodeBuilder) ValWithUnit(v *ast.BasicLit, t types.Type, unit string) *CodeBuilder {
if debugInstr {
log.Println("ValWithUnit", v.Value, t, unit)
}
named, ok := t.(*types.Named)
if !ok {
panic("TODO: ValWithUnit: t isn't a named type")
}
e := toExpr(p.pkg, v, v)
o := named.Obj()
if o.Name() == "Duration" && o.Pkg().Path() == "time" { // time.Duration
u, ok := timeDurationUnits[unit]
if !ok {
panic("TODO: ValWithUnit: unknown unit of time.Duration - " + unit)
}
val := constant.BinaryOp(e.CVal, token.MUL, constant.MakeInt64(int64(u)))
e.CVal = val
e.Val = &ast.BasicLit{Kind: token.INT, Value: val.ExactString()}
e.Type = t
p.Val(e, v)
} else {
panic("TODO: notimpl")
}
return p
}

var timeDurationUnits = map[string]time.Duration{
"ns": time.Nanosecond,
"us": time.Microsecond,
"µs": time.Microsecond,
"ms": time.Millisecond,
"s": time.Second,
"m": time.Minute,
"h": time.Hour,
"d": 24 * time.Hour,
}

// Star func
func (p *CodeBuilder) Star(src ...ast.Node) *CodeBuilder {
if debugInstr {
Expand Down