-
Notifications
You must be signed in to change notification settings - Fork 0
/
test09.js
95 lines (77 loc) · 1.93 KB
/
test09.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// define a macro called defTestMacro that looks like a function call
JaM.defTestMacro( function( intree ) {
if( intree[ 0 ] && intree[ 0 ].value == 'defTestMacro' ) {
intree.shift(); // "defTestMacro"
var arglist = intree.shift();
var curlyblock = intree.shift();
var param = arglist[ 1 ][ 0 ][ 0 ];
var test = arglist[ 1 ][ 1 ];
var block = curlyblock[ 1 ];
intree.unshift( {{
JaM.defTestMacro( function( #param ) {
if( #test ) {
#block
return true;
}
return false;
});
}} );
return true;
}
return false;
});
defTestMacro( intree, intree[ 0 ] && intree[ 0 ].value == 'rubyDef' ) {
intree.shift(); // rubyDef
var rubyfuncname = intree.shift();
var argparens = intree.shift();
var curlybody = intree.shift();
//var stringname = JaM.it( '(string)', "'" + rubyfuncname.value + "'" );
var arglist = argparens[1];
var rubyfunc = JaM.genSym();
intree.unshift( {{
function #rubyfunc( yield, #arglist )
#curlybody
}} );
defTestMacro( ctree, ctree[ 0 ] && ctree[ 0 ].value == rubyfuncname.value ) {
ctree.shift(); // rubyfuncname
var argparens = ctree.shift();
var curlybody = ctree.shift();
var caargs = argparens[1];
var cbody = curlybody[1];
var cfargs = cbody[ 0 ].shift();
ctree.unshift( {{
#rubyfunc(
function #cfargs {
#cbody
},
#caargs
);
}} );
}
}
/*
function rubyfunc( yield, ary ) {
for( var i = 0; i < ary.length; ++i ) {
yield( ary[ i ], i );
}
}
*/
rubyDef eachArrayElement( ary ) {
for( var i = 0; i < ary.length; ++i ) {
yield( ary[ i ], i );
}
}
/*
rubyfunc(
function( v, k ) {
JaM.msg( k + ": " + v );
},
[ 'a', 'b', 'c', 'd' ]
);
*/
eachArrayElement( [ 'a', 'b', 'c', 'd' ] ) { ( v, k )
JaM.msg( k + ": " + v );
}
eachArrayElement( [ 'foo', 'bar' ] ) { ( name, num )
JaM.msg( name + " is number " + num );
}