Skip to content

Commit

Permalink
cl: compileNumberUnitLit
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Nov 7, 2024
1 parent ce33bae commit 88acdf2
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cl/_testgop/unit/in.gop
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import "time"

func Wait(time.Duration) {}

wait 1
wait 0.5µs
2 changes: 1 addition & 1 deletion cl/_testgop/unit/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import "time"
func Wait(time.Duration) {
}
func main() {
Wait(1)
Wait(500)
}
11 changes: 10 additions & 1 deletion cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,12 +813,15 @@ func compileCallArgs(ctx *blockCtx, pfn *gogen.Element, fn *fnType, v *ast.CallE
if typetype {
return
}
case *ast.NumberUnitLit:
compileNumberUnitLit(ctx, expr, fn.arg(i, ellipsis))
default:
compileExpr(ctx, arg)
}
}
if needInferFunc {
typ, err := gogen.InferFunc(ctx.pkg, pfn, fn.sig, nil, ctx.cb.InternalStack().GetArgs(len(v.Args)), flags)
args := ctx.cb.InternalStack().GetArgs(len(v.Args))
typ, err := gogen.InferFunc(ctx.pkg, pfn, fn.sig, nil, args, flags)
if err != nil {
return err
}
Expand Down Expand Up @@ -977,6 +980,12 @@ func compileFuncLit(ctx *blockCtx, v *ast.FuncLit) {
}
}

func compileNumberUnitLit(ctx *blockCtx, v *ast.NumberUnitLit, expected types.Type) {
ctx.cb.ValWithUnit(
&goast.BasicLit{ValuePos: v.ValuePos, Kind: gotoken.Token(v.Kind), Value: v.Value},
expected, v.Unit)
}

func compileBasicLit(ctx *blockCtx, v *ast.BasicLit) {
cb := ctx.cb
switch kind := v.Kind; kind {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/fsnotify/fsnotify v1.7.0
github.com/goplus/gogen v1.16.0
github.com/goplus/gogen v1.16.2-0.20241107075831-8fd274c85e06
github.com/goplus/llgo v0.9.7
github.com/goplus/mod v0.13.12
github.com/qiniu/x v1.13.10
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/goplus/gogen v1.16.0 h1:hAK2ZX8vCjH+Y2QoJl9viSZ8Gw9pzE0vCz5voYBYnv4=
github.com/goplus/gogen v1.16.0/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gogen v1.16.2-0.20241107075831-8fd274c85e06 h1:TZhHNYHia8bP8JoTO5utGWFTLBp2JrdyWukX/tnA/n4=
github.com/goplus/gogen v1.16.2-0.20241107075831-8fd274c85e06/go.mod h1:6TQYbabXDF9LCdDkOOzHmfg1R4ENfXQ3XpHa9RhTSD8=
github.com/goplus/llgo v0.9.7 h1:LRF2Fq9ts4QrVxOPZufexalbIoJ1oiBERjCWQ45wxbg=
github.com/goplus/llgo v0.9.7/go.mod h1:5Fs+08NslqofJ7xtOiIXugkurYOoQvY02ZkFNWA1uEI=
github.com/goplus/mod v0.13.12 h1:Trwk6j3i9VvBuW6/9ZxmkoFlEL2v3HKQu0Na1c6DAdw=
Expand Down
4 changes: 2 additions & 2 deletions parser/_testdata/unit/parser.expect
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ ast.FuncDecl:
ast.CallExpr:
Fun:
ast.Ident:
Name: step
Name: wait
Args:
ast.NumberUnitLit:
Kind: INT
Value: 1
Unit: cm
Unit: µs
2 changes: 1 addition & 1 deletion parser/_testdata/unit/step.gop
Original file line number Diff line number Diff line change
@@ -1 +1 @@
step 1cm
wait 1µs
2 changes: 1 addition & 1 deletion scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ func (s *Scanner) scanNumber() (token.Token, string) {
} else if s.ch == 'r' {
tok = token.RAT
s.next()
} else if s.ch >= 'a' && s.ch <= 'z' || s.ch >= 'A' && s.ch <= 'Z' {
} else if isLetter(s.ch) {
s.needUnit = true
}

Expand Down

0 comments on commit 88acdf2

Please sign in to comment.