Skip to content

Commit e2f6072

Browse files
committed
minor refactor for maps + setVar returns value (like other setXXX)
1 parent 505442e commit e2f6072

File tree

1 file changed

+57
-39
lines changed

1 file changed

+57
-39
lines changed

hscript/Interp.hx

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class Interp {
113113

114114
function setVar( name : String, v : Dynamic ) {
115115
variables.set(name, v);
116+
return v;
116117
}
117118

118119
function assign( e1 : Expr, e2 : Expr ) : Dynamic {
@@ -443,56 +444,34 @@ class Interp {
443444
}
444445
return f;
445446
case EArrayDecl(arr):
446-
if (arr.length > 0 && Tools.expr(arr[0]).match(EBinop("=>", _))) {
447-
var isAllString:Bool = true;
448-
var isAllInt:Bool = true;
449-
var isAllObject:Bool = true;
450-
var isAllEnum:Bool = true;
451-
var keys:Array<Dynamic> = [];
452-
var values:Array<Dynamic> = [];
453-
for (e in arr) {
447+
if( arr.length > 0 && Tools.expr(arr[0]).match(EBinop("=>", _)) ) {
448+
var keys = [];
449+
var values = [];
450+
for( e in arr ) {
454451
switch(Tools.expr(e)) {
455-
case EBinop("=>", eKey, eValue): {
456-
var key:Dynamic = expr(eKey);
457-
var value:Dynamic = expr(eValue);
458-
isAllString = isAllString && (key is String);
459-
isAllInt = isAllInt && (key is Int);
460-
isAllObject = isAllObject && Reflect.isObject(key);
461-
isAllEnum = isAllEnum && Reflect.isEnumValue(key);
462-
keys.push(key);
463-
values.push(value);
464-
}
465-
default: throw("=> expected");
452+
case EBinop("=>", eKey, eValue):
453+
keys.push(expr(eKey));
454+
values.push(expr(eValue));
455+
default:
456+
#if hscriptPos
457+
curExpr = e;
458+
#end
459+
error(ECustom("Invalid map key=>value expression"));
466460
}
467461
}
468-
var map:Dynamic = {
469-
if (isAllInt) new haxe.ds.IntMap<Dynamic>();
470-
else if (isAllString) new haxe.ds.StringMap<Dynamic>();
471-
else if (isAllEnum) new haxe.ds.EnumValueMap<Dynamic, Dynamic>();
472-
else if (isAllObject) new haxe.ds.ObjectMap<Dynamic, Dynamic>();
473-
else throw 'Inconsistent key types';
474-
}
475-
for (n in 0...keys.length) {
476-
setMapValue(map, keys[n], values[n]);
477-
}
478-
return map;
479-
}
480-
else {
462+
return makeMap(keys,values);
463+
} else {
481464
var a = new Array();
482-
for ( e in arr ) {
465+
for( e in arr )
483466
a.push(expr(e));
484-
}
485467
return a;
486468
}
487469
case EArray(e, index):
488470
var arr:Dynamic = expr(e);
489471
var index:Dynamic = expr(index);
490-
if (isMap(arr)) {
472+
if( isMap(arr) )
491473
return getMapValue(arr, index);
492-
}
493-
else {
494-
return arr[index];
495-
}
474+
return arr[index];
496475
case ENew(cl,params):
497476
var a = new Array();
498477
for( e in params )
@@ -624,6 +603,45 @@ class Interp {
624603
cast(map, haxe.Constraints.IMap<Dynamic, Dynamic>).set(key, value);
625604
}
626605

606+
function makeMap( keys : Array<Dynamic>, values : Array<Dynamic> ) : Dynamic {
607+
var isAllString:Bool = true;
608+
var isAllInt:Bool = true;
609+
var isAllObject:Bool = true;
610+
var isAllEnum:Bool = true;
611+
for( key in keys ) {
612+
isAllString = isAllString && (key is String);
613+
isAllInt = isAllInt && (key is Int);
614+
isAllObject = isAllObject && Reflect.isObject(key);
615+
isAllEnum = isAllEnum && Reflect.isEnumValue(key);
616+
}
617+
if( isAllInt ) {
618+
var m = new Map<Int,Dynamic>();
619+
for( i => key in keys )
620+
m.set(key, values[i]);
621+
return m;
622+
}
623+
if( isAllString ) {
624+
var m = new Map<String,Dynamic>();
625+
for( i => key in keys )
626+
m.set(key, values[i]);
627+
return m;
628+
}
629+
if( isAllEnum ) {
630+
var m = new haxe.ds.EnumValueMap<Dynamic,Dynamic>();
631+
for( i => key in keys )
632+
m.set(key, values[i]);
633+
return m;
634+
}
635+
if( isAllObject ) {
636+
var m = new Map<{},Dynamic>();
637+
for( i => key in keys )
638+
m.set(key, values[i]);
639+
return m;
640+
}
641+
error(ECustom("Invalid map keys "+keys));
642+
return null;
643+
}
644+
627645
function get( o : Dynamic, f : String ) : Dynamic {
628646
if ( o == null ) error(EInvalidAccess(f));
629647
return {

0 commit comments

Comments
 (0)