Skip to content

Commit

Permalink
typecast support twoValue
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Apr 24, 2022
1 parent 6f92a1a commit 53fb125
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ var y *big.Rat = 4/5r
println x, y
```

### Large number: uint128, int128
### Large integer: uint128, int128

```go
var x uint128 = 1 << 65
Expand Down
20 changes: 20 additions & 0 deletions builtin/ng/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,26 @@ func (a Bigint) Gop_RshAssign(n Gop_ninteger) {

// -----------------------------------------------------------------------------

// Gop_Rcast: func int64(x bigint) int64
func (a Bigint) Gop_Rcast__0() int64 {
return a.Int64()
}

// Gop_Rcast: func int64(x bigint) (int64, bool)
func (a Bigint) Gop_Rcast__1() (int64, bool) {
return a.Int64(), a.IsInt64()
}

// Gop_Rcast: func uint64(x bigint) uint64
func (a Bigint) Gop_Rcast__2() uint64 {
return a.Uint64()
}

// Gop_Rcast: func uint64(x bigint) (uint64, bool)
func (a Bigint) Gop_Rcast__3() (uint64, bool) {
return a.Uint64(), a.IsUint64()
}

// Bigint_Cast: func bigint(x untyped_int) bigint
func Bigint_Cast__0(x int) Bigint {
return Bigint{big.NewInt(int64(x))}
Expand Down
10 changes: 10 additions & 0 deletions builtin/ng/uint128.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ func (u Uint128) Gop_Rcast__2() (out uint64, inRange bool) {
return u.lo, u.hi == 0
}

// Gop_Rcast: func int64(v uint128) int64
func (u Uint128) Gop_Rcast__3() int64 {
return int64(u.lo)
}

// Gop_Rcast: func int64(v uint128) (int64, bool)
func (u Uint128) Gop_Rcast__4() (out int64, inRange bool) {
return int64(u.lo), u.hi == 0 && u.lo <= maxInt64
}

// -----------------------------------------------------------------------------

func (u Uint128) IsZero() bool {
Expand Down
24 changes: 18 additions & 6 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const (
clIdentLHS
clIdentSelectorExpr
clIdentGoto
clCallWithTwoValue
)

func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) *gox.PkgRef {
Expand Down Expand Up @@ -179,7 +180,11 @@ func compileExpr(ctx *blockCtx, expr ast.Expr, twoValue ...bool) {
case *ast.BasicLit:
compileBasicLit(ctx, v)
case *ast.CallExpr:
compileCallExpr(ctx, v, 0)
flags := 0
if twoValue != nil && twoValue[0] {
flags = clCallWithTwoValue
}
compileCallExpr(ctx, v, flags)
case *ast.SelectorExpr:
compileSelectorExpr(ctx, v, clIdentAutoCall)
case *ast.BinaryExpr:
Expand Down Expand Up @@ -409,18 +414,25 @@ func (p *fnType) initWith(fnt types.Type, idx, nin int) {
}
}

func compileCallExpr(ctx *blockCtx, v *ast.CallExpr, flags int) {
func compileCallExpr(ctx *blockCtx, v *ast.CallExpr, inFlags int) {
switch fn := v.Fun.(type) {
case *ast.Ident:
compileIdent(ctx, fn, clIdentAllowBuiltin|flags)
compileIdent(ctx, fn, clIdentAllowBuiltin|inFlags)
case *ast.SelectorExpr:
compileSelectorExpr(ctx, fn, 0)
default:
compileExpr(ctx, fn)
}
var fn fnType
fnt := ctx.cb.Get(-1).Type
ellipsis := v.Ellipsis != gotoken.NoPos
var fnt = ctx.cb.Get(-1).Type
var flags gox.InstrFlags
var ellipsis = v.Ellipsis != gotoken.NoPos
if ellipsis {
flags = gox.InstrFlagEllipsis
}
if (inFlags & clCallWithTwoValue) != 0 {
flags |= gox.InstrFlagTwoValue
}
for i, arg := range v.Args {
switch expr := arg.(type) {
case *ast.LambdaExpr:
Expand All @@ -438,7 +450,7 @@ func compileCallExpr(ctx *blockCtx, v *ast.CallExpr, flags int) {
compileExpr(ctx, arg)
}
}
ctx.cb.CallWith(len(v.Args), ellipsis, v)
ctx.cb.CallWith(len(v.Args), flags, v)
}

type clLambaFlag string
Expand Down
16 changes: 14 additions & 2 deletions cl/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ const (
}
`, `36893488147419103232
4 1
`
testType_twoval_code, testType_twoval_ret = `
{
var x $(type) = 1 << 65
var y = (x >> 2) - 1
v1, ok1 := int64(x)
v2, ok2 := int64(y)
println v1, ok1
println v2, ok2
}
`, `0 false
9223372036854775807 true
`
testType_cast_code, testType_cast_ret = `
{
Expand Down Expand Up @@ -173,8 +185,8 @@ import "fmt"
)

const (
testType_com_code, testType_com_ret = testType_inc_code + testType_init_code + testType_cast_code + testType_printf_code,
testType_inc_ret + testType_init_ret + testType_cast_ret + testType_printf_ret
testType_com_code, testType_com_ret = testType_inc_code + testType_init_code + testType_cast_code + testType_printf_code + testType_twoval_code,
testType_inc_ret + testType_init_ret + testType_cast_ret + testType_printf_ret + testType_twoval_ret
testType_fixint_code, testType_fixint_ret = testTypescanf_code + testType_com_code,
testType_scanf_ret + testType_com_ret
)
Expand Down

0 comments on commit 53fb125

Please sign in to comment.