Skip to content

Commit

Permalink
File path argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Jun 4, 2023
1 parent 7b379aa commit c7ed368
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions data/hello.no
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let main = (): Unit {
print("Hello, World!")
}
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { compactToken, flattenToken, parse } from './parser/parser'
import { tokenize } from './lexer/lexer'
import { inspect } from 'util'
import { readFileSync } from 'fs'
import { resolve } from 'path'

const code = `\
let main = (): Unit {
} if`
const path = process.argv.slice(2).at(0)
if (!path) {
throw Error('no file provided')
}

const code = readFileSync(resolve(path)).toString()

const token = parse(tokenize(code))
if (token === true) {
Expand All @@ -14,7 +19,4 @@ if ('expect' in token) {
throw Error(`parsing error: ${inspect(token, { depth: null, colors: true })}`)
}

const flatten = flattenToken(token)
console.dir({ rule: flatten }, { depth: null, colors: true })
const compact = compactToken(flatten)
console.dir({ compact }, { depth: null, colors: true })
console.dir(compactToken(flattenToken(token)), { depth: null, colors: true, compact: true })
11 changes: 11 additions & 0 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ export const flattenToken = (token: Token): Token => {
}
}

export const parserTokensOnly = (token: ParserToken): ParserToken => {
const nodes = token.nodes.flatMap(n => {
if ('nodes' in n) {
return [parserTokensOnly(n)]
} else {
return []
}
})
return { name: token.name, nodes, location: token.location }
}

export const compactToken = (token: Token): any => {
if ('nodes' in token) {
return { name: token.name, nodes: token.nodes.map(n => compactToken(n)) }
Expand Down

0 comments on commit c7ed368

Please sign in to comment.