Skip to content

Commit

Permalink
Ast: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Jun 16, 2023
1 parent 89d5586 commit 7332b0f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/ast/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Parser, ParseTree } from '../parser/parser'
import { tokenize } from '../lexer/lexer'
import { parseModule } from '../parser/fns'
import { compactAstNode } from './index'
import { buildExpr } from './expr'

describe('ast', () => {

const parseTree = (source: string): ParseTree => {
const p = new Parser(tokenize(source))
parseModule(p)
return p.buildTree()
}

it('expr', () => {
const ast = buildExpr(parseTree('1 + 2 * 3'))
expect(compactAstNode(ast)).toEqual({
'operand': {
'operand': {
'binaryOp': { 'type': 'add-op' },
'lOperand': {
'operand': {
'operand': { 'type': 'int-literal', 'value': '1' },
'type': 'operand-expr'
}, 'type': 'operand-expr'
},
'rOperand': {
'binaryOp': { 'type': 'mult-op' },
'lOperand': {
'operand': {
'operand': { 'type': 'int-literal', 'value': '2' },
'type': 'operand-expr'
}, 'type': 'operand-expr'
},
'rOperand': {
'operand': {
'operand': { 'type': 'int-literal', 'value': '3' },
'type': 'operand-expr'
}, 'type': 'operand-expr'
},
'type': 'binary-expr'
},
'type': 'binary-expr'
}, 'type': 'operand-expr'
}, 'type': 'operand-expr'
})
})

})
5 changes: 5 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { tokenize } from './lexer/lexer'
import { readFileSync } from 'fs'
import { Parser } from './parser/parser'
import { parseModule } from './parser/fns'
import { buildModule } from './ast'

describe('nois', () => {
it('parse features', () => {
Expand All @@ -19,5 +20,9 @@ describe('nois', () => {
const root = parser.buildTree()

expect(root.kind).toEqual('module')

const astRoot = buildModule(root)

expect(astRoot.type).toEqual('module')
})
})

0 comments on commit 7332b0f

Please sign in to comment.