|
| 1 | +table_contains = (list, target) -> |
| 2 | + for elem in *list |
| 3 | + return true if elem == target |
| 4 | + false |
| 5 | + |
| 6 | +int_list = (list) -> "{#{table.concat list, ', '}}" |
| 7 | + |
| 8 | +instruction_list = (list, level) -> |
| 9 | + if #list <= 2 |
| 10 | + "{#{table.concat [quote elem for elem in *list], ', '}}" |
| 11 | + else |
| 12 | + instrs = [indent quote(elem) .. ',', level + 1 for elem in *list] |
| 13 | + table.insert instrs, 1, '{' |
| 14 | + table.insert instrs, indent('}', level) |
| 15 | + table.concat instrs, '\n' |
| 16 | + |
| 17 | + |
| 18 | +{ |
| 19 | + module_name: 'Forth', |
| 20 | + |
| 21 | + generate_test: (case, level) -> |
| 22 | + local lines |
| 23 | + if case.scenarios and table_contains case.scenarios, 'local-scope' |
| 24 | + lines = { |
| 25 | + "interp1 = Forth!", |
| 26 | + "interp2 = Forth!", |
| 27 | + "interp1\\evaluate #{instruction_list case.input.instructionsFirst, level}", |
| 28 | + "interp2\\evaluate #{instruction_list case.input.instructionsSecond, level}", |
| 29 | + "assert.are.same #{int_list case.expected[1]}, interp1\\stack!", |
| 30 | + "assert.are.same #{int_list case.expected[2]}, interp2\\stack!", |
| 31 | + } |
| 32 | + |
| 33 | + else |
| 34 | + lines = { |
| 35 | + 'interpreter = Forth!', |
| 36 | + "instructions = #{instruction_list case.input.instructions, level}", |
| 37 | + } |
| 38 | + |
| 39 | + if case.expected.error |
| 40 | + table.insert lines, line for line in *{ |
| 41 | + 'f = -> interpreter\\evaluate instructions', |
| 42 | + "assert.has.errors f, #{quote case.expected.error}" |
| 43 | + } |
| 44 | + |
| 45 | + else |
| 46 | + table.insert lines, line for line in *{ |
| 47 | + 'interpreter\\evaluate instructions', |
| 48 | + "expected = #{int_list case.expected}", |
| 49 | + 'assert.is.same expected, interpreter\\stack!' |
| 50 | + } |
| 51 | + |
| 52 | + table.concat [indent line, level for line in *lines], '\n' |
| 53 | +} |
0 commit comments