| 1 |
/* |
| 2 |
* Copyright (C)2005-2022 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
type void; |
| 24 |
type int; |
| 25 |
type int32; |
| 26 |
type float; |
| 27 |
type char; |
| 28 |
type string; |
| 29 |
type error; |
| 30 |
type 'a array; |
| 31 |
type 'a ref; |
| 32 |
type 'a format; |
| 33 |
type 'a stream; |
| 34 |
|
| 35 |
type 'a option { |
| 36 |
None; |
| 37 |
Some : 'a; |
| 38 |
} |
| 39 |
|
| 40 |
type neko_value; |
| 41 |
|
| 42 |
exception Neko_error : neko_value; |
| 43 |
exception Invalid_argument : string; |
| 44 |
exception Assert_failure : (string,int); |
| 45 |
exception Error : string; |
| 46 |
exception Stream_error; |
| 47 |
exception Not_found; |
| 48 |
exception Exit; |
| 49 |
|
| 50 |
/* ----- |
| 51 |
|
| 52 |
theses are defined directly in the compiler |
| 53 |
because they need additional magic. |
| 54 |
|
| 55 |
type bool { |
| 56 |
true; |
| 57 |
false; |
| 58 |
} |
| 59 |
|
| 60 |
type 'a list { |
| 61 |
[]; |
| 62 |
:: : ('a , 'a list); |
| 63 |
} |
| 64 |
|
| 65 |
val neko : string -> 'a |
| 66 |
|
| 67 |
----- */ |
| 68 |
|
| 69 |
neko(" |
| 70 |
|
| 71 |
@sprintf = $loader.loadprim('std@sprintf',2); |
| 72 |
|
| 73 |
@string = function(x) { |
| 74 |
// tuples |
| 75 |
if( $typeof(x) == $tarray ) { |
| 76 |
// constructor |
| 77 |
var printer = x[1]; |
| 78 |
if( $typeof(printer) == $tfunction && $nargs(printer) == 1 ) |
| 79 |
return printer(x); |
| 80 |
var s = '('; |
| 81 |
var l = $asize(x); |
| 82 |
var i = 0; |
| 83 |
while i < l { |
| 84 |
s = s + @string(x[i]); |
| 85 |
i = i + 1; |
| 86 |
if i < l |
| 87 |
s = s + ', '; |
| 88 |
} |
| 89 |
s = s + ')'; |
| 90 |
return s; |
| 91 |
} |
| 92 |
// others |
| 93 |
return $string(x); |
| 94 |
} |
| 95 |
|
| 96 |
$exports.@print_record = function() { |
| 97 |
var o = $objfields(this); |
| 98 |
var s = '{ '; |
| 99 |
var i = 0; |
| 100 |
var l = $asize(o) - 1; |
| 101 |
while i < l { |
| 102 |
var f = o[i]; |
| 103 |
var fname = $field(f); |
| 104 |
if fname == '__string' |
| 105 |
l = l + 1; |
| 106 |
else |
| 107 |
s = s + fname + ' = ' + @string($objget(this,f))+'; '; |
| 108 |
i = i + 1; |
| 109 |
} |
| 110 |
s = s + '}'; |
| 111 |
s |
| 112 |
} |
| 113 |
|
| 114 |
$exports.@print_union = function(s,a) { |
| 115 |
var l = $asize(a); |
| 116 |
if l == 2 |
| 117 |
return s; |
| 118 |
var i = 2; |
| 119 |
s = s + '('; |
| 120 |
while i < l { |
| 121 |
s = s + @string(a[i]); |
| 122 |
i = i + 1; |
| 123 |
if i < l |
| 124 |
s = s + ', '; |
| 125 |
} |
| 126 |
s = s + ')'; |
| 127 |
return s; |
| 128 |
} |
| 129 |
|
| 130 |
@compare = function(a,b,history) { |
| 131 |
if( $pcompare(a,b) == 0 ) |
| 132 |
return 0; |
| 133 |
if( $typeof(a) == $tarray ) { |
| 134 |
var l1 = $asize(a); |
| 135 |
var l2 = $asize(b); |
| 136 |
if( l1 != l2 ) |
| 137 |
return $compare(l1,l2); |
| 138 |
var h = history; |
| 139 |
while h != null { |
| 140 |
if( h[0] == a && h[1] == b ) |
| 141 |
return $pcompare(a,b); |
| 142 |
h = h[2]; |
| 143 |
} |
| 144 |
var i = 0; |
| 145 |
while i < l1 { |
| 146 |
var k = @compare(a[i],b[i],$array(a,b,history)); |
| 147 |
if( k != 0 ) |
| 148 |
return k; |
| 149 |
i = i + 1; |
| 150 |
} |
| 151 |
return 0; |
| 152 |
} |
| 153 |
if( $typeof(a) == $tobject ) { |
| 154 |
var h = history; |
| 155 |
while h != null { |
| 156 |
if( h[0] == a && h[1] == b ) |
| 157 |
return $pcompare(a,b); |
| 158 |
h = h[2]; |
| 159 |
} |
| 160 |
var o = $objfields(a); |
| 161 |
var i = 0; |
| 162 |
var l = $asize(o); |
| 163 |
while i < l { |
| 164 |
var v = $objget(b,o[i]); |
| 165 |
var k = @compare($objget(a,o[i]),v,$array(a,b,history)); |
| 166 |
if( k != 0 ) |
| 167 |
return k; |
| 168 |
i = i + 1; |
| 169 |
} |
| 170 |
return 0; |
| 171 |
} |
| 172 |
return $compare(a,b); |
| 173 |
} |
| 174 |
|
| 175 |
$exports.@compare = function(a,b) { @compare(a,b,null) }; |
| 176 |
|
| 177 |
$exports.@aget = function(a,i) { |
| 178 |
if( i < 0 || i >= a[2] ) |
| 179 |
$exports.invalid_arg('Array.get'); |
| 180 |
a[0][i] |
| 181 |
} |
| 182 |
|
| 183 |
$exports.@aset = function(a,i,v) { |
| 184 |
if( i < 0 || i >= a[2] ) |
| 185 |
$exports.invalid_arg('Array.set'); |
| 186 |
a[0][i] = v; |
| 187 |
} |
| 188 |
|
| 189 |
@empty = $array(0,function(_){ '[]' }); |
| 190 |
@pcons = function(c) { @string(c[2]) + ' :: ' + @string(c[3]) }; |
| 191 |
@cons = function(x,l) { |
| 192 |
$array(1,@pcons,x,l) |
| 193 |
} |
| 194 |
$exports.@pcons = @pcons; |
| 195 |
$exports.@cons = @cons; |
| 196 |
$exports.@empty = @empty; |
| 197 |
"); |
| 198 |
|
| 199 |
function magic(x) { |
| 200 |
neko("x"); |
| 201 |
} |
| 202 |
|
| 203 |
function throw(x : error) : 'a { |
| 204 |
neko("$throw(x)"); |
| 205 |
} |
| 206 |
|
| 207 |
function assert(file,line) { |
| 208 |
throw Assert_failure(file,line) |
| 209 |
} |
| 210 |
|
| 211 |
function invalid_arg(fun) : 'a { |
| 212 |
throw Invalid_argument(fun); |
| 213 |
} |
| 214 |
|
| 215 |
function error(x : string) { |
| 216 |
throw Error(x) |
| 217 |
} |
| 218 |
|
| 219 |
function ignore(x) { |
| 220 |
} |
| 221 |
|
| 222 |
function fst((a,_)) { |
| 223 |
a |
| 224 |
} |
| 225 |
|
| 226 |
function snd((_,a)) { |
| 227 |
a |
| 228 |
} |
| 229 |
|
| 230 |
function int(x:string) : int { |
| 231 |
var i = neko("$int(x)"); |
| 232 |
if i == neko("null") then invalid_arg(); |
| 233 |
i |
| 234 |
} |
| 235 |
|
| 236 |
function float(x:string) : float { |
| 237 |
var i = neko("$float(x)"); |
| 238 |
if i == neko("null") then invalid_arg(); |
| 239 |
i |
| 240 |
} |
| 241 |
|
| 242 |
function ord(x:char) : int { |
| 243 |
neko("x"); |
| 244 |
} |
| 245 |
|
| 246 |
function chr(x:int) : char { |
| 247 |
if x < 0 || x > 255 then invalid_arg(); |
| 248 |
neko("x"); |
| 249 |
} |
| 250 |
|
| 251 |
function string(x : 'a) : string { |
| 252 |
neko("@string(x)"); |
| 253 |
} |
| 254 |
|
| 255 |
function nstring(x : 'a) : string { |
| 256 |
neko("$string(x)"); |
| 257 |
} |
| 258 |
|
| 259 |
function print(x) : void { |
| 260 |
neko("$print(string(x))"); |
| 261 |
} |
| 262 |
|
| 263 |
function nprint(x) : void { |
| 264 |
neko("$print(x)"); |
| 265 |
} |
| 266 |
|
| 267 |
function compare(x : 'a, y : 'a) : int { |
| 268 |
neko("$exports.@compare(x,y)"); |
| 269 |
} |
| 270 |
|
| 271 |
function min(x,y) { |
| 272 |
if x < y then x else y |
| 273 |
} |
| 274 |
|
| 275 |
function max(x,y) { |
| 276 |
if x < y then y else x |
| 277 |
} |
| 278 |
|
| 279 |
function sprintf(fmt : 'a format, p : 'a ) : string { |
| 280 |
neko("@sprintf(fmt,p)") |
| 281 |
} |
| 282 |
|
| 283 |
function printf(fmt : 'a format, p : 'a ) : void { |
| 284 |
print (sprintf fmt p) |
| 285 |
} |
| 286 |
|
| 287 |
function stream(f : void -> 'a ) : 'a stream { |
| 288 |
neko("{ |
| 289 |
get => f, |
| 290 |
pos => 0, |
| 291 |
cache => $array() |
| 292 |
}"); |
| 293 |
} |
| 294 |
|
| 295 |
function stream_token( s : 'a stream, x : int ) : 'a { |
| 296 |
if x < 0 then invalid_arg(); |
| 297 |
neko(" |
| 298 |
var tmp = s.cache[x]; |
| 299 |
if( tmp != null ) |
| 300 |
return tmp; |
| 301 |
var n = $asize(s.cache); |
| 302 |
var c = $amake(x + 1); |
| 303 |
$ablit(c,0,s.cache,0,n); |
| 304 |
while n <= x { |
| 305 |
c[n] = s.get(null); |
| 306 |
n = n + 1; |
| 307 |
} |
| 308 |
s.cache = c; |
| 309 |
return c[x]; |
| 310 |
"); |
| 311 |
} |
| 312 |
|
| 313 |
function stream_junk( s : 'a stream, x : int ) : void { |
| 314 |
if x < 0 then invalid_arg(); |
| 315 |
neko(" |
| 316 |
var c = $asize(s.cache); |
| 317 |
s.pos = s.pos + x; |
| 318 |
if( c >= x ) |
| 319 |
s.cache = $asub(s.cache,x,c - x); |
| 320 |
else { |
| 321 |
s.cache = $array(); |
| 322 |
x = x - c; |
| 323 |
while x > 0 { |
| 324 |
s.get(null); |
| 325 |
x = x - 1; |
| 326 |
} |
| 327 |
} |
| 328 |
"); |
| 329 |
} |
| 330 |
|
| 331 |
function stream_pos( s : 'a stream ) : int { |
| 332 |
neko("s.pos"); |
| 333 |
} |