GenIO.icl 3.39 KB
Newer Older
Artem Alimarine's avatar
Artem Alimarine committed
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
implementation module GenIO

import StdEnv, StdGeneric, StdMaybe

//--------------------------------------------------------------------------------
($) infixl 9
($) x y = y o x

(@) infix 8 
(@) x y = x y

:: Pos :== Int

//--------------------------------------------------------------------------------

:: *StringStream 	= { ss_str :: !*String, ss_pos :: !Pos}

instance IStream StringStream where
	streamRead s=:{ss_pos, ss_str}
		#! size_str = size ss_str
		| size_str == ss_pos 
			= (Nothing, {s & ss_str = ss_str})
		| otherwise
			#! ch = ss_str.[ss_pos]
			= (Just ch, {s & ss_str = ss_str, ss_pos = inc ss_pos})
	streamEnd s=:{ss_pos, ss_str}
		#! size_str = size ss_str
		= (size_str == ss_pos, {s & ss_str = ss_str})


instance OStream StringStream where
	streamWrite ch s=:{ss_str, ss_pos}
		#! new_str = realloc_if_needed ss_pos ss_str 
		= {s & ss_str = {new_str & [ss_pos] = ch}, ss_pos = inc ss_pos}
	where
		realloc_if_needed :: Int u:String -> v:String, [u <= v]
		realloc_if_needed pos str
			#! size_str = size str
			| pos == size_str
				#! new_str = createArray ((size_str + 1) * 3 /2) ' '
				#! (new_str, str) = fill 0 size_str new_str str
				= new_str
			| otherwise	
				= str 
		fill i n new_str str 
			| i == n
				= (new_str, str)
			| otherwise	
				#! (ch, str) = str![i] 
				= ({new_str & [i] = ch}, str)
				
instance RandomStream StringStream where
	streamGetPos s=:{ss_pos} = (ss_pos, s)
	streamSetPos pos s = {s & ss_pos = pos}

//-----------------------------------------------------------------------

instance IStream File where
	streamRead f 
		# (ok, c, f) = freadc f
		| ok
			= (Just c, f)
			= (Nothing, f)
	streamEnd f = fend f 

instance OStream File where
	streamWrite c f = fwritec c f

instance RandomStream File where
	streamSetPos pos f
		# (ok, f) = fseek f FSeekSet pos
		| not ok 	
			= abort "fseek failed\n"
			= f
	streamGetPos f = fposition f

//--------------------------------------------------------------------------------
:: Assoc = AssocNone | AssocLeft | AssocRight
:: GenIOEnv 
	= GIOE_None							// initial env
	| GIOE_Record						// record constructor	
	| GIOE_Tuple						// tuple constructor
	| GIOE_Nonfix						// normal nonfix constructor
	| GIOE_Infix String Assoc Int		// infix constructor

//--------------------------------------------------------------------------------

generic gOutput a :: GenIOEnv -> (a -> (*s -> *s)) | IStream s & RandomStream s
/*
gOutput{|Int|} env = undef
gOutput{|Real|} env = undef
gOutput{|Char|} env = undef
gOutput{|Bool|} env = undef
gOutput{|String|} env = undef
gOutput{|UNIT|} env = undef
gOutput{|PAIR|} fx fy env = undef
gOutput{|EITHER|} fl fr env = undef
gOutput{|CONS of d|} f env = undef
gOutput{|FIELD of d|} f env = undef
gOutput{|[]|} f env = undef
gOutput{|{}|} f env = undef
gOutput{|{!}|} f env = undef
*/
//--------------------------------------------------------------------------------

generic gInput a :: GenIOEnv -> (*s -> (Maybe a, *s)) | IStream s & RandomStream s
/*
gInput{|Int|} env = undef
gInput{|Real|} env = undef
gInput{|Char|} env = undef
gInput{|Bool|} env = undef
gInput{|String|} env = undef
gInput{|UNIT|} env = undef
gInput{|PAIR|} fx fy env = undef
gInput{|EITHER|} fl fr env = undef
gInput{|CONS of d|} f env = undef
gInput{|FIELD of d|} f env = undef
gInput{|[]|} f env = undef
gInput{|{}|} f env = undef
gInput{|{!}|} f env = undef
*/
//--------------------------------------------------------------------------------