@@ -8,45 +8,63 @@ rl = readline.createInterface do
8
8
isDigit = (n )->
9
9
return n >='0' &&n <='9'
10
10
11
- put = (needle ,haystack ,ins )->
12
-
13
-
14
11
class State
15
- chunk :''
12
+ line :''
16
13
ins :0
17
14
depth :0
18
- code :''
15
+ chunk :''
19
16
wrap :false
20
17
chain :''
21
- putraw : (tk )->
18
+ pending : []
19
+ put : (tk ,chain )->
22
20
#if @wrap
23
- # @chunk = tk + '(' + @chunk + ')'
21
+ # @line = tk + '(' + @line + ')'
24
22
# @wrap = false
25
23
#else
26
- @chunk = @chunk .substr(0 ,@ins ) +tk + @chunk .substr(@ins )
24
+ if @chain
25
+ @line = @line .substr(0 ,@ins ) + @chain + @line .substr(@ins )
26
+ @ins += @chain .length
27
+ @chain =''
28
+ @line = @line .substr(0 ,@ins ) +tk + @line .substr(@ins )
27
29
@ins +=tk .length
30
+ @chain =chain
28
31
@depth ++
29
- put : (tk )->
32
+ putfunc : (tk )->
30
33
if @wrap
31
- @chunk =tk +'(' + @chunk +')'
34
+ @line =tk +'(' + @line +')'
32
35
@wrap =false
33
36
else
34
37
if @chain
35
- @chunk = @chunk .substr(0 ,@ins ) + @chain + @chunk .substr(@ins )
38
+ @line = @line .substr(0 ,@ins ) + @chain + @line .substr(@ins )
36
39
@ins += @chain .length
37
40
@chain =''
38
- @chunk = @chunk .substr(0 ,@ins ) +tk +"()" + @chunk .substr(@ins )
41
+ @line = @line .substr(0 ,@ins ) +tk +"()" + @line .substr(@ins )
39
42
@ins +=tk .length +1
40
43
@depth ++
44
+ pend : ->
45
+ if @line
46
+ @pending .unshift(@line +';' )
47
+ @line =''
48
+ @ins =0
49
+ @depth =0
50
+ @chain =''
51
+ @wrap =false
41
52
push : ->
42
- if @chunk
43
- console .log @chunk +';'
44
- @code += @chunk +';\n ' ;
45
- @chunk =''
53
+ if @line
54
+ console .log @line +';'
55
+ @chunk += @line +';\n '
56
+ @line =''
57
+ if @pending
58
+ for line in @pending
59
+ console .logline
60
+ #console.log @pending.join('\n')
61
+ @chunk += @pending .join('\n ' )
62
+ @pending = []
46
63
@ins =0
47
64
@depth =0
65
+ @chain =''
66
+ @wrap =false
48
67
49
- code =''
50
68
iter = (cb )->
51
69
line <-rl .question'iox> '
52
70
tokens =line .split(' ' )
@@ -58,43 +76,59 @@ iter = (cb)->
58
76
state =new State()
59
77
60
78
i =rtokens .length-1
61
- for tk in rtokens
79
+ for i from 0 to rtokens .length-1
80
+ tk =rtokens [i ]
62
81
82
+ left =void
63
83
try
64
- left =rtokens [i - 1 ]
84
+ left =rtokens [i + 1 ]
65
85
if left [left .length-1 ]==','
66
86
# combine
67
87
void
68
88
69
- console .logstate .chunk
70
89
tk0 =tk [0 ]
90
+ tke =tk [tk .length-1 ]
71
91
if tk0 =='$'
72
- if state .depth
73
- state .putraw(tk .substr(1 ))
74
- state .chain =' = '
75
- else
76
- state .putraw(tk .substr(1 ))
92
+ #if state.depth
93
+ state .put(tk .substr(1 ),' = ' )
94
+ #else
95
+ # state.put(tk.substr(1))
77
96
else if isDigit(tk0 )
78
97
state .put(tk )
98
+ state .pend()
99
+ else if tk0 =='\\ '
100
+ state .put("'" +tk .substr(1 ) +"'" )
101
+ else if tke =='"'
102
+ void
103
+ #t = tk
104
+ #s = ''
105
+ #until t[0]=='"'
106
+ # s += t
107
+ # i += 1
108
+ # t = rtokens[i]
109
+ #state.put(tk)
79
110
else if tk0 =='['
80
111
# parse array/range
81
- state .put( tk )
112
+ void
82
113
else if tk0 =='('
83
114
# parse range
84
115
void
85
116
else if tk0 =='{'
86
117
# parse dict
87
- state .put( tk )
118
+ void
88
119
else if tk0 =='-'
89
120
# flag
90
121
void
91
122
else if tk0 =='+'
92
123
# flag
93
124
void
94
- else # function?
95
- state .put(tk )
96
-
97
- i --
125
+ else if tk0 ==';'
126
+ state .pend()
127
+ else # function
128
+ if tk =='out'
129
+ state .putfunc('console.log' )
130
+ else
131
+ state .putfunc(tk )
98
132
99
133
state .push()
100
134
return cb !