source: to-imperative/trunk/library/StdIO/refal/StdIO.rf @ 1643

Last change on this file since 1643 was 1643, checked in by orlov, 16 years ago
  • Some parts of the library written in Refal.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1// $Id: StdIO.rf 1643 2004-12-10 02:51:39Z orlov $
2
3$use Arithm Box Class Compare Convert Error Table;
4
5//X/$use Lexer(Stream FakeStreamIndex);
6$use "../../Lexer/refal/Lexer";
7$use "../../Stream/refal/Stream";
8$use "../../Stream/refal/StreamErr";
9
10
11$table Buffers;
12/*
13 * For the moment the format is:
14 *   s.channel  -->  stream
15 */
16
17
18Open-File s.channel e.file s.mode, \{
19  # <Channel? s.channel> =
20    <Lib-Error (StdIO Open-File) "Arg 1 is not a Channel">;
21  # <Word? s.mode> =
22    <Lib-Error (StdIO Open-File) "Arg 3 is not a Word">;
23  <Prim-Open-File s.channel e.file s.mode>,
24    <Bind &Buffers (s.channel) (<Stream &Prim-Read s.channel &Read-Chunk-Size>)>;
25};
26
27/*
28 * FIXME:
29 * Should open &StdIn, &StdOut, and &StdErr like this:
30 *   <Bind &Buffers (&StdIn) (<Stream &Prim-Read &StdIn 1>)>;
31 */
32
33
34//FIXME: probably shouldn't close &StdIn, &StdOut, and &StdErr?
35Close-Channel s.channel, {
36  # <Channel? s.channel> =
37    <Lib-Error (StdIO Close-Channel) "Arg 1 is not a Channel">;
38  <Prim-Close-Channel s.channel>,
39    <Unbind &Buffers s.channel>;
40};
41
42
43EOF? s.channel, {
44  <Lookup &Buffers s.channel> : stream =
45    <End-of-Stream? stream>;
46  <Lib-Error (StdIO EOF?) "Channel is not open">;
47};
48
49
50Read-Char! s.channel, {
51  <Lookup &Buffers s.channel> : stream =
52    <Getc stream> : s.char, s.char;
53  <Lib-Error (StdIO Read-Char!) "Channel is not open"> = $fail;
54};
55
56Read-Char = <Read-Char! &StdIn>;
57
58
59Read-Line! s.channel, {
60  <Lookup &Buffers s.channel> : stream =
61    <Get-Line stream> : \{
62      e.line '\n' = e.line;
63      v.line      = v.line;
64    };
65  <Lib-Error (StdIO Read-Line!) "Channel is not open"> = $fail;
66};
67
68Read-Line = <Read-Line! &StdIn>;
69
70
71$func? Read-Main s.fname s.channel = term;
72
73Read! s.channel = <Read-Main Read! s.channel>;
74
75Read = <Read-Main Read &StdIn>;
76
77Read-Main s.fname s.channel, {
78  <Lookup &Buffers s.channel> : stream =
79    {
80      <Read-Term? stream>;
81      /*empty*/;
82    } :: e.result,
83    {
84      <Errors> : (t.name t.pos (e.message)) e =
85        <Lib-Error (StdIO s.fname) e.message>, $fail;;
86    },
87    e.result : term = term;
88  <Lib-Error (StdIO s.fname) "Channel is not open"> = $fail;
89};
90
Note: See TracBrowser for help on using the repository browser.