source: to-imperative/trunk/java/refal/refal/plus/internal/StdIO.rf @ 3659

Last change on this file since 3659 was 3659, checked in by yura, 13 years ago
  • StdIO java+refal;
  • Property svn:eol-style set to native
  • Property svn:keywords set to "Author Date Id Revision"
File size: 2.1 KB
Line 
1// $Id: StdIO.rf 3659 2008-03-31 15:31:21Z yura $
2
3$use Arithm Box Class Compare Convert Error Table;
4
5//X/$use Lexer(Stream FakeStreamIndex);
6$use Lexer;
7$use Stream;
8$use StreamErr;
9$use "refal.plus.StdIO";
10
11OpenFile s.channel e.file s.mode, \{
12  # <IsChannel s.channel> =
13    <Lib_Error (StdIO OpenFile) "Arg 1 is not a Channel">;
14  # <IsWord s.mode> =
15    <Lib_Error (StdIO OpenFile) "Arg 3 is not a Word">;
16  <PrimOpenFile s.channel e.file s.mode>,
17    <Bind &Buffers (s.channel) (<Stream &PrimRead s.channel &ReadChunkSize>)>;
18};
19
20/*
21 * FIXME:
22 * Should open &StdIn, &StdOut, and &StdErr like this:
23 *   <Bind &Buffers (&StdIn) (<Stream &Prim-Read &StdIn 1>)>;
24 */
25
26
27//FIXME: probably shouldn't close &StdIn, &StdOut, and &StdErr?
28CloseChannel s.channel, {
29  # <IsChannel s.channel> =
30    <Lib_Error (StdIO CloseChannel) "Arg 1 is not a Channel">;
31  <PrimCloseChannel s.channel>,
32    <Unbind &Buffers s.channel>;
33};
34
35
36IsEof s.channel, {
37  <Lookup &Buffers s.channel> : stream =
38    <IsEnd_of_Stream stream>;
39  <Lib_Error (StdIO IsEof) "Channel is not open">;
40};
41
42
43ReadCharCh s.channel, {
44  <Lookup &Buffers s.channel> : stream =
45    <Getc stream> : s.char, s.char;
46  <Lib_Error (StdIO ReadCharCh) "Channel is not open"> = $fail;
47};
48
49ReadChar = <ReadCharCh &StdIn>;
50
51
52ReadLineCh s.channel, {
53  <Lookup &Buffers s.channel> : stream =
54    <Get_Line stream> : \{
55      e.line '\r\n' = e.line;
56      e.line '\n'   = e.line;
57      e.line '\r'   = e.line;
58      v.line        = v.line;
59    };
60  <Lib_Error (StdIO ReadLineCh) "Channel is not open"> = $fail;
61};
62
63ReadLine = <ReadLineCh &StdIn>;
64
65
66$func? Read_Main s.fname s.channel = term;
67
68ReadCh s.channel = <Read_Main ReadCh s.channel>;
69
70Read = <Read_Main Read &StdIn>;
71
72Read_Main s.fname s.channel, {
73  <Lookup &Buffers s.channel> : stream =
74    {
75      <IsRead_Term stream>;
76      /*empty*/;
77    } :: e.result,
78    {
79      <Errors> : (t.name t.pos (e.message)) e =
80        <Lib_Error (StdIO s.fname) e.message>, $fail;;
81    },
82    e.result : term = term;
83  <Lib_Error (StdIO s.fname) "Channel is not open"> = $fail;
84};
85
86StaticInit = <Bind &Buffers (&StdIn) (<Stream &PrimRead &StdIn &ReadChunkSize>)>;
Note: See TracBrowser for help on using the repository browser.