annotate modules/file.tp @ 353:95bc24c729e6

Move right hand parameter to cmp in _compileBinary to a temp reg if it is a constant since those are only supported in the left hand param currently
author Michael Pavone <pavone@retrodev.com>
date Tue, 14 Apr 2015 19:54:03 -0700
parents dcb495cf2b97
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
297
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 _getStdin <- {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 _stdin <- (file fromFD: 0)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 _getStdin <- { _stdin }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 _stdin
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 _getStdout <- {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 _stdout <- (file fromFD: 1)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 _getStdout <- { _stdout }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 _stdout
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 _getStderr <- {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 _stderr <- (file fromFD: 2)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 _getStderr <- { _stderr }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 _stderr
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 #{
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 fromFD <- :_fd {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 _buffers <- #[]
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 _readPos <- 0
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 _buffered <- 0
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 _bufferChunk <- 4096
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 _eof? <- false
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 _addBuffer <- {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 newbuf <- bytearray normal: _bufferChunk
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 justRead <- os readFrom: _fd to: newbuf
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 if: justRead > 0 {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 _buffered <- _buffered + justRead
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 _buffers append: (newbuf shrinkTo: justRead)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 } else: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 _eof? <- true
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 _lineIter <- :f {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 iter <- #{
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 foreach <- :self fun {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 idx <- 0
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 while: { not: (f eof?) } do: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 line <- f nextLine
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 fun: idx line
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 idx <- idx + 1
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 self
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 map <- :fun {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 new <- #[]
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 foreach: :idx el {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 new append: (fun: el)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 new
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 filter <- :fun {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 new <- #[]
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 foreach: :idx el {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 if: (fun: el) {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 new append: el
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 new
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 fold:with <- :acc :fun {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 foreach: :idx el {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 acc <- fun: acc el
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 acc
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 foldr:with <- :acc :fun {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 (map: :el { el }) foldr: acc with: fun
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 _lineIter <- { iter }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 iter
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 #{
304
dcb495cf2b97 Add fd method to file objects for accessing file descriptor
Michael Pavone <pavone@retrodev.com>
parents: 299
diff changeset
79 fd <- { _fd }
297
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 read <- :bytes {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 while: { bytes > _buffered && (not: _eof?)} do: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 _addBuffer:
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 nextBuffers <- #[]
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 pieces <- #[]
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 toRead <- bytes
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87 i <- 0
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
88 while: { toRead > 0 && i < (_buffers length) } do: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
89 curBuf <- _buffers get: i
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
90 if: toRead >= (curBuf length) - _readPos {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 pieces append: (curBuf stringFrom: _readPos to: (curBuf length))
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 _buffered <- _buffered - ((curBuf length) - _readPos)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 toRead <- toRead - ((curBuf length) - _readPos)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 _readPos <- 0
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 } else: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 nextPos <- _readPos + toRead
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 pieces append: (curBuf stringFrom: _readPos to: nextPos)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 _readPos <- nextPos
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99 _buffered <- _buffered - toRead
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 toRead <- 0
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101 nextBuffers append: curBuf
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 i <- i + 1
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 while: {i < (_buffers length)} do: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 nextBuffers append: (_buffers get: i)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 i <- i + 1
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 _buffers <- nextBuffers
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 pieces join: ""
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 }
299
c0561cb06ee3 Add readAll method to file objects
Michael Pavone <pavone@retrodev.com>
parents: 297
diff changeset
113 readAll <- {
c0561cb06ee3 Add readAll method to file objects
Michael Pavone <pavone@retrodev.com>
parents: 297
diff changeset
114 while: { not: _eof? } do: {
c0561cb06ee3 Add readAll method to file objects
Michael Pavone <pavone@retrodev.com>
parents: 297
diff changeset
115 _addBuffer:
c0561cb06ee3 Add readAll method to file objects
Michael Pavone <pavone@retrodev.com>
parents: 297
diff changeset
116 }
c0561cb06ee3 Add readAll method to file objects
Michael Pavone <pavone@retrodev.com>
parents: 297
diff changeset
117 read: _buffered
c0561cb06ee3 Add readAll method to file objects
Michael Pavone <pavone@retrodev.com>
parents: 297
diff changeset
118 }
297
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 write <- :data {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120 //TODO: write buffering for small writes
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 os write: _fd data
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
123 nextLine <- {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
124 nl <- "\n" byte: 0
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 notfound <- true
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 pos <- 0
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127 i <- 0
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
128 while: { notfound } do: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
129 while: { notfound && i < (_buffers length) } do: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
130 curBuf <- _buffers get: i
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
131 start <- if: i = 0 { _readPos } else: { 0 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
132 curBuf findChar: nl from: start :idx {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
133 notfound <- false
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
134 pos <- pos + idx + 1
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
135 } else: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
136 pos <- pos + (curBuf length)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
137 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
138
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
139 i <- i + 1
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
140 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
141
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
142 if: notfound && (not: _eof?) {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
143 _addBuffer:
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
144 } else: {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
145 notfound <- false
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
146 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
147 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
148 read: pos - _readPos
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
149 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
150 lines <- {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
151 _lineIter: self
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
152 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
153 close <- {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
154 os close: _fd
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
155 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
156 eof? <- { _eof? && _buffered <= 0 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
157 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
158 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
159
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
160 open <- :filename {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
161 fromFD: (os open: filename (os O_RDONLY))
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
162 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
163
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
164 openWrite <- :filename {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
165 fromFD: (os open: filename ((os O_WRONLY) or (os O_CREAT) or (os O_TRUNC)) 0x1B6)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
166 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
167
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
168 openAppend <- :filename {
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
169 fromFD: (os open: filename ((os O_RDWR) or (os O_CREAT) or (os O_APPEND)) 0x1B6)
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
170 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
171
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
172 stdin <- { _getStdin: }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
173 stdout <- { _getStdout: }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
174 stderr <- { _getStderr: }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
175 }
abe3141caefe Added file module and sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
176 }