switch bin/shrimp to new parser
This commit is contained in:
parent
0d3f9867e6
commit
ae9896c8a2
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env bun
|
#!/usr/bin/env bun
|
||||||
|
|
||||||
import { colors, globals as prelude } from '../src/prelude'
|
import { colors, globals as prelude } from '../src/prelude'
|
||||||
import { treeToString } from '../src/utils/tree'
|
import { treeToString2 } from '../src/utils/tree'
|
||||||
import { runCode, runFile, compileFile, parseCode } from '../src'
|
import { runCode, runFile, compileFile, parseCode } from '../src'
|
||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import { bytecodeToString } from 'reefvm'
|
import { bytecodeToString } from 'reefvm'
|
||||||
|
|
@ -143,7 +143,7 @@ async function main() {
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
const input = readFileSync(file, 'utf-8')
|
const input = readFileSync(file, 'utf-8')
|
||||||
console.log(treeToString(parseCode(input), input))
|
console.log(treeToString2(parseCode(input), input))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
16
src/index.ts
16
src/index.ts
|
|
@ -1,15 +1,15 @@
|
||||||
import { readFileSync } from 'fs'
|
import { readFileSync } from 'fs'
|
||||||
import { VM, fromValue, toValue, isValue, type Bytecode } from 'reefvm'
|
import { VM, fromValue, toValue, isValue, type Bytecode } from 'reefvm'
|
||||||
import { type Tree } from '@lezer/common'
|
|
||||||
import { Compiler } from '#compiler/compiler'
|
import { Compiler } from '#compiler/compiler'
|
||||||
import { parser } from '#parser/shrimp'
|
import { parse } from '#parser/parser2'
|
||||||
|
import { type SyntaxNode, Tree } from '#parser/node'
|
||||||
import { globals as parserGlobals, setGlobals as setParserGlobals } from '#parser/tokenizer'
|
import { globals as parserGlobals, setGlobals as setParserGlobals } from '#parser/tokenizer'
|
||||||
import { globals as prelude } from '#prelude'
|
import { globals as prelude } from '#prelude'
|
||||||
|
|
||||||
export { Compiler } from '#compiler/compiler'
|
export { Compiler } from '#compiler/compiler'
|
||||||
export { parser } from '#parser/shrimp'
|
export { parse } from '#parser/parser2'
|
||||||
|
export { type SyntaxNode, Tree } from '#parser/node'
|
||||||
export { globals as prelude } from '#prelude'
|
export { globals as prelude } from '#prelude'
|
||||||
export type { Tree } from '@lezer/common'
|
|
||||||
export { type Value, type Bytecode } from 'reefvm'
|
export { type Value, type Bytecode } from 'reefvm'
|
||||||
export { toValue, fromValue, isValue, Scope, VM, bytecodeToString } from 'reefvm'
|
export { toValue, fromValue, isValue, Scope, VM, bytecodeToString } from 'reefvm'
|
||||||
|
|
||||||
|
|
@ -41,7 +41,7 @@ export class Shrimp {
|
||||||
return isValue(result) ? fromValue(result, this.vm) : result
|
return isValue(result) ? fromValue(result, this.vm) : result
|
||||||
}
|
}
|
||||||
|
|
||||||
parse(code: string): Tree {
|
parse(code: string): SyntaxNode {
|
||||||
return parseCode(code, this.globals)
|
return parseCode(code, this.globals)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -95,17 +95,17 @@ export function compileCode(code: string, globals?: Record<string, any>): Byteco
|
||||||
return compiler.bytecode
|
return compiler.bytecode
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseFile(path: string, globals?: Record<string, any>): Tree {
|
export function parseFile(path: string, globals?: Record<string, any>): SyntaxNode {
|
||||||
const code = readFileSync(path, 'utf-8')
|
const code = readFileSync(path, 'utf-8')
|
||||||
return parseCode(code, globals)
|
return parseCode(code, globals)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseCode(code: string, globals?: Record<string, any>): Tree {
|
export function parseCode(code: string, globals?: Record<string, any>): SyntaxNode {
|
||||||
const oldGlobals = [...parserGlobals]
|
const oldGlobals = [...parserGlobals]
|
||||||
const globalNames = [...Object.keys(prelude), ...(globals ? Object.keys(globals) : [])]
|
const globalNames = [...Object.keys(prelude), ...(globals ? Object.keys(globals) : [])]
|
||||||
|
|
||||||
setParserGlobals(globalNames)
|
setParserGlobals(globalNames)
|
||||||
const result = parser.parse(code)
|
const result = parse(code)
|
||||||
setParserGlobals(oldGlobals)
|
setParserGlobals(oldGlobals)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user