Skip to content

Commit

Permalink
Merge pull request #443 from xushiwei/q
Browse files Browse the repository at this point in the history
cb.ValWithUnit
  • Loading branch information
xushiwei authored Nov 7, 2024
2 parents 5a5a749 + 1605e44 commit 8fd274c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
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

0 comments on commit 8fd274c

Please sign in to comment.