-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_js.def
126 lines (110 loc) · 3.45 KB
/
output_js.def
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
include: output.def
page: output_js, output_main
type: pm
output_dir: lib/MyDef
ext: js
package: MyDef::output_js
subcode:: init_page
$global %js_globals, @js_globals
%js_globals=()
@js_globals=()
subcode:: dumpout
dump_js_globals()
fncode: dump_js_globals
my $block=MyDef::compileutil::get_named_block("js_init")
$foreach $v in @js_globals
push @$block, "var $v;\n"
#------------------------------------
subcode: parsecode
$call parsecode_common
subcode: parsecode_func_param
# $call @js_parse_func_param
subcode: parsecode_other
$if $l!~/[:\(\{\};,]\s*$/
$l.=';'
subcode: parsecode_special
$elif $l=~/^(\S+)\s*=\s*"(.*\$\w+.*)"\s*$/
push @$out, "$1=". js_string($2)
return
subcode: parsecode_func
my $param1=""
my $param2=$param
$if $func eq "global"
$param=~s/\s*;\s*$//
my @tlist=MyDef::utils::proper_split($param)
$foreach $v in @tlist
$if !$js_globals{$v}
$js_globals{$v}=1
push @js_globals, $v
return
$elif $func =~ /^(function)$/
return single_block("$1 $param\{", "}")
$elif $func =~ /^(if|while|switch|with)$/
return single_block("$1($param){", "}")
$elif $func =~ /^(el|els|else)if$/
return single_block("else if($param){", "}")
$elif $func eq "else"
return single_block("else{", "}")
$elif $func eq "for" or $func eq "foreach"
$call js_parse_for
$call @parsecode_func_other
subcode: js_parse_for
$if $param=~/(\w+)=(.*?):(.*?)(:.*)?$/
my ($var, $i0, $i1, $step)=($1, $2, $3, $4);
my $stepclause;
$if $step
my $t=substr($step, 1);
$if $t eq "-1"
$stepclause="var $var=$i0;$var>$i1;$var--";
$elif $t=~/^-/
$stepclause="var $var=$i0;$var>$i1;$var=$var$t";
$else
$stepclause="var $var=$i0;$var<$i1;$var+=$t";
$else
$if $i1 eq "0"
$stepclause="var $var=$i0-1;$var>=0;$var--";
$elif $i1=~/^-?\d+/ and $i0=~/^-?\d+/ and $i1<$i0
$stepclause="var $var=$i0;$var>$i1;$var--";
$else
$stepclause="var $var=$i0;$var<$i1;$var++";
return single_block("for($stepclause){", "}")
$elif $param=~/^(\S+)$/
$call set_macro, "item", "$1\[i]"
return single_block("for(i=0;i<$1.length;i++){", "}")
$else
return single_block("$func($param){", "}")
# -------------------------
fncode: js_string($t)
my @parts=split /(\$\w+)/, $t
$if $parts[0]=~/^$/
shift @parts
$for $i=0:@parts
$if $parts[$i]=~/^\$(\w+)/
$parts[$i]=$1
$while $parts[$i+1]=~/^(\[.*?\])/
$parts[$i].=$1
$parts[$i+1]=$'
$else
$parts[$i]= "\"$parts[$i]\""
return join(' + ', @parts)
#---- $print ------------------
include: macros_output/print.def
subcode:: parsecode_func_other
$elif $func eq "print"
$param=~s/^\s+//
my ($n, $fmt)=fmt_string($param, 0)
$call get_print_to
$if !$print_to
push @$out, "console.log($fmt)"
$else
push @$out, "console.$print_to($fmt)"
return
$elif $func eq "dump"
push @$out, "console.log($param)"
return
fncode: find_var($v)
return {direct=>"\"+$v+\""}
macros:
printf_formats: s
fncode: get_var_fmt($v, $warn)
return '%s'