/* * Copyright (C)2005-2022 Haxe Foundation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ open Nekoml.Typer; function complete(s) { var l = String.length s; if l > 0 && String.get s (l - 1) != '\\' && String.get s (l-1) != '/' then s + "/" else s } function report(msg,p) { if p == Lexer.null_pos then { Stack.dump IO.stderr Stack.exc(); IO.printf IO.stderr "Exception : %s\n" msg } else IO.printf IO.stderr "%s(%d): %s\n" (Lexer.source p,Lexer.line p,msg); Sys.exit(-1); } function gen_neko(ctx,m,e) { var path , deps, idents = module_infos m; var file = Nekoml.Type.file_name path ".neko"; if *verbose then printf "Generating %s\n" file; var e = Nekoml.Neko.generate ctx e deps idents path; var ch = IO.write_file file false; var ctx = Neko.Printer.create ch; Neko.Printer.print ctx e; IO.close_out ch } function compile(ctx,m,e) { var path, deps, idents = module_infos m; var file = Nekoml.Type.file_name path ".n"; if *verbose then printf "Compiling %s\n" file; /* don't use version 1+ since this will use left-to-right evaluation for $array, leading to too much stack used with big chained lists */ var code = Neko.Compile.compile 0 (Nekoml.Neko.generate ctx e deps idents path); var ch = IO.write_file file true; Neko.Bytecode.write ch code; IO.close_out ch } function capitalize(p) { var c = String.get p 0; if c < 'a' || c > 'z' then p else { var p = String.sub p 0 (String.length p); String.set p 0 chr(ord c - ord 'a' + ord 'A'); p } } exception FileNotFound : string; try { var v = Sys.version; var year : int = neko "$neko_build_year()"; var head = "NekoML Compiler v" + v.maj + "." + v.min + "." + v.build + " - (c)2005-" + year + " Haxe Foundation\n Usage : nekoml [options] files..."; var path = &[""]; var files = &[]; var neko = &false; var std = &Some("nekoml.std"); var pack = &None; var packs = Hashtbl.create(); function use_pack(file) { var data = try IO.file_contents file catch { _ -> throw FileNotFound(file) }; Zip.init(); var h = String.unserialize (Zip.uncompress data); Hashtbl.iter (function(key,m) { if *verbose then printf "Cached %s [%d bytes]\n" (key,String.length m); Hashtbl.add packs key m }) h; } var decl = [ ("-p", Args.String (function(p) { path := complete p :: *path }) , " : additional file search path"); ("-v", Args.Void (function() { verbose := true }) , ": verbose mode"); ("-n", Args.Void (function() { neko := true }) , ": generate intermediate .neko files"); ("-pack", Args.String (function(p) { pack := Some p })," : build module packages"); ("-use", Args.String use_pack," : use this module package"); ("-nostd", Args.Void (function() { path := List.append (*path) ["core/"]; std := None; }),": disable std lib"); ]; var std_path = List.append (*path) Reflect.loader_path(); function rec loop(std,l) { match l { | [] -> if *verbose then printf "%s not found in %s\n" (std,String.concat ":" std_path) | path :: l -> try use_pack (path+std) catch { FileNotFound _ -> loop std l } } } Args.parse head decl (function(f) { files := f :: *files }); match *std { | None -> () | Some std -> loop std std_path } var ctx = context (*path) packs (function(ctx,m,e) { if *neko then gen_neko ctx m e else compile ctx m e }); List.iter (function(file) { if *verbose then printf "Compiling %s\n" file; var modname = String.split (Sys.without_extension file) "/"; var modname = match List.map capitalize modname { | "Core" :: m -> m | m -> m }; ignore(load_module ctx modname Lexer.null_pos); }) (List.rev (*files)); match *pack { | None -> () | Some file -> var h = Hashtbl.create(); Hashtbl.iter (function(key,m) { Hashtbl.add h (String.concat "." key) (IO.file_contents m.file) }) ctx.modules; Zip.init(); var ch = IO.write_file file true; IO.write ch (Zip.compress (String.serialize h) 9); IO.close_out ch; } } catch { | Neko.Lexer.Error (msg,p) -> report Neko.Lexer.error_msg(msg) p | Neko.Parser.Error (msg,p) -> report Neko.Parser.error_msg(msg) p | Neko.Compile.Error (msg,p) -> report Neko.Compile.error_msg(msg) p | Nekoml.Lexer.Error (msg,p) -> report Nekoml.Lexer.error_msg(msg) p | Nekoml.Parser.Error (msg,p) -> report Nekoml.Parser.error_msg(msg) p | Nekoml.Typer.Error (msg,p) -> report Nekoml.Typer.error_msg(msg) p | e -> report string(e) Lexer.null_pos }