@@ -30,6 +30,7 @@ const EasyCoder_IWSY = {
30
30
}
31
31
break ;
32
32
case `init` :
33
+ case `stop` :
33
34
compiler . next ( ) ;
34
35
compiler . addCommand ( {
35
36
domain : `iwsy` ,
@@ -38,6 +39,16 @@ const EasyCoder_IWSY = {
38
39
action
39
40
} ) ;
40
41
return true ;
42
+ case `script` :
43
+ const script = compiler . getNextValue ( ) ;
44
+ compiler . addCommand ( {
45
+ domain : `iwsy` ,
46
+ keyword : `iwsy` ,
47
+ lino,
48
+ action,
49
+ script
50
+ } ) ;
51
+ return true ;
41
52
case `goto` :
42
53
const target = compiler . getNextValue ( ) ;
43
54
compiler . addCommand ( {
@@ -48,16 +59,47 @@ const EasyCoder_IWSY = {
48
59
target
49
60
} ) ;
50
61
return true ;
51
- case `script` :
52
- const script = compiler . getNextValue ( ) ;
62
+ case `run` :
63
+ const pc = compiler . getPc ( ) ;
64
+ compiler . next ( ) ;
53
65
compiler . addCommand ( {
54
66
domain : `iwsy` ,
55
67
keyword : `iwsy` ,
56
68
lino,
57
69
action,
58
- script
70
+ then : 0
59
71
} ) ;
72
+ // Get the 'then' code, if any
73
+ if ( compiler . tokenIs ( `then` ) ) {
74
+ const goto = compiler . getPc ( ) ;
75
+ // Add a 'goto' to skip the 'then'
76
+ compiler . addCommand ( {
77
+ domain : `core` ,
78
+ keyword : `goto` ,
79
+ goto : 0
80
+ } ) ;
81
+ // Fixup the link to the 'then' branch
82
+ compiler . getCommandAt ( pc ) . then = compiler . getPc ( ) ;
83
+ // Process the 'then' branch
84
+ compiler . next ( ) ;
85
+ compiler . compileOne ( true ) ;
86
+ compiler . addCommand ( {
87
+ domain : `core` ,
88
+ keyword : `stop`
89
+ } ) ;
90
+ // Fixup the 'goto'
91
+ compiler . getCommandAt ( goto ) . goto = compiler . getPc ( ) ;
92
+ }
60
93
return true ;
94
+ case `onstep` :
95
+ compiler . next ( ) ;
96
+ compiler . addCommand ( {
97
+ domain : `iwsy` ,
98
+ keyword : `iwsy` ,
99
+ lino,
100
+ action
101
+ } ) ;
102
+ return compiler . completeHandler ( ) ;
61
103
default :
62
104
break ;
63
105
}
@@ -67,6 +109,7 @@ const EasyCoder_IWSY = {
67
109
run : ( program ) => {
68
110
const command = program [ program . pc ] ;
69
111
const action = command . action ;
112
+ let script ;
70
113
switch ( action ) {
71
114
case `init` :
72
115
program . require ( `js` , `iwsy.js` ,
@@ -80,16 +123,53 @@ const EasyCoder_IWSY = {
80
123
player . innerHTML = `` ;
81
124
player . style . background = `none` ;
82
125
player . style . border = `none` ;
83
- const script = program . getValue ( command . script ) ;
84
- EasyCoder . iwsyFunctions = IWSY ( player , JSON . parse ( script ) ) ;
126
+ script = program . getValue ( command . script ) ;
127
+ try {
128
+ script = JSON . parse ( script ) ;
129
+ EasyCoder . iwsyFunctions = IWSY ( player , script ) ;
130
+ } catch ( err ) {
131
+ alert ( `Badly formatted script` ) ;
132
+ }
133
+ break ;
134
+ case `script` :
135
+ script = program . getValue ( command . script ) ;
136
+ try {
137
+ script = JSON . parse ( script ) ;
138
+ if ( EasyCoder . iwsyFunctions ) {
139
+ EasyCoder . iwsyFunctions . setScript ( script ) ;
140
+ }
141
+ } catch ( err ) {
142
+ alert ( `Badly formatted script` ) ;
143
+ }
85
144
break ;
86
145
case `goto` :
87
- EasyCoder . iwsyFunctions . gotoStep ( program . getValue ( command . target ) ) ;
146
+ if ( EasyCoder . iwsyFunctions ) {
147
+ EasyCoder . iwsyFunctions . gotoStep ( program . getValue ( command . target ) ) ;
148
+ }
88
149
break ;
89
- case `script` :
90
- EasyCoder . iwsyFunctions . setScript ( JSON . parse ( program . getValue ( command . script ) ) ) ;
150
+ case `run` :
151
+ if ( EasyCoder . iwsyFunctions ) {
152
+ EasyCoder . iwsyFunctions . run ( function ( ) {
153
+ program . run ( command . then ) ;
154
+ } ) ;
155
+ return 0 ;
156
+ }
157
+ break ;
158
+ case `stop` :
159
+ if ( EasyCoder . iwsyFunctions ) {
160
+ EasyCoder . iwsyFunctions . stop ( ) ;
161
+ }
162
+ break ;
163
+ case `onstep` :
164
+ const cb = command . pc + 2 ;
165
+ if ( EasyCoder . iwsyFunctions ) {
166
+ EasyCoder . iwsyFunctions . onStep ( function ( step ) {
167
+ program . iwsyStep = step ;
168
+ program . run ( cb ) ;
169
+ } ) ;
170
+ }
91
171
break ;
92
- }
172
+ }
93
173
return command . pc + 1 ;
94
174
}
95
175
} ,
0 commit comments