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

cl: compileNumberUnitLit #2008

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
File renamed without changes.
5 changes: 5 additions & 0 deletions cl/_testgop/unit/in.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "time"

func Wait(time.Duration) {}

wait 0.5µs
9 changes: 9 additions & 0 deletions cl/_testgop/unit/out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "time"

func Wait(time.Duration) {
}
func main() {
Wait(500)
}
4 changes: 4 additions & 0 deletions cl/compile_testdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
"github.com/goplus/gop/cl/cltest"
)

func TestTestgop(t *testing.T) {
cltest.FromDir(t, "", "./_testgop")
}

func TestTestc(t *testing.T) {
cltest.FromDir(t, "", "./_testc")
}
Expand Down
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 @@ -3,8 +3,8 @@ module github.com/goplus/gop
go 1.18

require (
github.com/goplus/gogen v1.16.2-0.20241107075831-8fd274c85e06
github.com/fsnotify/fsnotify v1.8.0
github.com/goplus/gogen v1.16.1
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/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/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/goplus/gogen v1.16.1 h1:f55LIfyTO5GcXxCfwXKqLKiQV3AORvn9PSGCncWvB1A=
github.com/goplus/gogen v1.16.1/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