Reworks how various types print to provide better output/feedback from the system

master
sloum 2 years ago
parent 43d6ba870c
commit b54c7b626b

@ -26,9 +26,9 @@ import (
)
var stdLibrary = vars{
"stdin": &IOHandle{os.Stdin, true},
"stdout": &IOHandle{os.Stdout, true},
"stderr": &IOHandle{os.Stderr, true},
"stdin": &IOHandle{os.Stdin, true, "file (read-only)"},
"stdout": &IOHandle{os.Stdout, true, "file (write-only)"},
"stderr": &IOHandle{os.Stderr, true, "file (write-only)"},
"PI": number(math.Pi),
"E": number(math.E),
"sys-args": func(a ...expression) expression {
@ -508,7 +508,7 @@ var stdLibrary = vars{
if len(a) > 0 {
b.WriteString(String(a[0], false))
}
obj := &IOHandle{&b, true}
obj := &IOHandle{&b, true, "string-buf"}
openFiles = append(openFiles, obj)
return obj
},
@ -1399,7 +1399,7 @@ var stdLibrary = vars{
if err != nil {
return exception("'file-create-temp' was unable to create the requested temporary file: " + err.Error())
}
obj := &IOHandle{f, true}
obj := &IOHandle{f, true, "file (write-only)"}
openFiles = append(openFiles, obj)
return obj
},
@ -1468,7 +1468,7 @@ var stdLibrary = vars{
if err != nil {
return exception("'file-create' encountered an error: " + err.Error())
}
obj := &IOHandle{f, true}
obj := &IOHandle{f, true, "file (write-only)"}
openFiles = append(openFiles, obj)
return obj
},
@ -1481,7 +1481,7 @@ var stdLibrary = vars{
if err != nil {
return false
}
obj := &IOHandle{f, true}
obj := &IOHandle{f, true, "file (read-only)"}
openFiles = append(openFiles, obj)
return obj
}
@ -1503,7 +1503,7 @@ var stdLibrary = vars{
if err != nil {
return exception("'file-write' could not open the given filepath")
}
obj := &IOHandle{f, true}
obj := &IOHandle{f, true, "file (write-only)"}
openFiles = append(openFiles, obj)
return obj
}
@ -1768,9 +1768,9 @@ var stdLibrary = vars{
// based on the tls value use tls or not, same with the timeout value for adding a timeout
var handle *IOHandle
if usetls {
handle = &IOHandle{tlsconn, true}
handle = &IOHandle{tlsconn, true, "net-conn (tls)"}
} else {
handle = &IOHandle{&conn, true}
handle = &IOHandle{&conn, true, "net-conn"}
}
openFiles = append(openFiles, handle)
return handle

@ -59,6 +59,21 @@ func String(v expression, quoteString bool) string {
return "#f"
case number:
return strconv.FormatFloat(float64(v), 'f', -1, 64)
case proc:
var b strings.Builder
b.WriteString("(lambda ")
b.WriteString(String(v.params, true))
b.WriteRune(' ')
body := String(v.body, true)
if strings.HasPrefix(body, "(begin ") {
body = body[7:]
}
b.WriteString(body)
return b.String()
case func(...expression) expression:
return fmt.Sprint("Built-in: ", &v)
case *IOHandle:
return fmt.Sprintf("%+v", v)
default:
return fmt.Sprint(v)
}

@ -1,6 +1,7 @@
package main
import (
"fmt"
"strconv"
"strings"
)
@ -10,6 +11,16 @@ type expression interface{}
type IOHandle struct {
Obj expression
Open bool
Kind string
}
func (i IOHandle) String() string {
state := "closed"
if i.Open {
state = "open"
}
return fmt.Sprintf("%s %s io-handle", state, i.Kind)
}
type number float64

Loading…
Cancel
Save