Compare commits
22
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ad52138fe | ||
|
|
a616f2affe | ||
|
|
6721a838ca | ||
|
|
826f0184bf | ||
|
|
6c20ad3a8f | ||
|
|
fd5a2d9143 | ||
|
|
8da9ed942d | ||
|
|
f31f380602 | ||
|
|
85ea6c6f44 | ||
|
|
13bdf30055 | ||
|
|
6daea2fb5b | ||
|
|
f18c7a2210 | ||
|
|
123a7de64a | ||
|
|
bc8a59877e | ||
|
|
4347fe9305 | ||
|
|
e0e1436930 | ||
|
|
a41222328a | ||
|
|
080c441f24 | ||
|
|
d6d6e7a45e | ||
|
|
cb645d6bb1 | ||
|
|
5d58ea56c9 | ||
|
|
4170a4aafc |
@@ -1,2 +1,4 @@
|
||||
/hscript.swf
|
||||
/release.zip
|
||||
|
||||
tests/bin/*
|
||||
@@ -1,5 +1,4 @@
|
||||
--macro keep('IntIterator')
|
||||
--macro hscript.macros.UsingHandler.init()
|
||||
--macro hscript.macros.ClassExtendMacro.init()
|
||||
--macro hscript.macros.AbstractHandler.init()
|
||||
--define HSCRIPT_NEW
|
||||
|
||||
@@ -1,678 +0,0 @@
|
||||
package hscript;
|
||||
|
||||
import hscript.Expr;
|
||||
import hscript.Error;
|
||||
import hscript.Tools;
|
||||
import hscript.Parser;
|
||||
|
||||
enum AVarType {
|
||||
VUnknown;
|
||||
VVar(name:String, type:ADeclType);
|
||||
}
|
||||
|
||||
typedef ATypeInfo = {
|
||||
impl: String,
|
||||
cl: Class<Any>,
|
||||
type: AType,
|
||||
}
|
||||
|
||||
enum AType {
|
||||
TUnknown;
|
||||
TBasic(name:String);
|
||||
TAbstract(type:Class<Any>, impl:String);
|
||||
}
|
||||
|
||||
@:structInit
|
||||
class ADeclVar {
|
||||
public var name:String;
|
||||
public var type:AVarType;
|
||||
public var depth:Int;
|
||||
|
||||
public function toString():String {
|
||||
return "var " + name + " : " + type + " @ " + depth;
|
||||
}
|
||||
}
|
||||
|
||||
@:structInit
|
||||
class ADeclType {
|
||||
public var name:String;
|
||||
public var inf:ATypeInfo;
|
||||
public var depth:Int;
|
||||
|
||||
public function toString():String {
|
||||
return "type " + name + " : " + inf.type + " @ " + depth;
|
||||
}
|
||||
}
|
||||
|
||||
typedef AbstractData = {
|
||||
var impl:String;
|
||||
var funcs:Array<Array<Dynamic>>;
|
||||
var props:Array<Array<String>>;
|
||||
}
|
||||
|
||||
@:access(hscript.Parser)
|
||||
class AbstractProcessor {
|
||||
static inline function expr(e:Expr) return Tools.expr(e);
|
||||
|
||||
private static var importStackName:Array<String> = [];
|
||||
private static var importStackMode:Array<KImportMode> = [];
|
||||
private static function addImport(e:String, mode:KImportMode = INormal) {
|
||||
for(i in importStackName) if(i == e) return;
|
||||
importStackName.push(e);
|
||||
importStackMode.push(mode);
|
||||
}
|
||||
|
||||
private static function popImport(e:Expr) {
|
||||
return mk(EImport(importStackName.pop(), importStackMode.pop()), e);
|
||||
}
|
||||
|
||||
private static var depth:Int = 0;
|
||||
private static var declared:Array<ADeclVar>;
|
||||
private static var declaredTypes:Array<ADeclType>;
|
||||
private static var abstractData:Map<String, AbstractData>;
|
||||
private static var unusedImports:Map<String, String>;
|
||||
|
||||
private static function addDeclared(e:String, type:AVarType = VUnknown) {
|
||||
declared.push({
|
||||
name: e,
|
||||
type: type,
|
||||
depth: depth
|
||||
});
|
||||
}
|
||||
|
||||
private static function addTypeDeclared(e:String, inf:ATypeInfo) {
|
||||
declaredTypes.push({
|
||||
name: e,
|
||||
inf: inf,
|
||||
depth: depth
|
||||
});
|
||||
}
|
||||
|
||||
private static function getDeclared(e:String):ADeclVar {
|
||||
var len = declared.length;
|
||||
for(i in 0...len) {
|
||||
var idx = len - i - 1;
|
||||
var v = declared[idx];
|
||||
if(v.name == e) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getDeclaredType(e:String):ADeclType {
|
||||
var len = declaredTypes.length;
|
||||
for(i in 0...len) {
|
||||
var idx = len - i - 1;
|
||||
var v = declaredTypes[idx];
|
||||
if(v.name == e) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
var abstr = resolveAbstract(e);
|
||||
if(abstr != null) {
|
||||
return {
|
||||
name: getFieldName(e),
|
||||
inf: abstr,
|
||||
depth: depth
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static function removeUnusedDeclared() {
|
||||
while(declared.length > 0 && declared[declared.length - 1].depth > depth) {
|
||||
declared.pop();
|
||||
}
|
||||
while(declaredTypes.length > 0 && declaredTypes[declaredTypes.length - 1].depth > depth) {
|
||||
declaredTypes.pop();
|
||||
}
|
||||
}
|
||||
|
||||
public static function process(e:Expr):Expr {
|
||||
importStackName = [];
|
||||
importStackMode = [];
|
||||
depth = 0;
|
||||
declared = new Array();
|
||||
declaredTypes = new Array();
|
||||
abstractData = new Map();
|
||||
unusedImports = new Map();
|
||||
storedTypes = [];
|
||||
|
||||
var e = _process(e, true);
|
||||
|
||||
// Remove data
|
||||
declared = null;
|
||||
declaredTypes = null;
|
||||
abstractData = null;
|
||||
unusedImports = null;
|
||||
storedTypes = null;
|
||||
|
||||
e = removeMetaType(e);
|
||||
|
||||
// Automatically add imports for stuff
|
||||
switch(expr(e)) {
|
||||
case EBlock(exprs):
|
||||
while(importStackName.length > 0) {
|
||||
exprs.unshift(popImport(e));
|
||||
}
|
||||
return mk(EBlock(exprs), e);
|
||||
default:
|
||||
if(importStackName.length > 0) {
|
||||
var exprs = [];
|
||||
while(importStackName.length > 0) {
|
||||
exprs.unshift(popImport(e));
|
||||
}
|
||||
exprs.push(e);
|
||||
return mk(EBlock(exprs), e);
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static function removeMetaType(e:Expr) {
|
||||
if(e == null)
|
||||
return null;
|
||||
switch(Tools.expr(e)) {
|
||||
case EMeta(":$type", [expr(_) => EConst(CInt(t))], e):
|
||||
return e;
|
||||
default:
|
||||
}
|
||||
return Tools.map(e, removeMetaType);
|
||||
}
|
||||
|
||||
static function getAbstractImport(name:String, m:KImportMode = INormal):String {
|
||||
var typeInfo = resolveAbstract(name);
|
||||
if(typeInfo == null) return null;
|
||||
|
||||
var fname = getFieldName(name);
|
||||
var varName = switch(m) {
|
||||
case IAs(a): a;
|
||||
default: fname;
|
||||
}
|
||||
addTypeDeclared(varName, typeInfo);
|
||||
trace("Adding abstract " + name + " as " + varName);
|
||||
|
||||
return typeInfo.impl;
|
||||
}
|
||||
|
||||
public static function loadAbstractData(name:String, ?type:Class<Any>) {
|
||||
if(abstractData.exists(name)) return abstractData.get(name);
|
||||
|
||||
if(type == null) {
|
||||
var t = resolveClassAbstract(name);
|
||||
if(t == null) return null;
|
||||
type = t.cl;
|
||||
if(type == null) return null;
|
||||
}
|
||||
|
||||
var data:AbstractData = {
|
||||
//var t:Dynamic = Type.createInstance(type, []);
|
||||
var t:Dynamic = type;
|
||||
{
|
||||
impl: t.__hx_impl__,
|
||||
funcs: t.abstract_funcs(),
|
||||
props: t.abstract_props()
|
||||
}
|
||||
};
|
||||
|
||||
abstractData.set(name, data);
|
||||
abstractData.set(data.impl, data);
|
||||
trace("Loading abstract " + name + " as " + data);
|
||||
return data;
|
||||
}
|
||||
|
||||
static function resolveAbstract(name:String, m:KImportMode = INormal) {
|
||||
var typeInfo = resolveClassAbstract(name);
|
||||
if(typeInfo == null) return null;
|
||||
|
||||
//loadAbstractData(name, type);
|
||||
addImport(typeInfo.impl, m);
|
||||
typeInfo.type = TAbstract(typeInfo.cl, typeInfo.impl);
|
||||
return typeInfo;
|
||||
}
|
||||
|
||||
static function resolveClassAbstract(name:String):ATypeInfo {
|
||||
var cl = Type.resolveClass(name + "_HSA");
|
||||
trace("Resolving " + name + "_HSA");
|
||||
if(cl == null) {
|
||||
var spr = name.split(".");
|
||||
if(spr.length < 2)
|
||||
return null;
|
||||
spr.splice(-2, 1); // remove the last last name;
|
||||
trace("Resolving " + spr.join(".") + "_HSA");
|
||||
cl = Type.resolveClass(spr.join(".") + "_HSA");
|
||||
}
|
||||
if(cl != null) {
|
||||
var cld:Dynamic = cl;
|
||||
return {
|
||||
impl: cld.__hx_impl__,
|
||||
cl: cl,
|
||||
type: null,
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static function findAbstractFromFieldName(name:String):AType {
|
||||
if(name.indexOf(".") == -1) {
|
||||
trace("Finding abstract from field name " + name + " " + unusedImports);
|
||||
if(unusedImports.exists(name)) {
|
||||
var fullName = unusedImports.get(name);
|
||||
addImport(getAbstractImport(fullName, INormal), INormal);
|
||||
unusedImports.remove(name);
|
||||
var v = resolveAbstract(fullName, INormal);
|
||||
if(v != null)
|
||||
return v.type;
|
||||
}
|
||||
}
|
||||
|
||||
trace("Finding abstract from field name " + name);
|
||||
var v = resolveAbstract(name, INormal);
|
||||
if(v != null)
|
||||
return v.type;
|
||||
return null;
|
||||
}
|
||||
|
||||
static function getFieldName(e:String) {
|
||||
var name = e.substr(e.lastIndexOf(".") + 1);
|
||||
return name;
|
||||
}
|
||||
|
||||
private static var storedTypes:Array<AType>;
|
||||
|
||||
static function setStoredType(type:AType):Int {
|
||||
var idx = storedTypes.indexOf(type);
|
||||
if(idx != -1) return idx;
|
||||
storedTypes.push(type);
|
||||
return storedTypes.length - 1;
|
||||
}
|
||||
|
||||
static inline function getStoredType(idx:Int):AType {
|
||||
return storedTypes[idx];
|
||||
}
|
||||
|
||||
static function wrapType(e:Expr, type:AType):Expr {
|
||||
return mk(EMeta(":$type", [mk(EConst(CInt(setStoredType(type))), e)], e), e);
|
||||
}
|
||||
|
||||
private static function _process(e:Expr, top:Bool = false):Expr {
|
||||
if(e == null)
|
||||
return null;
|
||||
|
||||
// If stuff looks wrong, add this back
|
||||
//e = Tools.map(e, function(e) {
|
||||
// return _process(e, false);
|
||||
//});
|
||||
|
||||
e = endProcess(e);
|
||||
|
||||
if(e == null) return null;
|
||||
|
||||
var ge = e;
|
||||
|
||||
switch(expr(e)) {
|
||||
case EVar(vname, t, e, p, s):
|
||||
var type = null;
|
||||
switch(Tools.expr(e)) {
|
||||
case ENew(n, _):
|
||||
type = n;
|
||||
case EIdent(n): // i dont think this worksc
|
||||
// var a = Abstract;
|
||||
// var b = a; // Gets detected
|
||||
type = getDeclaredType(n).name;
|
||||
default:
|
||||
}
|
||||
|
||||
if(t != null) {
|
||||
type = Printer.convertTypeToString(t);
|
||||
}
|
||||
|
||||
if(type != null) {
|
||||
var v = getDeclaredType(type);
|
||||
if(v != null) {
|
||||
addDeclared(vname, VVar(vname, v));
|
||||
trace("Adding var " + vname + " with type " + v.name);
|
||||
//return mk(EField(mk(EIdent(v.name), e), "new", p), e);
|
||||
}
|
||||
} else {
|
||||
trace("No type for " + vname + " " + t);
|
||||
addDeclared(vname, VUnknown);
|
||||
}
|
||||
|
||||
// TODO: make this not be only inside EVar
|
||||
var e = switch(Tools.expr(e)) {
|
||||
case ENew(n, args):
|
||||
var type = getDeclaredType(n);
|
||||
if(type == null) return endProcess(ge);
|
||||
|
||||
var stype = t != null ? Printer.convertTypeToString(t) : null;
|
||||
trace("New " + n + " " + stype);
|
||||
|
||||
switch(type.inf.type) {
|
||||
case TAbstract(at, impl):
|
||||
var helper:AbstractDataHelper = new AbstractDataHelper(type.name, at);
|
||||
var constructor = helper.getFuncForConstructor();
|
||||
trace("Converting new call to " + constructor);
|
||||
_process(mk(
|
||||
ECall(
|
||||
mk(
|
||||
EField(
|
||||
mk(
|
||||
EIdent(getFieldName(impl)),
|
||||
e
|
||||
),
|
||||
constructor),
|
||||
e), args
|
||||
), e
|
||||
));
|
||||
default: null;
|
||||
}
|
||||
default: null;
|
||||
};
|
||||
if(e != null)
|
||||
return mk(EVar(vname, t, e, p, s), ge);
|
||||
case EField(e, f, p):
|
||||
var typeInfo = switch(Tools.expr(e)) {
|
||||
case EIdent(n):
|
||||
var a = getDeclared(n);
|
||||
if(a != null) {
|
||||
switch(a.type) {
|
||||
case VVar(name, type): type.inf.type;
|
||||
default: null;
|
||||
}
|
||||
} else {
|
||||
null;
|
||||
}
|
||||
case EMeta(":$type", [expr(_) => EConst(CInt(t))], _):
|
||||
getStoredType(t);
|
||||
default:
|
||||
null;
|
||||
}
|
||||
if(typeInfo == null) return endProcess(ge);
|
||||
trace("EField " + e + " " + f);
|
||||
trace("Type: " + typeInfo);
|
||||
|
||||
var e = switch(typeInfo) {
|
||||
case TAbstract(at, impl):
|
||||
var helper:AbstractDataHelper = new AbstractDataHelper(impl, at);
|
||||
|
||||
var resolve = helper.getResolveFunc();
|
||||
trace("Resolve func: " + resolve);
|
||||
if(resolve != null) { // a.b -> a.resolve(b);
|
||||
var returnType = helper.getTypeOfFunc(resolve);
|
||||
var retType = findAbstractFromFieldName(returnType);
|
||||
trace("Expr: " + Printer.toString(e));
|
||||
|
||||
var field = mk(EField(mk(EIdent(getFieldName(impl)), e), resolve, p), e);
|
||||
var call = mk(ECall(field, [e, mk(EConst(CString(f)), e)]), e);
|
||||
|
||||
if(retType != null) {
|
||||
call = wrapType(call, retType);
|
||||
}
|
||||
return endProcess(call);
|
||||
}
|
||||
|
||||
// access.has -> Access_Impl_.get_has()
|
||||
var v = helper.getProps(f);
|
||||
if(v != null) // get
|
||||
{
|
||||
var getterType = helper.getTypeOfFunc(v[0]);
|
||||
if(getterType != null) {
|
||||
trace("Getter: " + getterType);
|
||||
trace("v:" + v);
|
||||
|
||||
var retType = findAbstractFromFieldName(getterType);
|
||||
|
||||
var field = mk(EField(mk(EIdent(getFieldName(impl)), e), v[0], p), e);
|
||||
var call = mk(ECall(field, [e]), e);
|
||||
var meta = wrapType(call, retType);
|
||||
|
||||
trace("Expr: " + Printer.toString(meta));
|
||||
endProcess(meta);
|
||||
} else {
|
||||
null;
|
||||
}
|
||||
}
|
||||
else
|
||||
null;
|
||||
default: null;
|
||||
}
|
||||
|
||||
if(e != null)
|
||||
return e;
|
||||
case EImport(n, m):
|
||||
var abstractImport = getAbstractImport(n, m);
|
||||
if(abstractImport != null) {
|
||||
addImport(abstractImport, m);
|
||||
if(n == "haxe.xml.Access") {
|
||||
var toImport = [
|
||||
"haxe.xml.Access.NodeAccess",
|
||||
"haxe.xml.Access.AttribAccess",
|
||||
"haxe.xml.Access.HasAttribAccess",
|
||||
"haxe.xml.Access.HasNodeAccess",
|
||||
"haxe.xml.Access.NodeListAccess",
|
||||
];
|
||||
for(n in toImport) {
|
||||
//var m = INormal;//IAs("$" + n);
|
||||
//addImport(getAbstractImport(n, m), m);
|
||||
unusedImports.set(getFieldName(n), n);
|
||||
}
|
||||
}
|
||||
return null;//mk(abstractImport, e);
|
||||
}
|
||||
default:
|
||||
}
|
||||
|
||||
return (e);
|
||||
}
|
||||
|
||||
static function endProcess(e:Expr):Expr {
|
||||
if(e == null) return null;
|
||||
var ge = e;
|
||||
return Tools.map(e, function(e) {
|
||||
if(e == null) return mk(EBlock([]), ge); // Hacky
|
||||
switch(Tools.expr(e)) {
|
||||
case EBlock(exprs):
|
||||
depth++;
|
||||
for(i in 0...exprs.length) {
|
||||
exprs[i] = _process(exprs[i]);
|
||||
}
|
||||
while(exprs.contains(null)) exprs.remove(null);
|
||||
depth--;
|
||||
removeUnusedDeclared();
|
||||
return mk(EBlock(exprs), e);
|
||||
case EFunction(args, expr, name, a, isPublic, isStatic, isOverride):
|
||||
depth++;
|
||||
_process(expr);
|
||||
//for(i in 0...expr.length) {
|
||||
// expr[i] = _process(expr[i]);
|
||||
//}
|
||||
depth--;
|
||||
removeUnusedDeclared();
|
||||
return mk(EFunction(args, expr, name, a, isPublic, isStatic, isOverride), e);
|
||||
default:
|
||||
}
|
||||
return _process(e);
|
||||
});
|
||||
}
|
||||
|
||||
static function mk(e:ExprDef, s:Expr):Expr {
|
||||
#if hscriptPos
|
||||
return new Expr(e, s.pmin, s.pmax, s.origin, s.line);
|
||||
#else
|
||||
return e;
|
||||
#end
|
||||
}
|
||||
|
||||
public static var isStringFromCharCodeFixed(get, null):Null<Bool> = null;
|
||||
static function get_isStringFromCharCodeFixed():Null<Bool> {
|
||||
if(isStringFromCharCodeFixed == null) {
|
||||
try {
|
||||
Reflect.callMethod(null, Reflect.field(String, "fromCharCode"), [65]);
|
||||
isStringFromCharCodeFixed = true;
|
||||
} catch(e:Dynamic) {
|
||||
isStringFromCharCodeFixed = false;
|
||||
}
|
||||
}
|
||||
return isStringFromCharCodeFixed;
|
||||
}
|
||||
}
|
||||
|
||||
class AbstractDataHelper {
|
||||
public var data:AbstractData;
|
||||
|
||||
public function new(name:String, ?cls: Class<Any>) {
|
||||
data = AbstractProcessor.loadAbstractData(name, cls);
|
||||
}
|
||||
|
||||
// [name, args, ret, access, special, ?op]
|
||||
|
||||
public function getProps(fieldName:String):Array<String> {
|
||||
var prop = data.props;
|
||||
for(v in prop) {
|
||||
if(v[0] == fieldName) {
|
||||
return [v[1], v[2]];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getResolveFunc(?wantedType:Null<String>):String {
|
||||
var t = getFuncForOp("a.b", wantedType);
|
||||
if(t != null) return t;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getTypeOfFunc(f:String):String {
|
||||
var funcs = data.funcs;
|
||||
for(func in funcs) {
|
||||
if(func[0] == f) {
|
||||
return func[2];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getFuncForOp(op:String, ?wantedType:Null<String>):String {
|
||||
var funcs = data.funcs;
|
||||
for(func in funcs) {
|
||||
if(func[4] & 16 != 0) {
|
||||
if(func[5] == op) {
|
||||
if(wantedType != null) {
|
||||
var types:Array<String> = func[1].split("|");
|
||||
var wtypes:Array<String> = wantedType.split("|");
|
||||
if(types.length != wtypes.length) continue;
|
||||
var didBreak = false;
|
||||
for(i=>type in types) {
|
||||
if(type != wtypes[i]) {
|
||||
didBreak = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(didBreak) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return func[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getFuncForArrayRead(?wantedType:Null<String>):String {
|
||||
var funcs = data.funcs;
|
||||
for(func in funcs) {
|
||||
if(func[4] & 1 != 0) {
|
||||
if(wantedType != null) {
|
||||
var types:Array<String> = func[1].split("|");
|
||||
var wtypes:Array<String> = wantedType.split("|");
|
||||
if(types.length != wtypes.length) continue;
|
||||
var didBreak = false;
|
||||
for(i=>type in types) {
|
||||
if(type != wtypes[i]) {
|
||||
didBreak = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(didBreak) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return func[0];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getFuncForArrayWrite(?wantedType:Null<String>):String {
|
||||
var funcs = data.funcs;
|
||||
for(func in funcs) {
|
||||
if(func[4] & 2 != 0) {
|
||||
if(wantedType != null) {
|
||||
var types:Array<String> = func[1].split("|");
|
||||
var wtypes:Array<String> = wantedType.split("|");
|
||||
if(types.length != wtypes.length) continue;
|
||||
var didBreak = false;
|
||||
for(i=>type in types) {
|
||||
if(type != wtypes[i]) {
|
||||
didBreak = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(didBreak) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return func[0];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getFuncForConstructor(?wantedType:Null<String>):String {
|
||||
var funcs = data.funcs;
|
||||
for(func in funcs) {
|
||||
if(func[4] & 4 != 0) {
|
||||
if(wantedType != null) {
|
||||
var types:Array<String> = func[1].split("|");
|
||||
var wtypes:Array<String> = wantedType.split("|");
|
||||
if(types.length != wtypes.length) continue;
|
||||
var didBreak = false;
|
||||
for(i=>type in types) {
|
||||
if(type != wtypes[i]) {
|
||||
didBreak = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(didBreak) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return func[0];
|
||||
}
|
||||
}
|
||||
for(func in funcs) {
|
||||
if(func[0] == "_new")
|
||||
return func[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getFuncForToConversion(?wantedType:Null<String>):Array<Dynamic> {
|
||||
var funcs = data.funcs;
|
||||
for(func in funcs) {
|
||||
if(func[4] & 8 != 0) {
|
||||
if(wantedType != null) {
|
||||
var types = func[2];
|
||||
var wtypes = wantedType;
|
||||
if(types != wantedType)
|
||||
continue;
|
||||
}
|
||||
return func[0];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,10 @@ class CustomClassHandler implements IHScriptCustomConstructor {
|
||||
}
|
||||
|
||||
public function hnew(args:Array<Dynamic>):Dynamic {
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
throw "todo: do all this lmao -lunar";
|
||||
return null;
|
||||
#else
|
||||
var interp = new Interp();
|
||||
interp.errorHandler = ogInterp.errorHandler;
|
||||
|
||||
@@ -53,8 +57,15 @@ class CustomClassHandler implements IHScriptCustomConstructor {
|
||||
}
|
||||
}
|
||||
|
||||
var comparisonMap = new Map();
|
||||
for(key => value in interp.variables) {
|
||||
comparisonMap.set(key, value);
|
||||
}
|
||||
|
||||
_class.__custom__variables = interp.variables;
|
||||
|
||||
//trace(fields);
|
||||
|
||||
for(expr in fields) {
|
||||
@:privateAccess
|
||||
interp.exprReturn(expr);
|
||||
@@ -64,12 +75,27 @@ class CustomClassHandler implements IHScriptCustomConstructor {
|
||||
|
||||
_class.__interp = interp; // TODO: Remove
|
||||
interp.scriptObject = _class;
|
||||
// get only variables that were not set before
|
||||
var classVariables = [for(key => value in interp.variables) if(!comparisonMap.exists(key) || comparisonMap[key] != value) key => value];
|
||||
for(variable => value in classVariables) {
|
||||
if(variable == "this" || variable == "super") continue;
|
||||
@:privateAccess
|
||||
if(!interp.__instanceFields.contains(variable)) {
|
||||
interp.__instanceFields.push(variable);
|
||||
}
|
||||
}
|
||||
|
||||
//trace([for(key => value in classVariables) key]);
|
||||
//@:privateAccess
|
||||
//trace(interp.__instanceFields);
|
||||
|
||||
_class.__allowSetGet = false;
|
||||
|
||||
for(variable => value in interp.variables) {
|
||||
if(variable == "this") continue;
|
||||
|
||||
if(variable.startsWith("set_") || variable.startsWith("get_")) {
|
||||
_class.__allowSetGet = false;
|
||||
_class.__allowSetGet = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +104,12 @@ class CustomClassHandler implements IHScriptCustomConstructor {
|
||||
UnsafeReflect.callMethodUnsafe(null, newFunc, args);
|
||||
}
|
||||
|
||||
var newFunc = interp.variables.get("new");
|
||||
if(newFunc != null) {
|
||||
UnsafeReflect.callMethodUnsafe(null, newFunc, args);
|
||||
}
|
||||
return _class;
|
||||
#end
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
@@ -124,6 +155,6 @@ class TemplateClass implements IHScriptCustomClassBehaviour implements IHScriptC
|
||||
}
|
||||
}
|
||||
|
||||
class StaticHandler {
|
||||
final class StaticHandler {
|
||||
public function new() {}
|
||||
}
|
||||
+1
-1
@@ -62,7 +62,7 @@ enum Error_
|
||||
EInvalidEscape( s : String );
|
||||
}
|
||||
|
||||
enum abstract ErrorMessage(Int) from Int to Int {
|
||||
enum abstract ErrorMessage(Expr.UInt8) from Expr.UInt8 to Expr.UInt8 {
|
||||
final INVALID_CHAR_CODE_MULTI;
|
||||
final FROM_CHAR_CODE_NON_INT;
|
||||
final EMPTY_INTERPOLATION;
|
||||
|
||||
+221
-35
@@ -21,6 +21,16 @@
|
||||
*/
|
||||
package hscript;
|
||||
|
||||
typedef Int8 = #if cpp cpp.Int8 #else Int #end;
|
||||
typedef Int16 = #if cpp cpp.Int16 #else Int #end;
|
||||
typedef Int32 = #if cpp cpp.Int32 #else Int #end;
|
||||
typedef Int64 = #if cpp cpp.Int64 #else Int #end;
|
||||
|
||||
typedef UInt8 = #if cpp cpp.UInt8 #else Int #end;
|
||||
typedef UInt16 = #if cpp cpp.UInt16 #else Int #end;
|
||||
typedef UInt32 = #if cpp cpp.UInt32 #else Int #end;
|
||||
typedef UInt64 = #if cpp cpp.UInt64 #else Int #end;
|
||||
|
||||
enum Const {
|
||||
CInt( v : Int );
|
||||
CFloat( f : Float );
|
||||
@@ -30,12 +40,13 @@ enum Const {
|
||||
#end
|
||||
}
|
||||
|
||||
enum MapType {
|
||||
IntMap;
|
||||
StringMap;
|
||||
EnumMap;
|
||||
ObjectMap;
|
||||
UnknownMap;
|
||||
enum abstract MapType(UInt8) {
|
||||
var Null;
|
||||
var IntMap;
|
||||
var StringMap;
|
||||
var EnumMap;
|
||||
var ObjectMap;
|
||||
var UnknownMap;
|
||||
}
|
||||
|
||||
typedef Error = hscript.Error.Error_;
|
||||
@@ -54,10 +65,6 @@ class Expr {
|
||||
this.origin = origin;
|
||||
this.line = line;
|
||||
}
|
||||
|
||||
public function toString() {
|
||||
return "<Expr " + e + ">";
|
||||
}
|
||||
}
|
||||
enum ExprDef
|
||||
#else
|
||||
@@ -66,28 +73,28 @@ enum Expr
|
||||
#end
|
||||
{
|
||||
EConst( c : Const );
|
||||
EIdent( v : String );
|
||||
EVar( n : String, ?t : CType, ?e : Expr, ?isPublic : Bool, ?isStatic : Bool );
|
||||
EIdent( v : VarN );
|
||||
EVar( n : VarN, ?t : CType, ?e : Expr, ?isPublic : Bool, ?isStatic : Bool );
|
||||
EParent( e : Expr, ?noOptimize : Bool );
|
||||
EBlock( e : Array<Expr> );
|
||||
EField( e : Expr, f : String , ?safe : Bool );
|
||||
EBinop( op : String, e1 : Expr, e2 : Expr );
|
||||
EUnop( op : String, prefix : Bool, e : Expr );
|
||||
EBinop( op : Binop, e1 : Expr, e2 : Expr );
|
||||
EUnop( op : Unop, prefix : Bool, e : Expr );
|
||||
ECall( e : Expr, params : Array<Expr> );
|
||||
EIf( cond : Expr, e1 : Expr, ?e2 : Expr );
|
||||
EWhile( cond : Expr, e : Expr );
|
||||
EFor( v : String, it : Expr, e : Expr);
|
||||
EForKeyValue( v : String, it : Expr, e : Expr, ithv: String);
|
||||
EFor( v : VarN, it : Expr, e : Expr);
|
||||
EForKeyValue( v : VarN, it : Expr, e : Expr, ithv: VarN);
|
||||
EBreak;
|
||||
EContinue;
|
||||
EFunction( args : Array<Argument>, e : Expr, ?name : String, ?ret : CType, ?isPublic : Bool, ?isStatic : Bool, ?isOverride : Bool );
|
||||
EFunction( args : Array<Argument>, e : Expr, ?name : VarN, ?ret : CType, ?isPublic : Bool, ?isStatic : Bool, ?isOverride : Bool );
|
||||
EReturn( ?e : Expr );
|
||||
EArray( e : Expr, index : Expr );
|
||||
EMapDecl( type: MapType, keys: Array<Expr>, values: Array<Expr> );
|
||||
EArrayDecl( e : Array<Expr> );
|
||||
ENew( cl : String, params : Array<Expr> );
|
||||
ENew( cl : VarN, params : Array<Expr> );
|
||||
EThrow( e : Expr );
|
||||
ETry( e : Expr, v : String, t : Null<CType>, ecatch : Expr );
|
||||
ETry( e : Expr, v : VarN, t : Null<CType>, ecatch : Expr );
|
||||
EObject( fl : Array<ObjectField> );
|
||||
ETernary( cond : Expr, e1 : Expr, e2 : Expr );
|
||||
ESwitch( e : Expr, cases : Array<SwitchCase>, ?defaultExpr : Expr );
|
||||
@@ -96,12 +103,186 @@ enum Expr
|
||||
ECheckType( e : Expr, t : CType );
|
||||
|
||||
EImport( c : String, mode: KImportMode );
|
||||
EClass( name:String, fields:Array<Expr>, ?extend:String, interfaces:Array<String> );
|
||||
EClass( name:VarN, fields:Array<Expr>, ?extend:String, interfaces:Array<String> );
|
||||
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
EInfo( info: InfoClass, e: Expr );
|
||||
#end
|
||||
}
|
||||
|
||||
enum Binop {
|
||||
/**
|
||||
`+`
|
||||
**/
|
||||
OpAdd;
|
||||
|
||||
/**
|
||||
`*`
|
||||
**/
|
||||
OpMult;
|
||||
|
||||
/**
|
||||
`/`
|
||||
**/
|
||||
OpDiv;
|
||||
|
||||
/**
|
||||
`-`
|
||||
**/
|
||||
OpSub;
|
||||
|
||||
/**
|
||||
`=`
|
||||
**/
|
||||
OpAssign;
|
||||
|
||||
/**
|
||||
`==`
|
||||
**/
|
||||
OpEq;
|
||||
|
||||
/**
|
||||
`!=`
|
||||
**/
|
||||
OpNotEq;
|
||||
|
||||
/**
|
||||
`>`
|
||||
**/
|
||||
OpGt;
|
||||
|
||||
/**
|
||||
`>=`
|
||||
**/
|
||||
OpGte;
|
||||
|
||||
/**
|
||||
`<`
|
||||
**/
|
||||
OpLt;
|
||||
|
||||
/**
|
||||
`<=`
|
||||
**/
|
||||
OpLte;
|
||||
|
||||
/**
|
||||
`&`
|
||||
**/
|
||||
OpAnd;
|
||||
|
||||
/**
|
||||
`|`
|
||||
**/
|
||||
OpOr;
|
||||
|
||||
/**
|
||||
`^`
|
||||
**/
|
||||
OpXor;
|
||||
|
||||
/**
|
||||
`&&`
|
||||
**/
|
||||
OpBoolAnd;
|
||||
|
||||
/**
|
||||
`||`
|
||||
**/
|
||||
OpBoolOr;
|
||||
|
||||
/**
|
||||
`<<`
|
||||
**/
|
||||
OpShl;
|
||||
|
||||
/**
|
||||
`>>`
|
||||
**/
|
||||
OpShr;
|
||||
|
||||
/**
|
||||
`>>>`
|
||||
**/
|
||||
OpUShr;
|
||||
|
||||
/**
|
||||
`%`
|
||||
**/
|
||||
OpMod;
|
||||
|
||||
/**
|
||||
`+=` `-=` `/=` `*=` `<<=` `>>=` `>>>=` `|=` `&=` `^=` `%=`
|
||||
**/
|
||||
OpAssignOp(op:Binop);
|
||||
|
||||
/**
|
||||
`...`
|
||||
**/
|
||||
OpInterval;
|
||||
|
||||
/**
|
||||
`=>`
|
||||
**/
|
||||
OpArrow;
|
||||
|
||||
/**
|
||||
`is`
|
||||
**/
|
||||
OpIs; // used to be OpIn, but our system treats that differently
|
||||
|
||||
/**
|
||||
`??`
|
||||
**/
|
||||
OpNullCoal;
|
||||
}
|
||||
|
||||
enum abstract Unop(UInt8) {
|
||||
/**
|
||||
`++`
|
||||
**/
|
||||
var OpIncrement;
|
||||
|
||||
/**
|
||||
`--`
|
||||
**/
|
||||
var OpDecrement;
|
||||
|
||||
/**
|
||||
`!`
|
||||
**/
|
||||
var OpNot;
|
||||
|
||||
/**
|
||||
`-`
|
||||
**/
|
||||
var OpNeg;
|
||||
|
||||
/**
|
||||
`~`
|
||||
**/
|
||||
var OpNegBits;
|
||||
|
||||
/**
|
||||
`...`
|
||||
**/
|
||||
var OpSpread;
|
||||
}
|
||||
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
class InfoClass {
|
||||
public var variables:Array<String>;
|
||||
|
||||
public function new(variables:Array<String>) {
|
||||
this.variables = variables;
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
enum KImportMode {
|
||||
INormal;
|
||||
IAs( name : String );
|
||||
IAsReturn;
|
||||
IAll;
|
||||
}
|
||||
|
||||
@@ -115,20 +296,15 @@ class ObjectField {
|
||||
}
|
||||
}
|
||||
|
||||
class SwitchCase {
|
||||
@:structInit
|
||||
final class SwitchCase {
|
||||
public var values : Array<Expr>;
|
||||
public var expr : Expr;
|
||||
|
||||
public function new(values, expr) {
|
||||
this.values = values;
|
||||
this.expr = expr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//typedef Argument = { name : String, ?t : CType, ?opt : Bool, ?value : Expr };
|
||||
class Argument {
|
||||
public var name : String;
|
||||
public var name : VarN;
|
||||
public var t : Null<CType>;
|
||||
public var opt : Bool;
|
||||
public var value : Null<Expr>;
|
||||
@@ -138,6 +314,10 @@ class Argument {
|
||||
this.opt = opt;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public function toString() {
|
||||
return (opt ? "?" : "") + name + (t != null ? ":" + Printer.convertTypeToString(t) : "") + (value != null ? "=" + Printer.convertExprToString(value) : "");
|
||||
}
|
||||
}
|
||||
|
||||
typedef Metadata = Array<{ name : String, params : Array<Expr> }>;
|
||||
@@ -183,13 +363,13 @@ typedef FieldDecl = {
|
||||
var access : Array<FieldAccess>;
|
||||
}
|
||||
|
||||
enum FieldAccess {
|
||||
APublic;
|
||||
APrivate;
|
||||
AInline;
|
||||
AOverride;
|
||||
AStatic;
|
||||
AMacro;
|
||||
enum abstract FieldAccess(UInt8) {
|
||||
var APublic;
|
||||
var APrivate;
|
||||
var AInline;
|
||||
var AOverride;
|
||||
var AStatic;
|
||||
var AMacro;
|
||||
}
|
||||
|
||||
enum FieldKind {
|
||||
@@ -209,3 +389,9 @@ typedef VarDecl = {
|
||||
var expr : Null<Expr>;
|
||||
var type : Null<CType>;
|
||||
}
|
||||
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
abstract VarN(Dynamic) from Int from String to Int to String {}
|
||||
#else
|
||||
typedef VarN = String;
|
||||
#end
|
||||
+595
-124
File diff suppressed because it is too large
Load Diff
+34
-31
@@ -50,6 +50,7 @@ class Optimizer {
|
||||
newExprs.push(e);
|
||||
}
|
||||
}
|
||||
|
||||
return mk(EBlock(newExprs), s);
|
||||
|
||||
case EIf(econd, e1, e2):
|
||||
@@ -73,7 +74,7 @@ class Optimizer {
|
||||
var c2 = getBool(e2);
|
||||
|
||||
if(c1 == false && c2 == true) { // (VAR ? false : true)
|
||||
return optimize(mk(EUnop("!", true, econd), s));
|
||||
return optimize(mk(EUnop(OpNot, true, econd), s));
|
||||
}
|
||||
if(c1 == true && c2 == false) { // (VAR ? true : false)
|
||||
return optimize(mk(Tools.expr(econd), s));
|
||||
@@ -112,7 +113,7 @@ class Optimizer {
|
||||
var c2 = getBool(e2);
|
||||
|
||||
if(c1 == false && c2 == true) { // (VAR ? false : true)
|
||||
return optimize(mk(EUnop("!", true, econd), s));
|
||||
return optimize(mk(EUnop(OpNot, true, econd), s));
|
||||
}
|
||||
if(c1 == true && c2 == false) { // (VAR ? true : false)
|
||||
return optimize(mk(Tools.expr(econd), s));
|
||||
@@ -390,9 +391,9 @@ class Optimizer {
|
||||
// Possible bugs here
|
||||
if(isNumber(e1) && !isConstant(e2)) {
|
||||
var c1 = getNumber(e1);
|
||||
if(compareNumber(c1, 0) && op == "+")
|
||||
if(compareNumber(c1, 0) && op == OpAdd)
|
||||
return mk(Tools.expr(e2), s);
|
||||
if(compareNumber(c1, 1) && op == "*")
|
||||
if(compareNumber(c1, 1) && op == OpMult)
|
||||
return mk(Tools.expr(e2), s);
|
||||
//if(compareNumber(c1, 0) && op == "*")
|
||||
// return mk(convertConstant(0), s);
|
||||
@@ -400,11 +401,11 @@ class Optimizer {
|
||||
|
||||
if(!isConstant(e1) && isNumber(e2)) {
|
||||
var c2 = getNumber(e2);
|
||||
if(op == "+" && compareNumber(c2, 0))
|
||||
if(op == OpAdd && compareNumber(c2, 0))
|
||||
return mk(Tools.expr(e1), s);
|
||||
if(op == "/" && compareNumber(c2, 1))
|
||||
if(op == OpDiv && compareNumber(c2, 1))
|
||||
return mk(Tools.expr(e1), s);
|
||||
if(op == "*" && compareNumber(c2, 1))
|
||||
if(op == OpMult && compareNumber(c2, 1))
|
||||
return mk(Tools.expr(e1), s);
|
||||
//if(op == "*" && compareNumber(c2, 0))
|
||||
// return mk(convertConstant(0), s);
|
||||
@@ -418,11 +419,12 @@ class Optimizer {
|
||||
if(isConstant(e) && prefix) {
|
||||
var constant:Dynamic = getConstant(e);
|
||||
switch(op) {
|
||||
case "-": return mk(convertConstant(-constant), s);
|
||||
case "!": return mk(convertConstant(!constant), s);
|
||||
case "~":
|
||||
case OpNeg: return mk(convertConstant(-constant), s);
|
||||
case OpNot: return mk(convertConstant(!constant), s);
|
||||
case OpNegBits:
|
||||
var complement = #if (neko && !haxe3) haxe.Int32.complement(constant) #else ~constant #end;
|
||||
return mk(convertConstant(complement), s);
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
@@ -430,6 +432,7 @@ class Optimizer {
|
||||
default:
|
||||
Sys.println("Unknown expr: " + e);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -603,29 +606,29 @@ class Optimizer {
|
||||
#end
|
||||
}
|
||||
|
||||
static function optimizeOp(op, f1:Dynamic, f2:Dynamic):Dynamic {
|
||||
static function optimizeOp(op:Binop, f1:Dynamic, f2:Dynamic):Dynamic {
|
||||
//trace("Optimizing " + f1 + " " + op + " " + f2);
|
||||
return switch(op) {
|
||||
case "+": f1 + f2;
|
||||
case "-": f1 - f2;
|
||||
case "*": f1 * f2;
|
||||
case "/": f1 / f2;
|
||||
case "%": f1 % f2;
|
||||
case "&": f1 & f2;
|
||||
case "|": f1 | f2;
|
||||
case "^": f1 ^ f2;
|
||||
case "<<": f1 << f2;
|
||||
case ">>": f1 >> f2;
|
||||
case ">>>": f1 >>> f2;
|
||||
case "==": f1 == f2;
|
||||
case "!=": f1 != f2;
|
||||
case ">=": f1 >= f2;
|
||||
case "<=": f1 <= f2;
|
||||
case ">": f1 > f2;
|
||||
case "<": f1 < f2;
|
||||
case "||": f1 == true || f2 == true;
|
||||
case "&&": f1 == true && f2 == true;
|
||||
case "??": f1 == null ? f2 : f1;
|
||||
case OpAdd: f1 + f2;
|
||||
case OpSub: f1 - f2;
|
||||
case OpMult: f1 * f2;
|
||||
case OpDiv: f1 / f2;
|
||||
case OpMod: f1 % f2;
|
||||
case OpAnd: f1 & f2;
|
||||
case OpOr: f1 | f2;
|
||||
case OpXor: f1 ^ f2;
|
||||
case OpShl: f1 << f2;
|
||||
case OpShr: f1 >> f2;
|
||||
case OpUShr: f1 >>> f2;
|
||||
case OpEq: f1 == f2;
|
||||
case OpNotEq: f1 != f2;
|
||||
case OpGte: f1 >= f2;
|
||||
case OpLte: f1 <= f2;
|
||||
case OpGt: f1 > f2;
|
||||
case OpLt: f1 < f2;
|
||||
case OpBoolOr: f1 == true || f2 == true;
|
||||
case OpBoolAnd: f1 == true && f2 == true;
|
||||
case OpNullCoal: f1 == null ? f2 : f1;
|
||||
default: null;
|
||||
}
|
||||
}
|
||||
|
||||
+57
-65
@@ -224,13 +224,14 @@ class Parser {
|
||||
parseFullExpr(a);
|
||||
}
|
||||
var expr = if( a.length == 1 ) a[0] else mk(EBlock(a),0);
|
||||
expr = AbstractProcessor.process(expr);
|
||||
expr = Preprocessor.process(expr);
|
||||
if(Parser.optimize) {
|
||||
if(Parser.optimize)
|
||||
expr = Optimizer.optimize(expr);
|
||||
trace("INPUT: " + s);
|
||||
trace("OUTPUT: " + Printer.toString(expr));
|
||||
}
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
expr = Postprocessor.processvars(expr);
|
||||
#end
|
||||
// trace("INPUT: " + s); // useful for debugging
|
||||
//trace("OUTPUT: " + Printer.toString(expr));
|
||||
return expr;
|
||||
}
|
||||
|
||||
@@ -399,10 +400,8 @@ class Parser {
|
||||
function parseExprFromTokens(t:TokenList) {
|
||||
var oldPos = readPos;
|
||||
#if hscriptPos
|
||||
var _oldTokenMin = tokenMin;
|
||||
var _oldTokenMax = tokenMax;
|
||||
var oldOldTokenMin = oldTokenMin;
|
||||
var oldOldTokenMax = oldTokenMax;
|
||||
var oldTokenMin = tokenMin;
|
||||
var oldTokenMax = tokenMax;
|
||||
#end
|
||||
var oldTokens = tokens;
|
||||
|
||||
@@ -410,11 +409,7 @@ class Parser {
|
||||
// unsure about these
|
||||
#if hscriptPos
|
||||
tokenMin = 0;
|
||||
//tokenMax = t.length - 1;
|
||||
tokenMax = 0;
|
||||
oldTokenMin = 0;
|
||||
oldTokenMax = 0;
|
||||
//oldTokenMax = t.length - 1;
|
||||
tokenMax = t.length - 1;
|
||||
#end
|
||||
readPos = 0;
|
||||
//trace(t.map(getTk).map(tokenString));
|
||||
@@ -425,8 +420,6 @@ class Parser {
|
||||
#if hscriptPos
|
||||
tokenMin = oldTokenMin;
|
||||
tokenMax = oldTokenMax;
|
||||
oldTokenMin = oldOldTokenMin;
|
||||
oldTokenMax = oldOldTokenMax;
|
||||
#end
|
||||
readPos = oldPos;
|
||||
return e;
|
||||
@@ -474,7 +467,7 @@ class Parser {
|
||||
}
|
||||
default:
|
||||
}
|
||||
e = mk(EBinop("+", e, er));
|
||||
e = mk(EBinop(OpAdd, e, er));
|
||||
//e = mk(EParent(e));
|
||||
}
|
||||
while(tg.length > 0) {
|
||||
@@ -613,9 +606,8 @@ class Parser {
|
||||
var tmp2 = "__m_" + (uid++);
|
||||
var e = mk(EBlock([
|
||||
// TODO: Make it detect simple map comprehensions so we can optimize the map type
|
||||
// TODO: make it detect the type from nextType
|
||||
mk(EVar(tmp, parseTypeString("haxe.ds.ObjectMap<Dynamic, Dynamic>"), mk(EMapDecl(ObjectMap, [], []), p1)), p1), // Assume ObjectMap, since it supports dynamic keys
|
||||
mk(EVar(tmp2, parseTypeString("(Dynamic, Dynamic) -> Void"), mk(EField(mk(EIdent(tmp), p1), "set"), p1)), p1),
|
||||
mk(EVar(tmp, null, mk(EMapDecl(ObjectMap, [], []), p1)), p1), // Assume ObjectMap, since it supports dynamic keys
|
||||
mk(EVar(tmp2, null, mk(EField(mk(EIdent(tmp), p1), "set"), p1)), p1),
|
||||
mapMapCompr(tmp2, a[0]),
|
||||
mk(EIdent(tmp),p1),
|
||||
]),p1);
|
||||
@@ -625,8 +617,8 @@ class Parser {
|
||||
var tmp = "__a_" + (uid++);
|
||||
var tmp2 = "__a_" + (uid++);
|
||||
var e = mk(EBlock([
|
||||
mk(EVar(tmp, parseTypeString("Array<Dynamic>"), mk(EArrayDecl([]), p1)), p1),
|
||||
mk(EVar(tmp2, parseTypeString("(Dynamic) -> Int"), mk(EField(mk(EIdent(tmp), p1), "push"), p1)), p1),
|
||||
mk(EVar(tmp, null, mk(EArrayDecl([]), p1)), p1),
|
||||
mk(EVar(tmp2, null, mk(EField(mk(EIdent(tmp), p1), "push"), p1)), p1),
|
||||
mapArrCompr(tmp2, a[0]),
|
||||
mk(EIdent(tmp),p1),
|
||||
]),p1);
|
||||
@@ -638,7 +630,7 @@ class Parser {
|
||||
var isTypeMap = (nextType != null) && nextType.match(CTPath(["Map"], [_, _]));
|
||||
var isMap = isTypeMap;
|
||||
if(!isMap) {
|
||||
isMap = Lambda.exists(a, (e) -> Tools.expr(e).match(EBinop("=>", _))); // Check if any element is a => b
|
||||
isMap = Lambda.exists(a, (e) -> Tools.expr(e).match(EBinop(OpArrow, _))); // Check if any element is a => b
|
||||
}
|
||||
if(isMap) {
|
||||
// TODO: clean up this code more
|
||||
@@ -650,7 +642,7 @@ class Parser {
|
||||
var values:Array<Expr> = [];
|
||||
for (e in a) {
|
||||
switch (Tools.expr(e)) {
|
||||
case EBinop("=>", eKey, eValue): {
|
||||
case EBinop(OpArrow, eKey, eValue): {
|
||||
switch(Tools.expr(eKey)) {
|
||||
case EConst(CInt(_)):// | EConst(CFloat(_)):
|
||||
isKeyInt = true;
|
||||
@@ -684,14 +676,14 @@ class Parser {
|
||||
|
||||
var t = b2i(isKeyString) + b2i(isKeyInt) + b2i(isKeyObject) + b2i(isKeyEnum);
|
||||
|
||||
var type:MapType = null;
|
||||
var type:MapType = Null;
|
||||
if(t != 1) type = UnknownMap;
|
||||
else if(isKeyInt) type = IntMap;
|
||||
else if(isKeyString) type = StringMap;
|
||||
else if(isKeyEnum) type = EnumMap;
|
||||
else if(isKeyObject) type = ObjectMap;
|
||||
|
||||
if(type == null)
|
||||
if(type == Null)
|
||||
error(EPreset(UNKNOWN_MAP_TYPE), p1, p1);
|
||||
|
||||
return parseExprNext(mk(EMapDecl(type, keys, values), p1));
|
||||
@@ -705,7 +697,12 @@ class Parser {
|
||||
}
|
||||
}
|
||||
|
||||
#if cpp
|
||||
static inline function b2i(b:Bool) return untyped __cpp__("({0} ? 1 : 0)", b);
|
||||
//static inline function b2i(b:Bool) return untyped __cpp__("(int)({0})", b);
|
||||
#else
|
||||
static inline function b2i(b:Bool) return b ? 1 : 0;
|
||||
#end
|
||||
|
||||
function parseLambda( args : Array<Argument>, pmin ) {
|
||||
while( true ) {
|
||||
@@ -800,7 +797,7 @@ class Parser {
|
||||
default:
|
||||
// tmp.set(k, v);
|
||||
switch( Tools.expr(e) ) {
|
||||
case EBinop("=>", e1, e2):
|
||||
case EBinop(OpArrow, e1, e2):
|
||||
//ECall( mk(EField(mk(EIdent(tmp), pmin(e), pmax(e)), "set"), pmin(e), pmax(e)), [e1, e2]);
|
||||
ECall( mk(EIdent(tmp), pmin(e), pmax(e)), [e1, e2]);
|
||||
default: // default incase of error
|
||||
@@ -833,37 +830,48 @@ class Parser {
|
||||
isMapCompr(e2);
|
||||
default:
|
||||
// tmp.set(k, v);
|
||||
return Tools.expr(e).match(EBinop("=>", _));
|
||||
return Tools.expr(e).match(EBinop(OpArrow, _));
|
||||
}
|
||||
}
|
||||
|
||||
function makeUnop( op, e ) {
|
||||
function makeUnop( op:String, e:Expr ):Expr {
|
||||
var op = Tools.getUnopEnum(op);
|
||||
return _makeUnop(op, e);
|
||||
}
|
||||
|
||||
function _makeUnop( op:Unop, e:Expr ):Expr {
|
||||
if( e == null && resumeErrors )
|
||||
return null;
|
||||
return switch( Tools.expr(e) ) {
|
||||
case EBinop(bop, e1, e2): mk(EBinop(bop, makeUnop(op, e1), e2), pmin(e1), pmax(e2));
|
||||
case ETernary(e1, e2, e3): mk(ETernary(makeUnop(op, e1), e2, e3), pmin(e1), pmax(e3));
|
||||
case EBinop(bop, e1, e2): mk(EBinop(bop, _makeUnop(op, e1), e2), pmin(e1), pmax(e2));
|
||||
case ETernary(e1, e2, e3): mk(ETernary(_makeUnop(op, e1), e2, e3), pmin(e1), pmax(e3));
|
||||
default: mk(EUnop(op,true,e),pmin(e),pmax(e));
|
||||
}
|
||||
}
|
||||
|
||||
function makeBinop( op, e1, e ) {
|
||||
function makeBinop( op:String, e1:Expr, e:Expr ) {
|
||||
// TODO: clean this up
|
||||
if(!Tools.isValidBinOp(op))
|
||||
error(EInvalidOp(op),pmin(e1),pmax(e1));
|
||||
var op = Tools.getOpEnum(op);
|
||||
return _makeBinop(op, e1, e);
|
||||
}
|
||||
|
||||
function _makeBinop( op:Binop, e1:Expr, e:Expr ) {
|
||||
// TODO: clean this up
|
||||
if( e == null && resumeErrors )
|
||||
return mk(EBinop(op,e1,e),pmin(e1),pmax(e1));
|
||||
return switch( Tools.expr(e) ) {
|
||||
case EBinop(op2,e2,e3):
|
||||
if( opPriority.get(op) <= opPriority.get(op2) && !opRightAssoc.exists(op) )
|
||||
mk(EBinop(op2,makeBinop(op,e1,e2),e3),pmin(e1),pmax(e3));
|
||||
if( opPriority.get(Printer.getBinaryOp(op)) <= opPriority.get(Printer.getBinaryOp(op2)) && !opRightAssoc.exists(Printer.getBinaryOp(op)) )
|
||||
mk(EBinop(op2,_makeBinop(op,e1,e2),e3),pmin(e1),pmax(e3));
|
||||
else
|
||||
mk(EBinop(op, e1, e), pmin(e1), pmax(e));
|
||||
case ETernary(e2,e3,e4):
|
||||
if( opRightAssoc.exists(op) )
|
||||
if( opRightAssoc.exists(Printer.getBinaryOp(op)) )
|
||||
mk(EBinop(op,e1,e),pmin(e1),pmax(e));
|
||||
else
|
||||
mk(ETernary(makeBinop(op, e1, e2), e3, e4), pmin(e1), pmax(e));
|
||||
mk(ETernary(_makeBinop(op, e1, e2), e3, e4), pmin(e1), pmax(e));
|
||||
default:
|
||||
mk(EBinop(op,e1,e),pmin(e1),pmax(e));
|
||||
}
|
||||
@@ -1177,10 +1185,12 @@ class Parser {
|
||||
|
||||
var fields = [];
|
||||
ensure(TBrOpen);
|
||||
while( !maybe(TBrClose) ) {
|
||||
if(token() == TSemicolon) continue;
|
||||
var a = parseExpr();
|
||||
fields.push(a);
|
||||
while( true ) {
|
||||
parseFullExpr(fields);
|
||||
tk = token();
|
||||
if( tk == TBrClose || (resumeErrors && tk == TEof) )
|
||||
break;
|
||||
push(tk);
|
||||
}
|
||||
|
||||
var tk = token();
|
||||
@@ -1234,7 +1244,7 @@ class Parser {
|
||||
var tk = token();
|
||||
switch( tk ) {
|
||||
case TId("case"):
|
||||
var c = new SwitchCase([], null);
|
||||
var c:SwitchCase = { values : [], expr : null };
|
||||
cases.push(c);
|
||||
disableOrOp = true;
|
||||
while( true ) {
|
||||
@@ -1337,7 +1347,7 @@ class Parser {
|
||||
push(tk);
|
||||
return e1;
|
||||
}
|
||||
return parseExprNext(mk(EUnop(op,false,e1),pmin(e1)));
|
||||
return parseExprNext(mk(EUnop(Tools.getUnopEnum(op),false,e1),pmin(e1)));
|
||||
}
|
||||
return makeBinop(op,e1,parseExpr());
|
||||
case TDot | TQuestionDot:
|
||||
@@ -1431,27 +1441,8 @@ class Parser {
|
||||
return path;
|
||||
}
|
||||
|
||||
static var typeCache:Map<String, CType> = [];
|
||||
|
||||
function parseTypeString(str:String) {
|
||||
if(typeCache.exists(str))
|
||||
return typeCache.get(str);
|
||||
|
||||
var parser = new Parser();
|
||||
parser.initParser("__INTERNAL_TYPE_PARSER__:" + origin);
|
||||
parser.line = line;
|
||||
parser.input = str;
|
||||
parser.readPos = 0;
|
||||
var type = parser.parseType();
|
||||
|
||||
typeCache.set(str, type);
|
||||
return type;
|
||||
}
|
||||
|
||||
function parseType() : CType {
|
||||
var t = token();
|
||||
trace("Token: " + tokenString(t));
|
||||
if( t == TEof ) return null;
|
||||
switch( t ) {
|
||||
case TId(v):
|
||||
push(t);
|
||||
@@ -2101,6 +2092,7 @@ class Parser {
|
||||
oldTokenMax = tokenMax;
|
||||
tokenMin = (this.char < 0) ? readPos : readPos - 1;
|
||||
var t = _token();
|
||||
//trace(t, infos);
|
||||
//ttrace(t, infos);
|
||||
tokenMax = (this.char < 0) ? readPos - 1 : readPos - 2;
|
||||
return t;
|
||||
@@ -2487,7 +2479,7 @@ class Parser {
|
||||
}
|
||||
mk(EIdent(id), tokenMin, tokenMax);
|
||||
case TOp("!"):
|
||||
mk(EUnop("!", true, parsePreproCond()), tokenMin, tokenMax);
|
||||
mk(EUnop(OpNot, true, parsePreproCond()), tokenMin, tokenMax);
|
||||
default:
|
||||
unexpected(tk);
|
||||
}
|
||||
@@ -2505,13 +2497,13 @@ class Parser {
|
||||
error(EInvalidPreprocessor("Can't eval " + Tools.expr(e).getName() + " with " + Tools.expr(e2).getName()), readPos, readPos);
|
||||
return false;
|
||||
}
|
||||
case EUnop("!", _, e):
|
||||
case EUnop(OpNot, _, e):
|
||||
return !evalPreproCond(e);
|
||||
case EParent(e):
|
||||
return evalPreproCond(e);
|
||||
case EBinop("&&", e1, e2):
|
||||
case EBinop(OpBoolAnd, e1, e2):
|
||||
return evalPreproCond(e1) && evalPreproCond(e2);
|
||||
case EBinop("||", e1, e2):
|
||||
case EBinop(OpBoolOr, e1, e2):
|
||||
return evalPreproCond(e1) || evalPreproCond(e2);
|
||||
default:
|
||||
error(EInvalidPreprocessor("Can't eval " + Tools.expr(e).getName()), readPos, readPos);
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package hscript;
|
||||
|
||||
import hscript.Expr;
|
||||
import hscript.Error;
|
||||
import hscript.Tools;
|
||||
import hscript.Parser;
|
||||
|
||||
@:access(hscript.Parser)
|
||||
class Postprocessor {
|
||||
static inline function expr(e:Expr) return Tools.expr(e);
|
||||
|
||||
static function mk(e:ExprDef, s:Expr):Expr {
|
||||
#if hscriptPos
|
||||
return new Expr(e, s.pmin, s.pmax, s.origin, s.line);
|
||||
#else
|
||||
return e;
|
||||
#end
|
||||
}
|
||||
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
public static function processvars(e:Expr, ?vars:Array<String>) {
|
||||
if(vars == null) vars = [];
|
||||
for(v in getvars(e)) vars.push(v);
|
||||
e = _processvars(e, vars);
|
||||
|
||||
return mk(EInfo(new InfoClass(vars), e), e);
|
||||
}
|
||||
|
||||
private static function _processvars(e:Expr, vars:Array<String>):Expr {
|
||||
if(e == null) return null;
|
||||
var ge = e;
|
||||
var doExtra = true;
|
||||
var e = switch (expr(e)) {
|
||||
case EIdent(v) if (v is String):
|
||||
mk(EIdent(vars.indexOf(v)), e);
|
||||
case EVar(n, t, _e, p, s) if (n is String):
|
||||
mk(EVar(vars.indexOf(n), t, _e, p, s), e);
|
||||
case EImport(c, IAs(n)) if (n is String):
|
||||
mk(EVar(vars.indexOf(n), null, mk(EImport(c, IAsReturn), e), false, false), e);
|
||||
case EFunction(args, _e, n, r, p, s, o) if (n is String):
|
||||
var _args:Array<Argument> = [
|
||||
for (arg in args)
|
||||
new Argument(vars.indexOf(arg.name), arg.t, arg.opt, arg.value)
|
||||
];
|
||||
mk(EFunction(_args, _e, vars.indexOf(n), r, p, s, o), e);
|
||||
case EFunction(args, _e, null, r, p, s, o):
|
||||
var _args:Array<Argument> = [
|
||||
for (arg in args)
|
||||
new Argument(vars.indexOf(arg.name), arg.t, arg.opt, arg.value)
|
||||
];
|
||||
mk(EFunction(_args, _e, null, r, p, s, o), e);
|
||||
case ENew(cl, p) if (cl is String):
|
||||
var p = [for(arg in p) arg];
|
||||
mk(ENew(vars.indexOf(cl), p), e);
|
||||
case EClass(n, fls, extnd, i) if (n is String):
|
||||
var fls:Array<Expr> = [for(f in fls) f];
|
||||
mk(EClass(vars.indexOf(n), fls, extnd, i), e);
|
||||
case ETry(_e, v, t, ec) if (v is String):
|
||||
mk(ETry(_e, vars.indexOf(v), t, ec), e);
|
||||
case EFor(v, it, _e) if (v is String):
|
||||
mk(EFor(vars.indexOf(v), it, _e), e);
|
||||
case EForKeyValue(v, it, _e, ithv) if (v is String && ithv is String):
|
||||
mk(EForKeyValue(vars.indexOf(v), it, _e, vars.indexOf(ithv)), e);
|
||||
default: {
|
||||
doExtra = false;
|
||||
e;
|
||||
}
|
||||
};
|
||||
|
||||
e = Tools.map(e, function(e) {
|
||||
return _processvars(e, vars);
|
||||
});
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
public static function getvars(se:Expr, ?vars:Array<String>):Array<String> {
|
||||
inline function pushVar(v:VarN) {
|
||||
if(v is String) {
|
||||
var v:String = cast v;
|
||||
if (!vars.contains(v))
|
||||
vars.push(v);
|
||||
}
|
||||
};
|
||||
|
||||
if(vars == null) vars = [];
|
||||
Tools.iterExprRecursive(se, (e) -> {
|
||||
switch (expr(e)) {
|
||||
case EVar(n, t, e, _,_): pushVar(n);
|
||||
case EIdent(v): pushVar(v);
|
||||
case EFunction(args,e,name,_,_,_,_):
|
||||
pushVar(name);
|
||||
for (a in args) {
|
||||
pushVar(a.name);
|
||||
}
|
||||
case EFor(v,it,e): pushVar(v);
|
||||
case EForKeyValue(v,it,e,ithv): pushVar(ithv); pushVar(v);
|
||||
case ETry(e, v, _, ec): pushVar(v);
|
||||
case ENew(cl, params): pushVar(cl);
|
||||
case EClass(name, fls,_,_): pushVar(name);
|
||||
default:
|
||||
}
|
||||
});
|
||||
return vars;
|
||||
}
|
||||
#end
|
||||
}
|
||||
+78
-13
@@ -78,11 +78,14 @@ class Printer {
|
||||
type(t);
|
||||
case CTFun(args, ret) if (Lambda.exists(args, function (a) return a.match(CTNamed(_, _)))):
|
||||
add('(');
|
||||
for (a in args)
|
||||
var first = true;
|
||||
for (i=>a in args) {
|
||||
switch a {
|
||||
case CTNamed(_, _): type(a);
|
||||
default: type(CTNamed('_', a));
|
||||
}
|
||||
if( i != args.length - 1 ) add(", ");
|
||||
}
|
||||
add(')->');
|
||||
type(ret);
|
||||
case CTFun(args, ret):
|
||||
@@ -201,6 +204,59 @@ class Printer {
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
function getVar(v:VarN) {
|
||||
if( v == null ) {
|
||||
return ("??NULL??");
|
||||
}
|
||||
|
||||
if(v is Int) {
|
||||
return ("$" + v);
|
||||
} else {
|
||||
return (v);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getBinaryOp(op:Binop) {
|
||||
return switch(op) {
|
||||
case OpAdd: "+";
|
||||
case OpSub: "-";
|
||||
case OpMult: "*";
|
||||
case OpDiv: "/";
|
||||
case OpMod: "%";
|
||||
case OpAnd: "&";
|
||||
case OpOr: "|";
|
||||
case OpXor: "^";
|
||||
case OpShl: "<<";
|
||||
case OpShr: ">>";
|
||||
case OpUShr: ">>>";
|
||||
case OpEq: "==";
|
||||
case OpNotEq: "!=";
|
||||
case OpGt: ">";
|
||||
case OpGte: ">=";
|
||||
case OpLt: "<";
|
||||
case OpLte: "<=";
|
||||
case OpBoolAnd: "&&";
|
||||
case OpBoolOr: "||";
|
||||
case OpIs: "is";
|
||||
case OpNullCoal: "??";
|
||||
case OpAssign: "=";
|
||||
case OpArrow: "=>";
|
||||
case OpInterval: "...";
|
||||
case OpAssignOp(op): getBinaryOp(op) + "=";
|
||||
}
|
||||
}
|
||||
|
||||
public static function getUnaryOp(op:Unop) {
|
||||
return switch(op) {
|
||||
case OpIncrement: "++";
|
||||
case OpDecrement: "--";
|
||||
case OpNot: "!";
|
||||
case OpNeg: "-";
|
||||
case OpNegBits: "~";
|
||||
case OpSpread: "...";
|
||||
}
|
||||
}
|
||||
|
||||
function expr( e : Expr ) {
|
||||
if( e == null ) {
|
||||
add("??NULL??");
|
||||
@@ -218,7 +274,7 @@ class Printer {
|
||||
default:
|
||||
}
|
||||
case EClass(name, fields, extend, interfaces):
|
||||
add('class $name');
|
||||
add('class ${getVar(name)}');
|
||||
if (extend != null)
|
||||
add(' extends $extend');
|
||||
for(_interface in interfaces) {
|
||||
@@ -243,9 +299,9 @@ class Printer {
|
||||
add('"');
|
||||
}
|
||||
case EIdent(v):
|
||||
add(v);
|
||||
add(getVar(v));
|
||||
case EVar(n, t, e): // TODO: static, public, override
|
||||
add("var " + n);
|
||||
add("var " + getVar(n));
|
||||
if( t != null )
|
||||
addType(t);
|
||||
else
|
||||
@@ -258,6 +314,7 @@ class Printer {
|
||||
case EnumMap: add("EnumValue");
|
||||
case IntMap: add("Int");
|
||||
case UnknownMap: add("?");
|
||||
case Null: add("Null");
|
||||
}
|
||||
add(", Dynamic>");
|
||||
default:
|
||||
@@ -287,8 +344,7 @@ class Printer {
|
||||
expr(e);
|
||||
add((s == true ? "?." : ".") + f);
|
||||
case EBinop(op, e1, e2):
|
||||
var shouldParen = false;
|
||||
var op1 = switch(Tools.expr(e1)) {
|
||||
/*var op1 = switch(Tools.expr(e1)) {
|
||||
case EBinop(op, _, _): op;
|
||||
case EConst(_): "_";
|
||||
case EIdent(_): "_";
|
||||
@@ -300,7 +356,10 @@ class Printer {
|
||||
case EIdent(_): "_";
|
||||
default: null;
|
||||
}
|
||||
var paran = Tools.checkOpPrecedence(op, op1, op2);
|
||||
var paran = Tools.checkOpPrecedence(op, op1, op2);*/
|
||||
var paran = 2;
|
||||
|
||||
var op = getBinaryOp(op);
|
||||
|
||||
if(paran == 0 || paran == 2) {
|
||||
add("(");
|
||||
@@ -321,6 +380,8 @@ class Printer {
|
||||
expr(e2);
|
||||
}
|
||||
case EUnop(op, pre, e):
|
||||
var op = getUnaryOp(op);
|
||||
|
||||
if( pre ) {
|
||||
add(op);
|
||||
expr(e);
|
||||
@@ -369,12 +430,12 @@ class Printer {
|
||||
expr(cond);
|
||||
add(" )");
|
||||
case EFor(v, it, e):
|
||||
add("for( "+v+" in ");
|
||||
add("for( "+getVar(v)+" in ");
|
||||
expr(it);
|
||||
add(" )");
|
||||
block(e, true);
|
||||
case EForKeyValue(v, it, e, ithv):
|
||||
add("for( "+ithv+" => "+v+" in ");
|
||||
add("for( "+getVar(ithv)+" => "+getVar(v)+" in ");
|
||||
expr(it);
|
||||
add(" )");
|
||||
block(e, true);
|
||||
@@ -385,13 +446,13 @@ class Printer {
|
||||
case EFunction(params, e, name, ret): // TODO: static, public, override
|
||||
add("function");
|
||||
if( name != null )
|
||||
add(" " + name);
|
||||
add(" " + getVar(name));
|
||||
add("(");
|
||||
var first = true;
|
||||
for( a in params ) {
|
||||
if( first ) first = false else add(", ");
|
||||
if( a.opt ) add("?");
|
||||
add(a.name);
|
||||
add(getVar(a.name));
|
||||
addType(a.t);
|
||||
}
|
||||
add(")");
|
||||
@@ -428,7 +489,7 @@ class Printer {
|
||||
}
|
||||
add("]");
|
||||
case ENew(cl, args):
|
||||
add("new " + cl + "(");
|
||||
add("new " + getVar(cl) + "(");
|
||||
var first = true;
|
||||
for( e in args ) {
|
||||
if( first ) first = false else add(", ");
|
||||
@@ -506,7 +567,7 @@ class Printer {
|
||||
var first = true;
|
||||
for( a in args ) {
|
||||
if( first ) first = false else add(", ");
|
||||
expr(a);
|
||||
expr(e);
|
||||
}
|
||||
add(")");
|
||||
}
|
||||
@@ -518,6 +579,10 @@ class Printer {
|
||||
add(" : ");
|
||||
addType(t);
|
||||
add(")");
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
case EInfo(_, e):
|
||||
expr(e);
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+101
-3
@@ -27,11 +27,11 @@ class Tools {
|
||||
|
||||
public static function iter( e : Expr, f : Expr -> Void ) {
|
||||
switch( expr(e) ) {
|
||||
case EConst(_), EIdent(_):
|
||||
case EConst(_), EIdent(_): f(e);
|
||||
case EImport(c, _): f(e);
|
||||
case EClass(_, e, _, _): for( a in e ) f(a);
|
||||
case EVar(_, _, e): if( e != null ) f(e);
|
||||
case EParent(e): f(e);
|
||||
case EParent(e, _): f(e);
|
||||
case EBlock(el): for( e in el ) f(e);
|
||||
case EField(e, _): f(e);
|
||||
case EBinop(_, e1, e2): f(e1); f(e2);
|
||||
@@ -62,7 +62,54 @@ class Tools {
|
||||
if( def != null ) f(def);
|
||||
case EMeta(name, args, e): if( args != null ) for( a in args ) f(a); f(e);
|
||||
case ECheckType(e,_): f(e);
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
case EInfo(info, e): f(e);
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
||||
public static function iterExprRecursive( ve : Expr, f : Expr -> Void ) {
|
||||
var rec = (e:Expr) -> iterExprRecursive(e, f);
|
||||
f(ve);
|
||||
switch( expr(ve) ) {
|
||||
case EConst(_), EIdent(_): {}
|
||||
case EImport(c, _): {}
|
||||
case EClass(_, e, _, _): for( a in e ) rec(a);
|
||||
case EVar(_, _, e): if( e != null ) rec(e);
|
||||
case EParent(e, _): rec(e);
|
||||
case EBlock(el): for( e in el ) rec(e);
|
||||
case EField(e, _): rec(e);
|
||||
case EBinop(_, e1, e2): rec(e1); rec(e2);
|
||||
case EUnop(_, _, e): rec(e);
|
||||
case ECall(e, args): rec(e); for( a in args ) rec(a);
|
||||
case EIf(c, e1, e2): rec(c); rec(e1); if( e2 != null ) rec(e2);
|
||||
case EWhile(c, e): rec(c); rec(e);
|
||||
case EDoWhile(c, e): rec(c); rec(e);
|
||||
case EFor(_, it, e): rec(it); rec(e);
|
||||
case EForKeyValue(_, it, e, _): rec(it); rec(e);
|
||||
case EBreak,EContinue: {}
|
||||
case EFunction(_, e, _, _): rec(e);
|
||||
case EReturn(e): if( e != null ) rec(e);
|
||||
case EArray(e, i): rec(e); rec(i);
|
||||
case EMapDecl(type, keys, values): for( e in keys ) rec(e); for( e in values ) rec(e);
|
||||
case EArrayDecl(el): for( e in el ) rec(e);
|
||||
case ENew(_,el): for( e in el ) rec(e);
|
||||
case EThrow(e): rec(e);
|
||||
case ETry(e, _, _, c): rec(e); rec(c);
|
||||
case EObject(fl): for( fi in fl ) rec(fi.e);
|
||||
case ETernary(c, e1, e2): rec(c); rec(e1); rec(e2);
|
||||
case ESwitch(e, cases, def):
|
||||
rec(e);
|
||||
for( c in cases ) {
|
||||
for( v in c.values ) rec(v);
|
||||
rec(c.expr);
|
||||
}
|
||||
if( def != null ) rec(def);
|
||||
case EMeta(name, args, e): if( args != null ) for( a in args ) rec(a); rec(e);
|
||||
case ECheckType(e,_): rec(e);
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
case EInfo(info, e): rec(e);
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,11 +138,14 @@ class Tools {
|
||||
case ETry(e, v, t, c): ETry(f(e), v, t, f(c));
|
||||
case EObject(fl): EObject([for( fi in fl ) new ObjectField(fi.name, f(fi.e))]);
|
||||
case ETernary(c, e1, e2): ETernary(f(c), f(e1), f(e2));
|
||||
case ESwitch(e, cases, def): ESwitch(f(e), [for( c in cases ) new SwitchCase([for( v in c.values ) f(v)], f(c.expr))], def == null ? null : f(def));
|
||||
case ESwitch(e, cases, def): ESwitch(f(e), [for( c in cases ) { values : [for( v in c.values ) f(v)], expr : f(c.expr) } ], def == null ? null : f(def));
|
||||
case EMeta(name, args, e): EMeta(name, args == null ? null : [for( a in args ) f(a)], f(e));
|
||||
case ECheckType(e,t): ECheckType(f(e), t);
|
||||
case EImport(c, m): EImport(c, m);
|
||||
case EClass(name, el, extend, interfaces): EClass(name, [for( e in el ) f(e)], extend, interfaces);
|
||||
#if !HSCRIPT_NO_INT_VARS
|
||||
case EInfo(info, e): EInfo(info, f(e));
|
||||
#end
|
||||
}
|
||||
return mk(edef, e);
|
||||
}
|
||||
@@ -133,6 +183,54 @@ class Tools {
|
||||
}
|
||||
}
|
||||
|
||||
public static function getOpEnum(op:String):Binop {
|
||||
return switch(op) {
|
||||
case "+": OpAdd;
|
||||
case "-": OpSub;
|
||||
case "*": OpMult;
|
||||
case "/": OpDiv;
|
||||
case "%": OpMod;
|
||||
case "&": OpAnd;
|
||||
case "|": OpOr;
|
||||
case "^": OpXor;
|
||||
case "<<": OpShl;
|
||||
case ">>": OpShr;
|
||||
case ">>>": OpUShr;
|
||||
case "==": OpEq;
|
||||
case "!=": OpNotEq;
|
||||
case ">=": OpGte;
|
||||
case "<=": OpLte;
|
||||
case ">": OpGt;
|
||||
case "<": OpLt;
|
||||
case "||": OpBoolOr;
|
||||
case "&&": OpBoolAnd;
|
||||
case "is": OpIs;
|
||||
case "=": OpAssign;
|
||||
case "=>": OpArrow;
|
||||
case "??": OpNullCoal;
|
||||
case "...": OpInterval;
|
||||
default: {
|
||||
var op2 = op.substr(0, op.length - 1);
|
||||
if(isValidBinOp(op2)) {
|
||||
return OpAssignOp(getOpEnum(op2));
|
||||
}
|
||||
throw "Unknown binary operator: " + op;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function getUnopEnum(op:String):Unop {
|
||||
return switch(op) {
|
||||
case "++": OpIncrement;
|
||||
case "--": OpDecrement;
|
||||
case "!": OpNot;
|
||||
case "-": OpNeg;
|
||||
case "~": OpNegBits;
|
||||
case "...": OpSpread;
|
||||
default: throw "Unknown unary operator: " + op;
|
||||
}
|
||||
}
|
||||
|
||||
static var priorities = [
|
||||
["%"],
|
||||
["*", "/"],
|
||||
|
||||
@@ -1,519 +0,0 @@
|
||||
package hscript.macros;
|
||||
|
||||
#if macro
|
||||
import Type.ValueType;
|
||||
import haxe.macro.ComplexTypeTools;
|
||||
import haxe.macro.Expr;
|
||||
import haxe.macro.Type;
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Printer;
|
||||
import haxe.macro.Compiler;
|
||||
import haxe.macro.ComplexTypeTools;
|
||||
|
||||
using StringTools;
|
||||
using haxe.macro.PositionTools;
|
||||
|
||||
class AbstractHandler {
|
||||
public static inline final CLASS_SUFFIX = "_HSA";
|
||||
|
||||
public static function init() {
|
||||
#if !display
|
||||
if(Context.defined("display")) return;
|
||||
//for(apply in Config.ALLOWED_ABSTRACT_AND_ENUM) {
|
||||
//}
|
||||
Compiler.addGlobalMetadata("", '@:build(hscript.macros.AbstractHandler.build())');
|
||||
|
||||
//var module = cl.module + cl.name;
|
||||
#end
|
||||
}
|
||||
|
||||
static var abstracts = new Map<String, {
|
||||
|
||||
}>();
|
||||
|
||||
/*static function finalizeAbstract(a:AbstractType) {
|
||||
var name = a.name;
|
||||
if(name.endsWith(CLASS_SUFFIX)) return; // !name.endsWith("_Impl_") ||
|
||||
if(name != "NodeListAccess") return;
|
||||
|
||||
Sys.println("");
|
||||
Sys.println("");
|
||||
Sys.println("");
|
||||
Sys.println("");
|
||||
Sys.println("");
|
||||
|
||||
//Sys.println("name: " + a.name);
|
||||
//Sys.println("module: " + a.module);
|
||||
|
||||
Sys.println(MacroPrinter.convertAbstractTypeToString(a));
|
||||
|
||||
//trace("buildAbstract", a);
|
||||
}*/
|
||||
|
||||
static var currentSelf = "";
|
||||
|
||||
/*static function oldTypeToString(t:Type, selfCheck:String = null):String {
|
||||
var str = switch(t) {
|
||||
case TInst(_.get() => t, params):
|
||||
var str = "";
|
||||
if(t.pack.length > 0) {
|
||||
str += t.pack.join(".") + ".";
|
||||
}
|
||||
str += t.name;
|
||||
if(t.params != null && t.params.length > 0) {
|
||||
str += "<" + t.params.map((v)->oldTypeToString(v.t, t.name + "." + v.name)).join(", ") + ">";
|
||||
}
|
||||
//if(t.params.length > 0) {
|
||||
// throw "Params not supported yet " + t.name + " " + t.params;
|
||||
//}
|
||||
str;
|
||||
case TAbstract(_.get() => t, params):
|
||||
var str = "";
|
||||
if(t.pack.length > 0) {
|
||||
str += t.pack.join(".") + ".";
|
||||
}
|
||||
str += t.name;
|
||||
if(t.params != null && t.params.length > 0) {
|
||||
str += "<" + t.params.map((v)->oldTypeToString(v.t, t.name + "." + v.name)).join(", ") + ">";
|
||||
}
|
||||
//if(t.params.length > 0) {
|
||||
// throw "Params not supported yet " + t.name + " " + t.params;
|
||||
//}
|
||||
str;
|
||||
case TEnum(_.get() => t, params):
|
||||
var str = "";
|
||||
if(t.pack.length > 0) {
|
||||
str += t.pack.join(".") + ".";
|
||||
}
|
||||
str += t.name;
|
||||
if(t.params != null && t.params.length > 0) {
|
||||
str += "<" + t.params.map((v)->oldTypeToString(v.t, t.name + "." + v.name)).join(", ") + ">";
|
||||
}
|
||||
//if(t.params.length > 0) {
|
||||
// throw "Params not supported yet " + t.name + " " + t.params;
|
||||
//}
|
||||
str;
|
||||
case TType(_.get() => t, params):
|
||||
var str = "";
|
||||
if(t.pack.length > 0) {
|
||||
str += t.pack.join(".") + ".";
|
||||
}
|
||||
str += t.name;
|
||||
if(t.params != null && t.params.length > 0) {
|
||||
str += "<" + t.params.map((v)->oldTypeToString(v.t, t.name + "." + v.name)).join(", ") + ">";
|
||||
//trace("TType", str);
|
||||
}
|
||||
str;
|
||||
case TAnonymous(_.get() => t):
|
||||
//var str = "Dynamic {"+t.fields.map((f)->f.name).join(", ")+"}";
|
||||
//str += t.name;
|
||||
var str = "{ ";
|
||||
var first = true;
|
||||
for(f in t.fields) {
|
||||
if(!first) str += ", ";
|
||||
first = false;
|
||||
str += f.name + ":" + oldTypeToString(f.type);
|
||||
}
|
||||
str += " }";
|
||||
//trace("TAnonymous", str);
|
||||
str;
|
||||
case TDynamic(t):
|
||||
var str = "Dynamic";
|
||||
if(t != null) {
|
||||
str += "<" + oldTypeToString(t) + ">";
|
||||
}
|
||||
str;
|
||||
default:
|
||||
Sys.println("Unknown type " + Std.string(t));
|
||||
null;
|
||||
}
|
||||
|
||||
if(selfCheck != null) {
|
||||
if(str == selfCheck) {
|
||||
return selfCheck;
|
||||
//return currentSelf;
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
static function getResolvedType(t:ComplexType):String {
|
||||
if(t == null) return null;
|
||||
|
||||
try {
|
||||
//var type = Context.getType(checkType);
|
||||
var paramFree = switch(t) {
|
||||
case TPath(t):
|
||||
//trace(t);
|
||||
TPath({
|
||||
name: t.name,
|
||||
pack: t.pack,
|
||||
params: t.params,
|
||||
sub: t.sub
|
||||
});
|
||||
case TAnonymous(a):
|
||||
t;
|
||||
case TFunction(args, ret):
|
||||
t;
|
||||
default:
|
||||
trace("");
|
||||
trace(ComplexTypeTools.toString(t));
|
||||
throw "Unknown type " + Std.string(t);
|
||||
}
|
||||
var type = ComplexTypeTools.toType(paramFree);
|
||||
var strType = oldTypeToString(type);
|
||||
if(strType != null) return strType;
|
||||
} catch(e:Dynamic) {
|
||||
Sys.println(e);
|
||||
Sys.println(ComplexTypeTools.toString(t));
|
||||
Sys.println(haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
|
||||
}
|
||||
|
||||
return MacroPrinter.typeToString(t);
|
||||
}*/
|
||||
|
||||
public static function build():Array<Field> {
|
||||
var fields = Context.getBuildFields();
|
||||
var clRef = Context.getLocalClass();
|
||||
if (clRef == null) return fields;
|
||||
var cl = clRef.get();
|
||||
|
||||
if(cl.isExtern) return fields;
|
||||
|
||||
//if(cl.name != "Access_Impl_") return fields;
|
||||
//if(cl.name != "DrawQuadsView_Impl_") return fields;
|
||||
|
||||
if(cl.name.endsWith("_Impl_") && !cl.name.endsWith(CLASS_SUFFIX)) // && ["Access_Impl_", "AttribAccess_Impl_"].contains(cl.name))
|
||||
{
|
||||
//trace("Processing " + cl);
|
||||
//if(!initialized) {
|
||||
// initialized = true;
|
||||
// Context.onAfterTyping(function(mods) {
|
||||
// for(mod in mods) {
|
||||
// switch(mod) {
|
||||
// case TAbstract(_.get() => a):
|
||||
// //trace("onAfterTyping", a);
|
||||
// finalizeAbstract(a);
|
||||
// //ab = a;
|
||||
// default:
|
||||
// // return;
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
//}
|
||||
|
||||
currentSelf = cl.name.substr(0, cl.name.length - "_Impl_".length);
|
||||
|
||||
var funcInfos = [];
|
||||
var propsInfo = [];
|
||||
//for(f in fields) {
|
||||
// trace(f.name, f.kind);
|
||||
//}
|
||||
for(f in fields) {
|
||||
if(f.name.startsWith("__abstract_helper"))
|
||||
continue;
|
||||
//if(f.name != "escapes")
|
||||
// continue;
|
||||
//trace(f);
|
||||
//trace(
|
||||
//MacroPrinter.convertFieldToString(f, cl.module + "." + cl.name)
|
||||
//);
|
||||
//trace();
|
||||
//continue;
|
||||
switch(f.kind) {
|
||||
case FFun(fun):
|
||||
if(fun.expr != null) {
|
||||
var obj:Dynamic = {
|
||||
name: f.name,
|
||||
args: [for(a in fun.args) {
|
||||
//name: a.name,
|
||||
//opt: a.opt,
|
||||
//type: MacroPrinter.typeToString(a.type),
|
||||
//value: a.value,
|
||||
//meta: a.meta,
|
||||
var arg = a.type == null ? "_" : MacroPrinter.typeToString(a.type);
|
||||
if(a.opt) {
|
||||
arg = "?" + arg;
|
||||
}
|
||||
arg;
|
||||
}].join("|"),
|
||||
ret: fun.ret,
|
||||
//access: f.access.map(function(a) return Std.string(a)).join(","),
|
||||
access: {
|
||||
var special = 0;
|
||||
var map:Array<Access> = [AStatic, APrivate, APublic, AOverride, AInline, ADynamic, AExtern, AStatic];
|
||||
for(a in f.access) {
|
||||
var index = map.indexOf(a);
|
||||
if(index == -1) {
|
||||
trace("Unknown access " + a);
|
||||
continue;
|
||||
}
|
||||
special |= 1 << index;
|
||||
}
|
||||
special;
|
||||
},
|
||||
special: {
|
||||
var special = 0;//{arrayAccess: false, arrayWrite: false};
|
||||
for(m in f.meta) {
|
||||
if(m.name == ":arrayAccess") {
|
||||
if(fun.args.length == 1) {
|
||||
special |= 1;
|
||||
//special.arrayAccess = true;
|
||||
} else if(fun.args.length == 2) {
|
||||
special |= 2;
|
||||
//special.arrayWrite = true;
|
||||
} else {
|
||||
trace(f.pos);
|
||||
throw "Unknown :arrayAccess meta " + fun.args;
|
||||
}
|
||||
} else if(m.name == ":from") {
|
||||
//throw "Unknown :from meta " + m;
|
||||
special |= 4;
|
||||
} else if(m.name == ":to") {
|
||||
//throw "Unknown :to meta " + m;
|
||||
special |= 8;
|
||||
} else if(m.name == ":op") {
|
||||
special |= 16;
|
||||
}
|
||||
}
|
||||
special;
|
||||
},
|
||||
op: {
|
||||
var op = null;
|
||||
//trace("");
|
||||
//trace("");
|
||||
//trace("");
|
||||
//trace(f.name, f.meta);
|
||||
//trace(f.name, fun);
|
||||
for(m in f.meta) {
|
||||
if(m.name == ":resolve") {
|
||||
if(f.access.contains(AStatic) && fun.args.length == 2) {
|
||||
op = "a.b";
|
||||
} else if(!f.access.contains(AStatic) && fun.args.length == 1) {
|
||||
op = "a.b";
|
||||
} else {
|
||||
throw "Unknown :resolve meta " + fun.args;
|
||||
}
|
||||
}
|
||||
if(m.name == ":op") {
|
||||
switch (m.params[0].expr) {
|
||||
case EField(_.expr => EConst(CIdent(_)), _):
|
||||
op = "a.b";
|
||||
case EArrayDecl([]):
|
||||
op = "[]";
|
||||
case EUnop(o, suffix, _.expr => EConst(CIdent(_))):
|
||||
var opStr = switch(o) {
|
||||
case OpNeg: "-";
|
||||
case OpIncrement: "++";
|
||||
case OpDecrement: "--";
|
||||
case OpNot: "!";
|
||||
case OpNegBits: "~";
|
||||
case OpSpread: "...";
|
||||
}
|
||||
if(suffix) {
|
||||
op = "a" + opStr;
|
||||
} else {
|
||||
op = opStr + "a";
|
||||
}
|
||||
case EBinop(o, _.expr => EConst(CIdent(_)), _.expr => EConst(CIdent(_))):
|
||||
var opStr = getBinopStr(o);
|
||||
op = "a " + opStr + " b";
|
||||
#if (haxe >= "4.3.0")
|
||||
case ECall(_.expr => EConst(CIdent(_)), []):
|
||||
op = "a()";
|
||||
#end
|
||||
default:
|
||||
trace(f.name, cl.module);
|
||||
throw "Unknown op " + MacroPrinter.convertExprToString(m.params[0]) + "\n in " + f.name + " in " + cl.module;
|
||||
}
|
||||
//trace(op, convertExprToString(m.params[0]));
|
||||
//trace(Type.typeof(m.params[0]));
|
||||
//switch (m.params[0].expr) {
|
||||
// case EConst(CIdent(_i)):
|
||||
// id = _i;
|
||||
// default:
|
||||
//}
|
||||
}
|
||||
}
|
||||
op;
|
||||
}
|
||||
}
|
||||
|
||||
if(obj.op == null) {
|
||||
Reflect.deleteField(obj, "op");
|
||||
}
|
||||
|
||||
if(obj.ret == null && f.name.startsWith("get_")) {
|
||||
var v = getVarFromFields(fields, f.name.substr(4));
|
||||
var type = getTypeFromField(v);
|
||||
//Sys.println("getter " + f.name + " " + getResolvedType(type) + " " + type);
|
||||
obj.ret = type;
|
||||
} else if(obj.ret == null && f.name.startsWith("set_")) {
|
||||
var v = getVarFromFields(fields, f.name.substr(4));
|
||||
var type = getTypeFromField(v);
|
||||
//Sys.println("setter " + f.name + " " + getResolvedType(type) + " " + type);
|
||||
obj.ret = type;
|
||||
} else {
|
||||
if(obj.ret != null) {
|
||||
//Sys.println("normal field " + f.name + " " + getResolvedType(obj.ret) + " " + obj.ret);
|
||||
}
|
||||
}
|
||||
|
||||
//if(obj.name == "_new")
|
||||
// obj.name = "new";
|
||||
|
||||
obj.ret = MacroPrinter.typeToString(obj.ret);
|
||||
|
||||
//trace("FFun", obj);
|
||||
var save:Dynamic = null;
|
||||
if(true) {
|
||||
save = [obj.name, obj.args, obj.ret, obj.access, obj.special];
|
||||
if(obj.op != null) {
|
||||
save.push(obj.op);
|
||||
}
|
||||
} else {
|
||||
save = obj;
|
||||
}
|
||||
funcInfos.push(save);
|
||||
//trace(cl.name, obj);
|
||||
}
|
||||
case FProp(get, set, t, e):
|
||||
if(f.access.contains(AStatic)) {
|
||||
continue;
|
||||
}
|
||||
if(get != "get") {
|
||||
trace("Unknown FProp getter " + get, f.pos);
|
||||
continue;
|
||||
}
|
||||
if(e != null) {
|
||||
trace("Unknown FProp expression " + e, f.pos);
|
||||
continue;
|
||||
}
|
||||
//var getsetbits = 0;
|
||||
var canSet = false;
|
||||
// allow transform of getter setter
|
||||
//switch(get) {
|
||||
// case "get": getsetbits |= 1;
|
||||
// case "dynamic": getsetbits |= 1;
|
||||
// //case "null": getsetbits |= 1; // since this cant be called outside of the class
|
||||
//}
|
||||
switch(set) {
|
||||
case "set": canSet = true;
|
||||
case "dynamic": canSet = true;
|
||||
case "null": canSet = false; // since this cant be called outside of the class
|
||||
}
|
||||
|
||||
var obj:Dynamic = {
|
||||
name: f.name,
|
||||
get: "get_" + f.name,
|
||||
set: canSet ? "set_" + f.name : null
|
||||
}
|
||||
|
||||
//var getset = get + "," + set;
|
||||
var save:Dynamic = null;
|
||||
if(true) {
|
||||
save = [obj.name, obj.get, obj.set];
|
||||
} else {
|
||||
save = obj;
|
||||
}
|
||||
propsInfo.push(save);
|
||||
//trace("FProp", getset, t, e);
|
||||
default:
|
||||
}
|
||||
//funcInfos.push([f.name, ]);
|
||||
}
|
||||
|
||||
//trace(funcInfos);
|
||||
|
||||
//trace(cl.pos);
|
||||
|
||||
var shadowClass = macro class {
|
||||
public static var __hx_impl__:String = $v{cl.pack.join(".") + "." + cl.name};
|
||||
|
||||
public static function abstract_funcs():Array<Array<Dynamic>> {
|
||||
return @:fixed $v{funcInfos};
|
||||
}
|
||||
public static function abstract_props():Array<Array<String>> {
|
||||
return @:fixed $v{propsInfo};
|
||||
}
|
||||
}
|
||||
|
||||
shadowClass.kind = TDClass(null, [
|
||||
//{name: "IHScriptAbstractHelper", pack: ["hscript"]},
|
||||
//{name: "IHScriptCustomClassBehaviour", pack: ["hscript"]}
|
||||
], false, true, false);
|
||||
shadowClass.name = '${cl.name.substr(0, cl.name.length - "_Impl_".length)}$CLASS_SUFFIX';
|
||||
//trace(shadowClass.name);
|
||||
var imports = Context.getLocalImports().copy();
|
||||
Utils.setupMetas(shadowClass, [], false);
|
||||
|
||||
var moduleName = cl.module;
|
||||
Context.defineModule(moduleName, [shadowClass], imports);
|
||||
|
||||
//var printer = new haxe.macro.Printer();
|
||||
//var code = printer.printTypeDefinition(shadowClass);
|
||||
//trace(code);
|
||||
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
static var initialized = false;
|
||||
|
||||
static function getBinopStr(op:Binop):String {
|
||||
return switch(op) {
|
||||
case OpAdd: "+";
|
||||
case OpSub: "-";
|
||||
case OpMult: "*";
|
||||
case OpDiv: "/";
|
||||
case OpMod: "%";
|
||||
case OpEq: "==";
|
||||
case OpNotEq: "!=";
|
||||
case OpGt: ">";
|
||||
case OpGte: ">=";
|
||||
case OpLt: "<";
|
||||
case OpLte: "<=";
|
||||
case OpAnd: "&";
|
||||
case OpOr: "|";
|
||||
case OpXor: "^";
|
||||
case OpShl: "<<";
|
||||
case OpShr: ">>";
|
||||
case OpUShr: ">>>";
|
||||
case OpBoolAnd: "&&";
|
||||
case OpBoolOr: "||";
|
||||
case OpAssign: "=";
|
||||
case OpArrow: "=>";
|
||||
case OpAssignOp(op):
|
||||
getBinopStr(op) + "=";
|
||||
case OpIn: "in";
|
||||
case OpInterval: "...";
|
||||
}
|
||||
}
|
||||
|
||||
static function getVarFromFields(fields:Array<Field>, name:String):Field {
|
||||
for(f in fields) {
|
||||
if(f.name == name) {
|
||||
switch(f.kind) {
|
||||
case FProp(_, _, _, _):
|
||||
return f;
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static function getTypeFromField(field:Field):ComplexType {
|
||||
//if(field == null) return null;
|
||||
return switch(field.kind) {
|
||||
case FProp(_, _, t, _): t;
|
||||
case FVar(t, _): t;
|
||||
case FFun(f): f.ret;
|
||||
default: null;
|
||||
}
|
||||
}
|
||||
}
|
||||
#end
|
||||
@@ -144,12 +144,16 @@ class ClassExtendMacro {
|
||||
|
||||
//trace(getModuleName(cl));
|
||||
|
||||
var hasNew = false;
|
||||
|
||||
for(_field in [fields.copy(), superFields.copy()])
|
||||
for(f in _field) {
|
||||
if (f == null)
|
||||
continue;
|
||||
if (f.name == "new")
|
||||
if (f.name == "new") {
|
||||
hasNew = true;
|
||||
continue;
|
||||
}
|
||||
if (f.name.startsWith(FUNC_PREFIX))
|
||||
continue;
|
||||
if (f.access.contains(ADynamic) || f.access.contains(AStatic) || f.access.contains(AExtern) || f.access.contains(AInline))
|
||||
@@ -183,7 +187,7 @@ class ClassExtendMacro {
|
||||
overrideExpr = macro {
|
||||
var name:String = $v{name};
|
||||
|
||||
if (__interp != null) {
|
||||
if (__custom__variables != null) {
|
||||
if(__custom__variables.exists(name)) {
|
||||
var v:Dynamic = null;
|
||||
if (Reflect.isFunction(v = __custom__variables.get(name))) {
|
||||
@@ -197,7 +201,7 @@ class ClassExtendMacro {
|
||||
overrideExpr = macro {
|
||||
var name:String = $v{name};
|
||||
|
||||
if (__interp != null) {
|
||||
if (__custom__variables != null) {
|
||||
if(__custom__variables.exists(name)) {
|
||||
var v:Dynamic = null;
|
||||
if (Reflect.isFunction(v = __custom__variables.get(name))) {
|
||||
@@ -257,6 +261,13 @@ class ClassExtendMacro {
|
||||
}
|
||||
}
|
||||
|
||||
var totalFields = definedFields.length;
|
||||
|
||||
if(totalFields == 0 && !hasNew) {
|
||||
//Sys.println(cl.pack.join(".") + "." + cl.name + ", " + totalFields);
|
||||
return fields;
|
||||
}
|
||||
|
||||
shadowClass.kind = TDClass({
|
||||
pack: cl.pack.copy(),
|
||||
name: cl.name
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -25,11 +25,10 @@ class Utils {
|
||||
return type;
|
||||
}
|
||||
|
||||
public static function setupMetas(shadowClass:TypeDefinition, imports:Array<ImportExpr>, doImports:Bool = true) {
|
||||
public static function setupMetas(shadowClass:TypeDefinition, imports) {
|
||||
shadowClass.meta = [];
|
||||
shadowClass.meta.push({name: ":dox", params: [macro hide], pos: Context.currentPos()});
|
||||
shadowClass.meta.push({name: ":noCompletion", params: [], pos: Context.currentPos()});
|
||||
if(!doImports) return;
|
||||
var module = Context.getModule(Context.getLocalModule());
|
||||
for(t in module) {
|
||||
switch(t) {
|
||||
|
||||
+2
-7
@@ -17,14 +17,9 @@
|
||||
# File Specific Defines
|
||||
--define linux
|
||||
|
||||
--macro keep('IntIterator')
|
||||
# --macro hscript.macros.UsingHandler.init()
|
||||
# --macro hscript.macros.ClassExtendMacro.init()
|
||||
--macro hscript.macros.AbstractHandler.init()
|
||||
|
||||
# Copying the assets from source code to bin folder
|
||||
--cmd rm -rf bin/cpp/assets
|
||||
--cmd cp -r assets bin/cpp/assets
|
||||
# --cmd rm -rf bin/cpp/assets
|
||||
# --cmd cp -r assets bin/cpp/assets
|
||||
--cpp bin/cpp
|
||||
--cmd cd bin/cpp
|
||||
--cmd ./Main
|
||||
+2
-3
@@ -24,12 +24,11 @@
|
||||
--macro keep('IntIterator')
|
||||
# --macro hscript.macros.UsingHandler.init()
|
||||
# --macro hscript.macros.ClassExtendMacro.init()
|
||||
--macro hscript.macros.AbstractHandler.init()
|
||||
|
||||
|
||||
# Copying the assets from source code to bin folder
|
||||
--cmd rm -rf bin/cpp/assets
|
||||
--cmd cp -r assets bin/cpp/assets
|
||||
# --cmd rm -rf bin/cpp/assets
|
||||
# --cmd cp -r assets bin/cpp/assets
|
||||
--cpp bin/cpp
|
||||
--cmd cd bin/cpp
|
||||
--cmd ./Main
|
||||
+29
-2
@@ -12,7 +12,35 @@ class Main {
|
||||
Sys.println("Running benchmark");
|
||||
|
||||
//new Benchmark("Test", 1000);
|
||||
new TestBenchmark();
|
||||
var haxeTotalTime:Float = 0;
|
||||
var hscriptTotalTime:Float = 0;
|
||||
|
||||
var haxeTimes:Array<Float> = [];
|
||||
var hscriptTimes:Array<Float> = [];
|
||||
|
||||
var iterations:Int = 10;
|
||||
for (i in 0...iterations) {
|
||||
var benchmark:TestBenchmark = new TestBenchmark();
|
||||
|
||||
haxeTimes.push(benchmark.haxeTotalTime);
|
||||
hscriptTimes.push(benchmark.hscriptTotalTime);
|
||||
|
||||
haxeTotalTime += benchmark.haxeTotalTime;
|
||||
hscriptTotalTime += benchmark.hscriptTotalTime;
|
||||
}
|
||||
|
||||
haxeTimes.sort((a, b) -> return a > b ? 1 : -1);
|
||||
hscriptTimes.sort((a, b) -> return a > b ? 1 : -1);
|
||||
|
||||
var slowestHaxeTime:Float = haxeTimes[haxeTimes.length-1];
|
||||
var slowestHscriptTime:Float = hscriptTimes[hscriptTimes.length-1];
|
||||
var fastestHaxeTime:Float = haxeTimes[0];
|
||||
var fastestHscriptTime:Float = hscriptTimes[0];
|
||||
|
||||
var haxeWon = hscriptTotalTime > haxeTotalTime;
|
||||
Sys.println('${haxeWon ? "Haxe" : "Hscript"} was faster overall (Faster by: ${Util.roundWith((haxeWon ? hscriptTotalTime/haxeTotalTime : haxeTotalTime/hscriptTotalTime), 100)}x)');
|
||||
Sys.println('Haxe average time: ${Util.convertToReadableTime(haxeTotalTime/iterations)} (Highest: ${Util.convertToReadableTime(slowestHaxeTime)}) (Lowest: ${Util.convertToReadableTime(fastestHaxeTime)})');
|
||||
Sys.println('Hscript average time: ${Util.convertToReadableTime(hscriptTotalTime/iterations)} (Highest: ${Util.convertToReadableTime(slowestHscriptTime)}) (Lowest: ${Util.convertToReadableTime(fastestHscriptTime)})');
|
||||
#else
|
||||
Sys.println("Beginning tests");
|
||||
if(!Main.SHOW_KNOWN_BUGS) {
|
||||
@@ -40,7 +68,6 @@ class Main {
|
||||
// TODO: UnicodeCase.hx?
|
||||
// TODO: UnicodeStringCase.hx?
|
||||
runTest("Final", new FinalCase());
|
||||
runTest("Abstract", new AbstractCase());
|
||||
Util.printTestResults();
|
||||
#end
|
||||
}
|
||||
|
||||
@@ -210,6 +210,7 @@ class Util {
|
||||
// Print stack trace
|
||||
var stack = haxe.CallStack.toString(haxe.CallStack.exceptionStack());
|
||||
Sys.println("## Stack trace: " + stack);
|
||||
Sys.exit(1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,10 @@ class Benchmark extends HScriptRunner {
|
||||
|
||||
public function new(name:String, iterations:Int) {
|
||||
super();
|
||||
doBenchmark(name, iterations);
|
||||
}
|
||||
|
||||
public function doBenchmark(name:String, iterations:Int) {
|
||||
haxeTimes = cast new haxe.ds.Vector<Float>(iterations);
|
||||
hscriptTimes = cast new haxe.ds.Vector<Float>(iterations);
|
||||
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
package tests;
|
||||
|
||||
import haxe.xml.Access;
|
||||
import Xml;
|
||||
|
||||
class AbstractCase extends TestCase {
|
||||
override function setup() {
|
||||
super.setup();
|
||||
}
|
||||
|
||||
override function run() {
|
||||
var access = new Access(Xml.parse("<test test=\"cool\"><hello>world</hello></test>").firstElement());
|
||||
|
||||
trace(access.att.test);
|
||||
|
||||
getNewInterp().execute(Util.parse("
|
||||
|
||||
import haxe.xml.Access;
|
||||
|
||||
var access = new Access(Xml.parse('<test test=\"cool\"><hello>world</hello></test>').firstElement());
|
||||
var a2 = new haxe.xml.Access(Xml.parse('<test test=\"cool\"><hello>world</hello></test>').firstElement());
|
||||
|
||||
trace(access.att.test);
|
||||
"));
|
||||
|
||||
//var a2:Access = Xml.parse('<test test=\"cool\"><hello>world</hello></test>').firstElement();
|
||||
/*getNewInterp().execute(Util.parse("
|
||||
// Converts to
|
||||
import haxe.xml._Access.Access_Impl_;
|
||||
import haxe.xml._Access.AttribAccess_Impl_;
|
||||
|
||||
var access = Xml.parse('<test test=\"cool\"><hello>world</hello></test>').firstElement();
|
||||
|
||||
trace(AttribAccess_Impl_.resolve(Access_Impl_.get_att(access), 'test'));
|
||||
|
||||
|
||||
// trace(Access_Impl_.get_att(access));
|
||||
//abstract import haxe.xml.Access;
|
||||
//import haxe.xml.Access with Abstract;
|
||||
|
||||
|
||||
import haxe.xml.Access;
|
||||
|
||||
var access = new Access(Xml.parse('<test test=\"cool\"><hello>world</hello></test>').firstElement());
|
||||
|
||||
trace(access.att.test);
|
||||
"));*/
|
||||
|
||||
/*headerCode = "function area(a:Int, b:Int):Int { return a * b; };";
|
||||
function area(a:Int, b:Int):Int { return a * b; }
|
||||
|
||||
assertEq("[for(i in 0...10) for(j in 0...10) area(i, j)]", [for(i in 0...10) for(j in 0...10) area(i, j)]);*/
|
||||
}
|
||||
|
||||
override function teardown() {
|
||||
super.teardown();
|
||||
}
|
||||
}
|
||||
|
||||
abstract TestAbstract(Int)
|
||||
{
|
||||
//public static inline var TRANSPARENT:TestAbstract = 0x00000000;
|
||||
//public static inline var WHITE:TestAbstract = 0xFFFFFFFF;
|
||||
//public static inline var GRAY:TestAbstract = 0xFF808080;
|
||||
//public static inline var BLACK:TestAbstract = 0xFF000000;
|
||||
}
|
||||
|
||||
enum abstract TestEnumAbstract(Int) {
|
||||
|
||||
}
|
||||
@@ -27,6 +27,7 @@ class FinalCase extends TestCase {
|
||||
assertDisplay("(i - x) * 30 + 90", ((i-x) * 30) + 90);
|
||||
assertDisplay("i - x * 30 + 90", i-x * 30 + 90);
|
||||
|
||||
/*
|
||||
// Test operator precedence
|
||||
assertDisplay("i / x * 30 + 90", i / x * 30 + 90);
|
||||
assertDisplay("i / (x * 30) + 90", i / (x * 30) + 90);
|
||||
@@ -44,6 +45,7 @@ class FinalCase extends TestCase {
|
||||
bloomShader.dim = dim = .8 + (.3 * FlxMath.fastSin(__totalTime));
|
||||
bloomShader.size = size = 18 + (8 * FlxMath.fastSin(__totalTime));
|
||||
');
|
||||
*/
|
||||
|
||||
Util.parse('sprite.setPosition(codesList.x + (sprite.ID % 2 == 1 ? 240 : 67),codesList.y + (15 * sprite.ID) + (sprite.ID%2 == 1 ? 35 : 54));');
|
||||
|
||||
@@ -74,7 +76,6 @@ class FinalCase extends TestCase {
|
||||
|
||||
assertEq("[for(i in 0...10) if(i % 2 == 0) i => isEven(i) else i => isEven(i)]", [for(i in 0...10) if(i % 2 == 0) i => isEven(i) else i => isEven(i)]);
|
||||
assertEq("[for(i in 0...10) if(i % 2 == 0) isEven(i) else isEven(i)]", [for(i in 0...10) if(i % 2 == 0) isEven(i) else isEven(i)]);
|
||||
|
||||
headerCode = "function area(a:Int, b:Int):Int { return a * b; };";
|
||||
function area(a:Int, b:Int):Int { return a * b; }
|
||||
|
||||
@@ -126,8 +127,6 @@ class FinalCase extends TestCase {
|
||||
import Math.round;
|
||||
round(1.9);", Math.round(1.9));
|
||||
|
||||
trace(Type.resolveClass("hscript._Error.ErrorMessage_Impl_"));
|
||||
|
||||
// Test EOF with preprocessor
|
||||
}
|
||||
|
||||
|
||||
@@ -18,11 +18,6 @@
|
||||
# File Specific Defines
|
||||
--define windows
|
||||
|
||||
--macro keep('IntIterator')
|
||||
# --macro hscript.macros.UsingHandler.init()
|
||||
# --macro hscript.macros.ClassExtendMacro.init()
|
||||
--macro hscript.macros.AbstractHandler.init()
|
||||
|
||||
# Copying the assets from source code to bin folder
|
||||
# --cmd Xcopy assets\ bin\cpp\assets\ /E /H /C /I /Y
|
||||
--cpp bin/cpp
|
||||
|
||||
Reference in New Issue
Block a user