Skip to content

Commit

Permalink
refactor(assembler/parser): mv some checking to tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Dec 11, 2023
1 parent 43fcbf3 commit 5b3becb
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 226 deletions.
9 changes: 9 additions & 0 deletions __tests__/common/utils/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
decToBin,
isFunction,
noop,
pipe,
range,
splitCamelCaseToString,
throttle,
Expand Down Expand Up @@ -107,6 +108,14 @@ describe('curryRight2', () => {
})
})

describe('pipe', () => {
it('should pipe functions', () => {
const add1 = (a: number): number => a + 1
const multiply2 = (a: number): number => a * 2
expect(pipe(add1, multiply2)(1)).toBe(4)
})
})

describe('throttle', () => {
it('should throttle a function', () => {
const fn = jest.fn()
Expand Down
116 changes: 0 additions & 116 deletions __tests__/features/assembler/__snapshots__/tokenizer.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,121 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`tokenizer should emit token with type \`Unknown\` when tokenizing address if closing bracket is missing 1`] = `
[
{
"range": {
"from": 0,
"to": 3,
},
"raw": "[00",
"type": "Unknown",
"value": "[00",
},
]
`;

exports[`tokenizer should emit token with type \`Unknown\` when tokenizing address if closing bracket is missing 2`] = `
[
{
"range": {
"from": 0,
"to": 3,
},
"raw": "[al",
"type": "Unknown",
"value": "[AL",
},
]
`;

exports[`tokenizer should emit token with type \`Unknown\` when tokenizing address if closing bracket is missing 3`] = `
[
{
"range": {
"from": 0,
"to": 3,
},
"raw": "[Bl",
"type": "Unknown",
"value": "[BL",
},
]
`;

exports[`tokenizer should emit token with type \`Unknown\` when tokenizing address if closing bracket is missing 4`] = `
[
{
"range": {
"from": 0,
"to": 3,
},
"raw": "[cL",
"type": "Unknown",
"value": "[CL",
},
]
`;

exports[`tokenizer should emit token with type \`Unknown\` when tokenizing address if closing bracket is missing 5`] = `
[
{
"range": {
"from": 0,
"to": 4,
},
"raw": "[ DL",
"type": "Unknown",
"value": "[ DL",
},
]
`;

exports[`tokenizer should emit token with type \`Unknown\` when tokenizing string if closing quote is missing 1`] = `
[
{
"range": {
"from": 0,
"to": 3,
},
"raw": "\\"\\\\\\"",
"type": "Unknown",
"value": "\\"\\\\\\"",
},
]
`;

exports[`tokenizer should emit token with type \`Unknown\` when tokenizing string if closing quote is missing 2`] = `
[
{
"range": {
"from": 0,
"to": 4,
},
"raw": "\\"foo",
"type": "Unknown",
"value": "\\"FOO",
},
{
"range": {
"from": 5,
"to": 8,
},
"raw": "bar",
"type": "Unknown",
"value": "BAR",
},
{
"range": {
"from": 8,
"to": 9,
},
"raw": "\\"",
"type": "Unknown",
"value": "\\"",
},
]
`;

exports[`tokenizer should remove invalid escape 1`] = `
[
{
Expand Down
8 changes: 4 additions & 4 deletions __tests__/features/assembler/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ start: inc al
start: dec bl
end
`)
}).toThrowError("Duplicate label 'START'")
}).toThrowErrorMatchingInlineSnapshot(`"Duplicate label 'START'."`)
})

it('should throw EndOfMemoryError', () => {
Expand All @@ -40,13 +40,13 @@ org ff
inc al
end
`)
}).toThrowError('Can not generate code beyond the end of RAM')
}).toThrowErrorMatchingInlineSnapshot(`"Can not generate code beyond the end of RAM."`)
})

it('should throw LabelNotExistError', () => {
expect(() => {
assemble('jmp start end')
}).toThrowError("Label 'start' does not exist")
}).toThrowErrorMatchingInlineSnapshot(`"Label 'start' does not exist."`)
})

it('should throw JumpDistanceError', () => {
Expand All @@ -58,6 +58,6 @@ org fd
jmp start
end
`)
}).toThrowError('Jump distance should be between -128 and 127')
}).toThrowErrorMatchingInlineSnapshot(`"Jump distance should be between -128 and 127."`)
})
})
Loading

0 comments on commit 5b3becb

Please sign in to comment.