Mercurial > repos > rhope
comparison stringjs.rhope @ 136:fc3815b7462f
Javascript backend now produces working code for some simple examples, still more of the standard lib that needs to be ported.
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 14 Nov 2010 23:07:55 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
135:18a4403fe576 | 136:fc3815b7462f |
---|---|
1 | |
2 | |
3 Blueprint String | |
4 { | |
5 Buffer | |
6 } | |
7 | |
8 Foreign Javascript:builtin | |
9 { | |
10 print[val] | |
11 parseInt[string,base(Int32,Naked):out(Int32,Naked)] | |
12 _internal_js_eq[left,right:out(Boolean,Naked)] | |
13 slice@[string,start(Int32,Naked),end(Int32,Naked):out] | |
14 concat@[left,right:out] | |
15 } | |
16 | |
17 Print@String[string:out] | |
18 { | |
19 print[[string]Buffer >>] | |
20 out <- Yes | |
21 } | |
22 | |
23 Int32@String[string:out] | |
24 { | |
25 out <- parseInt[[string]Buffer >>, 10] | |
26 } | |
27 | |
28 Hex Int32[str:out] | |
29 { | |
30 out <- parseInt[[str]Buffer >>, 16] | |
31 } | |
32 | |
33 Flatten@String[string:out] | |
34 { | |
35 out <- string | |
36 } | |
37 | |
38 Slice@String[string,slicepoint:left,right] | |
39 { | |
40 If[[slicepoint]>=[[string]Length]] | |
41 { | |
42 left <- string | |
43 right <- "" | |
44 }{ | |
45 If[[slicepoint]<=[0]] | |
46 { | |
47 left <- "" | |
48 right <- string | |
49 }{ | |
50 [string]Buffer >> | |
51 { | |
52 left <- [Build[String()]]Buffer <<[[~]slice[0,slicepoint]] | |
53 right <- [Build[String()]]Buffer <<[[~]slice[slicepoint,_internal_js_length[~]]] | |
54 } | |
55 } | |
56 } | |
57 } | |
58 /* | |
59 Byte@String[string,index:out,invalid] | |
60 { | |
61 out,invalid <- [[string]Buffer >>]Index[index] | |
62 } | |
63 */ | |
64 Length@String[string:out] | |
65 { | |
66 out <- _internal_js_length[[string]Buffer >>] | |
67 } | |
68 | |
69 Append@String[left,right(String):out] | |
70 { | |
71 out <- [Build[String()]]Buffer <<[[[left]Buffer >>]concat[[right]Buffer >>]] | |
72 } | |
73 | |
74 =@String[left,right(String):out] | |
75 { | |
76 out <- [[left]Buffer >>]_internal_js_eq[[right]Buffer >>] | |
77 } | |
78 | |
79 /* | |
80 =Delim[string,delims,index:outindex,after,nomatch] | |
81 { | |
82 delim <- [delims]Index[index] | |
83 If[[[string]Length]<[[delim]Length]] | |
84 { | |
85 try next <- Yes | |
86 }{ | |
87 check,mafter <- [string]Slice[[delim]Length] | |
88 ,try next <- If[[check]=[delim]] | |
89 { | |
90 outindex <- index | |
91 after <- Val[mafter] | |
92 } | |
93 } | |
94 Val[try next] | |
95 { | |
96 ,nomatch <- [delims]Next[index] | |
97 { | |
98 outindex,after,nomatch <- =Delim[string,delims,~] | |
99 } | |
100 } | |
101 } | |
102 | |
103 Pattern@String[string:out] | |
104 { | |
105 out <- string | |
106 } | |
107 | |
108 Match@String[string,cmp:num,no match,idx] | |
109 { | |
110 n <- [string]Length | |
111 ,no match <- If[[string]=[[cmp]Slice[n]]] | |
112 { | |
113 num <- Val[n] | |
114 idx <- 0 | |
115 } | |
116 } | |
117 | |
118 _Partition[string,delims:matched,after,not found] | |
119 { | |
120 not found <- If[[string]=[""]] {} | |
121 { | |
122 [delims]Match[string] | |
123 { | |
124 matched,after <- [string]Slice[~] | |
125 }{ | |
126 [string]Slice[1] {} | |
127 { matched,after,not found <- _Partition[~,delims] } | |
128 } | |
129 } | |
130 } | |
131 | |
132 Partition[string,delims:before,matched,after,not found] | |
133 { | |
134 matched,after,not found <- _Partition[string,Pattern[delims]] | |
135 { dlen <- Length[~] } | |
136 { alen <- Length[~] } | |
137 before <- [string]Slice[ [[string]Length]-[[dlen]+[alen]] ] | |
138 } | |
139 | |
140 | |
141 Dict Type ID@String[string:out] | |
142 { | |
143 out <- ID[String()] | |
144 } | |
145 | |
146 Dict Bits@String[string,index:out,invalid] | |
147 { | |
148 ,invalid <- [string]Byte[index] | |
149 { out <- UInt32[~] } | |
150 } | |
151 | |
152 _From Dict String[arr,el:out] | |
153 { | |
154 out <- [arr]Append[Trunc UInt8[el]] | |
155 } | |
156 | |
157 From Dict Key@String[string,data:out] | |
158 { | |
159 out <- String[Fold[_From Dict String[?], Array[], data]] | |
160 } | |
161 */ | |
162 | |
163 String@String[string:out] | |
164 { | |
165 out <- string | |
166 } | |
167 | |
168 /* | |
169 Replace[string,otoreplace,with:out] | |
170 { | |
171 toreplace <- Pattern[otoreplace] | |
172 ,delim,after <-[string]Partition[toreplace] | |
173 { | |
174 wt <- Blueprint Of[with] | |
175 If[ [[[wt]=[String()]] Or [[wt]=[String Slice()]]] Or [[wt]=[String Cat()]] ] | |
176 { | |
177 replacement <- with | |
178 }{ | |
179 ,,idx <- [toreplace]Match[delim] | |
180 replacement <- [with]Index[idx] | |
181 } | |
182 out <- [[~]Append[replacement]]Append[Replace[after,toreplace,with]] | |
183 } {} {} { | |
184 out <- string | |
185 } | |
186 } | |
187 | |
188 _Join[list,delim,current,index:out] | |
189 { | |
190 [list]Next[index] | |
191 { | |
192 out <- _Join[list, delim, [[current]Append[delim]]Append[String[[list]Index[~]]], ~] | |
193 }{ | |
194 out <- current | |
195 } | |
196 } | |
197 | |
198 Join[list,delim:out] | |
199 { | |
200 [list]First | |
201 { | |
202 out <- _Join[list, delim, String[[list]Index[~]], ~] | |
203 }{ | |
204 out <- "" | |
205 } | |
206 } | |
207 | |
208 */ | |
209 | |
210 Starts With[thing,starts with:out] | |
211 { | |
212 out <- [[thing]Slice[[starts with]Length]] = [starts with] | |
213 } | |
214 | |
215 Ends With[thing,ends with:out] | |
216 { | |
217 ,compare <- [thing]Slice[ [[thing]Length] - [[ends with]Length] ] | |
218 out <- [compare] = [ends with] | |
219 } | |
220 | |
221 If@String[str:yes,no] | |
222 { | |
223 yes,no <- If[[str]Length] | |
224 } | |
225 | |
226 /* | |
227 _Split[list,string,delim:out] | |
228 { | |
229 ,,rest <- [string]Partition[delim] | |
230 { | |
231 out <- _Split[[list]Append[~], rest, delim] | |
232 } {} {} { | |
233 out <- [list]Append[string] | |
234 } | |
235 } | |
236 | |
237 Split[string,delim:out] | |
238 { | |
239 If[string] | |
240 { out <- _Split[(),string,delim] } | |
241 { out <- () } | |
242 } | |
243 */ | |
244 | |
245 In[needle,haystack:out] | |
246 { | |
247 If[haystack] | |
248 { | |
249 out <- If[[[haystack]Slice[Length[needle]]]=[needle]] {} | |
250 { | |
251 [haystack]Slice[1] {} | |
252 { out <- [needle]In[~] } | |
253 } | |
254 }{ | |
255 out <- No | |
256 } | |
257 } | |
258 | |
259 Left Trim[string,trim:trimmed] | |
260 { | |
261 If[ [[string]Length] > [0] ] | |
262 { | |
263 first,rest <- [string]Slice[1] | |
264 If[ [first]In[trim] ] | |
265 { | |
266 trimmed <- Left Trim[rest, trim] | |
267 }{ | |
268 trimmed <- string | |
269 } | |
270 }{ | |
271 trimmed <- string | |
272 } | |
273 } | |
274 | |
275 Right Trim[string,trim:trimmed] | |
276 { | |
277 If[ [[string]Length] > [0] ] | |
278 { | |
279 rest,last <- [string]Slice[ [[string]Length] - [1]] | |
280 If[ [last]In[trim] ] | |
281 { | |
282 trimmed <- Right Trim[rest, trim] | |
283 }{ | |
284 trimmed <- string | |
285 } | |
286 }{ | |
287 trimmed <- string | |
288 } | |
289 } | |
290 | |
291 Trim[string,trim:trimmed] | |
292 { | |
293 left <- Left Trim[string, trim] | |
294 trimmed <- Right Trim[left, trim] | |
295 } | |
296 | |
297 /* | |
298 Contains[haystack,needle:out] | |
299 { | |
300 [haystack]Partition[needle] | |
301 { | |
302 out <- Yes | |
303 } {} {} { | |
304 out <- No | |
305 } | |
306 } | |
307 */ | |
308 | |
309 After[text,prefix:after,not found] | |
310 { | |
311 If[[text]Starts With[prefix]] | |
312 { | |
313 ,after <- [text]Slice[[prefix]Length] | |
314 }{ | |
315 not found <- text | |
316 } | |
317 } | |
318 |