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

Refactor some code #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
130 changes: 72 additions & 58 deletions base/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,25 @@ func (g *Globals) ParseBytes(src []byte) []ast.Node {
// print phase
func (g *Globals) Print(values []r.Value, types []xr.Type) {
opts := g.Options
if opts&OptShowEval != 0 {
if opts&OptShowEvalType != 0 {
for i, vi := range values {
var ti interface{}
if types != nil && i < len(types) {
ti = types[i]
} else {
ti = reflect.Type(vi)
}
g.Fprintf(g.Stdout, "%v\t// %v\n", vi, ti)
}
if !(opts&OptShowEval != 0) {
return
}

if !(opts&OptShowEvalType != 0) {
for _, vi := range values {
g.Fprintf(g.Stdout, "%v\n", vi)
}
return
}

for i, vi := range values {
var ti interface{}
if types != nil && i < len(types) {
ti = types[i]
} else {
for _, vi := range values {
g.Fprintf(g.Stdout, "%v\n", vi)
}
ti = reflect.Type(vi)
}
g.Fprintf(g.Stdout, "%v\t// %v\n", vi, ti)
}
}

Expand Down Expand Up @@ -265,72 +268,83 @@ func (g *Globals) CollectNode(node ast.Node) {
},
}
*/
if len(node.Specs) == 1 {
if decl, ok := node.Specs[0].(*ast.ValueSpec); ok {
if len(decl.Values) == 1 {
if lit, ok := decl.Values[0].(*ast.BasicLit); ok {
if lit.Kind == token.STRING {
path := bstrings.MaybeUnescapeString(lit.Value)
g.PackagePath = path
}
}
}
}
if len(node.Specs) != 1 {
break
}

decl, ok := node.Specs[0].(*ast.ValueSpec)
if !ok || (len(decl.Values) != 1) {
break
}

lit, ok := decl.Values[0].(*ast.BasicLit)
if !ok || (lit.Kind != token.STRING) {
break
}
path := bstrings.MaybeUnescapeString(lit.Value)
g.PackagePath = path

default:
g.Declarations = append(g.Declarations, node)
}
}
case *ast.FuncDecl:
if collectDecl {
if node.Recv == nil || len(node.Recv.List) != 0 {
// function or method declaration.
// skip macro declarations, Go compilers would choke on them
g.Declarations = append(g.Declarations, node)
}
if !collectDecl {
break
}
if node.Recv == nil || len(node.Recv.List) != 0 {
// function or method declaration.
// skip macro declarations, Go compilers would choke on them
g.Declarations = append(g.Declarations, node)
}
case ast.Decl:
if collectDecl {
g.Declarations = append(g.Declarations, node)
}
case *ast.AssignStmt:
if node.Tok == token.DEFINE {
if collectDecl {
idents := make([]*ast.Ident, len(node.Lhs))
for i, lhs := range node.Lhs {
idents[i] = lhs.(*ast.Ident)
}
decl := &ast.GenDecl{
TokPos: node.Pos(),
Tok: token.VAR,
Specs: []ast.Spec{
&ast.ValueSpec{
Names: idents,
Type: nil,
Values: node.Rhs,
},
},
}
g.Declarations = append(g.Declarations, decl)
}
} else {
if node.Tok != token.DEFINE {
if collectStmt {
g.Statements = append(g.Statements, node)
break
}
}
if collectDecl {
idents := make([]*ast.Ident, len(node.Lhs))
for i, lhs := range node.Lhs {
idents[i] = lhs.(*ast.Ident)
}
decl := &ast.GenDecl{
TokPos: node.Pos(),
Tok: token.VAR,
Specs: []ast.Spec{
&ast.ValueSpec{
Names: idents,
Type: nil,
Values: node.Rhs,
},
},
}
g.Declarations = append(g.Declarations, decl)
}
case ast.Stmt:
if collectStmt {
g.Statements = append(g.Statements, node)
}
case ast.Expr:
if unary, ok := node.(*ast.UnaryExpr); ok && collectDecl {
if unary.Op == token.PACKAGE && unary.X != nil {
if ident, ok := unary.X.(*ast.Ident); ok {
g.PackagePath = ident.Name
break
}
}
unary, ok := node.(*ast.UnaryExpr)
if !(ok && collectDecl) {
break
}

if !(unary.Op == token.PACKAGE && unary.X != nil) {
break
}

if ident, ok := unary.X.(*ast.Ident); ok {
g.PackagePath = ident.Name
break
}

if collectStmt {
stmt := &ast.ExprStmt{X: node}
g.Statements = append(g.Statements, stmt)
Expand Down
20 changes: 11 additions & 9 deletions base/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,19 @@ func (st *Stringer) nodeToPrintable(node ast.Node) interface{} {
}

func (st *Stringer) rvalueToPrintable(format string, value r.Value) interface{} {
var i interface{}
if !value.IsValid() {
i = nil
} else if value == reflect.None {
i = "/*no value*/"
} else if value.CanInterface() {
i = st.toPrintable(format, value.Interface())
} else {
i = value
return nil
}

if value == reflect.None {
return "/*no value*/"
}
return i

if value.CanInterface() {
return st.toPrintable(format, value.Interface())
}

return value
}

func (st *Stringer) typeToPrintable(t r.Type) interface{} {
Expand Down
5 changes: 2 additions & 3 deletions base/readline.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ func (tty TtyReadline) Close(historyfile string) (err error) {
return tty.Term.Close()
}
f, err1 := os.OpenFile(historyfile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err1 != nil {
err = fmt.Errorf("could not open %q to append history: %v", historyfile, err1)
} else {
err = fmt.Errorf("could not open %q to append history: %v", historyfile, err1)
if err1 == nil {
defer f.Close()
_, err2 := tty.Term.WriteHistory(f)
if err2 != nil {
Expand Down
20 changes: 10 additions & 10 deletions base/reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ func ConvertValue(v r.Value, to r.Type) r.Value {
// reflect.Value does not allow conversions from/to complex types
k := v.Kind()
kto := to.Kind()
if IsCategory(kto, r.Complex128) {
if IsCategory(k, r.Int, r.Uint, r.Float64) {
temp := v.Convert(TypeOfFloat64).Float()
v = r.ValueOf(complex(temp, 0.0))
}
} else if IsCategory(k, r.Complex128) {
if IsCategory(k, r.Int, r.Uint, r.Float64) {
temp := real(v.Complex())
v = r.ValueOf(temp)
}
if IsCategory(kto, r.Complex128) && IsCategory(k, r.Int, r.Uint, r.Float64) {
temp := v.Convert(TypeOfFloat64).Float()
v = r.ValueOf(complex(temp, 0.0))
return v.Convert(to)
}

if IsCategory(k, r.Complex128) && IsCategory(k, r.Int, r.Uint, r.Float64) {
temp := real(v.Complex())
v = r.ValueOf(temp)
return v.Convert(to)
}
}
return v.Convert(to)
Expand Down
31 changes: 16 additions & 15 deletions base/strings/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ func FindFirstToken(src []byte) int {
// suffix is trimmed with strings.TrimSpace() before returning it
func Split2(s string, separator rune) (string, string) {
var prefix, suffix string
prefix = s
if space := strings.IndexByte(s, ' '); space > 0 {
prefix = s[:space]
suffix = strings.TrimSpace(s[space+1:])
} else {
prefix = s
}
return prefix, suffix
}
Expand All @@ -124,28 +123,30 @@ func TailIdentifier(s string) string {
var digit bool
for i = n - 1; i >= 0; i-- {
ch := chars[i]
if ch < 0x80 {
if ch >= 'A' && ch <= 'Z' || ch == '_' || ch >= 'a' && ch <= 'z' {
digit = false
} else if ch >= '0' && ch <= '9' {
digit = true
} else {
break
}
} else if unicode.IsLetter(ch) {
digit = false
} else if unicode.IsDigit(ch) {
digit = true
} else {
is_digit, err := CharIsDigit(ch)
if err != nil {
break
}
digit = is_digit
}
if digit {
i++
}
return string(chars[i+1:])
}

func CharIsDigit(chr rune) (bool, error) {
if chr < 0x80 && (chr >= '0' && chr <= '9') {
return true, nil
}

if unicode.IsDigit(chr) {
return true, nil
}

return false, nil
}

/*
func extractFirstIdentifier(src []byte) []byte {
n := len(src)
Expand Down
7 changes: 4 additions & 3 deletions base/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ func (o Options) String() string {
func ParseOptions(str string) Options {
var opts Options
for _, name := range strings.Split(str, " ") {
if opt, ok := optValues[name]; ok {
opts ^= opt
} else if len(name) != 0 {
opt, ok := optValues[name]
if !ok && len(name) != 0 {
for k, v := range optNames {
if strings.HasPrefix(v, name) {
opts ^= k
}
}
continue
}
opts ^= opt
}
return opts
}
Expand Down
27 changes: 17 additions & 10 deletions base/untyped/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,25 @@ func MakeLit(kind Kind, val constant.Value, basicTypes *[]xr.Type) Lit {
// pretty-print untyped constants
func (untyp Lit) String() string {
val := untyp.Val
var strobj interface{}
if untyp.Kind == Rune && val.Kind() == constant.Int {
if i, exact := constant.Int64Val(val); exact {
if i >= 0 && i <= 0x10FFFF {
strobj = fmt.Sprintf("%q", i)
}
}
if !(untyp.Kind == Rune && val.Kind() == constant.Int) {
return MakeString(untyp, val.ExactString())
}
if strobj == nil {
strobj = val.ExactString()

i, exact := constant.Int64Val(val)
if !exact {
return MakeString(untyp, val.ExactString())
}

if i >= 0 && i <= 0x10FFFF {
strobj := fmt.Sprintf("%q", i)
return MakeString(untyp, strobj)
}
return fmt.Sprintf("{%v %v}", untyp.Kind, strobj)

return MakeString(untyp, val.ExactString())
}

func MakeString(untyp Lit, str string) string {
return fmt.Sprintf("{%v %v}", untyp.Kind, str)
}

func MakeKind(ckind constant.Kind) Kind {
Expand Down