diff --git a/hscript/Async.hx b/hscript/Async.hx index 4e398187..0220dc7f 100644 --- a/hscript/Async.hx +++ b/hscript/Async.hx @@ -20,8 +20,8 @@ * DEALINGS IN THE SOFTWARE. */ package hscript; -import hscript.Expr; +import hscript.expr.Expr; enum VarMode { Defined; diff --git a/hscript/Bytes.hx b/hscript/Bytes.hx index e60e33b7..d5d48dd5 100644 --- a/hscript/Bytes.hx +++ b/hscript/Bytes.hx @@ -20,7 +20,8 @@ * DEALINGS IN THE SOFTWARE. */ package hscript; -import hscript.Expr; + +import hscript.expr.Expr; class Bytes { diff --git a/hscript/Checker.hx b/hscript/Checker.hx index 1eec60ad..c2583fde 100644 --- a/hscript/Checker.hx +++ b/hscript/Checker.hx @@ -1,5 +1,6 @@ package hscript; -import hscript.Expr; + +import hscript.expr.Expr; /** This is a special type that can be used in API. diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 6730ba01..01bc33d6 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -28,14 +28,20 @@ */ package hscript; +import hscript.behaviours.*; +import hscript.expr.*; + +import hscript.utils.Tools; +import hscript.utils.UsingHandler; +import hscript.utils.UnsafeReflect; +import hscript.expr.HEnum.HEnumValue; +import hscript.expr.HEnum; +import hscript.expr.Expr; + import haxe.Exception; import haxe.ds.StringMap; -import hscript.HEnum.HEnumValue; import haxe.CallStack; -import hscript.utils.UsingHandler; -import hscript.utils.UnsafeReflect; import haxe.PosInfos; -import hscript.Expr; import haxe.Constraints.IMap; using StringTools; @@ -83,7 +89,7 @@ class RedeclaredVar { public var depth:Int; } -@:access(hscript.CustomClass) +@:access(hscript.expr.CustomClass) @:analyzer(optimize, local_dce, fusion, user_var_fusion) class Interp { private var hasScriptObject(default, null):Bool = false; @@ -1751,7 +1757,7 @@ class Interp { } // Custom Class Static Extension - @:access(hscript.CustomClassHandler) + @:access(hscript.expr.CustomClassHandler) function setCustomClassUsing(name:String, cls:CustomClassHandler) { if (usingHandler.entryExists(name)) return; diff --git a/hscript/Parser.hx b/hscript/Parser.hx index 33ae19fe..9cf23ba7 100644 --- a/hscript/Parser.hx +++ b/hscript/Parser.hx @@ -20,7 +20,8 @@ * DEALINGS IN THE SOFTWARE. */ package hscript; -import hscript.Expr; + +import hscript.expr.Expr; using StringTools; diff --git a/hscript/Printer.hx b/hscript/Printer.hx index 8861f474..3c2455ae 100644 --- a/hscript/Printer.hx +++ b/hscript/Printer.hx @@ -20,7 +20,10 @@ * DEALINGS IN THE SOFTWARE. */ package hscript; -import hscript.Expr; + +import hscript.expr.Expr; +import hscript.expr.Expr.Error; +import hscript.utils.Tools; class Printer { @@ -468,7 +471,7 @@ class Printer { return new Printer().exprToString(e); } - public static function errorToString( e : Expr.Error ):String { + public static function errorToString( e : Error ):String { var message = switch( #if hscriptPos e.e #else e #end ) { case EInvalidChar(c): "Invalid character: '"+(StringTools.isEof(c) ? "EOF (End Of File)" : String.fromCharCode(c))+"' ("+c+")"; case EUnexpected(s): "Unexpected token: \""+s+"\""; diff --git a/hscript/IHScriptAbstractBehaviour.hx b/hscript/behaviours/IHScriptAbstractBehaviour.hx similarity index 92% rename from hscript/IHScriptAbstractBehaviour.hx rename to hscript/behaviours/IHScriptAbstractBehaviour.hx index 96f8919f..d6f5ec85 100644 --- a/hscript/IHScriptAbstractBehaviour.hx +++ b/hscript/behaviours/IHScriptAbstractBehaviour.hx @@ -1,4 +1,4 @@ -package hscript; +package hscript.behaviours; // Soon... interface IHScriptAbstractBehaviour extends IHScriptCustomBehaviour { diff --git a/hscript/IHScriptCustomAccessBehaviour.hx b/hscript/behaviours/IHScriptCustomAccessBehaviour.hx similarity index 91% rename from hscript/IHScriptCustomAccessBehaviour.hx rename to hscript/behaviours/IHScriptCustomAccessBehaviour.hx index d73a24d3..ff0accaa 100644 --- a/hscript/IHScriptCustomAccessBehaviour.hx +++ b/hscript/behaviours/IHScriptCustomAccessBehaviour.hx @@ -1,4 +1,4 @@ -package hscript; +package hscript.behaviours; /** * Same Interface as IHScriptCustomBehaviour but for Property. diff --git a/hscript/IHScriptCustomBehaviour.hx b/hscript/behaviours/IHScriptCustomBehaviour.hx similarity index 94% rename from hscript/IHScriptCustomBehaviour.hx rename to hscript/behaviours/IHScriptCustomBehaviour.hx index bc94d3a1..21346660 100644 --- a/hscript/IHScriptCustomBehaviour.hx +++ b/hscript/behaviours/IHScriptCustomBehaviour.hx @@ -1,4 +1,4 @@ -package hscript; +package hscript.behaviours; /** * Special Interface for handling field access behaviour. diff --git a/hscript/IHScriptCustomClassBehaviour.hx b/hscript/behaviours/IHScriptCustomClassBehaviour.hx similarity index 90% rename from hscript/IHScriptCustomClassBehaviour.hx rename to hscript/behaviours/IHScriptCustomClassBehaviour.hx index 4d2e6833..4336b070 100644 --- a/hscript/IHScriptCustomClassBehaviour.hx +++ b/hscript/behaviours/IHScriptCustomClassBehaviour.hx @@ -1,4 +1,4 @@ -package hscript; +package hscript.behaviours; /** * Special Interface to make a class usable for Custom Classes. diff --git a/hscript/IHScriptCustomConstructor.hx b/hscript/behaviours/IHScriptCustomConstructor.hx similarity index 85% rename from hscript/IHScriptCustomConstructor.hx rename to hscript/behaviours/IHScriptCustomConstructor.hx index c65f9cb1..768f084d 100644 --- a/hscript/IHScriptCustomConstructor.hx +++ b/hscript/behaviours/IHScriptCustomConstructor.hx @@ -1,4 +1,4 @@ -package hscript; +package hscript.behaviours; /** * Special Interface for handling new instances of an object. diff --git a/hscript/CustomClass.hx b/hscript/expr/CustomClass.hx similarity index 98% rename from hscript/CustomClass.hx rename to hscript/expr/CustomClass.hx index 5ed24e27..4a4c6f59 100644 --- a/hscript/CustomClass.hx +++ b/hscript/expr/CustomClass.hx @@ -1,6 +1,10 @@ -package hscript; +package hscript.expr; +import hscript.behaviours.IHScriptCustomClassBehaviour; import hscript.utils.UnsafeReflect; +import hscript.utils.Tools; +import hscript.Interp; + import haxe.Constraints.Function; using Lambda; @@ -12,7 +16,7 @@ using Lambda; * * @author Jamextreme140 */ -@:access(hscript.CustomClassHandler) +@:access(hscript.expr.CustomClassHandler) class CustomClass implements IHScriptCustomClassBehaviour { public var className(get, never):String; diff --git a/hscript/CustomClassHandler.hx b/hscript/expr/CustomClassHandler.hx similarity index 94% rename from hscript/CustomClassHandler.hx rename to hscript/expr/CustomClassHandler.hx index 067c0d2c..faab802f 100644 --- a/hscript/CustomClassHandler.hx +++ b/hscript/expr/CustomClassHandler.hx @@ -1,10 +1,16 @@ -package hscript; +package hscript.expr; + +import hscript.behaviours.IHScriptCustomAccessBehaviour; +import hscript.behaviours.IHScriptCustomConstructor; +import hscript.behaviours.IHScriptCustomBehaviour; +import hscript.utils.Tools; +import hscript.Interp; /** * Provides handlers for static custom class fields and instantiation. */ -@:access(hscript.Property) -class CustomClassHandler implements IHScriptCustomConstructor implements IHScriptCustomAccessBehaviour{ +@:access(hscript.expr.Property) +class CustomClassHandler implements IHScriptCustomConstructor implements IHScriptCustomAccessBehaviour { public var ogInterp:Interp; public var name:String; public var fields:Array; diff --git a/hscript/Expr.hx b/hscript/expr/Expr.hx similarity index 99% rename from hscript/Expr.hx rename to hscript/expr/Expr.hx index 7ef5b66d..283e97bb 100644 --- a/hscript/Expr.hx +++ b/hscript/expr/Expr.hx @@ -19,7 +19,7 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -package hscript; +package hscript.expr; typedef Int8 = #if cpp cpp.Int8 #elseif java java.Int8 #elseif cs cs.Int8 #else Int #end; typedef Int16 = #if cpp cpp.Int16 #elseif java java.Int16 #elseif cs cs.Int16 #else Int #end; diff --git a/hscript/HEnum.hx b/hscript/expr/HEnum.hx similarity index 98% rename from hscript/HEnum.hx rename to hscript/expr/HEnum.hx index a34dd2a6..c778f8d1 100644 --- a/hscript/HEnum.hx +++ b/hscript/expr/HEnum.hx @@ -1,5 +1,6 @@ -package hscript; +package hscript.expr; +import hscript.behaviours.IHScriptCustomBehaviour; import hscript.utils.UnsafeReflect; /** diff --git a/hscript/Property.hx b/hscript/expr/Property.hx similarity index 98% rename from hscript/Property.hx rename to hscript/expr/Property.hx index fcd1355b..a8046e7b 100644 --- a/hscript/Property.hx +++ b/hscript/expr/Property.hx @@ -1,8 +1,8 @@ -package hscript; +package hscript.expr; import hscript.utils.UnsafeReflect; import hscript.Interp; -import hscript.Expr.FieldPropertyAccess; +import hscript.expr.Expr.FieldPropertyAccess; /** * Special variable that handles 'getter/setter' function calls diff --git a/hscript/macros/AbstractHandler.hx b/hscript/macros/AbstractHandler.hx index 6b97832c..e49b1b23 100644 --- a/hscript/macros/AbstractHandler.hx +++ b/hscript/macros/AbstractHandler.hx @@ -8,6 +8,8 @@ import haxe.macro.Context; import haxe.macro.Printer; import haxe.macro.Compiler; +import hscript.Config; + using StringTools; class AbstractHandler { diff --git a/hscript/macros/ClassExtendMacro.hx b/hscript/macros/ClassExtendMacro.hx index 0e52daec..b6ce15ae 100644 --- a/hscript/macros/ClassExtendMacro.hx +++ b/hscript/macros/ClassExtendMacro.hx @@ -11,6 +11,8 @@ import haxe.macro.Type.ClassField; import haxe.macro.Type.VarAccess; import haxe.macro.*; +import hscript.Config; + using StringTools; // BIG TODO: make typed classes scriptable @@ -339,7 +341,7 @@ class ClassExtendMacro { pack: cl.pack.copy(), name: cl.name }, [ - {name: "IHScriptCustomClassBehaviour", pack: ["hscript"]} + {name: "IHScriptCustomClassBehaviour", pack: ["hscript.behaviours"]} ], false, true, false); shadowClass.name = '${cl.name}$CLASS_SUFFIX'; var imports = Context.getLocalImports().copy(); @@ -484,17 +486,17 @@ class ClassExtendMacro { if(__class__fields.contains(name)) { var v:Dynamic = __interp.variables.get(name); var ba:Bool = @:privateAccess __interp.isBypassAccessor; - if(v != null && v is hscript.Property) - return cast(v, hscript.Property).get(ba); + if(v != null && v is hscript.expr.Property) + return cast(v, hscript.expr.Property).get(ba); return v; } else @:privateAccess { - var cls:hscript.CustomClass = cast __interp.__customClass.__upperClass; + var cls:hscript.expr.CustomClass = cast __interp.__customClass.__upperClass; while(cls != null) { if(cls.hasField(name)) return cls.getField(name); - var prev:hscript.CustomClass = cast cls.__upperClass; + var prev:hscript.expr.CustomClass = cast cls.__upperClass; if(prev == null) break; cls = prev; @@ -510,17 +512,17 @@ class ClassExtendMacro { if(__class__fields.contains(name)) { var v:Dynamic = __interp.variables.get(name); var ba:Bool = @:privateAccess __interp.isBypassAccessor; - if(v != null && v is hscript.Property) - return cast(v, hscript.Property).get(ba); + if(v != null && v is hscript.expr.Property) + return cast(v, hscript.expr.Property).get(ba); return v; } else @:privateAccess { - var cls:hscript.CustomClass = cast __interp.__customClass.__upperClass; + var cls:hscript.expr.CustomClass = cast __interp.__customClass.__upperClass; while(cls != null) { if(cls.hasField(name)) return cls.getField(name); - var prev:hscript.CustomClass = cast cls.__upperClass; + var prev:hscript.expr.CustomClass = cast cls.__upperClass; if(prev == null) break; cls = prev; @@ -538,18 +540,18 @@ class ClassExtendMacro { if(__class__fields.contains(name)) { var v:Dynamic = __interp.variables.get(name); var ba:Bool = @:privateAccess __interp.isBypassAccessor; - if(v != null && v is hscript.Property) - return cast(v, hscript.Property).set(val, ba); + if(v != null && v is hscript.expr.Property) + return cast(v, hscript.expr.Property).set(val, ba); __interp.variables.set(name, val); return val; } else @:privateAccess { - var cls:hscript.CustomClass = cast __interp.__customClass.__upperClass; + var cls:hscript.expr.CustomClass = cast __interp.__customClass.__upperClass; while(cls != null) { if(cls.hasField(name)) return cls.setField(name, val); - var prev:hscript.CustomClass = cast cls.__upperClass; + var prev:hscript.expr.CustomClass = cast cls.__upperClass; if(prev == null) break; cls = prev; @@ -569,18 +571,18 @@ class ClassExtendMacro { if(__class__fields.contains(name)) { var v:Dynamic = __interp.variables.get(name); var ba:Bool = @:privateAccess __interp.isBypassAccessor; - if(v != null && v is hscript.Property) - return cast(v, hscript.Property).set(val, ba); + if(v != null && v is hscript.expr.Property) + return cast(v, hscript.expr.Property).set(val, ba); __interp.variables.set(name, val); return val; } else @:privateAccess { - var cls:hscript.CustomClass = cast __interp.__customClass.__upperClass; + var cls:hscript.expr.CustomClass = cast __interp.__customClass.__upperClass; while(cls != null) { if(cls.hasField(name)) return cls.setField(name, val); - var prev:hscript.CustomClass = cast cls.__upperClass; + var prev:hscript.expr.CustomClass = cast cls.__upperClass; if(prev == null) break; cls = prev; diff --git a/hscript/Macro.hx b/hscript/macros/Macro.hx similarity index 96% rename from hscript/Macro.hx rename to hscript/macros/Macro.hx index 0ed684c6..6ccca038 100644 --- a/hscript/Macro.hx +++ b/hscript/macros/Macro.hx @@ -1,229 +1,230 @@ -/* - * Copyright (C)2008-2017 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. - */ -package hscript; -import hscript.Expr.Error; -#if hscriptPos -import hscript.Expr.ErrorDef; -#end -import haxe.macro.Expr; - -class Macro { - - var p : Position; - var binops : Map; - var unops : Map; - - public function new(pos) { - p = pos; - binops = new Map(); - unops = new Map(); - for( c in Type.getEnumConstructs(Binop) ) { - if( c == "OpAssignOp" ) continue; - var op = Type.createEnum(Binop, c); - var assign = false; - var str = switch( op ) { - case OpAdd: assign = true; "+"; - case OpMult: assign = true; "*"; - case OpDiv: assign = true; "/"; - case OpSub: assign = true; "-"; - case OpAssign: "="; - case OpEq: "=="; - case OpNotEq: "!="; - case OpGt: ">"; - case OpGte: ">="; - case OpLt: "<"; - case OpLte: "<="; - case OpAnd: assign = true; "&"; - case OpOr: assign = true; "|"; - case OpXor: assign = true; "^"; - case OpBoolAnd: "&&"; - case OpBoolOr: "||"; - case OpShl: assign = true; "<<"; - case OpShr: assign = true; ">>"; - case OpUShr: assign = true; ">>>"; - case OpMod: assign = true; "%"; - case OpAssignOp(_): ""; - case OpInterval: "..."; - case OpArrow: "=>"; - #if (haxe_ver >= 4) - case OpIn: "in"; - #end - default: - continue; - }; - binops.set(str, op); - if( assign ) - binops.set(str + "=", OpAssignOp(op)); - } - for( c in Type.getEnumConstructs(Unop) ) { - var op = Type.createEnum(Unop, c); - var str = switch( op ) { - case OpNot: "!"; - case OpNeg: "-"; - case OpNegBits: "~"; - case OpIncrement: "++"; - case OpDecrement: "--"; - #if (haxe_ver >= 4.2) - case OpSpread: continue; - #end - } - unops.set(str, op); - } - } - - function map < T, R > ( a : Array, f : T -> R ) : Array { - var b = []; - for( x in a ) - b.push(f(x)); - return b; - } - - function convertType( t : Expr.CType ) : ComplexType { - return switch( t ) { - case CTOpt(t): TOptional(convertType(t)); - case CTPath(pack, args): - var params = []; - if( args != null ) - for( t in args ) - params.push(TPType(convertType(t))); - TPath({ - pack : pack, - name : pack.pop(), - params : params, - sub : null, - }); - case CTParent(t): TParent(convertType(t)); - case CTFun(args, ret): - TFunction(map(args,convertType), convertType(ret)); - case CTNamed(name, convertType(_) => ct): - #if (haxe_ver >= 4) - TNamed(name, ct); - #else - ct; - #end - case CTAnon(fields): - var tf = []; - for( f in fields ) { - var meta = f.meta == null ? [] : [for( m in f.meta ) { name : m.name, params : m.params == null ? [] : [for( e in m.params ) convert(e)], pos : p }]; - tf.push( { name : f.name, meta : meta, doc : null, access : [], kind : FVar(convertType(f.t), null), pos : p } ); - } - TAnonymous(tf); - case CTExpr(_): - throw "assert"; - }; - } - - public function convert( e : hscript.Expr ) : Expr { - return { expr : switch( #if hscriptPos e.e #else e #end ) { - case EConst(c): - EConst(switch(c) { - case CInt(v): CInt(Std.string(v)); - case CFloat(f): CFloat(Std.string(f)); - case CString(s): CString(s); - }); - case EIdent(v): - EConst(CIdent(v)); - case EVar(n, t, e): - EVars([ { name : n, expr : if( e == null ) null else convert(e), type : if( t == null ) null else convertType(t) } ]); - case EParent(e): - EParenthesis(convert(e)); - case EBlock(el): - EBlock(map(el,convert)); - case EField(e, f): - EField(convert(e), f); - case EBinop(op, e1, e2): - var b = binops.get(op.toString()); - if( b == null ) throw EInvalidOp(op.toString()); - EBinop(b, convert(e1), convert(e2)); - case EUnop(op, prefix, e): - var opStr = op.toString(); - var u = unops.get(opStr); - if( u == null ) throw EInvalidOp(opStr); - EUnop(u, !prefix, convert(e)); - case ECall(e, params): - ECall(convert(e), map(params, convert)); - case EIf(c, e1, e2): - EIf(convert(c), convert(e1), e2 == null ? null : convert(e2)); - case EWhile(c, e): - EWhile(convert(c), convert(e), true); - case EDoWhile(c, e): - EWhile(convert(c), convert(e), false); - case EFor(v, it, efor): - #if (haxe_ver >= 4) - var p = #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end; - EFor({ expr : EBinop(OpIn,{ expr : EConst(CIdent(v)), pos : p },convert(it)), pos : p }, convert(efor)); - #else - var p = #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end; - EFor({ expr : EIn({ expr : EConst(CIdent(v)), pos : p },convert(it)), pos : p }, convert(efor)); - #end - case EBreak: - EBreak; - case EContinue: - EContinue; - case EFunction(args, e, name, ret): - var targs = []; - for( a in args ) - targs.push( { - name : a.name, - type : a.t == null ? null : convertType(a.t), - opt : false, - value : null, - }); - EFunction(#if haxe4 FNamed(name,false) #else name #end, { - params : [], - args : targs, - expr : convert(e), - ret : ret == null ? null : convertType(ret), - }); - case EReturn(e): - EReturn(e == null ? null : convert(e)); - case EArray(e, index): - EArray(convert(e), convert(index)); - case EArrayDecl(el): - EArrayDecl(map(el,convert)); - case ENew(cl, params): - var pack = cl.split("."); - ENew( { pack : pack, name : pack.pop(), params : [], sub : null }, map(params, convert)); - case EThrow(e): - EThrow(convert(e)); - case ETry(e, v, t, ec): - ETry(convert(e), [ { type : convertType(t), name : v, expr : convert(ec) } ]); - case EObject(fields): - var tf = []; - for( f in fields ) - tf.push( { field : f.name, expr : convert(f.e) } ); - EObjectDecl(tf); - case ETernary(cond, e1, e2): - ETernary(convert(cond), convert(e1), convert(e2)); - case ESwitch(e, cases, edef): - ESwitch(convert(e), [for( c in cases ) { values : [for( v in c.values ) convert(v)], expr : convert(c.expr) } ], edef == null ? null : convert(edef)); - case EMeta(m, params, esub): - var mpos = #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end; - EMeta({ name : m, params : params == null ? [] : [for( p in params ) convert(p)], pos : mpos }, convert(esub)); - case ECheckType(e, t): - ECheckType(convert(e), convertType(t)); - default: - null; - }, pos : #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end } - } - -} +/* + * Copyright (C)2008-2017 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. + */ +package hscript.macros; + +import hscript.expr.Expr.Error; +#if hscriptPos +import hscript.expr.Expr.ErrorDef; +#end +import haxe.macro.Expr; + +class Macro { + + var p : Position; + var binops : Map; + var unops : Map; + + public function new(pos) { + p = pos; + binops = new Map(); + unops = new Map(); + for( c in Type.getEnumConstructs(Binop) ) { + if( c == "OpAssignOp" ) continue; + var op = Type.createEnum(Binop, c); + var assign = false; + var str = switch( op ) { + case OpAdd: assign = true; "+"; + case OpMult: assign = true; "*"; + case OpDiv: assign = true; "/"; + case OpSub: assign = true; "-"; + case OpAssign: "="; + case OpEq: "=="; + case OpNotEq: "!="; + case OpGt: ">"; + case OpGte: ">="; + case OpLt: "<"; + case OpLte: "<="; + case OpAnd: assign = true; "&"; + case OpOr: assign = true; "|"; + case OpXor: assign = true; "^"; + case OpBoolAnd: "&&"; + case OpBoolOr: "||"; + case OpShl: assign = true; "<<"; + case OpShr: assign = true; ">>"; + case OpUShr: assign = true; ">>>"; + case OpMod: assign = true; "%"; + case OpAssignOp(_): ""; + case OpInterval: "..."; + case OpArrow: "=>"; + #if (haxe_ver >= 4) + case OpIn: "in"; + #end + default: + continue; + }; + binops.set(str, op); + if( assign ) + binops.set(str + "=", OpAssignOp(op)); + } + for( c in Type.getEnumConstructs(Unop) ) { + var op = Type.createEnum(Unop, c); + var str = switch( op ) { + case OpNot: "!"; + case OpNeg: "-"; + case OpNegBits: "~"; + case OpIncrement: "++"; + case OpDecrement: "--"; + #if (haxe_ver >= 4.2) + case OpSpread: continue; + #end + } + unops.set(str, op); + } + } + + function map < T, R > ( a : Array, f : T -> R ) : Array { + var b = []; + for( x in a ) + b.push(f(x)); + return b; + } + + function convertType( t : Expr.CType ) : ComplexType { + return switch( t ) { + case CTOpt(t): TOptional(convertType(t)); + case CTPath(pack, args): + var params = []; + if( args != null ) + for( t in args ) + params.push(TPType(convertType(t))); + TPath({ + pack : pack, + name : pack.pop(), + params : params, + sub : null, + }); + case CTParent(t): TParent(convertType(t)); + case CTFun(args, ret): + TFunction(map(args,convertType), convertType(ret)); + case CTNamed(name, convertType(_) => ct): + #if (haxe_ver >= 4) + TNamed(name, ct); + #else + ct; + #end + case CTAnon(fields): + var tf = []; + for( f in fields ) { + var meta = f.meta == null ? [] : [for( m in f.meta ) { name : m.name, params : m.params == null ? [] : [for( e in m.params ) convert(e)], pos : p }]; + tf.push( { name : f.name, meta : meta, doc : null, access : [], kind : FVar(convertType(f.t), null), pos : p } ); + } + TAnonymous(tf); + case CTExpr(_): + throw "assert"; + }; + } + + public function convert( e : hscript.Expr ) : Expr { + return { expr : switch( #if hscriptPos e.e #else e #end ) { + case EConst(c): + EConst(switch(c) { + case CInt(v): CInt(Std.string(v)); + case CFloat(f): CFloat(Std.string(f)); + case CString(s): CString(s); + }); + case EIdent(v): + EConst(CIdent(v)); + case EVar(n, t, e): + EVars([ { name : n, expr : if( e == null ) null else convert(e), type : if( t == null ) null else convertType(t) } ]); + case EParent(e): + EParenthesis(convert(e)); + case EBlock(el): + EBlock(map(el,convert)); + case EField(e, f): + EField(convert(e), f); + case EBinop(op, e1, e2): + var b = binops.get(op.toString()); + if( b == null ) throw EInvalidOp(op.toString()); + EBinop(b, convert(e1), convert(e2)); + case EUnop(op, prefix, e): + var opStr = op.toString(); + var u = unops.get(opStr); + if( u == null ) throw EInvalidOp(opStr); + EUnop(u, !prefix, convert(e)); + case ECall(e, params): + ECall(convert(e), map(params, convert)); + case EIf(c, e1, e2): + EIf(convert(c), convert(e1), e2 == null ? null : convert(e2)); + case EWhile(c, e): + EWhile(convert(c), convert(e), true); + case EDoWhile(c, e): + EWhile(convert(c), convert(e), false); + case EFor(v, it, efor): + #if (haxe_ver >= 4) + var p = #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end; + EFor({ expr : EBinop(OpIn,{ expr : EConst(CIdent(v)), pos : p },convert(it)), pos : p }, convert(efor)); + #else + var p = #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end; + EFor({ expr : EIn({ expr : EConst(CIdent(v)), pos : p },convert(it)), pos : p }, convert(efor)); + #end + case EBreak: + EBreak; + case EContinue: + EContinue; + case EFunction(args, e, name, ret): + var targs = []; + for( a in args ) + targs.push( { + name : a.name, + type : a.t == null ? null : convertType(a.t), + opt : false, + value : null, + }); + EFunction(#if haxe4 FNamed(name,false) #else name #end, { + params : [], + args : targs, + expr : convert(e), + ret : ret == null ? null : convertType(ret), + }); + case EReturn(e): + EReturn(e == null ? null : convert(e)); + case EArray(e, index): + EArray(convert(e), convert(index)); + case EArrayDecl(el): + EArrayDecl(map(el,convert)); + case ENew(cl, params): + var pack = cl.split("."); + ENew( { pack : pack, name : pack.pop(), params : [], sub : null }, map(params, convert)); + case EThrow(e): + EThrow(convert(e)); + case ETry(e, v, t, ec): + ETry(convert(e), [ { type : convertType(t), name : v, expr : convert(ec) } ]); + case EObject(fields): + var tf = []; + for( f in fields ) + tf.push( { field : f.name, expr : convert(f.e) } ); + EObjectDecl(tf); + case ETernary(cond, e1, e2): + ETernary(convert(cond), convert(e1), convert(e2)); + case ESwitch(e, cases, edef): + ESwitch(convert(e), [for( c in cases ) { values : [for( v in c.values ) convert(v)], expr : convert(c.expr) } ], edef == null ? null : convert(edef)); + case EMeta(m, params, esub): + var mpos = #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end; + EMeta({ name : m, params : params == null ? [] : [for( p in params ) convert(p)], pos : mpos }, convert(esub)); + case ECheckType(e, t): + ECheckType(convert(e), convertType(t)); + default: + null; + }, pos : #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end } + } + +} diff --git a/hscript/Tools.hx b/hscript/utils/Tools.hx similarity index 98% rename from hscript/Tools.hx rename to hscript/utils/Tools.hx index af5c28ea..1e2b2644 100644 --- a/hscript/Tools.hx +++ b/hscript/utils/Tools.hx @@ -19,8 +19,10 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -package hscript; -import hscript.Expr; +package hscript.utils; + +import hscript.behaviours.IHScriptAbstractBehaviour; +import hscript.expr.Expr; class Tools { diff --git a/hscript/utils/UsingHandler.hx b/hscript/utils/UsingHandler.hx index d4e96848..47d67d42 100644 --- a/hscript/utils/UsingHandler.hx +++ b/hscript/utils/UsingHandler.hx @@ -66,8 +66,8 @@ class UsingHandler { } ]; - @:allow(hscript.CustomClass) - @:allow(hscript.CustomClassHandler) + @:allow(hscript.expr.CustomClass) + @:allow(hscript.expr.CustomClassHandler) public var usingEntries(default, null):Map = []; public function new() {}