-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmakejs
More file actions
executable file
·127 lines (99 loc) · 2.76 KB
/
Copy pathxmakejs
File metadata and controls
executable file
·127 lines (99 loc) · 2.76 KB
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
#/bin/bash
#
#this tool is to expand macro for js
#the common macro define like this:#define MAX 100
#use it like this:x=2*MAX;
#the function macro define like this:#define SUM(a,b) ((a)+(b))
#use it like this:x=${SUM(1,2)};
#
#note:the function macro can't nested
#
#author fangyunjiang
#date:2011-12-15
#
FILE=$1.js
cp $1 $FILE
function match()
{
local rs
rs=$(echo $1|sed -r -n '/'$2'/p')
[ -z "$rs" ] && echo 0 || echo 1
}
function connect_two_line()
{
sed -i -e:a -e '/\\$/N;s/\\\n//;ta' $FILE
}
function do_with_common_macro()
{
local LINE ORIGIN TARGET
LINE="$1"
ORIGIN=$(echo $LINE|sed -r 's/#define[[:space:]]+([_0-9a-zA-Z]+)[[:space:]]+(.*)/\1/')
TARGET=$(echo $LINE|sed -r 's/#define[[:space:]]+([_0-9a-zA-Z]+)[[:space:]]+(.*)/\2/')
echo do_with_common macro $ORIGIN
echo origin_exp : $ORIGIN
echo target_exp : $TARGET
sed -i 's#'"$ORIGIN"'#'"$TARGET"'#g' $FILE
}
function do_with_function_macro()
{
local OLINE NLINE NAME OPARAMS NPARAMS OEXP NEXP tmp i
OLINE="$1"
NAME=$(echo $OLINE|sed -r 's/#define[[:space:]]+([_0-9a-zA-Z]+)[[:space:]]*.*/\1/')
OEXP=$(echo $OLINE|sed -r 's/#define[[:space:]]+[_0-9a-zA-Z]+[[:space:]]*\(([^()]*)\)[[:space:]]+(.*)/\2/')
tmp=$(echo $OLINE|sed -r 's/#define[[:space:]]+[_0-9a-zA-Z]+[[:space:]]*\(([^()]*)\).*/\1/')
eval $(echo $tmp|awk '{split($0,t,",")} END {for(i in t) print "OPARAMS["i"]="t[i]}')
echo do_with_function_macro : $NAME
echo old_exp : $OEXP
echo old_params : ${OPARAMS[*]}
echo
echo
while [ 1 ];do
NLINE=$(sed -n '/\${'"$NAME"'/{p;q}' $FILE)
if [ -z "$NLINE" ];then
break
fi
NEXP=$(echo $NLINE|sed -r 's/.*(\$\{'"$NAME"'[^}]*\}).*/\1/')
echo new_exp : $NEXP
tmp=$(echo $NEXP|sed -r 's/.*'"$NAME"'[[:space:]]*\((.*)\)[[:space:]]*\}/\1/'|sed -r 's/\(/@\[/g;s/\)/@\]/g')
eval $(echo $tmp|awk '{split($0,t,",")} END {for(i in t) print "NPARAMS["i"]="t[i]}')
echo new_params : ${NPARAMS[*]}
if (( ${#OPARAMS[*]} != ${#NPARAMS[*]} ));then
echo error:macro params num error
exit
fi
tmp="$OEXP"
for ((i=1;i<=${#NPARAMS[*]};i++));do
tmp=$(echo $tmp|sed -r 's/'"${OPARAMS[$i]}"'/'"${NPARAMS[$i]}"'/g')
done
tmp=$(echo $tmp|sed -r 's/@\[/\(/g;s/@\]/\)/g')
echo replace_exp : $tmp
sed -i 's#'"$NEXP"'#'"$tmp"'#g' $FILE
echo
echo
done
}
function parse_all()
{
while [ 1 ];do
LINE=$(sed -n '/#define/{p;q}' $FILE)
if [ -z "$LINE" ];then
break
fi
sed -i '0,/#define/{/#define/d}' $FILE
n=$(match "$LINE" "#define[[:space:]]+[_0-9a-zA-Z]+\(.*\)")
if [ $n -eq 0 ];then
do_with_common_macro "$LINE"
else
do_with_function_macro "$LINE"
fi
done
}
function main()
{
connect_two_line
parse_all
sed -i '/^[[:space:]]*$/d' $FILE
echo //////////////////////outfile//////////////////////////////////
cat $FILE
}
main