-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathio.tem
129 lines (123 loc) · 9.64 KB
/
io.tem
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
(page "I/O"
(import "docs/io1.html")
(newtable "Input"
(op readb "[input-port]" "Reads a byte from the input-port (or default of
stdin). Returns nil on end-of-file."
(tests (readb (pipe-from "echo hello"))))
(op readc "[input-port]" "Reads a character from the input-port (or default of
stdin). Returns nil on end-of-file."
(tests (readc (pipe-from "echo ©"))))
(op peekc "input-port" "Peeks at the next character from the input port, but
leaves the character for future reads. It
uses stdin if the argument is nil. It returns the character, or nil for
end-of-file."
(tests (peekc (pipe-from "echo hello"))))
(def readline "[input-port]" "Reads a line from the specified port or stdin. The line is read up to a newline, nil, or eof. An initial newline does not terminate the input and appears in the output. A trailing newline is not included in the returned value."
(tests "(w/instring ins \"\\n\\na\\nc\\n\\nd\"
(whiler l (readline ins) nil (prn \"{\" l \"}\")))"))
(op sread "input-port eof" "Reads a S-expression from the input port. Returns eof on end-of-file."
(tests (sread (pipe-from "echo '(1 2) (3)'") "junk")))
(def read "[input-source [eof]]" "Reads a S-expression from the input-source, which can be either a string or an input-port. If the end of file is reached, nil is returned or the specified eof value."
(tests (read "(+ 1 1)(foo bar)") (w/instring ins "(foo 42)" (read ins)) (read "" 'eof)))
(def readstring1 "string [eof]" "Reads a S-expression from the string. This is the same as <code>read</code>, except only handles a string." (tests (readstring1 "(+ 1 1)")))
(def saferead "input-source" "Reads a S-expression from the input-source, which can be either a string or an input-port. If the end of file is reached, nil is returned or the specified eof value. If an error is encountered in the input, returns nil." (tests (saferead "1 + 1")(saferead "###")))
(def readall "input-source [eof]" "Reads S-expressions from the input-source, which can be either a string or an input-port. Returns a list of the S-expressions. If the end of file is reached, nil is returned or the eof value if specified." (tests (w/instring ins "(+ 1 1)abc (foo)" (readall ins)) (readall "[_]" )))
(def load "path" "Loads the file of Arc code into the REPL environment." (faketest "(load \"strings.arc\")" "nil"))
)
(newtable "Output"
(op disp "[arg [output-port]]" "Displays the argument on the output-port (or
current-output-port) using MzScheme's display procedure. Returns nil."
(tests (disp '(1 2))
(disp "abc") (disp #\a) (disp "a\nb")))
(op write "[arg [output-port]]" "Writes the argument to the output-port (or
current-output-port). Returns nil. The <code>write</code> and <code>disp</code> operations are slightly different: <code>write</code> quotes strings and characters, while <code>disp</code> displays them. See the
MzScheme
<a
href='http://download.plt-scheme.org/doc/372/html/mzscheme/mzscheme.html#node_toc_node_sec_11.2.5'>Default
Printer</a> documentation for details."
(tests (write "abc") (write #\a) (write "a\nb")))
(op writeb "int [output-port]" "Writes the byte to the output-port (or
default of current-output-port)."
(tests (writeb 65)))
(op writec "char [output-port]" "Writes the character to the output-port (or
default of current-output-port)."
(tests (writec #\日)))
(def pr "[arg ...]" "Prints arguments using <code>disp</code>. Returns the first argument." (tests (pr 1 "a")))
(def prn "[arg ...]" "Prints arguments followed by a newline." (tests (prn 1 "a")))
(def prall "elts [initial-string [separator-string]]" "Prints each element of list elts. The initial-string is printed first. The elements are separated by separator-string. Returns <code>elts</code>." (tests (prall '(1 2 3)) (prall '(1 2 3) "Value: " "-")))
(def prs "[arg ...]" "Prints the arguments, separated by a space. Returns the arguments as a list." (tests (prs 1 2 3)))
(mac prf "str [arg ...]" "Prints args using the format string." (tests (let hi "hello" (prf "#hi #(+ 1 2)")) (prf "Num ~a and ~~" 1 2)))
(mac out "expr" "Prints what expr prints. expr is evaluated at macro expansion time. New in arc3." (tests (out (pr 42))))
(def parse-format "str" "Parses a format string. An internal helper for <code>prf</code>.")
(def ero "[arg ...]" "Writes the args to stderr. Returns the first arg." (tests (ero "This appears on stderr")))
(def warn "msg [arg ...]" "Displays warning msg, followed by the arguments." (tests (warn "Problem" '(1 2) 3)))
)
(newtable "File I/O"
(def readfile "filename" "Reads a file containing Lisp forms. Returns a list of the forms." (tests (readfile "foo.arc")))
(def readfile1 "filename" "Reads a single Lisp form from the specified file." (tests (readfile1 "foo.arc")))
(def writefile "obj filename" "Writes obj to the specified file. The contents are first written to filename.tmp, and then that is moved to filename." (tests (writefile '(+1 2) "bar.arc")))
(op infile "filename ['binary | 'text]" "Opens the specified path for
reading. By default, the file is opened in binary mode, and bytes are
returned as read from the file. In text mode, return and linefeed bytes are
filtered in a platform-specific way. (On Windows, return followed by
linefeed is filtered to a single linefeed.)"
(tests (infile "foo.arc")) )
(mac w/infile "var filename [body ...]" "Opens filename for reading with infile, assigns the input-port to var, executes body, and closes the file." (tests (w/infile inf "foo.arc" (read inf))))
(op outfile "filename ['append]" "Opens the specified path for writing. By
default, the file is truncated if it already exists. Returns an
output-port. Arc supports only 'text mode for outfile."
(tests (outfile "/tmp/junk" 'append)) )
(mac w/outfile "var filename [body ...]" "Opens filename for writing with outfile, assigns the output-port to var, executes body, and closes the file." (tests (w/outfile outf "bar.arc" (w/stdout outf (prn "Hello")))))
(mac w/appendfile "var filename [body ...]" "Opens filename for appending with outfile, assigns the output-port to var, executes body, and closes the file." (tests (w/appendfile af "bar.arc" w/stdout af (prn "Hello"))))
(op close "port [...]" "Closes a port or ports. In arc0, close took a single argument only." (tests (close (outfile "/tmp/junk"))))
(op pipe-from "command" "Executes command in the underlying OS. Then opens
an input-port to the results. Note that this is not a genuine pipe as the command completes before the results can be read."
(tests (readline (pipe-from "echo hello"))))
(op flushout "" "Flushes output. New in arc3."
(tests (flushout)))
)
(newtable "Disk variables"
(mac diskvar "var file" "Creates a variable that will be loaded from file and stored in file. If file exists, var is initialzed from file. New in arc3." (tests (diskvar foo "file.txt")))
(mac disktable "var file" "Creates a table variable that will be loaded from file and stored in file. New in arc3." (tests (disktable bar "file.txt")))
(mac todisk "var [expr]" "Writes var to disk, optionally setting it to expr first. New in arc3." (tests (todisk bar)))
(mac fromdisk "var file init load save" "If var is not bound, var is initialized either by calling load function on file or using init. The save function is saved for later use. Internal function. New in arc3.")
)
(newtable "stdin, stdout, and stderr"
(op stdin "" "stdin: returns the input-port stdin" (tests (stdin)))
(op stdout "" "current-output-port: returns the output-port stdout" (tests (stdout)))
(op stderr "" "current-error-port: returns the output-port stderr" (tests (stderr) (write "Error message" (stderr))))
(op call-w/stdin "input-port thunk" "Sets stdin
to the specified port and then executes the thunk."
(tests (call-w/stdin (instring "Hello") readline)))
(mac w/stdin "input-port [body ...]" "Sets stdin to the specified port and then executes the body." (tests (w/instring ins "Hello" (w/stdin ins (readline)))))
(op call-w/stdout "output-port thunk" "Calls thunk, setting stdin
to the specified port."
(tests
"(let sop (outstring)
(call-w/stdout sop (fn () (prn '(1 2))))
(inside sop))"
))
(mac w/stdout "output-port [body ...]" "Executes the body port with stdout redirected to the output-port. This is a wrapper around call-w/stdout."
(tests
"(let sop (outstring)
(w/stdout sop (prn '(1 2)))
(inside sop))"
))
)
(newtable "String Port I/O"
(op instring "string [name]" "Creates an input port to read UTF-8 bytes from the
string. This is MzScheme's open-input-string."
(tests (readline (instring "hello"))))
(mac w/instring "var str [body ...]" "Creates an string input-port from str with instring, assigns it to var, executes body, and then closes the input port." (tests (w/instring ins "(+ 1 1)" (read ins))))
(mac fromstring "str [body ...]" "Executes the body, using the contents of <code>str</code> as stdin." (tests (fromstring "(+ 1 1)" (read))))
(op outstring "[name]" "Creates an output-port that accumulates the output
into a byte string. The string can be retrieved with inside. This is
MzScheme's open-output-string."
(tests (outstring)))
(op inside "string-output-port" "Returns (as a string) the bytes accumulated
in a string-output-port generated by outstring. This is MzScheme's get-output-string."
(tests (let sop (outstring) (write "hello" sop) (inside sop))))
(mac w/outstring "var [body ...]" "Creates a string output-port with outstring, assigns it to var, and executes body." (tests (w/outstring os (prn "hi") (inside os))))
(mac tostring "[body ...]" "Executes the body. Any output that was sent to stdout is captured and returned as a string." (tests (tostring (prn "hello") (prn "world"))))
)
)