Fix ON GOTO and ON GOSUB - Should resolve #11 (#12)

* Fix ON GOTO and ON GOSUB - Should resolve #11

* Fix ON GOTO and ON GOSUB better - Should handle edge cases

* Fix ON GOTO and ON GOSUB - Added unit tests

* Fix ON GOTO and ON GOSUB - Fixed unit tests
This commit is contained in:
Sigurður Sveinn Halldórsson 2016-07-26 04:14:33 +02:00 committed by Joshua Bell
parent 26bb151874
commit d53e58db23
2 changed files with 93 additions and 42 deletions

View File

@ -523,13 +523,16 @@ this.basic = (function() {
}, },
'on_goto': function ON_GOTO(index /* , ...lines */) { 'on_goto': function ON_GOTO(index /* , ...lines */) {
index = (index - 1) >> 0; index = Math.floor(index)
var lines = Array.prototype.slice.call(arguments, 1); if (index < 0 || index > 255) {
if (index < 0 || index >= lines.length) {
runtime_error(ERRORS.ILLEGAL_QUANTITY); runtime_error(ERRORS.ILLEGAL_QUANTITY);
} }
throw new GoToLine(lines[index]); --index;
var lines = Array.prototype.slice.call(arguments, 1);
if (index >= 0 && index < lines.length) {
throw new GoToLine(lines[index]);
}
}, },
'gosub': function GOSUB(line) { 'gosub': function GOSUB(line) {
@ -541,16 +544,19 @@ this.basic = (function() {
}, },
'on_gosub': function ON_GOSUB(index /* , ...lines */) { 'on_gosub': function ON_GOSUB(index /* , ...lines */) {
index = (index - 1) >> 0; index = Math.floor(index)
var lines = Array.prototype.slice.call(arguments, 1); if (index < 0 || index > 255) {
if (index < 0 || index >= lines.length) {
runtime_error(ERRORS.ILLEGAL_QUANTITY); runtime_error(ERRORS.ILLEGAL_QUANTITY);
} }
state.stack.push({ --index;
gosub_return: state.stmt_index, var lines = Array.prototype.slice.call(arguments, 1);
line_number: state.line_number if (index >= 0 && index < lines.length) {
}); state.stack.push({
throw new GoToLine(lines[index]); gosub_return: state.stmt_index,
line_number: state.line_number
});
throw new GoToLine(lines[index]);
}
}, },
'return': function RETURN() { 'return': function RETURN() {

View File

@ -69,57 +69,102 @@
2031 T = T + 1 2031 T = T + 1
2032 T = T + 1 2032 T = T + 1
2033 T = T + 1 2033 T = T + 1
2039 S = (T=3) : GOSUB 1 2034 S = (T=3) : GOSUB 1
2035 T$ = "ON GOTO (EDGE CASE: 0)" : T = 1 : ONERR GOTO 2039
2036 ON 0 GOTO 2037, 2038 : T = T + 3
2037 T = T + 1
2038 T = T + 1
2039 S = (T=6) : POKE 216,0 : GOSUB 1
2040 T$ = "ON GOTO (EDGE CASE: -0.1)" : T = 1 : ONERR GOTO 2044
2041 ON -0.1 GOTO 2042, 2043 : T = T + 3
2042 T = T + 1
2043 T = T + 1
2044 S = (T=1) AND (PEEK(222)=53): POKE 216,0 : GOSUB 1
2040 T$ = "ON GOSUB" : T = 1 : ON 2 GOSUB 2041, 2042, 2043 : GOTO 2049
2041 T = T + 1 : RETURN
2042 T = T + 2 : RETURN
2043 T = T + 3 : RETURN
2049 S = (T=3) : GOSUB 1
2050 T$ = "POP" : T = 1 : GOSUB 2055 : T = T + 1 : GOTO 2059 2045 T$ = "ON GOTO (EDGE CASE: 255.2)" : T = 1 : ONERR GOTO 2049
2055 T = T + 1 : GOSUB 2056 : T = T + 5 2046 ON 255.2 GOTO 2047, 2048 : T = T + 3
2056 POP : T = T + 1 : RETURN 2047 T = T + 1
2059 S = (T=4) : GOSUB 1 2048 T = T + 1
2049 S = (T=6) : POKE 216,0 : GOSUB 1
2050 T$ = "ON GOTO (EDGE CASE: 256)" : T = 1 : ONERR GOTO 2054
2051 ON -0.1 GOTO 2052, 2053 : T = T + 3
2052 T = T + 1
2053 T = T + 1
2054 S = (T=1) AND (PEEK(222)=53): POKE 216,0 : GOSUB 1
2060 T$ = "FOR"
2060 T$ = "ON GOSUB" : T = 1 : ON 2 GOSUB 2061, 2062, 2063 : GOTO 2064
2061 T = T + 1 : RETURN
2062 T = T + 2 : RETURN
2063 T = T + 3 : RETURN
2064 S = (T=3) : GOSUB 1
2065 T$ = "ON GOSUB (EDGE CASE: 0)" : T = 1 : ONERR GOTO 2069
2066 ON 0 GOSUB 2067, 2068 : GOTO 2070
2067 T = T + 2 : RETURN
2068 T = T + 3 : RETURN
2069 T = 255
2070 S = (T=1) : POKE 216,0 : GOSUB 1
2071 T$ = "ON GOSUB (EDGE CASE: -0.1)" : T = 1 : ONERR GOTO 2075
2072 ON -0.1 GOSUB 2073, 2074 : T = T + 5 : GOTO 2075
2073 T = T + 2 : RETURN
2074 T = T + 3 : RETURN
2075 S = (T=1) AND (PEEK(222)=53): POKE 216,0 : GOSUB 1
2076 T$ = "ON GOSUB (EDGE CASE: 255.2)" : T = 1 : ONERR GOTO 2080
2077 ON 255.2 GOSUB 2078, 2079 : GOTO 2081
2078 T = T + 2 : RETURN
2079 T = T + 3 : RETURN
2080 T = 255
2081 S = (T=1) : POKE 216,0 : GOSUB 1
2082 T$ = "ON GOSUB (EDGE CASE: 256)" : T = 1 : ONERR GOTO 2086
2083 ON 256 GOSUB 2084, 2085 : T = T + 5 : GOTO 2086
2084 T = T + 2 : RETURN
2085 T = T + 3 : RETURN
2086 S = (T=1) AND (PEEK(222)=53): POKE 216,0 : GOSUB 1
2150 T$ = "POP" : T = 1 : GOSUB 2155 : T = T + 1 : GOTO 2159
2155 T = T + 1 : GOSUB 2156 : T = T + 5
2156 POP : T = T + 1 : RETURN
2159 S = (T=4) : GOSUB 1
2160 T$ = "FOR"
: T = 0 : FOR I = 1 TO 10 : T = T + I : NEXT : T = 0 : FOR I = 1 TO 10 : T = T + I : NEXT
: S = (T = 55) : GOSUB 1 : S = (T = 55) : GOSUB 1
2061 T$ = "FOR STEP" 2161 T$ = "FOR STEP"
: T = 0 : FOR I = 1 TO 10 STEP 2 : T = T + I : NEXT : T = 0 : FOR I = 1 TO 10 STEP 2 : T = T + I : NEXT
: S = (T = 25) : GOSUB 1 : S = (T = 25) : GOSUB 1
2062 T$ = "FOR STEP" 2162 T$ = "FOR STEP"
: T = 0 : FOR I = 10 TO 1 STEP -1 : T = T + I : NEXT : T = 0 : FOR I = 10 TO 1 STEP -1 : T = T + I : NEXT
: S = (T = 55) : GOSUB 1 : S = (T = 55) : GOSUB 1
2063 T$ = "FOR STEP" 2163 T$ = "FOR STEP"
: T = 0 : FOR I = 10 TO 1 : T = T + I : NEXT : T = 0 : FOR I = 10 TO 1 : T = T + I : NEXT
: S = (T = 10) : GOSUB 1 : S = (T = 10) : GOSUB 1
2070 T$ = "NEXT" 2170 T$ = "NEXT"
: T = 0 : FOR I = 1 TO 10 : FOR J = 1 TO 10 : FOR K = 1 TO 10 : T = T + 1 : NEXT J, I : T = 0 : FOR I = 1 TO 10 : FOR J = 1 TO 10 : FOR K = 1 TO 10 : T = T + 1 : NEXT J, I
: S = (T=100) : GOSUB 1 : S = (T=100) : GOSUB 1
2080 T$ = "IF THEN" 2180 T$ = "IF THEN"
: T = 1 : IF 0 THEN T = 2 : T = 1 : IF 0 THEN T = 2
2081 S = (T=1) : GOSUB 1 2181 S = (T=1) : GOSUB 1
: T = 1 : IF 1 THEN T = 2 : T = 3 : T = 1 : IF 1 THEN T = 2 : T = 3
2082 S = (T=3) : GOSUB 1 2182 S = (T=3) : GOSUB 1
2090 T$ = "IF GOTO" 2190 T$ = "IF GOTO"
: T = 1 : IF 0 GOTO 2092 : T = 2 : T = 1 : IF 0 GOTO 2192 : T = 2
2091 T = 3 2191 T = 3
2092 S = (T=3) : GOSUB 1 2192 S = (T=3) : GOSUB 1
: T = 1 : IF 1 GOTO 2094 : T = 2 : T = 1 : IF 1 GOTO 2194 : T = 2
2093 T = 3 2193 T = 3
2094 S = (T=1) : GOSUB 1 2194 S = (T=1) : GOSUB 1
2100 T$ = "Empty String is False" 2200 T$ = "Empty String is False"
: T = 1 : IF "" THEN T = 2 : T = 1 : IF "" THEN T = 2
2101 S = (T=1) : GOSUB 1 2201 S = (T=1) : GOSUB 1
2110 T$ = "Non-Empty String is True" 2210 T$ = "Non-Empty String is True"
: T = 1 : IF "abc" THEN T = 2 : T = 1 : IF "abc" THEN T = 2
2111 S = (T=2) : GOSUB 1 2211 S = (T=2) : GOSUB 1
3000 PRINT : PRINT "Error Handling "; 3000 PRINT : PRINT "Error Handling ";