Hello everyone, I'm new around here and I need some help about macros. I want to do the following:
Given an Array<Array<Int>> where each Int is a static inline variable which takes values between 0-31, for example:
class Class1 {
public static inline var A = 0;
public static inline var B = 1;
public static inline var C = 2;
...
}
...
var arr:Array<Array<Int>> = [ [ Class1.A, Class1.B, Class1.E ], [ Class1.A, Class1.C, Class1.D ], ... ];
I want to generate an Array<Int> encoding the prior array like following:
new_array[ i ] = ( 1 << arr[ i ][ 0 ] ) | ( 1 << arr[ i ][ 1 ] ) | ...;
This means:
new_array[ 0 ] = 1|2|16; // = (1<<Class1.A)|(1|Class1.B)|(1<<Class1.E) = (1<<0)|(1<<1)|(1<<5)
new_array[ 1 ] = 1|4|8; // = (1<<Class1.A)|(1|Class1.C)|(1<<Class1.D) = (1<<0)|(1<<2)|(1<<4)
...
The point is using it like this:
private static inline var _list:Array<Int> = Macros.encodeList( [
[ Class1.A, Class1.B, Class1.E ],
[ Class1.A, Class1.C, Class1.D ]
] );
So far I have this:
@:macro public static function encodeList( list:Array<Array<Expr>> ) : Expr {
var codes:Array<Int> = [];
var index:Int = 0;
for ( element in list ) {
for( type in element ) {
switch( type.expr ) {
case EField( e, field ):
switch( field ) { // I'd like to do this without switch but this works anyway
case "A":
codes[ index ] |= 1 << 0;
case "B":
codes[ index ] |= 1 << 1;
case "C":
codes[ index ] |= 1 << 2;
...
case "LAST":
codes[ index ] |= 1 << 31;
default:
}
default:
}
}
++index;
}
// Here I should build an expression using the array "codes"
}
Well, the point is I don't know how to build the expression. To be honest I don't fully understand the macro system. I don't know if I should return an expression or an Array<Field>. I saw the examples but I don't understand. I hope someone can help me. Thanks