'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(); });