-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnrs_io_simple.go
187 lines (170 loc) · 3.84 KB
/
rnrs_io_simple.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// Copyright (c) 2022-2023 Markku Rossi
//
// All rights reserved.
//
package scheme
import (
"fmt"
"io"
"github.com/markkurossi/scheme/types"
)
// Port implements Scheme ports.
type Port struct {
Native interface{}
}
// NewPort creates a new port for the native I/O channel.
func NewPort(native interface{}) *Port {
return &Port{
Native: native,
}
}
// Printf prints formatted output to the port.
func (p *Port) Printf(format string, a ...interface{}) (n int, err error) {
writer, ok := p.Native.(io.Writer)
if ok {
return fmt.Fprintf(writer, format, a...)
}
return 0, fmt.Errorf("invalid output port: %s", p)
}
// Println prints newline-terminated values to the port.
func (p *Port) Println(a ...interface{}) (n int, err error) {
writer, ok := p.Native.(io.Writer)
if ok {
return fmt.Fprintln(writer, a...)
}
return 0, fmt.Errorf("invalid output port: %s", p)
}
// Scheme returns the value as a Scheme string.
func (p *Port) Scheme() string {
return p.String()
}
func (p *Port) String() string {
_, isInput := p.Native.(io.Reader)
_, isOutput := p.Native.(io.Writer)
if isInput && isOutput {
return fmt.Sprintf("#<i/o-port %p>", p)
} else if isInput {
return fmt.Sprintf("#<input-port %p>", p)
} else {
return fmt.Sprintf("#<output-port %p>", p)
}
}
// Eq tests if the argument value is eq? to this value.
func (p *Port) Eq(o Value) bool {
op, ok := o.(*Port)
if !ok {
return false
}
return p.Native == op.Native
}
// Equal tests if the argument value is equal to this number.
func (p *Port) Equal(o Value) bool {
return p.Eq(o)
}
// Type implements Value.Type.
func (p *Port) Type() *types.Type {
return types.Port
}
var rnrsIOSimpleBuiltins = []Builtin{
{
Name: "input-port?",
Args: []string{"obj"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
port, ok := args[0].(*Port)
if !ok {
return Boolean(false), nil
}
_, ok = port.Native.(io.Reader)
return Boolean(ok), nil
},
},
{
Name: "output-port?",
Args: []string{"obj"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
port, ok := args[0].(*Port)
if !ok {
return Boolean(false), nil
}
_, ok = port.Native.(io.Writer)
return Boolean(ok), nil
},
},
{
Name: "current-output-port",
Return: types.Port,
Native: func(scm *Scheme, args []Value) (Value, error) {
return scm.Stdout, nil
},
},
{
Name: "current-error-port",
Return: types.Port,
Native: func(scm *Scheme, args []Value) (Value, error) {
return scm.Stderr, nil
},
},
{
Name: "newline",
Args: []string{"[port]"},
Return: types.Any,
Native: func(scm *Scheme, args []Value) (Value, error) {
var ok bool
port := scm.Stdout
if len(args) == 1 {
port, ok = args[0].(*Port)
if !ok {
return nil, fmt.Errorf("invalid output port: %v", args[0])
}
}
_, err := port.Println()
if err != nil {
return nil, err
}
return nil, nil
},
},
{
Name: "display",
Args: []string{"obj", "[port]"},
Return: types.Any,
Native: func(scm *Scheme, args []Value) (Value, error) {
var ok bool
port := scm.Stdout
if len(args) == 2 {
port, ok = args[1].(*Port)
if !ok {
return nil, fmt.Errorf("invalid output port: %v", args[1])
}
}
_, err := port.Printf("%v", ToString(args[0]))
if err != nil {
return nil, fmt.Errorf("%v", err)
}
return nil, nil
},
},
{
Name: "write",
Args: []string{"obj", "[port]"},
Return: types.Any,
Native: func(scm *Scheme, args []Value) (Value, error) {
var ok bool
port := scm.Stdout
if len(args) == 2 {
port, ok = args[1].(*Port)
if !ok {
return nil, fmt.Errorf("invalid output port: %v", args[1])
}
}
_, err := port.Printf("%v", ToScheme(args[0]))
if err != nil {
return nil, fmt.Errorf("%v", err)
}
return nil, nil
},
},
}