| 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 |
open Lexer; |
| 23 |
open Neko.Ast; |
| 24 |
open Nekoml.Type; |
| 25 |
|
| 26 |
type comparison { |
| 27 |
Native; |
| 28 |
Structural; |
| 29 |
} |
| 30 |
|
| 31 |
type context { |
| 32 |
typectx : Nekoml.Typer.context; |
| 33 |
current : string list; |
| 34 |
module_name : string; |
| 35 |
imports : (string list,string) Hashtbl.t; |
| 36 |
mutable counter : int; |
| 37 |
mutable refvars : string Set.t; |
| 38 |
} |
| 39 |
|
| 40 |
function gen_label(ctx) { |
| 41 |
var c = ctx.counter; |
| 42 |
ctx.counter := ctx.counter + 1; |
| 43 |
"l" + string c |
| 44 |
} |
| 45 |
|
| 46 |
function gen_variable(ctx) { |
| 47 |
var c = ctx.counter; |
| 48 |
ctx.counter := ctx.counter + 1; |
| 49 |
"v" + string c |
| 50 |
} |
| 51 |
|
| 52 |
function module_name(m) { |
| 53 |
"@" + String.concat "_" m |
| 54 |
} |
| 55 |
|
| 56 |
function builtin(name) { |
| 57 |
(EConst (Builtin name) , null_pos) |
| 58 |
} |
| 59 |
|
| 60 |
function ident(name) { |
| 61 |
(EConst (Ident name) , null_pos) |
| 62 |
} |
| 63 |
|
| 64 |
function int(n) { |
| 65 |
(EConst (Int n) , null_pos) |
| 66 |
} |
| 67 |
|
| 68 |
var enull = (EConst Null , null_pos) |
| 69 |
|
| 70 |
var core = ["Core"]; |
| 71 |
|
| 72 |
function record_index(f,t) { |
| 73 |
match tlinks false t { |
| 74 |
| TRecord fl -> |
| 75 |
function rec loop(i,l) { |
| 76 |
match l { |
| 77 |
| [] -> assert() |
| 78 |
| (ff,_,_) :: l -> |
| 79 |
if f == ff then i else loop (i+1) l |
| 80 |
} |
| 81 |
} |
| 82 |
loop 0 fl |
| 83 |
| _ -> |
| 84 |
assert() |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
function construct_id(ctx,m,c) { |
| 89 |
var m = if m == [] then ctx.current else m; |
| 90 |
var mod = try Nekoml.Typer.module ctx.typectx m catch { Not_found -> assert() }; |
| 91 |
var r = try Nekoml.Typer.union mod c catch { Not_found -> assert() }; |
| 92 |
if r == t_error then |
| 93 |
Hashtbl.hash (c :: m) |
| 94 |
else match tlinks false r { |
| 95 |
| TUnion (_,fl) -> |
| 96 |
function rec loop(i,l) { |
| 97 |
match l { |
| 98 |
| [] -> assert() |
| 99 |
| (cc,_) :: l -> |
| 100 |
if c == cc then i else loop (i+1) l |
| 101 |
} |
| 102 |
} |
| 103 |
loop 0 fl |
| 104 |
| _ -> |
| 105 |
assert() |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
function rec is_fun(t) { |
| 110 |
match t.texpr { |
| 111 |
| TNamed (_,_,t) | TLink t -> is_fun t |
| 112 |
| TPoly |
| 113 |
| TMono _ |
| 114 |
| TFun _ -> true |
| 115 |
| _ -> false |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
function call(ret,f,args,p) { |
| 120 |
if is_fun ret then |
| 121 |
(ECall (EConst (Builtin "apply"),p) (f :: args) , p) |
| 122 |
else |
| 123 |
(ECall f args , p) |
| 124 |
} |
| 125 |
|
| 126 |
function array(args,p) { |
| 127 |
(ECall (EConst (Builtin "array"),p) args , p) |
| 128 |
} |
| 129 |
|
| 130 |
function block(e) { |
| 131 |
match fst e { |
| 132 |
| EBlock _ -> e |
| 133 |
| _ -> (EBlock [e] , snd e) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
function rec arity(t) { |
| 138 |
match t.texpr { |
| 139 |
| TAbstract -> 0 |
| 140 |
| TTuple tl -> List.length tl |
| 141 |
| TLink t -> arity t |
| 142 |
| _ -> 1 |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
function comparison(t) { |
| 147 |
match tlinks true t { |
| 148 |
| TNamed (["int"],[],_) |
| 149 |
| TNamed (["char"],[],_) |
| 150 |
| TNamed (["float"],[],_) |
| 151 |
| TNamed (["string"],[],_) -> Native |
| 152 |
| _ -> Structural |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
function import(ctx,m,i,p) { |
| 157 |
if m == ctx.current then |
| 158 |
(EField ident(ctx.module_name) i,p) |
| 159 |
else { |
| 160 |
var key = i :: m; |
| 161 |
(EConst Ident(try { |
| 162 |
Hashtbl.find ctx.imports key |
| 163 |
} catch { |
| 164 |
Not_found -> |
| 165 |
var i = "@" + String.concat "_" m + "_" + i; |
| 166 |
Hashtbl.add ctx.imports key i; |
| 167 |
i |
| 168 |
}),p) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
function rec gen_constant(ctx,c,p) { |
| 173 |
(match c { |
| 174 |
| TVoid -> EConst Null |
| 175 |
| TInt n when n < 0 -> EBinop "-" int(0) int(-n) |
| 176 |
| TInt n -> EConst (Int n) |
| 177 |
| TFloat s -> EConst (Float s) |
| 178 |
| TChar c -> EConst Int(ord c) |
| 179 |
| TString s -> EConst (String s) |
| 180 |
| TIdent s -> |
| 181 |
if Set.exists ctx.refvars s then EArray ident(s) int(0) else EConst (Ident s) |
| 182 |
| TBool b -> EConst (if b then True else False) |
| 183 |
| TConstr "[]" | TModule (["Core"],TConstr "[]") -> fst (import ctx core "@empty" p) |
| 184 |
| TConstr "::" | TModule (["Core"],TConstr "::") -> fst (import ctx core "@cons" p) |
| 185 |
| TConstr s -> EConst (Ident s) |
| 186 |
| TModule ([],c) -> fst (gen_constant ctx c p) |
| 187 |
| TModule (m,c) -> |
| 188 |
fst (import ctx m (match c { TConstr x -> x | TIdent s -> s | _ -> assert() }) p) |
| 189 |
} , p) |
| 190 |
} |
| 191 |
|
| 192 |
type match_context { |
| 193 |
ctx : context; |
| 194 |
out : string; |
| 195 |
pos : pos; |
| 196 |
mutable cache : (match_op , string) list; |
| 197 |
mutable next : string; |
| 198 |
mutable first : bool; |
| 199 |
} |
| 200 |
|
| 201 |
var no_label = "<assert>" |
| 202 |
|
| 203 |
function rec gen_match_rec(mctx,fail,next,m) { |
| 204 |
try |
| 205 |
ident (List.phys m mctx.cache) |
| 206 |
catch { Not_found -> |
| 207 |
var p = mctx.pos; |
| 208 |
var ctx = mctx.ctx; |
| 209 |
var gen_rec = gen_match_rec mctx; |
| 210 |
match m { |
| 211 |
| MFailure -> |
| 212 |
var label = if mctx.first && mctx.next != no_label then mctx.next else fail; |
| 213 |
if label == no_label then |
| 214 |
(EBlock [] , p) |
| 215 |
else |
| 216 |
call t_void (builtin "goto") [ident label] p |
| 217 |
| MHandle (m1,m2) -> |
| 218 |
var label = gen_label ctx; |
| 219 |
var m1 = gen_rec label true m1; |
| 220 |
var m2 = gen_rec fail next m2; |
| 221 |
(EBlock [m1; (ELabel label, p); m2] , p) |
| 222 |
| MRoot -> |
| 223 |
assert() |
| 224 |
| MExecute (e,b) -> |
| 225 |
if !b then { |
| 226 |
var ematch = (EBinop "==" ident("@exc") (import ctx core "Stream_error" p) , p); |
| 227 |
var stream_pos = (ECall (import ctx core "stream_pos" p) [gen_rec fail true MRoot] , p); |
| 228 |
var test = (EBinop "==" ident("@pos") stream_pos , p); |
| 229 |
var reraise = (EIf (EBinop "&&" ematch test,p) gen_rec(fail,true,MFailure) Some((ECall (builtin "rethrow") [ident "@exc"],p)) , p); |
| 230 |
mctx.first := false; |
| 231 |
match e.edecl { |
| 232 |
| TConst _ -> gen_expr ctx e |
| 233 |
| _ -> (ETry gen_expr(ctx,e) "@exc" reraise , p) |
| 234 |
} |
| 235 |
} else { |
| 236 |
mctx.first := true; |
| 237 |
if !next then |
| 238 |
gen_expr ctx e |
| 239 |
else { |
| 240 |
var out = call t_void (builtin "goto") [ident mctx.out] p; |
| 241 |
(EBlock [gen_expr ctx e;out] , p) |
| 242 |
} |
| 243 |
} |
| 244 |
| MConstants (m,[(TIdent v,m1)]) -> |
| 245 |
var m = gen_rec fail true m; |
| 246 |
(EBlock [ |
| 247 |
(EVars [(v, Some m)] , p); |
| 248 |
gen_rec fail next m1 |
| 249 |
] , p) |
| 250 |
| MConstants (m,cl) -> |
| 251 |
var e = gen_rec fail true m; |
| 252 |
var cases = List.map (function((c,m)) { |
| 253 |
(gen_constant ctx c p, gen_rec fail next m) |
| 254 |
}) cl; |
| 255 |
(ESwitch e cases Some(gen_rec fail next MFailure), p) |
| 256 |
| MRecordField (m,f,t) -> |
| 257 |
(EArray gen_rec(fail,true,m) int(record_index f t) , p) |
| 258 |
| MTuple (m,n) -> |
| 259 |
(EArray gen_rec(fail,true,m) int(n) , p) |
| 260 |
| MField (m,n) -> |
| 261 |
(EArray gen_rec(fail,true,m) int(n + 2) , p) |
| 262 |
| MNext (m1,m2) -> |
| 263 |
var old = mctx.next; |
| 264 |
var label = gen_label ctx; |
| 265 |
mctx.next := label; |
| 266 |
var m1 = gen_rec fail true m1; |
| 267 |
mctx.next := old; |
| 268 |
var m2 = gen_rec fail next m2; |
| 269 |
(EBlock [m1; (ELabel label, p); m2] , p) |
| 270 |
| MSwitch (m,cl) -> |
| 271 |
var e = (EArray (gen_rec fail true m) int(0) , p); |
| 272 |
var cases = List.map (function((c,m)) { |
| 273 |
var path , c = match c { |
| 274 |
| TModule (path,TConstr c) -> (path , c) |
| 275 |
| TConstr c -> (ctx.current,c) |
| 276 |
| _ -> |
| 277 |
assert() |
| 278 |
} |
| 279 |
(int(construct_id ctx path c), gen_rec fail next m) |
| 280 |
}) cl; |
| 281 |
(ESwitch e cases Some(gen_rec fail next MFailure), p) |
| 282 |
| MBind (v,m1,m2) -> |
| 283 |
var e1 = gen_rec fail true m1; |
| 284 |
var old = mctx.cache; |
| 285 |
mctx.cache := (m1,v) :: mctx.cache; |
| 286 |
var e2 = gen_rec fail next m2; |
| 287 |
mctx.cache := old; |
| 288 |
(EBlock [(EVars [(v, Some e1)] , p); e2] , p) |
| 289 |
| MWhen (e,m) -> |
| 290 |
var e = gen_expr ctx e; |
| 291 |
var m = gen_rec fail next m; |
| 292 |
var fail = gen_rec fail next MFailure; |
| 293 |
(EIf e m Some(fail), p) |
| 294 |
| MToken (m,n) -> |
| 295 |
call t_void (import ctx core "stream_token" p) [gen_rec fail true m; int n] p |
| 296 |
| MJunk (m,n,m2) -> |
| 297 |
var m = gen_rec fail true m; |
| 298 |
mctx.first := false; |
| 299 |
(EBlock [ |
| 300 |
call t_void (import ctx core "stream_junk" p) [m; int n] p; |
| 301 |
gen_rec fail next m2 |
| 302 |
] , p) |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
function rec gen_matching(ctx,v,m,p,stream,next,out) { |
| 308 |
var mctx = { |
| 309 |
ctx = ctx; |
| 310 |
cache = [(MRoot,v)]; |
| 311 |
pos = p; |
| 312 |
out = out; |
| 313 |
first = stream; |
| 314 |
next = no_label; |
| 315 |
}; |
| 316 |
var label = (if stream then gen_label ctx else no_label); |
| 317 |
var e = gen_match_rec mctx label next m; |
| 318 |
if stream then { |
| 319 |
var vpos = "@pos"; |
| 320 |
var stream_pos = (ECall (import ctx core "stream_pos" p) [ident v] , p); |
| 321 |
var exc = (ECall builtin("throw") [import ctx core "Stream_error" p] , p); |
| 322 |
(EBlock [(EVars [(vpos , Some stream_pos)] , p); e; (ELabel label , p); exc] , p) |
| 323 |
} else |
| 324 |
e |
| 325 |
} |
| 326 |
|
| 327 |
function rec gen_match(ctx,e,m,stream,p) { |
| 328 |
var out = gen_label ctx; |
| 329 |
var v = gen_variable ctx; |
| 330 |
var m = gen_matching ctx v m p stream stream out; |
| 331 |
var m = (ENext (EVars [(v,Some e)],p) m, p); |
| 332 |
(EBlock [m; (ELabel out , p)] , p) |
| 333 |
} |
| 334 |
|
| 335 |
function rec gen_constructor(ctx,tname,c,id,t,p) { |
| 336 |
var field = ident c; |
| 337 |
var printer = (EConst Ident(tname + "__string"), p); |
| 338 |
function val_type(t) { |
| 339 |
match arity t { |
| 340 |
| 0 -> |
| 341 |
var make = array [int id;printer] p; |
| 342 |
(EBinop "=" field make , p) |
| 343 |
| n -> |
| 344 |
var args = Array.list (Array.init n (function(n) { "p" + string n })); |
| 345 |
var build = array (int id :: printer :: List.map (function(a) { (EConst (Ident a) , p) }) args) p; |
| 346 |
var func = (EFunction args (EBlock [(EReturn (Some build),p)] , p) , p); |
| 347 |
(EBinop "=" field func , p) |
| 348 |
} |
| 349 |
} |
| 350 |
var export = (EBinop "=" (EField ident(ctx.module_name) c,p) field , p); |
| 351 |
(ENext val_type(t) export , p) |
| 352 |
} |
| 353 |
|
| 354 |
function rec gen_type_printer(ctx,c,t,p) { |
| 355 |
var printer = mk TConst(TModule ["Core"] TIdent("@print_union")) t_void p; |
| 356 |
var e = mk (TCall printer [ |
| 357 |
mk TConst(TString c) t_string p; |
| 358 |
mk TConst(TIdent "v") t_void p |
| 359 |
]) t_string p; |
| 360 |
e |
| 361 |
} |
| 362 |
|
| 363 |
function rec gen_type(ctx,name,t,p) { |
| 364 |
match t.texpr { |
| 365 |
| TAbstract |
| 366 |
| TMono _ |
| 367 |
| TPoly |
| 368 |
| TRecord _ |
| 369 |
| TTuple _ |
| 370 |
| TFun _ |
| 371 |
| TNamed (_,_,{ texpr = TNamed _ }) -> |
| 372 |
(EBlock [] , p) |
| 373 |
| TLink t -> |
| 374 |
gen_type ctx name t p |
| 375 |
| TNamed (name,_,t) -> |
| 376 |
function rec loop(l) { |
| 377 |
match l { |
| 378 |
| [] -> assert() |
| 379 |
| [x] -> x |
| 380 |
| _ :: l -> loop l |
| 381 |
} |
| 382 |
} |
| 383 |
gen_type ctx (loop name) t p |
| 384 |
| TUnion (_,constrs) -> |
| 385 |
var cmatch = gen_match ctx (ident "v") (MSwitch MRoot (List.map (function((c,t)) { |
| 386 |
var e = gen_type_printer ctx c t p; |
| 387 |
(TConstr c , MExecute e true) |
| 388 |
}) constrs)) false p; |
| 389 |
var printer = (EFunction ["v"] cmatch , p); |
| 390 |
var i = &(-1); |
| 391 |
var regs = List.map (function((c,t)) { i := *i + 1; gen_constructor ctx name c (*i) t p }) constrs; |
| 392 |
(EBlock ((EVars [(name + "__string",Some printer)],p) :: regs) , p) |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
function rec gen_binop(ctx,op,e1,e2,p) { |
| 397 |
function compare(op) { |
| 398 |
var cmp = (ECall (import ctx core "@compare" p) [gen_expr ctx e1; gen_expr ctx e2] , p); |
| 399 |
(EBinop op cmp int(0) , p) |
| 400 |
} |
| 401 |
function make(op) { |
| 402 |
(EBinop op gen_expr(ctx,e1) gen_expr(ctx,e2) , p) |
| 403 |
} |
| 404 |
function builtin2(op) { |
| 405 |
(ECall (builtin op) [gen_expr ctx e1; gen_expr ctx e2] , p) |
| 406 |
} |
| 407 |
match op { |
| 408 |
| "and" -> make "&" |
| 409 |
| "or" -> make "|" |
| 410 |
| "xor" -> make "+" |
| 411 |
| "==" | "!=" | ">" | "<" | ">=" | "<=" -> |
| 412 |
match comparison e1.etype { |
| 413 |
| Structural -> compare op |
| 414 |
| Native -> make op |
| 415 |
} |
| 416 |
| "===" -> (EBinop "==" builtin2("pcompare") int(0) , p) |
| 417 |
| "!==" -> (EBinop "!=" builtin2("pcompare") int(0) , p) |
| 418 |
| ":=" -> |
| 419 |
match e1.edecl { |
| 420 |
| TField _ -> make "=" |
| 421 |
| TArray (a,i) -> |
| 422 |
(ECall (import ctx core "@aset" p) [gen_expr ctx a; gen_expr ctx i; gen_expr ctx e2] , p) |
| 423 |
| _ -> |
| 424 |
(EBinop "=" (EArray gen_expr(ctx,e1) int(0),e1.epos) gen_expr(ctx,e2) , p) |
| 425 |
} |
| 426 |
| "/" when is_int e1.etype && is_int e2.etype -> |
| 427 |
builtin2 "idiv" |
| 428 |
| _ -> |
| 429 |
make op |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
function rec gen_call(ctx,rt,e,el,p) { |
| 434 |
match e.edecl { |
| 435 |
| TConst (TIdent "neko") |
| 436 |
| TConst (TModule ([],TIdent "neko")) |
| 437 |
| TConst (TModule (["Core"],TIdent "neko")) -> |
| 438 |
match el { |
| 439 |
| [{ edecl = TConst (TString s) }] -> |
| 440 |
var ch = IO.read_string (String.concat "\"" (String.split s "'")); |
| 441 |
var lex = Lexer.create Buffer.create(); |
| 442 |
Lexer.input lex p.psource ch p.pline p.pmin; |
| 443 |
fst (Neko.Parser.parse lex) |
| 444 |
| _ -> |
| 445 |
assert() |
| 446 |
} |
| 447 |
| TConst (TConstr "::") |
| 448 |
| TConst (TModule ([],TConstr "::")) |
| 449 |
| TConst (TModule (["Core"],TConstr "::")) -> |
| 450 |
match el { |
| 451 |
| [e1;e2] -> ECall (EConst (Builtin "array"),p) [int 1;import ctx core "@pcons" p;gen_expr ctx e1;gen_expr ctx e2] |
| 452 |
| _ -> assert() |
| 453 |
} |
| 454 |
| _ -> |
| 455 |
fst (call rt (gen_expr ctx e) (List.map (gen_expr ctx) el) p) |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
function rec gen_expr(ctx,e) { |
| 460 |
var p = e.epos; |
| 461 |
var e = match e.edecl { |
| 462 |
| TConst c -> fst (gen_constant ctx c p) |
| 463 |
| TBlock el -> EBlock (gen_block ctx el p) |
| 464 |
| TParenthesis e -> EParenthesis (gen_expr ctx e) |
| 465 |
| TCall (f,el) -> gen_call ctx e.etype f el p |
| 466 |
| TField (e,s) -> EArray gen_expr(ctx,e) int(record_index s e.etype) |
| 467 |
| TArray (e1,e2) -> |
| 468 |
ECall (import ctx core "@aget" p) [gen_expr ctx e1;gen_expr ctx e2] |
| 469 |
| TVar ([v],e) -> |
| 470 |
ctx.refvars := Set.remove ctx.refvars v; |
| 471 |
EVars [(v , Some (gen_expr ctx e))] |
| 472 |
| TVar (vl,e) -> |
| 473 |
var n = &(-1); |
| 474 |
EVars (("@tmp" , Some (gen_expr ctx e)) :: List.map (function(v) { |
| 475 |
ctx.refvars := Set.remove ctx.refvars v; |
| 476 |
n := *n + 1; |
| 477 |
(v , Some (EArray ident("@tmp") int(*n),p)) |
| 478 |
}) vl) |
| 479 |
| TIf (e,e1,e2) -> EIf (gen_expr ctx e) (gen_expr ctx e1) (match e2 { None -> None | Some e2 -> Some (gen_expr ctx e2) }) |
| 480 |
| TWhile (e1,e2) -> EWhile (gen_expr ctx e1) (gen_expr ctx e2) NormalWhile |
| 481 |
| TFunction (rflag,name,params,code) -> |
| 482 |
if name == "_" then |
| 483 |
EFunction (List.map fst params) block(gen_expr ctx code) |
| 484 |
else if !rflag then |
| 485 |
EVars [(name , Some (EFunction (List.map fst params) block(gen_expr ctx code), p))] |
| 486 |
else |
| 487 |
EBlock [gen_functions ctx [e] p] |
| 488 |
| TBinop (op,e1,e2) -> fst (gen_binop ctx op e1 e2 p) |
| 489 |
| TTupleDecl tl -> fst (array (List.map (gen_expr ctx) tl) p) |
| 490 |
| TTypeDecl t -> fst (gen_type ctx "<assert>" t p) |
| 491 |
| TMut e -> fst (gen_expr ctx (*e)) |
| 492 |
| TRecordDecl fl -> |
| 493 |
var fl = List.sort (function((f1,_),(f2,_)) { compare (record_index f1 e.etype) (record_index f2 e.etype)}) fl; |
| 494 |
fst(array (List.map (function((_,e)) { gen_expr ctx e }) fl) p) |
| 495 |
| TListDecl el -> |
| 496 |
fst (match el { |
| 497 |
| [] -> array [] p |
| 498 |
| x :: l -> |
| 499 |
array [gen_expr ctx x; gen_expr ctx (mk TListDecl(l) e.etype p)] p |
| 500 |
}) |
| 501 |
| TUnop (op,e) -> |
| 502 |
match op { |
| 503 |
| "-" -> EBinop "-" int(0) gen_expr(ctx,e) |
| 504 |
| "*" -> EArray gen_expr(ctx,e) int(0) |
| 505 |
| "!" -> fst (call t_void (builtin "not") [gen_expr ctx e] p) |
| 506 |
| "&" -> fst (array [gen_expr ctx e] p) |
| 507 |
| _ -> assert() |
| 508 |
} |
| 509 |
| TMatch (e,m,stream) -> |
| 510 |
fst (gen_match ctx (gen_expr ctx e) m stream p) |
| 511 |
| TTupleGet (e,n) -> |
| 512 |
EArray (gen_expr ctx e) int(n) |
| 513 |
| TErrorDecl (e,t) -> |
| 514 |
var printer = gen_expr ctx (gen_type_printer ctx e t p); |
| 515 |
var printer = (EFunction ["v"] (EBlock [printer],p) , p); |
| 516 |
var printer = (EVars [(e + "__string",Some printer)] , p); |
| 517 |
ENext printer (gen_constructor ctx e e (Hashtbl.hash (e :: ctx.current)) t p) |
| 518 |
| TTry (e,m) -> |
| 519 |
var out = gen_label ctx; |
| 520 |
var not_exc = (EBinop "!=" (call t_void (builtin "typeof") [ident "@exc"] p) builtin("tarray"),p); |
| 521 |
var type_check = (EIf not_exc (EBinop "=" ident("@exc") (call t_void (import ctx core "Neko_error" p) [ident "@exc"] p),p) None,p); |
| 522 |
var matching = gen_matching ctx "@exc" m p false true out; |
| 523 |
var reraise = call t_void (builtin "rethrow") [ident "@exc"] p; |
| 524 |
var handle = (EBlock [type_check; matching; reraise;(ELabel out , p)] , p); |
| 525 |
ETry (gen_expr ctx e) "@exc" handle |
| 526 |
}; |
| 527 |
(e,p) |
| 528 |
} |
| 529 |
|
| 530 |
function rec gen_functions(ctx,fl,p) { |
| 531 |
var ell = &(EVars (List.map (function(e) { |
| 532 |
match e.edecl { |
| 533 |
| TFunction (_,"_",params,e) -> |
| 534 |
("_" , Some (EFunction (List.map fst params) block(gen_expr ctx e),p)) |
| 535 |
| TFunction (_,name,_,_) -> |
| 536 |
ctx.refvars := Set.add ctx.refvars name; |
| 537 |
(name , Some (array [enull] p)) |
| 538 |
| _ -> |
| 539 |
assert() |
| 540 |
} |
| 541 |
}) fl) , p); |
| 542 |
List.iter (function(e) { |
| 543 |
var p = e.epos; |
| 544 |
match e.edecl { |
| 545 |
| TFunction (_,name,params,e) -> |
| 546 |
if name != "_" then { |
| 547 |
var e = gen_expr ctx e; |
| 548 |
var e = (EFunction (List.map fst params) block(e) , p); |
| 549 |
var e = (EBinop "=" (EArray ident(name) int(0),p) e, p); |
| 550 |
var e = (EBlock [e; (EBinop "=" ident(name) (EArray ident(name) int(0),p) , p)] , p); |
| 551 |
ell := (ENext (*ell) e , p); |
| 552 |
ctx.refvars := Set.remove ctx.refvars name; |
| 553 |
} |
| 554 |
| _ -> |
| 555 |
assert() |
| 556 |
} |
| 557 |
}) fl; |
| 558 |
*ell |
| 559 |
} |
| 560 |
|
| 561 |
function rec gen_block(ctx,el,p) { |
| 562 |
var old = ctx.refvars; |
| 563 |
var ell = &[]; |
| 564 |
function rec loop(fl,l) { |
| 565 |
match l { |
| 566 |
| [] -> if fl != [] then ell := gen_functions ctx (List.rev fl) p :: *ell |
| 567 |
| ({ edecl = TFunction (true,name,p,f) } as e) :: l -> loop (e :: fl) l |
| 568 |
| { edecl = TMut r } :: l -> loop fl ((*r) :: l) |
| 569 |
| x :: l -> |
| 570 |
if fl != [] then ell := gen_functions ctx (List.rev fl) p :: *ell; |
| 571 |
ell := gen_expr ctx x :: *ell; |
| 572 |
loop [] l |
| 573 |
} |
| 574 |
} |
| 575 |
loop [] el; |
| 576 |
ctx.refvars := old; |
| 577 |
List.rev (*ell) |
| 578 |
} |
| 579 |
|
| 580 |
function generate(tctx,e,deps,idents,mod) { |
| 581 |
var m = module_name mod; |
| 582 |
var ctx = { |
| 583 |
typectx = tctx; |
| 584 |
current = mod; |
| 585 |
module_name = m; |
| 586 |
counter = 0; |
| 587 |
imports = Hashtbl.create(); |
| 588 |
refvars = Set.empty(); |
| 589 |
}; |
| 590 |
var p = e.epos; |
| 591 |
var init = (EBinop "=" ident(m) builtin("exports") , p); |
| 592 |
var deps = List.map (function(m) { |
| 593 |
var file = file_name m ""; |
| 594 |
var load = (ECall (EField builtin("loader") "loadmodule",p) [gen_constant ctx (TString file) p;builtin "loader"] , p); |
| 595 |
(EBinop "=" ident(module_name m) load , p) |
| 596 |
}) deps; |
| 597 |
var exports = List.map (function(i) { |
| 598 |
(EBinop "=" (EField builtin("exports") i,p) ident(i) , p) |
| 599 |
}) idents; |
| 600 |
var el = match gen_expr ctx e { |
| 601 |
| (EBlock el, _) -> el |
| 602 |
| e -> [e] |
| 603 |
} |
| 604 |
var imports = &[]; |
| 605 |
Hashtbl.iter (function(key,id) { |
| 606 |
var e = match key { |
| 607 |
| [] -> assert() |
| 608 |
| i :: m -> |
| 609 |
(EBinop "=" ident(id) (EField ident(module_name m) i , p) , p) |
| 610 |
} |
| 611 |
imports := e :: *imports; |
| 612 |
}) ctx.imports; |
| 613 |
(EBlock (List.append (init :: List.append deps (*imports)) (List.append el exports)) , e.epos) |
| 614 |
} |