Skip to content

Commit

Permalink
neko
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Nov 22, 2024
1 parent bdaf973 commit a32fe03
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/codegen/codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,12 @@ end
Build a default safe-cast expression :
{ var $t = <e>; if( Std.is($t,<t>) ) $t else throw "Class cast error"; }
*)
let default_cast ?(vtmp="$t") com e texpr t p =
let api = com.basic in
let default_cast ?(vtmp="$t") api std e texpr t p =
let vtmp = alloc_var VGenerated vtmp e.etype e.epos in
let var = mk (TVar (vtmp,Some e)) api.tvoid p in
let vexpr = mk (TLocal vtmp) e.etype p in
let texpr = Texpr.Builder.make_typeexpr texpr p in
let is = Texpr.Builder.resolve_and_make_static_call com.std "isOfType" [vexpr;texpr] p in
let is = Texpr.Builder.resolve_and_make_static_call std "isOfType" [vexpr;texpr] p in
let enull = Texpr.Builder.make_null vexpr.etype p in
let eop = Texpr.Builder.binop OpEq vexpr enull api.tbool p in
let echeck = Texpr.Builder.binop OpBoolOr is eop api.tbool p in
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ let generate ctx tctx ext actx =
in
Genswf.generate header,"swf"
| Neko ->
Genneko.generate,"neko"
(fun com ->
Genneko.generate com.neko_lib_paths (Common.to_gctx com)
),"neko"
| Js ->
(fun com ->
Genjs.generate com.js_gen (Common.to_gctx com)
Expand Down
1 change: 1 addition & 0 deletions src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ let to_gctx com = {
| Flash -> (com.native_libs.swf_libs :> NativeLibraries.native_library_base list)
| _ -> []);
include_files = com.include_files;
std = com.std;
}

let enter_stage com stage =
Expand Down
1 change: 1 addition & 0 deletions src/generators/gctx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type t = {
resources : (string,string) Hashtbl.t;
native_libs : NativeLibraries.native_library_base list;
include_files : (string * string) list;
std : tclass; (* TODO: I would prefer to not have this here, have to check default_cast *)
}

let defined com s =
Expand Down
23 changes: 12 additions & 11 deletions src/generators/genneko.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ open Ast
open Globals
open Type
open Nast
open Common
open Gctx

type context = {
version : int;
com : Common.context;
com : Gctx.t;
packages : (string list, unit) Hashtbl.t;
globals : (string list * string, string) Hashtbl.t;
mutable curglobal : int;
Expand All @@ -50,7 +50,7 @@ let pos ctx p =
try
Hashtbl.find files p.pfile
with Not_found ->
let path = (match Common.defined ctx.com Common.Define.AbsolutePath with
let path = (match Gctx.defined ctx.com Define.AbsolutePath with
| true -> if (Filename.is_relative p.pfile)
then Filename.concat (Sys.getcwd()) p.pfile
else p.pfile
Expand Down Expand Up @@ -371,7 +371,7 @@ and gen_expr ctx e =
| TCast (e,None) ->
gen_expr ctx e
| TCast (e1,Some t) ->
gen_expr ctx (Codegen.default_cast ~vtmp:"@tmp" ctx.com e1 t e.etype e.epos)
gen_expr ctx (Codegen.default_cast ~vtmp:"@tmp" ctx.com.basic ctx.com.std e1 t e.etype e.epos)
| TIdent s ->
ident p s
| TSwitch {switch_subject = e;switch_cases = cases;switch_default = eo} ->
Expand Down Expand Up @@ -771,27 +771,28 @@ let build ctx types =
let vars = List.concat (List.map (gen_static_vars ctx) types) in
packs @ methods @ boot :: names @ inits @ vars

let generate com =
let generate neko_lib_paths com =
Hashtbl.clear files;
let ctx = new_context com (if Common.defined com Define.NekoV1 then 1 else 2) false in
let ctx = new_context com (if Gctx.defined com Define.NekoV1 then 1 else 2) false in
let libs = (EBlock
(if Common.defined com Define.NekoNoHaxelibPaths then []
else generate_libs_init com.neko_lib_paths),
(if Gctx.defined com Define.NekoNoHaxelibPaths then []
else generate_libs_init neko_lib_paths),
{ psource = "<header>"; pline = 1; }
) in
let el = build ctx com.types in
let emain = (match com.main.main_expr with None -> [] | Some e -> [gen_expr ctx e]) in
let e = (EBlock ((header()) @ libs :: el @ emain), null_pos) in
let source = Common.defined com Define.NekoSource in
let use_nekoc = Common.defined com Define.UseNekoc in
let source = Gctx.defined com Define.NekoSource in
let use_nekoc = Gctx.defined com Define.UseNekoc in
let find_file f = (com.class_paths#find_file f).file in
if not use_nekoc then begin
try
Path.mkdir_from_path com.file;
let ch = IO.output_channel (open_out_bin com.file) in
Nbytecode.write ch (Ncompile.compile ctx.version e);
IO.close_out ch;
with Ncompile.Error (msg,pos) ->
let pfile = Common.find_file com pos.psource in
let pfile = find_file pos.psource in
let rec loop p =
let pp = { pfile = pfile; pmin = p; pmax = p; } in
if Lexer.get_error_line pp >= pos.pline then
Expand Down
3 changes: 2 additions & 1 deletion src/generators/genpy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,8 @@ module Transformer = struct
let r = { a_expr with eexpr = TArrayDecl exprs } in
lift_expr ae.a_next_id ~blocks:blocks r
| (is_value, TCast(e1,Some mt)) ->
let e = Codegen.default_cast ~vtmp:(ae.a_next_id()) (match !como with Some com -> com | None -> die "" __LOC__) e1 mt ae.a_expr.etype ae.a_expr.epos in
let com = (match !como with Some com -> com | None -> die "" __LOC__) in
let e = Codegen.default_cast ~vtmp:(ae.a_next_id()) com.basic com.std e1 mt ae.a_expr.etype ae.a_expr.epos in
transform_expr ae.a_next_id ~is_value:is_value e
| (is_value, TCast(e,None)) ->
let e = trans is_value [] e in
Expand Down

0 comments on commit a32fe03

Please sign in to comment.