| 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 |
open Nekoml.Typer; |
| 24 |
|
| 25 |
function complete(s) { |
| 26 |
var l = String.length s; |
| 27 |
if l > 0 && String.get s (l - 1) != '\\' && String.get s (l-1) != '/' then |
| 28 |
s + "/" |
| 29 |
else |
| 30 |
s |
| 31 |
} |
| 32 |
|
| 33 |
function report(msg,p) { |
| 34 |
if p == Lexer.null_pos then { |
| 35 |
Stack.dump IO.stderr Stack.exc(); |
| 36 |
IO.printf IO.stderr "Exception : %s\n" msg |
| 37 |
} else |
| 38 |
IO.printf IO.stderr "%s(%d): %s\n" (Lexer.source p,Lexer.line p,msg); |
| 39 |
Sys.exit(-1); |
| 40 |
} |
| 41 |
|
| 42 |
function gen_neko(ctx,m,e) { |
| 43 |
var path , deps, idents = module_infos m; |
| 44 |
var file = Nekoml.Type.file_name path ".neko"; |
| 45 |
if *verbose then printf "Generating %s\n" file; |
| 46 |
var e = Nekoml.Neko.generate ctx e deps idents path; |
| 47 |
var ch = IO.write_file file false; |
| 48 |
var ctx = Neko.Printer.create ch; |
| 49 |
Neko.Printer.print ctx e; |
| 50 |
IO.close_out ch |
| 51 |
} |
| 52 |
|
| 53 |
function compile(ctx,m,e) { |
| 54 |
var path, deps, idents = module_infos m; |
| 55 |
var file = Nekoml.Type.file_name path ".n"; |
| 56 |
if *verbose then printf "Compiling %s\n" file; |
| 57 |
/* don't use version 1+ since this will use left-to-right evaluation for $array, |
| 58 |
leading to too much stack used with big chained lists */ |
| 59 |
var code = Neko.Compile.compile 0 (Nekoml.Neko.generate ctx e deps idents path); |
| 60 |
var ch = IO.write_file file true; |
| 61 |
Neko.Bytecode.write ch code; |
| 62 |
IO.close_out ch |
| 63 |
} |
| 64 |
|
| 65 |
function capitalize(p) { |
| 66 |
var c = String.get p 0; |
| 67 |
if c < 'a' || c > 'z' then |
| 68 |
p |
| 69 |
else { |
| 70 |
var p = String.sub p 0 (String.length p); |
| 71 |
String.set p 0 chr(ord c - ord 'a' + ord 'A'); |
| 72 |
p |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
exception FileNotFound : string; |
| 77 |
|
| 78 |
try { |
| 79 |
var v = Sys.version; |
| 80 |
var year : int = neko "$neko_build_year()"; |
| 81 |
var head = "NekoML Compiler v" + v.maj + "." + v.min + "." + v.build + " - (c)2005-" + year + " Haxe Foundation\n Usage : nekoml [options] files..."; |
| 82 |
var path = &[""]; |
| 83 |
var files = &[]; |
| 84 |
var neko = &false; |
| 85 |
var std = &Some("nekoml.std"); |
| 86 |
var pack = &None; |
| 87 |
var packs = Hashtbl.create(); |
| 88 |
function use_pack(file) { |
| 89 |
var data = try IO.file_contents file catch { _ -> throw FileNotFound(file) }; |
| 90 |
Zip.init(); |
| 91 |
var h = String.unserialize (Zip.uncompress data); |
| 92 |
Hashtbl.iter (function(key,m) { |
| 93 |
if *verbose then printf "Cached %s [%d bytes]\n" (key,String.length m); |
| 94 |
Hashtbl.add packs key m |
| 95 |
}) h; |
| 96 |
} |
| 97 |
var decl = [ |
| 98 |
("-p", Args.String (function(p) { path := complete p :: *path }) , "<path> : additional file search path"); |
| 99 |
("-v", Args.Void (function() { verbose := true }) , ": verbose mode"); |
| 100 |
("-n", Args.Void (function() { neko := true }) , ": generate intermediate .neko files"); |
| 101 |
("-pack", Args.String (function(p) { pack := Some p }),"<file> : build module packages"); |
| 102 |
("-use", Args.String use_pack,"<file> : use this module package"); |
| 103 |
("-nostd", Args.Void (function() { path := List.append (*path) ["core/"]; std := None; }),": disable std lib"); |
| 104 |
]; |
| 105 |
var std_path = List.append (*path) Reflect.loader_path(); |
| 106 |
function rec loop(std,l) { |
| 107 |
match l { |
| 108 |
| [] -> if *verbose then printf "%s not found in %s\n" (std,String.concat ":" std_path) |
| 109 |
| path :: l -> |
| 110 |
try |
| 111 |
use_pack (path+std) |
| 112 |
catch { |
| 113 |
FileNotFound _ -> loop std l |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
Args.parse head decl (function(f) { files := f :: *files }); |
| 118 |
match *std { |
| 119 |
| None -> () |
| 120 |
| Some std -> loop std std_path |
| 121 |
} |
| 122 |
var ctx = context (*path) packs (function(ctx,m,e) { if *neko then gen_neko ctx m e else compile ctx m e }); |
| 123 |
List.iter (function(file) { |
| 124 |
if *verbose then printf "Compiling %s\n" file; |
| 125 |
var modname = String.split (Sys.without_extension file) "/"; |
| 126 |
var modname = match List.map capitalize modname { |
| 127 |
| "Core" :: m -> m |
| 128 |
| m -> m |
| 129 |
}; |
| 130 |
ignore(load_module ctx modname Lexer.null_pos); |
| 131 |
}) (List.rev (*files)); |
| 132 |
match *pack { |
| 133 |
| None -> () |
| 134 |
| Some file -> |
| 135 |
var h = Hashtbl.create(); |
| 136 |
Hashtbl.iter (function(key,m) { |
| 137 |
Hashtbl.add h (String.concat "." key) (IO.file_contents m.file) |
| 138 |
}) ctx.modules; |
| 139 |
Zip.init(); |
| 140 |
var ch = IO.write_file file true; |
| 141 |
IO.write ch (Zip.compress (String.serialize h) 9); |
| 142 |
IO.close_out ch; |
| 143 |
} |
| 144 |
} catch { |
| 145 |
| Neko.Lexer.Error (msg,p) -> report Neko.Lexer.error_msg(msg) p |
| 146 |
| Neko.Parser.Error (msg,p) -> report Neko.Parser.error_msg(msg) p |
| 147 |
| Neko.Compile.Error (msg,p) -> report Neko.Compile.error_msg(msg) p |
| 148 |
| Nekoml.Lexer.Error (msg,p) -> report Nekoml.Lexer.error_msg(msg) p |
| 149 |
| Nekoml.Parser.Error (msg,p) -> report Nekoml.Parser.error_msg(msg) p |
| 150 |
| Nekoml.Typer.Error (msg,p) -> report Nekoml.Typer.error_msg(msg) p |
| 151 |
| e -> report string(e) Lexer.null_pos |
| 152 |
} |