Skip to content

Commit

Permalink
close #1320
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 5, 2022
1 parent 232a527 commit f68ea55
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
39 changes: 39 additions & 0 deletions builtin/iox/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2022 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package iox

import (
"bufio"
"io"
)

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

type LineIter struct {
*bufio.Scanner
}

func (it LineIter) Next() (line string, ok bool) {
if ok = it.Scan(); ok {
line = it.Text()
}
return
}

func EnumLines(r io.Reader) LineIter {
scanner := bufio.NewScanner(r)
return LineIter{scanner}
}

// ----------------------------------------------------------------------------
16 changes: 12 additions & 4 deletions cl/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,29 @@ func initMathBig(pkg *gox.Package, conf *gox.Config, big *gox.PkgRef) {
conf.UntypedBigFloat = big.Ref("UntypedBigfloat").Type().(*types.Named)
}

func initBuiltin(pkg *gox.Package, builtin *types.Package, fmt, ng, buil *gox.PkgRef) {
func initBuiltin(pkg *gox.Package, builtin *types.Package, os, fmt, ng, buil *gox.PkgRef) {
scope := builtin.Scope()
typs := []string{"bigint", "bigrat", "bigfloat"}
for _, typ := range typs {
name := string(typ[0]-('a'-'A')) + typ[1:]
scope.Insert(types.NewTypeName(token.NoPos, builtin, typ, ng.Ref(name).Type()))
}
fns := []string{
fmtFns := []string{
"print", "println", "printf", "errorf",
"fprint", "fprintln", "fprintf",
"sprint", "sprintln", "sprintf",
}
for _, fn := range fns {
for _, fn := range fmtFns {
fnTitle := string(fn[0]-'a'+'A') + fn[1:]
scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, fn, fmt.Ref(fnTitle)))
}
osFns := []string{
"open", "create",
}
for _, fn := range osFns {
fnTitle := string(fn[0]-'a'+'A') + fn[1:]
scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, fn, os.Ref(fnTitle)))
}
scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, "newRange", buil.Ref("NewRange__0")))
scope.Insert(types.NewTypeName(token.NoPos, builtin, "uint128", ng.Ref("Uint128").Type()))
scope.Insert(types.NewTypeName(token.NoPos, builtin, "int128", ng.Ref("Int128").Type()))
Expand All @@ -57,12 +64,13 @@ func initBuiltin(pkg *gox.Package, builtin *types.Package, fmt, ng, buil *gox.Pk
func newBuiltinDefault(pkg *gox.Package, conf *gox.Config) *types.Package {
builtin := types.NewPackage("", "")
fmt := pkg.Import("fmt")
os := pkg.Import("os")
buil := pkg.Import("github.com/goplus/gop/builtin")
ng := pkg.Import("github.com/goplus/gop/builtin/ng")
pkg.Import("strconv")
pkg.Import("strings")
initMathBig(pkg, conf, ng)
initBuiltin(pkg, builtin, fmt, ng, buil)
initBuiltin(pkg, builtin, os, fmt, ng, buil)
gox.InitBuiltin(pkg, builtin, conf)
return builtin
}
Expand Down
5 changes: 5 additions & 0 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ const (
testingGoFile = "_test"
)

const (
ioxPkgPath = "github.com/goplus/gop/builtin/iox"
)

// NewPackage creates a Go+ package instance.
func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package, err error) {
workingDir := conf.WorkingDir
Expand Down Expand Up @@ -372,6 +376,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
NewBuiltin: newBuiltinDefault,
DefaultGoFile: defaultGoFile,
NoSkipConstant: conf.NoSkipConstant,
PkgPathIox: ioxPkgPath,
}
if enableRecover {
defer func() {
Expand Down
28 changes: 28 additions & 0 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ ls
`)
}

func TestFileEnumLines(t *testing.T) {
gopClTest(t, `
import "os"
for line <- os.Stdin {
println line
}
`, `package main
import (
fmt "fmt"
os "os"
iox "github.com/goplus/gop/builtin/iox"
)
func main() {
for _gop_it := iox.EnumLines(os.Stdin); ; {
var _gop_ok bool
line, _gop_ok := _gop_it.Next()
if !_gop_ok {
break
}
fmt.Println(line)
}
}
`)
}

func TestMixedGo(t *testing.T) {
gopMixedClTest(t, "main", `package main
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.16

require (
github.com/goplus/c2go v0.7.8
github.com/goplus/gox v1.11.12
github.com/goplus/gox v1.11.15
github.com/goplus/libc v0.3.10
github.com/goplus/mod v0.9.12
github.com/qiniu/x v1.11.9
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/goplus/c2go v0.7.8 h1:XI7A9/5l4kXShaEhJj4ei8VprCiaCpX6lnwudosOtuU=
github.com/goplus/c2go v0.7.8/go.mod h1:6D9HpX+q/8/dmTT08l7sCpWyDTh22Sx3rJk83OvyVu8=
github.com/goplus/gox v1.11.12 h1:zb+hNzKbUJ16tDyGlwDIHzdArRLeDz+wXaBsNXeSDew=
github.com/goplus/gox v1.11.12/go.mod h1:wRCRSNukie4cDqADF4w0Btc2Gk6V3p3V6hI5+rsVqa8=
github.com/goplus/gox v1.11.15 h1:5XUruNjUo7QQyIqECaLomv9eEaKEqC3OY8dSCeWZbR0=
github.com/goplus/gox v1.11.15/go.mod h1:wRCRSNukie4cDqADF4w0Btc2Gk6V3p3V6hI5+rsVqa8=
github.com/goplus/libc v0.3.10 h1:5kEwgx3HP0zMPGZsvgKwAiLAmMS6aDgoBhniBSWF/ok=
github.com/goplus/libc v0.3.10/go.mod h1:gSQ8dVF/iKdoBn1ABeQfAF/ybaYX0pij3iPtC72FtJc=
github.com/goplus/mod v0.9.12 h1:CjgBGQIYqUTPGl3MrAS5CICzJwxbIfSa4OlEb141Gs4=
Expand Down

0 comments on commit f68ea55

Please sign in to comment.