diff options
Diffstat (limited to 'src/parser/parser.test.js')
-rw-r--r-- | src/parser/parser.test.js | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/parser/parser.test.js b/src/parser/parser.test.js new file mode 100644 index 0000000..b496cdc --- /dev/null +++ b/src/parser/parser.test.js @@ -0,0 +1,109 @@ +'use strict'; + +const { assemble } = require('./parser.js'); + + +test('assemble the imp', () => { + expect(assemble(` + mov 0, 1 + end + `)).toEqual({ + start: 0, + program: [ + { + opcode: 'MOV', + a: { mode: 'direct', value: 0 }, + b: { mode: 'direct', value: 1 }, + }, + ], + }); +}); + + +test('assemble dwarf', () => { + expect(assemble(` + bomb dat #0 + dwarf add #4, bomb + mov bomb, @bomb + jmp dwarf + end dwarf + `)).toEqual({ + start: 1, + program: [ + { opcode: 'DAT', + a: { mode: 'immediate', value: 0 }, + b: { mode: 'immediate', value: 0 }, + }, + { opcode: 'ADD', + a: { mode: 'immediate', value: 4 }, + b: { mode: 'direct', value: -1 }, + }, + { opcode: 'MOV', + a: { mode: 'direct', value: -2 }, + b: { mode: 'indirect', value: -2 }, + }, + { opcode: 'JMP', + a: { mode: 'direct', value: -2 }, + b: { mode: 'direct', value: 0 }, + }, + ], + }); +}); + + +test('assemble armadillo', () => { + expect(assemble(` + ; armadillo + ; author: stefan strack + + bomb spl 0 + loop add #3039, ptr + ptr mov bomb, 81 + jmp loop + mov 1, <-1 + end bomb + `)).toEqual({ + start: 0, + program: [ + { opcode: 'SPL', + a: { mode: 'direct', value: 0 }, + b: { mode: 'direct', value: 0 }, + }, + { opcode: 'ADD', + a: { mode: 'immediate', value: 3039 }, + b: { mode: 'direct', value: 1 }, + }, + { opcode: 'MOV', + a: { mode: 'direct', value: -2 }, + b: { mode: 'direct', value: 81 }, + }, + { opcode: 'JMP', + a: { mode: 'direct', value: -2 }, + b: { mode: 'direct', value: 0 }, + }, + { opcode: 'MOV', + a: { mode: 'direct', value: 1 }, + b: { mode: 'predecrement', value: -1 }, + }, + ], + }); +}); + + +test('illegal instructions are not assembled', () => { + expect(() => assemble("dat 0, #0\nend")).toThrow(); + expect(() => assemble("dat @0, #0\nend")).toThrow(); + + expect(() => assemble("mov 0, #0\nend")).toThrow(); + expect(() => assemble("add 0, #0\nend")).toThrow(); + expect(() => assemble("sub 0, #0\nend")).toThrow(); + expect(() => assemble("cmp 0, #0\nend")).toThrow(); + expect(() => assemble("slt 0, #0\nend")).toThrow(); + + expect(() => assemble("jmp #0, 0\nend")).toThrow(); + expect(() => assemble("jmz #0, 0\nend")).toThrow(); + expect(() => assemble("jmn #0, 0\nend")).toThrow(); + expect(() => assemble("jmn #0, 0\nend")).toThrow(); + expect(() => assemble("djn #0, 0\nend")).toThrow(); + expect(() => assemble("spl #0, 0\nend")).toThrow(); +}); |