add find-closing-brace util function
This commit is contained in:
@ -1,6 +1,24 @@
|
||||
import { abstractSyntaxTree } from "./abstractSyntaxTree";
|
||||
import { abstractSyntaxTree, findClosingBrace } from "./abstractSyntaxTree";
|
||||
import { lexer } from "./tokeniser";
|
||||
|
||||
describe("findClosingBrace", () => {
|
||||
test("finds the closing brace", () => {
|
||||
const basic = "( hey )"
|
||||
expect(findClosingBrace(lexer(basic), 0)).toBe(4)
|
||||
|
||||
const handlesNonZeroIndex = "(indexForBracketToRightOfThisIsTwo(shouldBeFour)AndNotThisSix)"
|
||||
expect(findClosingBrace(lexer(handlesNonZeroIndex), 2)).toBe(4)
|
||||
expect(findClosingBrace(lexer(handlesNonZeroIndex), 0)).toBe(6)
|
||||
|
||||
const handlesNested = "{a{b{c(}d]}eathou athoeu tah u} thatOneToTheLeftIsLast }"
|
||||
expect(findClosingBrace(lexer(handlesNested), 0)).toBe(18)
|
||||
|
||||
// throws when not started on a brace
|
||||
expect(() => findClosingBrace(lexer(handlesNested), 1)).toThrow()
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
describe("testing AST", () => {
|
||||
test("test 5 + 6", () => {
|
||||
const tokens = lexer("5 +6");
|
||||
|
Reference in New Issue
Block a user