Compare commits

..

No commits in common. "main" and "atoms-can-be-piped" have entirely different histories.

116 changed files with 2937 additions and 8692 deletions

5
.gitignore vendored
View File

@ -34,7 +34,4 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
.DS_Store .DS_Store
/tmp /tmp
vscode-extension/tmp /docs
/docs
*.vsix

View File

@ -1,192 +0,0 @@
#!/usr/bin/env bun
// WARNING: [[ No human has been anywhere near this file. It's pure Claude slop.
// Enter at your own risk. ]]
import { readFileSync } from 'fs'
type CallInfo = {
method: string
line: number
calls: Set<string>
isRecursive?: boolean
}
// Parse the parser file and extract method calls
function analyzeParser(filePath: string): Map<string, CallInfo> {
const content = readFileSync(filePath, 'utf-8')
const lines = content.split('\n')
const methods = new Map<string, CallInfo>()
// Find all method definitions
const methodRegex = /^\s*(\w+)\s*\([^)]*\):\s*/
let currentMethod: string | null = null
let braceDepth = 0
let classDepth = 0
for (let i = 0; i < lines.length; i++) {
const line = lines[i] || ''
// Track if we're inside the Parser class
if (line.includes('class Parser')) {
classDepth = braceDepth + 1 // Will be the depth after we process this line's brace
}
// Check for method definition (only inside class, at class level)
// Check BEFORE incrementing braceDepth
if (classDepth > 0 && braceDepth === classDepth) {
const methodMatch = line.match(methodRegex)
if (methodMatch && !line.includes('class ')) {
currentMethod = methodMatch[1]!
methods.set(currentMethod, {
method: currentMethod,
line: i + 1,
calls: new Set(),
})
}
}
// Track brace depth
braceDepth += (line.match(/{/g) || []).length
braceDepth -= (line.match(/}/g) || []).length
// Find method calls within current method
if (currentMethod && braceDepth > 0) {
// Match this.methodName() calls
const callRegex = /this\.(\w+)\s*\(/g
let match
while ((match = callRegex.exec(line)) !== null) {
const calledMethod = match[1]!
const info = methods.get(currentMethod)!
info.calls.add(calledMethod)
// Mark recursive calls
if (calledMethod === currentMethod) {
info.isRecursive = true
}
}
}
// Reset when method ends
if (braceDepth === 0) {
currentMethod = null
}
}
return methods
}
// Build tree structure starting from a root method
function buildTree(
method: string,
callGraph: Map<string, CallInfo>,
visited: Set<string>,
indent = '',
isLast = true,
depth = 0,
maxDepth = 3,
): string[] {
const lines: string[] = []
const info = callGraph.get(method)
if (!info) return lines
// Add current method
const prefix = depth === 0 ? '' : isLast ? '└─> ' : '├─> '
const suffix = info.isRecursive ? ' (recursive)' : ''
const lineNum = `[line ${info.line}]`
lines.push(`${indent}${prefix}${method}() ${lineNum}${suffix}`)
// Stop if we've reached max depth
if (depth >= maxDepth) {
return lines
}
// Prevent infinite recursion in tree display
if (visited.has(method)) {
return lines
}
const newVisited = new Set(visited)
newVisited.add(method)
// Helper methods to filter out (low-level utilities)
const helperPatterns = /^(is|next|peek|expect|current|op)/i
// Get sorted unique calls (filter out recursive self-calls for display)
const calls = Array.from(info.calls)
.filter((c) => callGraph.has(c)) // Only show parser methods
.filter((c) => c !== method) // Don't show immediate self-recursion
.filter((c) => !helperPatterns.test(c)) // Filter out helpers
.sort()
// Add children
const newIndent = indent + (isLast ? ' ' : '│ ')
calls.forEach((call, idx) => {
const childLines = buildTree(
call,
callGraph,
newVisited,
newIndent,
idx === calls.length - 1,
depth + 1,
maxDepth,
)
lines.push(...childLines)
})
return lines
}
// Main
const parserPath = './src/parser/parser2.ts'
const maxDepth = parseInt(process.argv[2] || '5')
console.log('Parser Call Tree for', parserPath)
console.log(`Max depth: ${maxDepth}`)
console.log('═'.repeat(60))
console.log()
const callGraph = analyzeParser(parserPath)
// Start from parse() method
const tree = buildTree('parse', callGraph, new Set(), '', true, 0, maxDepth)
console.log(tree.join('\n'))
// Show some stats
console.log('\n' + '═'.repeat(60))
console.log('Stats:')
console.log(` Total methods: ${callGraph.size}`)
console.log(` Entry point: parse()`)
// Find methods that are never called (potential dead code or entry points)
const allCalled = new Set<string>()
for (const info of callGraph.values()) {
info.calls.forEach((c) => allCalled.add(c))
}
const uncalled = Array.from(callGraph.keys())
.filter((m) => !allCalled.has(m) && m !== 'parse')
.sort()
if (uncalled.length > 0) {
console.log(`\n Uncalled methods: ${uncalled.join(', ')}`)
}
// Find most-called methods
const callCount = new Map<string, number>()
for (const info of callGraph.values()) {
for (const called of info.calls) {
callCount.set(called, (callCount.get(called) || 0) + 1)
}
}
const topCalled = Array.from(callCount.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 5)
console.log(`\n Most-called methods:`)
for (const [method, count] of topCalled) {
console.log(` ${method}() - called ${count} times`)
}

View File

@ -7,9 +7,6 @@ import * as readline from 'readline'
import { readFileSync, writeFileSync } from 'fs' import { readFileSync, writeFileSync } from 'fs'
import { basename } from 'path' import { basename } from 'path'
globals.$.script.name = '(repl)'
globals.$.script.path = '(repl)'
async function repl() { async function repl() {
const commands = ['/clear', '/reset', '/vars', '/funcs', '/history', '/bytecode', '/exit', '/save', '/quit'] const commands = ['/clear', '/reset', '/vars', '/funcs', '/history', '/bytecode', '/exit', '/save', '/quit']
@ -148,7 +145,7 @@ async function repl() {
} }
try { try {
const compiler = new Compiler(trimmed, [...Object.keys(globals), ...vm.vars()]) const compiler = new Compiler(trimmed, Object.keys(globals))
// Save VM state before appending bytecode, in case execution fails // Save VM state before appending bytecode, in case execution fails
const savedInstructions = [...vm.instructions] const savedInstructions = [...vm.instructions]
@ -238,7 +235,7 @@ async function loadFile(filePath: string): Promise<{ vm: VM; codeHistory: string
if (!trimmed) continue if (!trimmed) continue
try { try {
const compiler = new Compiler(trimmed, [...Object.keys(globals), ...vm.vars()]) const compiler = new Compiler(trimmed)
vm.appendBytecode(compiler.bytecode) vm.appendBytecode(compiler.bytecode)
await vm.continue() await vm.continue()
codeHistory.push(trimmed) codeHistory.push(trimmed)

View File

@ -1,83 +1,71 @@
#!/usr/bin/env bun #!/usr/bin/env bun
import { colors, globals as prelude } from '../src/prelude' import { Compiler } from '../src/compiler/compiler'
import { treeToString2 } from '../src/utils/tree' import { colors, globals } from '../src/prelude'
import { runCode, runFile, compileFile, parseCode } from '../src' import { parser } from '../src/parser/shrimp'
import { resolve } from 'path' import { treeToString } from '../src/utils/tree'
import { bytecodeToString } from 'reefvm' import { VM, fromValue, bytecodeToString } from 'reefvm'
import { readFileSync } from 'fs' import { readFileSync, writeFileSync, mkdirSync } from 'fs'
import { randomUUID } from 'crypto'
import { spawn } from 'child_process' import { spawn } from 'child_process'
import { join } from 'path' import { join } from 'path'
async function runFile(filePath: string) {
try {
const code = readFileSync(filePath, 'utf-8')
const compiler = new Compiler(code, Object.keys(globals))
const vm = new VM(compiler.bytecode, globals)
await vm.run()
return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]) : null
} catch (error: any) {
console.error(`${colors.red}Error:${colors.reset} ${error.message}`)
process.exit(1)
}
}
async function compileFile(filePath: string) {
try {
const code = readFileSync(filePath, 'utf-8')
const compiler = new Compiler(code)
return bytecodeToString(compiler.bytecode)
} catch (error: any) {
console.error(`${colors.red}Error:${colors.reset} ${error.message}`)
process.exit(1)
}
}
async function parseFile(filePath: string) {
try {
const code = readFileSync(filePath, 'utf-8')
const tree = parser.parse(code)
return treeToString(tree, code)
} catch (error: any) {
console.error(`${colors.red}Error:${colors.reset} ${error.message}`)
process.exit(1)
}
}
function showHelp() { function showHelp() {
console.log(`${colors.bright}${colors.magenta}🦐 Shrimp${colors.reset} is a scripting language in a shell. console.log(`${colors.bright}${colors.magenta}🦐 Shrimp${colors.reset} is a scripting language in a shell.
${colors.bright}Usage:${colors.reset} shrimp <command> [options] [...args] ${colors.bright}Usage:${colors.reset} shrimp <command> [...args]
${colors.bright}Commands:${colors.reset} ${colors.bright}Commands:${colors.reset}
${colors.cyan}run ${colors.yellow}./my-file.sh${colors.reset} Execute a file with Shrimp ${colors.cyan}run ${colors.yellow}./my-file.sh${colors.reset} Execute a file with Shrimp
${colors.cyan}parse ${colors.yellow}./my-file.sh${colors.reset} Print parse tree for Shrimp file ${colors.cyan}parse ${colors.yellow}./my-file.sh${colors.reset} Print parse tree for Shrimp file
${colors.cyan}bytecode ${colors.yellow}./my-file.sh${colors.reset} Print bytecode for Shrimp file ${colors.cyan}bytecode ${colors.yellow}./my-file.sh${colors.reset} Print bytecode for Shrimp file
${colors.cyan}eval ${colors.yellow}'some code'${colors.reset} Evaluate a line of Shrimp code ${colors.cyan}eval ${colors.yellow}'some code'${colors.reset} Evaluate a line of Shrimp code
${colors.cyan}print ${colors.yellow}'some code'${colors.reset} Evaluate a line of Shrimp code and print the result
${colors.cyan}repl${colors.reset} Start REPL ${colors.cyan}repl${colors.reset} Start REPL
${colors.cyan}help${colors.reset} Print this help message ${colors.cyan}help${colors.reset} Print this help message
${colors.cyan}version${colors.reset} Print version ${colors.cyan}version${colors.reset} Print version`)
${colors.bright}Options:${colors.reset}
${colors.cyan}eval -I${colors.reset} ${colors.yellow}<module>${colors.reset} Import module (can be repeated)
Example: shrimp -I math -e 'random | echo'
Example: shrimp -Imath -Istr -e 'random | echo'`)
} }
function showVersion() { function showVersion() {
console.log('🦐 v0.0.1 (non-lezer parser)') console.log('🦐 v0.0.1')
}
async function evalCode(code: string, imports: string[]) {
const idx = Bun.argv.indexOf('--')
prelude.$.args = idx >= 0 ? Bun.argv.slice(idx + 1) : []
const importStatement = imports.length > 0 ? `import ${imports.join(' ')}` : ''
if (importStatement) code = `${importStatement}; ${code}`
return await runCode(code)
} }
async function main() { async function main() {
let args = process.argv.slice(2) const args = process.argv.slice(2)
if (args.length === 0) {
showHelp()
return
}
// Parse -I flags for imports (supports both "-I math" and "-Imath")
const imports: string[] = []
while (args.length > 0) {
const arg = args[0]
if (arg === '-I') {
// "-I math" format
if (args.length < 2) {
console.log(`${colors.bright}error: -I requires a module name${colors.reset}`)
process.exit(1)
}
imports.push(args[1])
args = args.slice(2)
} else if (arg.startsWith('-I')) {
// "-Imath" format
const moduleName = arg.slice(2)
if (!moduleName) {
console.log(`${colors.bright}error: -I requires a module name${colors.reset}`)
process.exit(1)
}
imports.push(moduleName)
args = args.slice(1)
} else {
break
}
}
if (args.length === 0) { if (args.length === 0) {
showHelp() showHelp()
@ -111,18 +99,10 @@ async function main() {
process.exit(1) process.exit(1)
} }
await evalCode(code, imports) try { mkdirSync('/tmp/shrimp') } catch { }
return const path = `/tmp/shrimp/${randomUUID()}.sh`
} writeFileSync(path, code)
console.log(await runFile(path))
if (['print', '-print', '--print', '-E'].includes(command)) {
const code = args[1]
if (!code) {
console.log(`${colors.bright}usage: shrimp print <code>${colors.reset}`)
process.exit(1)
}
console.log(await evalCode(code, imports))
return return
} }
@ -132,7 +112,7 @@ async function main() {
console.log(`${colors.bright}usage: shrimp bytecode <file>${colors.reset}`) console.log(`${colors.bright}usage: shrimp bytecode <file>${colors.reset}`)
process.exit(1) process.exit(1)
} }
console.log(bytecodeToString(compileFile(file))) console.log(await compileFile(file))
return return
} }
@ -142,8 +122,7 @@ async function main() {
console.log(`${colors.bright}usage: shrimp parse <file>${colors.reset}`) console.log(`${colors.bright}usage: shrimp parse <file>${colors.reset}`)
process.exit(1) process.exit(1)
} }
const input = readFileSync(file, 'utf-8') console.log(await parseFile(file))
console.log(treeToString2(parseCode(input).topNode, input))
return return
} }
@ -153,12 +132,10 @@ async function main() {
console.log(`${colors.bright}usage: shrimp run <file>${colors.reset}`) console.log(`${colors.bright}usage: shrimp run <file>${colors.reset}`)
process.exit(1) process.exit(1)
} }
prelude.$.script.path = resolve(file)
await runFile(file) await runFile(file)
return return
} }
prelude.$.script.path = resolve(command)
await runFile(command) await runFile(command)
} }

View File

@ -16,8 +16,6 @@
"@lezer/highlight": "^1.2.1", "@lezer/highlight": "^1.2.1",
"@lezer/lr": "^1.4.2", "@lezer/lr": "^1.4.2",
"@types/bun": "latest", "@types/bun": "latest",
"diff": "^8.0.2",
"kleur": "^4.1.5",
}, },
}, },
}, },
@ -46,7 +44,7 @@
"@marijn/find-cluster-break": ["@marijn/find-cluster-break@1.0.2", "", {}, "sha512-l0h88YhZFyKdXIFNfSWpyjStDjGHwZ/U7iobcK1cQQD8sejsONdQtTVU+1wVN1PBw40PiiHB1vA5S7VTfQiP9g=="], "@marijn/find-cluster-break": ["@marijn/find-cluster-break@1.0.2", "", {}, "sha512-l0h88YhZFyKdXIFNfSWpyjStDjGHwZ/U7iobcK1cQQD8sejsONdQtTVU+1wVN1PBw40PiiHB1vA5S7VTfQiP9g=="],
"@types/bun": ["@types/bun@1.3.2", "", { "dependencies": { "bun-types": "1.3.2" } }, "sha512-t15P7k5UIgHKkxwnMNkJbWlh/617rkDGEdSsDbu+qNHTaz9SKf7aC8fiIlUdD5RPpH6GEkP0cK7WlvmrEBRtWg=="], "@types/bun": ["@types/bun@1.3.1", "", { "dependencies": { "bun-types": "1.3.1" } }, "sha512-4jNMk2/K9YJtfqwoAa28c8wK+T7nvJFOjxI4h/7sORWcypRNxBpr+TPNaCfVWq70tLCJsqoFwcf0oI0JU/fvMQ=="],
"@types/node": ["@types/node@24.10.0", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-qzQZRBqkFsYyaSWXuEHc2WR9c0a0CXwiE5FWUvn7ZM+vdy1uZLfCunD38UzhuB7YN/J11ndbDBcTmOdxJo9Q7A=="], "@types/node": ["@types/node@24.10.0", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-qzQZRBqkFsYyaSWXuEHc2WR9c0a0CXwiE5FWUvn7ZM+vdy1uZLfCunD38UzhuB7YN/J11ndbDBcTmOdxJo9Q7A=="],
@ -54,7 +52,7 @@
"bun-plugin-tailwind": ["bun-plugin-tailwind@0.0.15", "", { "peerDependencies": { "typescript": "^5.0.0" } }, "sha512-qtAXMNGG4R0UGGI8zWrqm2B7BdXqx48vunJXBPzfDOHPA5WkRUZdTSbE7TFwO4jLhYqSE23YMWsM9NhE6ovobw=="], "bun-plugin-tailwind": ["bun-plugin-tailwind@0.0.15", "", { "peerDependencies": { "typescript": "^5.0.0" } }, "sha512-qtAXMNGG4R0UGGI8zWrqm2B7BdXqx48vunJXBPzfDOHPA5WkRUZdTSbE7TFwO4jLhYqSE23YMWsM9NhE6ovobw=="],
"bun-types": ["bun-types@1.3.2", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-i/Gln4tbzKNuxP70OWhJRZz1MRfvqExowP7U6JKoI8cntFrtxg7RJK3jvz7wQW54UuvNC8tbKHHri5fy74FVqg=="], "bun-types": ["bun-types@1.3.1", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-NMrcy7smratanWJ2mMXdpatalovtxVggkj11bScuWuiOoXTiKIu2eVS1/7qbyI/4yHedtsn175n4Sm4JcdHLXw=="],
"codemirror": ["codemirror@6.0.2", "", { "dependencies": { "@codemirror/autocomplete": "^6.0.0", "@codemirror/commands": "^6.0.0", "@codemirror/language": "^6.0.0", "@codemirror/lint": "^6.0.0", "@codemirror/search": "^6.0.0", "@codemirror/state": "^6.0.0", "@codemirror/view": "^6.0.0" } }, "sha512-VhydHotNW5w1UGK0Qj96BwSk/Zqbp9WbnyK2W/eVMv4QyF41INRGpjUhFJY7/uDNuudSc33a/PKr4iDqRduvHw=="], "codemirror": ["codemirror@6.0.2", "", { "dependencies": { "@codemirror/autocomplete": "^6.0.0", "@codemirror/commands": "^6.0.0", "@codemirror/language": "^6.0.0", "@codemirror/lint": "^6.0.0", "@codemirror/search": "^6.0.0", "@codemirror/state": "^6.0.0", "@codemirror/view": "^6.0.0" } }, "sha512-VhydHotNW5w1UGK0Qj96BwSk/Zqbp9WbnyK2W/eVMv4QyF41INRGpjUhFJY7/uDNuudSc33a/PKr4iDqRduvHw=="],
@ -62,17 +60,13 @@
"csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="], "csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="],
"diff": ["diff@8.0.2", "", {}, "sha512-sSuxWU5j5SR9QQji/o2qMvqRNYRDOcBTgsJ/DeCf4iSN4gW+gNMXM7wFIP+fdXZxoNiAnHUTGjCr+TSWXdRDKg=="],
"hono": ["hono@4.10.4", "", {}, "sha512-YG/fo7zlU3KwrBL5vDpWKisLYiM+nVstBQqfr7gCPbSYURnNEP9BDxEMz8KfsDR9JX0lJWDRNc6nXX31v7ZEyg=="], "hono": ["hono@4.10.4", "", {}, "sha512-YG/fo7zlU3KwrBL5vDpWKisLYiM+nVstBQqfr7gCPbSYURnNEP9BDxEMz8KfsDR9JX0lJWDRNc6nXX31v7ZEyg=="],
"kleur": ["kleur@4.1.5", "", {}, "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ=="], "reefvm": ["reefvm@git+https://git.nose.space/defunkt/reefvm#bffb83a5280a4d74e424c4e0f4fbd46f790227a3", { "peerDependencies": { "typescript": "^5" } }, "bffb83a5280a4d74e424c4e0f4fbd46f790227a3"],
"reefvm": ["reefvm@git+https://git.nose.space/defunkt/reefvm#3e2e68b31f504347225a4d705c7568a0957d629e", { "peerDependencies": { "typescript": "^5" } }, "3e2e68b31f504347225a4d705c7568a0957d629e"],
"style-mod": ["style-mod@4.1.3", "", {}, "sha512-i/n8VsZydrugj3Iuzll8+x/00GH2vnYsk1eomD8QiRrSAeW6ItbCQDtfXCeJHd0iwiNagqjQkvpvREEPtW3IoQ=="], "style-mod": ["style-mod@4.1.3", "", {}, "sha512-i/n8VsZydrugj3Iuzll8+x/00GH2vnYsk1eomD8QiRrSAeW6ItbCQDtfXCeJHd0iwiNagqjQkvpvREEPtW3IoQ=="],
"tailwindcss": ["tailwindcss@4.1.17", "", {}, "sha512-j9Ee2YjuQqYT9bbRTfTZht9W/ytp5H+jJpZKiYdP/bpnXARAuELt9ofP0lPnmHjbga7SNQIxdTAXCmtKVYjN+Q=="], "tailwindcss": ["tailwindcss@4.1.16", "", {}, "sha512-pONL5awpaQX4LN5eiv7moSiSPd/DLDzKVRJz8Q9PgzmAdd1R4307GQS2ZpfiN7ZmekdQrfhZZiSE5jkLR4WNaA=="],
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="], "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],

View File

@ -1,18 +0,0 @@
#!/usr/bin/env shrimp
# usage: dice <sides>
import math only=random
import list only=first
import str only=[replace starts-with?]
sides = $.args | first
sides ??= 20
if sides | starts-with? d:
sides = replace sides //\D// ''
end
sides = number sides
echo 'Rolling d$sides...'
random 1 sides | echo

View File

@ -1 +0,0 @@
echo

View File

@ -1,31 +0,0 @@
#!/usr/bin/env shrimp
year = date.now | date.year
project = fs.pwd | fs.basename | str.titlecase
{
Copyright $year $project Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
}
| str.trim
| echo

View File

@ -1,39 +0,0 @@
#!/usr/bin/env shrimp
# usage: password <length> [!spaced] [!symbols]
if ($.args | list.contains? -h):
echo 'usage: password <length> [!spaced] [!symbols]'
exit
end
password = do n=22 symbols=true spaced=true:
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
if symbols: chars += '!@#%^&*-=()[]<>' end
out = []
i = 0
max = length chars
while i < n:
idx = math.floor ((math.random) * max)
ch = chars | at idx
list.push out ch
i += 1
end
if spaced:
pos1 = math.floor((n - 2) / 3)
pos2 = math.floor((n - 2) * 2 / 3)
list.insert out pos2 ' '
list.insert out pos1 ' '
end
str.join out ''
end
missing-arg? = do x: $.args | list.contains? x | not end
num = $.args | list.reject (do x: x | str.starts-with? ! end) | list.first
password num symbols=(missing-arg? !symbols) spaced=(missing-arg? !spaced) | echo

View File

@ -1,9 +0,0 @@
#!/usr/bin/env shrimp
if not fs.exists? 'package.json':
echo '🦐 package.json not found'
exit 1
end
package = fs.read 'package.json' | json.decode
package.scripts | dict.keys | list.sort | each do x: echo x end

View File

@ -1,20 +1,14 @@
{ {
"name": "shrimp", "name": "shrimp",
"version": "0.1.0", "version": "0.1.0",
"exports": { "exports": "./src/index.ts",
".": "./src/index.ts",
"./editor": "./src/editor/index.ts",
"./editor.css": "./src/editor/editor.css"
},
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
"editor": "bun --hot src/editor/example/server.tsx", "dev": "bun generate-parser && bun --hot src/server/server.tsx",
"repl": "bun bin/repl", "generate-parser": "lezer-generator src/parser/shrimp.grammar --typeScript -o src/parser/shrimp.ts",
"update-reef": "rm -rf ~/.bun/install/cache/ && rm bun.lock && bun update reefvm", "repl": "bun generate-parser && bun bin/repl",
"cli:install": "ln -s \"$(pwd)/bin/shrimp\" ~/.bun/bin/shrimp", "update-reef": "rm -rf ~/.bun/install/cache/ && rm bun.lock && bun update reefvm"
"cli:remove": "rm ~/.bun/bin/shrimp",
"check": "bunx tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"@codemirror/view": "^6.38.3", "@codemirror/view": "^6.38.3",
@ -28,9 +22,7 @@
"devDependencies": { "devDependencies": {
"@lezer/highlight": "^1.2.1", "@lezer/highlight": "^1.2.1",
"@lezer/lr": "^1.4.2", "@lezer/lr": "^1.4.2",
"@types/bun": "latest", "@types/bun": "latest"
"diff": "^8.0.2",
"kleur": "^4.1.5"
}, },
"prettier": { "prettier": {
"semi": false, "semi": false,

View File

@ -1,7 +1,8 @@
import { CompilerError } from '#compiler/compilerError.ts' import { CompilerError } from '#compiler/compilerError.ts'
import { parse, setGlobals } from '#parser/parser2' import { parser } from '#parser/shrimp.ts'
import { SyntaxNode, Tree } from '#parser/node' import * as terms from '#parser/shrimp.terms'
import { tokenizeCurlyString } from '#parser/curlyTokenizer' import { setGlobals } from '#parser/tokenizer'
import type { SyntaxNode, Tree } from '@lezer/common'
import { assert, errorMessage } from '#utils/utils' import { assert, errorMessage } from '#utils/utils'
import { toBytecode, type Bytecode, type ProgramItem, bytecodeToString } from 'reefvm' import { toBytecode, type Bytecode, type ProgramItem, bytecodeToString } from 'reefvm'
import { import {
@ -50,23 +51,17 @@ function processEscapeSeq(escapeSeq: string): string {
export class Compiler { export class Compiler {
instructions: ProgramItem[] = [] instructions: ProgramItem[] = []
labelCount = 0
fnLabelCount = 0 fnLabelCount = 0
ifLabelCount = 0 ifLabelCount = 0
tryLabelCount = 0 tryLabelCount = 0
loopLabelCount = 0 loopLabelCount = 0
bytecode: Bytecode bytecode: Bytecode
pipeCounter = 0 pipeCounter = 0
pipeVarStack: string[] = [] // Stack of pipe variable names for nested pipes
constructor( constructor(public input: string, globals?: string[]) {
public input: string,
globals?: string[] | Record<string, any>,
) {
try { try {
if (globals) setGlobals(Array.isArray(globals) ? globals : Object.keys(globals)) if (globals) setGlobals(globals)
const ast = parse(input) const cst = parser.parse(input)
const cst = new Tree(ast)
const errors = checkTreeForErrors(cst) const errors = checkTreeForErrors(cst)
const firstError = errors[0] const firstError = errors[0]
@ -92,7 +87,7 @@ export class Compiler {
} }
#compileCst(cst: Tree, input: string) { #compileCst(cst: Tree, input: string) {
const isProgram = cst.topNode.type.is('Program') const isProgram = cst.topNode.type.id === terms.Program
assert(isProgram, `Expected Program node, got ${cst.topNode.type.name}`) assert(isProgram, `Expected Program node, got ${cst.topNode.type.name}`)
let child = cst.topNode.firstChild let child = cst.topNode.firstChild
@ -108,33 +103,15 @@ export class Compiler {
const value = input.slice(node.from, node.to) const value = input.slice(node.from, node.to)
if (DEBUG) console.log(`🫦 ${node.name}: ${value}`) if (DEBUG) console.log(`🫦 ${node.name}: ${value}`)
switch (node.type.name) { switch (node.type.id) {
case 'Number': case terms.Number:
// Handle sign prefix for hex, binary, and octal literals const number = Number(value)
// Number() doesn't parse '-0xFF', '+0xFF', '-0o77', etc. correctly if (Number.isNaN(number))
let numberValue: number
if (
value.startsWith('-') &&
(value.includes('0x') || value.includes('0b') || value.includes('0o'))
) {
numberValue = -Number(value.slice(1))
} else if (
value.startsWith('+') &&
(value.includes('0x') || value.includes('0b') || value.includes('0o'))
) {
numberValue = Number(value.slice(1))
} else {
numberValue = Number(value)
}
if (Number.isNaN(numberValue))
throw new CompilerError(`Invalid number literal: ${value}`, node.from, node.to) throw new CompilerError(`Invalid number literal: ${value}`, node.from, node.to)
return [[`PUSH`, numberValue]] return [[`PUSH`, number]]
case 'String': {
if (node.firstChild?.type.is('CurlyString')) return this.#compileCurlyString(value, input)
case terms.String: {
const { parts, hasInterpolation } = getStringParts(node, input) const { parts, hasInterpolation } = getStringParts(node, input)
// Simple string without interpolation or escapes - extract text directly // Simple string without interpolation or escapes - extract text directly
@ -149,19 +126,19 @@ export class Compiler {
parts.forEach((part) => { parts.forEach((part) => {
const partValue = input.slice(part.from, part.to) const partValue = input.slice(part.from, part.to)
switch (part.type.name) { switch (part.type.id) {
case 'StringFragment': case terms.StringFragment:
// Plain text fragment - just push as-is // Plain text fragment - just push as-is
instructions.push(['PUSH', partValue]) instructions.push(['PUSH', partValue])
break break
case 'EscapeSeq': case terms.EscapeSeq:
// Process escape sequence and push the result // Process escape sequence and push the result
const processed = processEscapeSeq(partValue) const processed = processEscapeSeq(partValue)
instructions.push(['PUSH', processed]) instructions.push(['PUSH', processed])
break break
case 'Interpolation': case terms.Interpolation:
// Interpolation contains either Identifier or ParenExpr (the $ is anonymous) // Interpolation contains either Identifier or ParenExpr (the $ is anonymous)
const child = part.firstChild const child = part.firstChild
if (!child) { if (!child) {
@ -175,7 +152,7 @@ export class Compiler {
throw new CompilerError( throw new CompilerError(
`Unexpected string part: ${part.type.name}`, `Unexpected string part: ${part.type.name}`,
part.from, part.from,
part.to, part.to
) )
} }
}) })
@ -185,15 +162,15 @@ export class Compiler {
return instructions return instructions
} }
case 'Boolean': { case terms.Boolean: {
return [[`PUSH`, value === 'true']] return [[`PUSH`, value === 'true']]
} }
case 'Null': { case terms.Null: {
return [[`PUSH`, null]] return [[`PUSH`, null]]
} }
case 'Regex': { case terms.Regex: {
// remove the surrounding slashes and any flags // remove the surrounding slashes and any flags
const [_, pattern, flags] = value.match(/^\/\/(.*)\/\/([gimsuy]*)$/) || [] const [_, pattern, flags] = value.match(/^\/\/(.*)\/\/([gimsuy]*)$/) || []
if (!pattern) { if (!pattern) {
@ -210,50 +187,29 @@ export class Compiler {
return [['PUSH', regex]] return [['PUSH', regex]]
} }
case 'Identifier': { case terms.Identifier: {
return [[`TRY_LOAD`, value]] return [[`TRY_LOAD`, value]]
} }
case 'Word': { case terms.Word: {
return [['PUSH', value]] return [['PUSH', value]]
} }
case 'DotGet': { case terms.DotGet: {
// DotGet is parsed into a nested tree because it's hard to parse it into a flat one.
// However, we want a flat tree - so we're going to pretend like we are getting one from the parser.
//
// This: DotGet(config, DotGet(script, name))
// Becomes: DotGet(config, script, name)
const { objectName, property } = getDotGetParts(node, input) const { objectName, property } = getDotGetParts(node, input)
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
instructions.push(['TRY_LOAD', objectName]) instructions.push(['TRY_LOAD', objectName])
if (property.type.id === terms.ParenExpr) {
const flattenProperty = (prop: SyntaxNode): void => { instructions.push(...this.#compileNode(property, input))
if (prop.type.is('DotGet')) { } else {
const nestedParts = getDotGetParts(prop, input) const propertyValue = input.slice(property.from, property.to)
instructions.push(['PUSH', propertyValue])
const nestedObjectValue = input.slice(nestedParts.object.from, nestedParts.object.to)
instructions.push(['PUSH', nestedObjectValue])
instructions.push(['DOT_GET'])
flattenProperty(nestedParts.property)
} else {
if (prop.type.is('ParenExpr')) {
instructions.push(...this.#compileNode(prop, input))
} else {
const propertyValue = input.slice(prop.from, prop.to)
instructions.push(['PUSH', propertyValue])
}
instructions.push(['DOT_GET'])
}
} }
instructions.push(['DOT_GET'])
flattenProperty(property)
return instructions return instructions
} }
case 'BinOp': { case terms.BinOp: {
const { left, op, right } = getBinaryParts(node) const { left, op, right } = getBinaryParts(node)
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
instructions.push(...this.#compileNode(left, input)) instructions.push(...this.#compileNode(left, input))
@ -276,24 +232,6 @@ export class Compiler {
case '%': case '%':
instructions.push(['MOD']) instructions.push(['MOD'])
break break
case 'band':
instructions.push(['BIT_AND'])
break
case 'bor':
instructions.push(['BIT_OR'])
break
case 'bxor':
instructions.push(['BIT_XOR'])
break
case '<<':
instructions.push(['BIT_SHL'])
break
case '>>':
instructions.push(['BIT_SHR'])
break
case '>>>':
instructions.push(['BIT_USHR'])
break
default: default:
throw new CompilerError(`Unsupported binary operator: ${opValue}`, op.from, op.to) throw new CompilerError(`Unsupported binary operator: ${opValue}`, op.from, op.to)
} }
@ -301,7 +239,7 @@ export class Compiler {
return instructions return instructions
} }
case 'Assign': { case terms.Assign: {
const assignParts = getAssignmentParts(node) const assignParts = getAssignmentParts(node)
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
@ -332,37 +270,17 @@ export class Compiler {
return instructions return instructions
} }
case 'CompoundAssign': { case terms.CompoundAssign: {
const { identifier, operator, right } = getCompoundAssignmentParts(node) const { identifier, operator, right } = getCompoundAssignmentParts(node)
const identifierName = input.slice(identifier.from, identifier.to) const identifierName = input.slice(identifier.from, identifier.to)
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
const opValue = input.slice(operator.from, operator.to)
// Special handling for ??= since it needs conditional evaluation // will throw if undefined
if (opValue === '??=') { instructions.push(['LOAD', identifierName])
instructions.push(['LOAD', identifierName])
const skipLabel: Label = `.skip_${this.labelCount++}`
const rightInstructions = this.#compileNode(right, input)
instructions.push(['DUP'])
instructions.push(['PUSH', null])
instructions.push(['NEQ'])
instructions.push(['JUMP_IF_TRUE', skipLabel])
instructions.push(['POP'])
instructions.push(...rightInstructions)
instructions.push([`${skipLabel}:`])
instructions.push(['DUP'])
instructions.push(['STORE', identifierName])
return instructions
}
// Standard compound assignments: evaluate both sides, then operate
instructions.push(['LOAD', identifierName]) // will throw if undefined
instructions.push(...this.#compileNode(right, input)) instructions.push(...this.#compileNode(right, input))
const opValue = input.slice(operator.from, operator.to)
switch (opValue) { switch (opValue) {
case '+=': case '+=':
instructions.push(['ADD']) instructions.push(['ADD'])
@ -383,7 +301,7 @@ export class Compiler {
throw new CompilerError( throw new CompilerError(
`Unknown compound operator: ${opValue}`, `Unknown compound operator: ${opValue}`,
operator.from, operator.from,
operator.to, operator.to
) )
} }
@ -394,14 +312,14 @@ export class Compiler {
return instructions return instructions
} }
case 'ParenExpr': { case terms.ParenExpr: {
const child = node.firstChild const child = node.firstChild
if (!child) return [] // I guess it is empty parentheses? if (!child) return [] // I guess it is empty parentheses?
return this.#compileNode(child, input) return this.#compileNode(child, input)
} }
case 'FunctionDef': { case terms.FunctionDef: {
const { paramNames, bodyNodes, catchVariable, catchBody, finallyBody } = const { paramNames, bodyNodes, catchVariable, catchBody, finallyBody } =
getFunctionDefParts(node, input) getFunctionDefParts(node, input)
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
@ -431,8 +349,8 @@ export class Compiler {
catchVariable, catchVariable,
catchBody, catchBody,
finallyBody, finallyBody,
input, input
), )
) )
} else { } else {
instructions.push(...compileFunctionBody()) instructions.push(...compileFunctionBody())
@ -447,31 +365,9 @@ export class Compiler {
return instructions return instructions
} }
case 'FunctionCallOrIdentifier': { case terms.FunctionCallOrIdentifier: {
if (node.firstChild?.type.is('DotGet')) { if (node.firstChild?.type.id === terms.DotGet) {
const instructions: ProgramItem[] = [] return this.#compileNode(node.firstChild, input)
const callLabel: Label = `.call_dotget_${++this.labelCount}`
const afterLabel: Label = `.after_dotget_${++this.labelCount}`
instructions.push(...this.#compileNode(node.firstChild, input))
instructions.push(['DUP'])
instructions.push(['TYPE'])
instructions.push(['PUSH', 'function'])
instructions.push(['EQ'])
instructions.push(['JUMP_IF_TRUE', callLabel])
instructions.push(['DUP'])
instructions.push(['TYPE'])
instructions.push(['PUSH', 'native'])
instructions.push(['EQ'])
instructions.push(['JUMP_IF_TRUE', callLabel])
instructions.push(['JUMP', afterLabel])
instructions.push([`${callLabel}:`])
instructions.push(['PUSH', 0])
instructions.push(['PUSH', 0])
instructions.push(['CALL'])
instructions.push([`${afterLabel}:`])
return instructions
} }
return [['TRY_CALL', value]] return [['TRY_CALL', value]]
@ -490,8 +386,7 @@ export class Compiler {
PUSH 1 ; Named count PUSH 1 ; Named count
CALL CALL
*/ */
case terms.FunctionCall: {
case 'FunctionCall': {
const { identifierNode, namedArgs, positionalArgs } = getFunctionCallParts(node, input) const { identifierNode, namedArgs, positionalArgs } = getFunctionCallParts(node, input)
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
instructions.push(...this.#compileNode(identifierNode, input)) instructions.push(...this.#compileNode(identifierNode, input))
@ -513,7 +408,7 @@ export class Compiler {
return instructions return instructions
} }
case 'Block': { case terms.Block: {
const children = getAllChildren(node) const children = getAllChildren(node)
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
@ -528,7 +423,7 @@ export class Compiler {
return instructions return instructions
} }
case 'FunctionCallWithBlock': { case terms.FunctionCallWithBlock: {
const [fn, _colon, ...block] = getAllChildren(node) const [fn, _colon, ...block] = getAllChildren(node)
let instructions: ProgramItem[] = [] let instructions: ProgramItem[] = []
@ -541,18 +436,18 @@ export class Compiler {
...block ...block
.filter((x) => x.type.name !== 'keyword') .filter((x) => x.type.name !== 'keyword')
.map((x) => this.#compileNode(x!, input)) .map((x) => this.#compileNode(x!, input))
.flat(), .flat()
) )
instructions.push(['RETURN']) instructions.push(['RETURN'])
instructions.push([`${afterLabel}:`]) instructions.push([`${afterLabel}:`])
if (fn?.type.is('FunctionCallOrIdentifier')) { if (fn?.type.id === terms.FunctionCallOrIdentifier) {
instructions.push(['LOAD', input.slice(fn!.from, fn!.to)]) instructions.push(['LOAD', input.slice(fn!.from, fn!.to)])
instructions.push(['MAKE_FUNCTION', [], fnLabel]) instructions.push(['MAKE_FUNCTION', [], fnLabel])
instructions.push(['PUSH', 1]) instructions.push(['PUSH', 1])
instructions.push(['PUSH', 0]) instructions.push(['PUSH', 0])
instructions.push(['CALL']) instructions.push(['CALL'])
} else if (fn?.type.is('FunctionCall')) { } else if (fn?.type.id === terms.FunctionCall) {
let body = this.#compileNode(fn!, input) let body = this.#compileNode(fn!, input)
const namedArgCount = (body[body.length - 2]![1] as number) * 2 const namedArgCount = (body[body.length - 2]![1] as number) * 2
const startSlice = body.length - namedArgCount - 3 const startSlice = body.length - namedArgCount - 3
@ -568,14 +463,14 @@ export class Compiler {
instructions.push(...body) instructions.push(...body)
} else { } else {
throw new Error( throw new Error(
`FunctionCallWithBlock: Expected FunctionCallOrIdentifier or FunctionCall`, `FunctionCallWithBlock: Expected FunctionCallOrIdentifier or FunctionCall`
) )
} }
return instructions return instructions
} }
case 'TryExpr': { case terms.TryExpr: {
const { tryBlock, catchVariable, catchBody, finallyBody } = getTryExprParts(node, input) const { tryBlock, catchVariable, catchBody, finallyBody } = getTryExprParts(node, input)
return this.#compileTryCatchFinally( return this.#compileTryCatchFinally(
@ -583,57 +478,50 @@ export class Compiler {
catchVariable, catchVariable,
catchBody, catchBody,
finallyBody, finallyBody,
input, input
) )
} }
case 'Throw': case terms.Throw: {
case 'Not': {
const keyword = node.type.is('Throw') ? 'Throw' : 'Not'
const children = getAllChildren(node) const children = getAllChildren(node)
const [_throwKeyword, expression] = children const [_throwKeyword, expression] = children
if (!expression) { if (!expression) {
throw new CompilerError( throw new CompilerError(
`${keyword} expected expression, got ${children.length} children`, `Throw expected expression, got ${children.length} children`,
node.from, node.from,
node.to, node.to
) )
} }
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
instructions.push(...this.#compileNode(expression, input)) instructions.push(...this.#compileNode(expression, input))
instructions.push([keyword.toUpperCase()]) // THROW or NOT instructions.push(['THROW'])
return instructions return instructions
} }
case 'IfExpr': { case terms.IfExpr: {
const { conditionNode, thenBlock, elseIfBlocks, elseThenBlock } = getIfExprParts( const { conditionNode, thenBlock, elseIfBlocks, elseThenBlock } = getIfExprParts(
node, node,
input, input
) )
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
instructions.push(...this.#compileNode(conditionNode, input)) instructions.push(...this.#compileNode(conditionNode, input))
this.ifLabelCount++ this.ifLabelCount++
const endLabel: Label = `.end_${this.ifLabelCount}` const endLabel: Label = `.end_${this.ifLabelCount}`
const elseLabel: Label = `.else_${this.ifLabelCount}`
const thenBlockInstructions = this.#compileNode(thenBlock, input) const thenBlockInstructions = this.#compileNode(thenBlock, input)
instructions.push(['JUMP_IF_FALSE', elseLabel]) instructions.push(['JUMP_IF_FALSE', thenBlockInstructions.length + 1])
instructions.push(...thenBlockInstructions) instructions.push(...thenBlockInstructions)
instructions.push(['JUMP', endLabel]) instructions.push(['JUMP', endLabel])
instructions.push([`${elseLabel}:`])
// Else if // Else if
elseIfBlocks.forEach(({ conditional, thenBlock }, index) => { elseIfBlocks.forEach(({ conditional, thenBlock }) => {
instructions.push(...this.#compileNode(conditional, input)) instructions.push(...this.#compileNode(conditional, input))
const nextLabel: Label = `.elsif_${this.ifLabelCount}_${index}`
const elseIfInstructions = this.#compileNode(thenBlock, input) const elseIfInstructions = this.#compileNode(thenBlock, input)
instructions.push(['JUMP_IF_FALSE', nextLabel]) instructions.push(['JUMP_IF_FALSE', elseIfInstructions.length + 1])
instructions.push(...elseIfInstructions) instructions.push(...elseIfInstructions)
instructions.push(['JUMP', endLabel]) instructions.push(['JUMP', endLabel])
instructions.push([`${nextLabel}:`])
}) })
// Else // Else
@ -650,7 +538,7 @@ export class Compiler {
} }
// - `EQ`, `NEQ`, `LT`, `GT`, `LTE`, `GTE` - Pop 2, push boolean // - `EQ`, `NEQ`, `LT`, `GT`, `LTE`, `GTE` - Pop 2, push boolean
case 'ConditionalOp': { case terms.ConditionalOp: {
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
const { left, op, right } = getBinaryParts(node) const { left, op, right } = getBinaryParts(node)
const leftInstructions: ProgramItem[] = this.#compileNode(left, input) const leftInstructions: ProgramItem[] = this.#compileNode(left, input)
@ -682,41 +570,22 @@ export class Compiler {
instructions.push(...leftInstructions, ...rightInstructions, ['GTE']) instructions.push(...leftInstructions, ...rightInstructions, ['GTE'])
break break
case 'and': { case 'and':
const skipLabel: Label = `.skip_${this.labelCount++}`
instructions.push(...leftInstructions) instructions.push(...leftInstructions)
instructions.push(['DUP']) instructions.push(['DUP'])
instructions.push(['JUMP_IF_FALSE', skipLabel]) instructions.push(['JUMP_IF_FALSE', rightInstructions.length + 1])
instructions.push(['POP']) instructions.push(['POP'])
instructions.push(...rightInstructions) instructions.push(...rightInstructions)
instructions.push([`${skipLabel}:`])
break break
}
case 'or': { case 'or':
const skipLabel: Label = `.skip_${this.labelCount++}`
instructions.push(...leftInstructions) instructions.push(...leftInstructions)
instructions.push(['DUP']) instructions.push(['DUP'])
instructions.push(['JUMP_IF_TRUE', skipLabel]) instructions.push(['JUMP_IF_TRUE', rightInstructions.length + 1])
instructions.push(['POP']) instructions.push(['POP'])
instructions.push(...rightInstructions) instructions.push(...rightInstructions)
instructions.push([`${skipLabel}:`])
break
}
case '??': {
// Nullish coalescing: return left if not null, else right
const skipLabel: Label = `.skip_${this.labelCount++}`
instructions.push(...leftInstructions)
instructions.push(['DUP'])
instructions.push(['PUSH', null])
instructions.push(['NEQ'])
instructions.push(['JUMP_IF_TRUE', skipLabel])
instructions.push(['POP'])
instructions.push(...rightInstructions)
instructions.push([`${skipLabel}:`])
break break
}
default: default:
throw new CompilerError(`Unsupported conditional operator: ${opValue}`, op.from, op.to) throw new CompilerError(`Unsupported conditional operator: ${opValue}`, op.from, op.to)
@ -725,7 +594,7 @@ export class Compiler {
return instructions return instructions
} }
case 'PipeExpr': { case terms.PipeExpr: {
const { pipedFunctionCall, pipeReceivers } = getPipeExprParts(node) const { pipedFunctionCall, pipeReceivers } = getPipeExprParts(node)
if (!pipedFunctionCall || pipeReceivers.length === 0) { if (!pipedFunctionCall || pipeReceivers.length === 0) {
throw new CompilerError('PipeExpr must have at least two operands', node.from, node.to) throw new CompilerError('PipeExpr must have at least two operands', node.from, node.to)
@ -734,77 +603,67 @@ export class Compiler {
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
instructions.push(...this.#compileNode(pipedFunctionCall, input)) instructions.push(...this.#compileNode(pipedFunctionCall, input))
// Use a unique variable name for this pipe level to handle nested pipes correctly
this.pipeCounter++ this.pipeCounter++
const pipeVarName = `_pipe_${this.pipeCounter}` const pipeValName = `_pipe_value_${this.pipeCounter}`
this.pipeVarStack.push(pipeVarName)
pipeReceivers.forEach((pipeReceiver) => { pipeReceivers.forEach((pipeReceiver) => {
// Store the piped value in the current pipe's variable instructions.push(['STORE', pipeValName])
// Also store as `_` for direct access in simple cases
instructions.push(['DUP'])
instructions.push(['STORE', pipeVarName])
instructions.push(['STORE', '_'])
const isFunctionCall = const { identifierNode, namedArgs, positionalArgs } = getFunctionCallParts(
pipeReceiver.type.is('FunctionCall') || pipeReceiver.type.is('FunctionCallOrIdentifier') pipeReceiver,
input
)
if (isFunctionCall) { instructions.push(...this.#compileNode(identifierNode, input))
// Function call receiver: check for explicit _ usage to determine arg handling
const { identifierNode, namedArgs, positionalArgs } = getFunctionCallParts(
pipeReceiver,
input,
)
instructions.push(...this.#compileNode(identifierNode, input)) const isUnderscoreInPositionalArgs = positionalArgs.some(
(arg) => arg.type.id === terms.Underscore
)
const isUnderscoreInNamedArgs = namedArgs.some((arg) => {
const { valueNode } = getNamedArgParts(arg, input)
return valueNode.type.id === terms.Underscore
})
const isUnderscoreInPositionalArgs = positionalArgs.some((arg) => const shouldPushPositionalArg = !isUnderscoreInPositionalArgs && !isUnderscoreInNamedArgs
arg.type.is('Underscore'),
)
const isUnderscoreInNamedArgs = namedArgs.some((arg) => {
const { valueNode } = getNamedArgParts(arg, input)
return valueNode.type.is('Underscore')
})
const shouldPushPositionalArg = !isUnderscoreInPositionalArgs && !isUnderscoreInNamedArgs // If no underscore is explicitly used, add the piped value as the first positional arg
if (shouldPushPositionalArg) {
// If no underscore is explicitly used, add the piped value as the first positional arg instructions.push(['LOAD', pipeValName])
if (shouldPushPositionalArg) {
instructions.push(['LOAD', pipeVarName])
}
positionalArgs.forEach((arg) => {
instructions.push(...this.#compileNode(arg, input))
})
namedArgs.forEach((arg) => {
const { name, valueNode } = getNamedArgParts(arg, input)
instructions.push(['PUSH', name])
instructions.push(...this.#compileNode(valueNode, input))
})
instructions.push(['PUSH', positionalArgs.length + (shouldPushPositionalArg ? 1 : 0)])
instructions.push(['PUSH', namedArgs.length])
instructions.push(['CALL'])
} else {
// Non-function-call receiver (Array, ParenExpr, etc.): compile directly
// The `_` variable is available for use in nested expressions
instructions.push(...this.#compileNode(pipeReceiver, input))
} }
positionalArgs.forEach((arg) => {
if (arg.type.id === terms.Underscore) {
instructions.push(['LOAD', pipeValName])
} else {
instructions.push(...this.#compileNode(arg, input))
}
})
namedArgs.forEach((arg) => {
const { name, valueNode } = getNamedArgParts(arg, input)
instructions.push(['PUSH', name])
if (valueNode.type.id === terms.Underscore) {
instructions.push(['LOAD', pipeValName])
} else {
instructions.push(...this.#compileNode(valueNode, input))
}
})
instructions.push(['PUSH', positionalArgs.length + (shouldPushPositionalArg ? 1 : 0)])
instructions.push(['PUSH', namedArgs.length])
instructions.push(['CALL'])
}) })
this.pipeVarStack.pop()
return instructions return instructions
} }
case 'Array': { case terms.Array: {
const children = getAllChildren(node) const children = getAllChildren(node)
// We can easily parse [=] as an empty dict, but `[ = ]` is tougher. // We can easily parse [=] as an empty dict, but `[ = ]` is tougher.
// = can be a valid word, and is also valid inside words, so for now we cheat // = can be a valid word, and is also valid inside words, so for now we cheat
// and check for arrays that look like `[ = ]` to interpret them as // and check for arrays that look like `[ = ]` to interpret them as
// empty dicts // empty dicts
if (children.length === 1 && children[0]!.type.is('Word')) { if (children.length === 1 && children[0]!.type.id === terms.Word) {
const child = children[0]! const child = children[0]!
if (input.slice(child.from, child.to) === '=') { if (input.slice(child.from, child.to) === '=') {
return [['MAKE_DICT', 0]] return [['MAKE_DICT', 0]]
@ -816,7 +675,7 @@ export class Compiler {
return instructions return instructions
} }
case 'Dict': { case terms.Dict: {
const children = getAllChildren(node) const children = getAllChildren(node)
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
@ -825,7 +684,7 @@ export class Compiler {
const valueNode = node.firstChild!.nextSibling const valueNode = node.firstChild!.nextSibling
// name= -> name // name= -> name
const key = input.slice(keyNode!.from, keyNode!.to).replace(/\s*=$/, '') const key = input.slice(keyNode!.from, keyNode!.to).slice(0, -1)
instructions.push(['PUSH', key]) instructions.push(['PUSH', key])
instructions.push(...this.#compileNode(valueNode!, input)) instructions.push(...this.#compileNode(valueNode!, input))
@ -835,7 +694,7 @@ export class Compiler {
return instructions return instructions
} }
case 'WhileExpr': { case terms.WhileExpr: {
const [_while, test, _colon, block] = getAllChildren(node) const [_while, test, _colon, block] = getAllChildren(node)
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
@ -853,45 +712,15 @@ export class Compiler {
return instructions return instructions
} }
case 'Import': { case terms.Comment: {
const instructions: ProgramItem[] = []
const [_import, ...nodes] = getAllChildren(node)
const args = nodes.filter((node) => node.type.is('Identifier'))
const namedArgs = nodes.filter((node) => node.type.is('NamedArg'))
instructions.push(['LOAD', 'import'])
args.forEach((dict) => instructions.push(['PUSH', input.slice(dict.from, dict.to)]))
namedArgs.forEach((arg) => {
const { name, valueNode } = getNamedArgParts(arg, input)
instructions.push(['PUSH', name])
instructions.push(...this.#compileNode(valueNode, input))
})
instructions.push(['PUSH', args.length])
instructions.push(['PUSH', namedArgs.length])
instructions.push(['CALL'])
return instructions
}
case 'Comment': {
return [] // ignore comments return [] // ignore comments
} }
case 'Underscore': {
// _ refers to the piped value for the current (innermost) pipe
// Use the stack to handle nested pipes correctly
const pipeVar = this.pipeVarStack.at(-1) ?? '_'
return [['LOAD', pipeVar]]
}
default: default:
throw new CompilerError( throw new CompilerError(
`Compiler doesn't know how to handle a "${node.type.name}" node.`, `Compiler doesn't know how to handle a "${node.type.name}" node.`,
node.from, node.from,
node.to, node.to
) )
} }
} }
@ -901,7 +730,7 @@ export class Compiler {
catchVariable: string | undefined, catchVariable: string | undefined,
catchBody: SyntaxNode | undefined, catchBody: SyntaxNode | undefined,
finallyBody: SyntaxNode | undefined, finallyBody: SyntaxNode | undefined,
input: string, input: string
): ProgramItem[] { ): ProgramItem[] {
const instructions: ProgramItem[] = [] const instructions: ProgramItem[] = []
this.tryLabelCount++ this.tryLabelCount++
@ -943,26 +772,4 @@ export class Compiler {
return instructions return instructions
} }
#compileCurlyString(value: string, input: string): ProgramItem[] {
const instructions: ProgramItem[] = []
const nodes = tokenizeCurlyString(value)
nodes.forEach((node) => {
if (typeof node === 'string') {
instructions.push(['PUSH', node])
} else {
const [input, topNode] = node
let child = topNode.firstChild
while (child) {
instructions.push(...this.#compileNode(child, input))
child = child.nextSibling
}
}
})
instructions.push(['STR_CONCAT', nodes.length])
return instructions
}
} }

View File

@ -1,9 +1,5 @@
export class CompilerError extends Error { export class CompilerError extends Error {
constructor( constructor(message: string, private from: number, private to: number) {
message: string,
private from: number,
private to: number,
) {
super(message) super(message)
if (from < 0 || to < 0 || to < from) { if (from < 0 || to < 0 || to < from) {

View File

@ -1,178 +0,0 @@
import { expect, describe, test } from 'bun:test'
describe('bitwise operators', () => {
describe('band (bitwise AND)', () => {
test('basic AND operation', () => {
expect('5 band 3').toEvaluateTo(1)
// 5 = 0101, 3 = 0011, result = 0001 = 1
})
test('AND with zero', () => {
expect('5 band 0').toEvaluateTo(0)
})
test('AND with all bits set', () => {
expect('15 band 7').toEvaluateTo(7)
// 15 = 1111, 7 = 0111, result = 0111 = 7
})
test('AND in assignment', () => {
expect('x = 12 band 10').toEvaluateTo(8)
// 12 = 1100, 10 = 1010, result = 1000 = 8
})
})
describe('bor (bitwise OR)', () => {
test('basic OR operation', () => {
expect('5 bor 3').toEvaluateTo(7)
// 5 = 0101, 3 = 0011, result = 0111 = 7
})
test('OR with zero', () => {
expect('5 bor 0').toEvaluateTo(5)
})
test('OR with all bits set', () => {
expect('8 bor 4').toEvaluateTo(12)
// 8 = 1000, 4 = 0100, result = 1100 = 12
})
})
describe('bxor (bitwise XOR)', () => {
test('basic XOR operation', () => {
expect('5 bxor 3').toEvaluateTo(6)
// 5 = 0101, 3 = 0011, result = 0110 = 6
})
test('XOR with itself returns zero', () => {
expect('5 bxor 5').toEvaluateTo(0)
})
test('XOR with zero returns same value', () => {
expect('7 bxor 0').toEvaluateTo(7)
})
test('XOR in assignment', () => {
expect('result = 8 bxor 12').toEvaluateTo(4)
// 8 = 1000, 12 = 1100, result = 0100 = 4
})
})
describe('bnot (bitwise NOT)', () => {
test('NOT of positive number', () => {
expect('bnot 5').toEvaluateTo(-6)
// ~5 = -6 (two\'s complement)
})
test('NOT of zero', () => {
expect('bnot 0').toEvaluateTo(-1)
})
test('NOT of negative number', () => {
expect('bnot -1').toEvaluateTo(0)
})
test('double NOT returns original', () => {
expect('bnot (bnot 5)').toEvaluateTo(5)
})
})
describe('<< (left shift)', () => {
test('basic left shift', () => {
expect('5 << 2').toEvaluateTo(20)
// 5 << 2 = 20
})
test('shift by zero', () => {
expect('5 << 0').toEvaluateTo(5)
})
test('shift by one', () => {
expect('3 << 1').toEvaluateTo(6)
})
test('large shift', () => {
expect('1 << 10').toEvaluateTo(1024)
})
})
describe('>> (signed right shift)', () => {
test('basic right shift', () => {
expect('20 >> 2').toEvaluateTo(5)
// 20 >> 2 = 5
})
test('shift by zero', () => {
expect('20 >> 0').toEvaluateTo(20)
})
test('preserves sign for negative numbers', () => {
expect('-20 >> 2').toEvaluateTo(-5)
// Sign is preserved
})
test('negative number right shift', () => {
expect('-8 >> 1').toEvaluateTo(-4)
})
})
describe('>>> (unsigned right shift)', () => {
test('basic unsigned right shift', () => {
expect('20 >>> 2').toEvaluateTo(5)
})
test('unsigned shift of -1', () => {
expect('-1 >>> 1').toEvaluateTo(2147483647)
// -1 >>> 1 = 2147483647 (unsigned, no sign extension)
})
test('unsigned shift of negative number', () => {
expect('-8 >>> 1').toEvaluateTo(2147483644)
})
})
describe('compound expressions', () => {
test('multiple bitwise operations', () => {
expect('(5 band 3) bor (8 bxor 12)').toEvaluateTo(5)
// (5 & 3) | (8 ^ 12) = 1 | 4 = 5
})
test('bitwise with variables', () => {
expect(`
a = 5
b = 3
a bor b
`).toEvaluateTo(7)
})
test('shift operations with variables', () => {
expect(`
x = 16
y = 2
x >> y
`).toEvaluateTo(4)
})
test('mixing shifts and bitwise', () => {
expect('(8 << 1) band 15').toEvaluateTo(0)
// (8 << 1) & 15 = 16 & 15 = 0
})
test('mixing shifts and bitwise 2', () => {
expect('(7 << 1) band 15').toEvaluateTo(14)
// (7 << 1) & 15 = 14 & 15 = 14
})
})
describe('precedence', () => {
test('bitwise has correct precedence with arithmetic', () => {
expect('1 + 2 band 3').toEvaluateTo(3)
// (1 + 2) & 3 = 3 & 3 = 3
})
test('shift has correct precedence', () => {
expect('4 + 8 << 1').toEvaluateTo(24)
// (4 + 8) << 1 = 12 << 1 = 24
})
})
})

View File

@ -110,14 +110,7 @@ describe('compiler', () => {
}) })
test('function call with no args', () => { test('function call with no args', () => {
expect(`bloop = do: 'bleep' end; bloop`).toEvaluateTo('bleep') expect(`bloop = do: 'bloop' end; bloop`).toEvaluateTo('bloop')
expect(`bloop = [ go=do: 'bleep' end ]; bloop.go`).toEvaluateTo('bleep')
expect(`bloop = [ go=do: 'bleep' end ]; abc = do x: x end; abc (bloop.go)`).toEvaluateTo(
'bleep',
)
expect(`num = ((math.random) * 10 + 1) | math.floor; num >= 1 and num <= 10 `).toEvaluateTo(
true,
)
}) })
test('function call with if statement and multiple expressions', () => { test('function call with if statement and multiple expressions', () => {
@ -192,16 +185,6 @@ describe('compiler', () => {
test('single line if', () => { test('single line if', () => {
expect(`if 3 < 9: shire end`).toEvaluateTo('shire') expect(`if 3 < 9: shire end`).toEvaluateTo('shire')
}) })
test('if statement with function definition (bytecode labels)', () => {
expect(`
if false:
abc = do x: x end
else:
nope
end
`).toEvaluateTo('nope')
})
}) })
describe('errors', () => { describe('errors', () => {
@ -298,43 +281,6 @@ describe('dot get', () => {
test('use parens expr with dot-get', () => { test('use parens expr with dot-get', () => {
expect(`a = 1; arr = array 'a' 'b' 'c'; arr.(1 + a)`).toEvaluateTo('c', { array }) expect(`a = 1; arr = array 'a' 'b' 'c'; arr.(1 + a)`).toEvaluateTo('c', { array })
}) })
test('chained dot get: two levels', () => {
expect(`obj = [inner=[value=42]]; obj.inner.value`).toEvaluateTo(42)
})
test('chained dot get: three levels', () => {
expect(`obj = [a=[b=[c=123]]]; obj.a.b.c`).toEvaluateTo(123)
})
test('chained dot get: four levels', () => {
expect(`obj = [w=[x=[y=[z='deep']]]]; obj.w.x.y.z`).toEvaluateTo('deep')
})
test('chained dot get with numeric index', () => {
expect(`obj = [items=[1 2 3]]; obj.items.0`).toEvaluateTo(1)
})
test('chained dot get in expression', () => {
expect(`config = [server=[port=3000]]; config.server.port + 1`).toEvaluateTo(3001)
})
test('chained dot get as function argument', () => {
const double = (x: number) => x * 2
expect(`obj = [val=[num=21]]; double obj.val.num`).toEvaluateTo(42, { double })
})
test('chained dot get in binary operation', () => {
expect(`a = [x=[y=10]]; b = [x=[y=20]]; a.x.y + b.x.y`).toEvaluateTo(30)
})
test('chained dot get with parens at end', () => {
expect(`idx = 1; obj = [items=[10 20 30]]; obj.items.(idx)`).toEvaluateTo(20)
})
test('mixed chained and simple dot get', () => {
expect(`obj = [a=1 b=[c=2]]; obj.a + obj.b.c`).toEvaluateTo(3)
})
}) })
describe('default params', () => { describe('default params', () => {
@ -352,23 +298,6 @@ describe('default params', () => {
expect('multiply = do x y=5: x * y end; multiply 5 2').toEvaluateTo(10) expect('multiply = do x y=5: x * y end; multiply 5 2').toEvaluateTo(10)
}) })
test('null triggers default value', () => {
expect('test = do n=true: n end; test').toEvaluateTo(true)
expect('test = do n=true: n end; test false').toEvaluateTo(false)
expect('test = do n=true: n end; test null').toEvaluateTo(true)
})
test('null triggers default for named parameters', () => {
expect("greet = do name='World': name end; greet name=null").toEvaluateTo('World')
expect("greet = do name='World': name end; greet name='Bob'").toEvaluateTo('Bob')
})
test('null triggers default with multiple parameters', () => {
expect('calc = do x=10 y=20: x + y end; calc null 5').toEvaluateTo(15)
expect('calc = do x=10 y=20: x + y end; calc 3 null').toEvaluateTo(23)
expect('calc = do x=10 y=20: x + y end; calc null null').toEvaluateTo(30)
})
test.skip('array default', () => { test.skip('array default', () => {
expect('abc = do alpha=[a b c]: alpha end; abc').toEvaluateTo(['a', 'b', 'c']) expect('abc = do alpha=[a b c]: alpha end; abc').toEvaluateTo(['a', 'b', 'c'])
expect('abc = do alpha=[a b c]: alpha end; abc [x y z]').toEvaluateTo(['x', 'y', 'z']) expect('abc = do alpha=[a b c]: alpha end; abc [x y z]').toEvaluateTo(['x', 'y', 'z'])
@ -380,154 +309,7 @@ describe('default params', () => {
age: 60, age: 60,
}) })
expect( expect(
'make-person = do person=[name=Bob age=60]: person end; make-person [name=Jon age=21]', 'make-person = do person=[name=Bob age=60]: person end; make-person [name=Jon age=21]'
).toEvaluateTo({ name: 'Jon', age: 21 }) ).toEvaluateTo({ name: 'Jon', age: 21 })
}) })
}) })
describe('Nullish coalescing operator (??)', () => {
test('returns left side when not null', () => {
expect('5 ?? 10').toEvaluateTo(5)
})
test('returns right side when left is null', () => {
expect('null ?? 10').toEvaluateTo(10)
})
test('returns left side when left is false', () => {
expect('false ?? 10').toEvaluateTo(false)
})
test('returns left side when left is 0', () => {
expect('0 ?? 10').toEvaluateTo(0)
})
test('returns left side when left is empty string', () => {
expect(`'' ?? 'default'`).toEvaluateTo('')
})
test('chains left to right', () => {
expect('null ?? null ?? 42').toEvaluateTo(42)
expect('null ?? 10 ?? 20').toEvaluateTo(10)
})
test('short-circuits evaluation', () => {
const throwError = () => {
throw new Error('Should not evaluate')
}
expect('5 ?? throw-error').toEvaluateTo(5, { 'throw-error': throwError })
})
test('works with variables', () => {
expect('x = null; x ?? 5').toEvaluateTo(5)
expect('y = 3; y ?? 5').toEvaluateTo(3)
})
test('works with function calls', () => {
const getValue = () => null
const getDefault = () => 42
// Note: identifiers without parentheses refer to the function, not call it
// Use explicit call syntax to invoke the function
expect('(get-value) ?? (get-default)').toEvaluateTo(42, {
'get-value': getValue,
'get-default': getDefault,
})
})
})
describe('Nullish coalescing assignment (??=)', () => {
test('assigns when variable is null', () => {
expect('x = null; x ??= 5; x').toEvaluateTo(5)
})
test('does not assign when variable is not null', () => {
expect('x = 3; x ??= 10; x').toEvaluateTo(3)
})
test('does not assign when variable is false', () => {
expect('x = false; x ??= true; x').toEvaluateTo(false)
})
test('does not assign when variable is 0', () => {
expect('x = 0; x ??= 100; x').toEvaluateTo(0)
})
test('does not assign when variable is empty string', () => {
expect(`x = ''; x ??= 'default'; x`).toEvaluateTo('')
})
test('returns the final value', () => {
expect('x = null; x ??= 5').toEvaluateTo(5)
expect('y = 3; y ??= 10').toEvaluateTo(3)
})
test('short-circuits evaluation when not null', () => {
const throwError = () => {
throw new Error('Should not evaluate')
}
expect('x = 5; x ??= throw-error; x').toEvaluateTo(5, { 'throw-error': throwError })
})
test('works with expressions', () => {
expect('x = null; x ??= 2 + 3; x').toEvaluateTo(5)
})
test('works with function calls', () => {
const getDefault = () => 42
expect('x = null; x ??= (get-default); x').toEvaluateTo(42, { 'get-default': getDefault })
})
test('throws when variable is undefined', () => {
expect(() => expect('undefined-var ??= 5').toEvaluateTo(null)).toThrow()
})
})
describe('Compound assignment operators', () => {
test('+=', () => {
expect('x = 5; x += 3; x').toEvaluateTo(8)
})
test('-=', () => {
expect('x = 10; x -= 4; x').toEvaluateTo(6)
})
test('*=', () => {
expect('x = 3; x *= 4; x').toEvaluateTo(12)
})
test('/=', () => {
expect('x = 20; x /= 5; x').toEvaluateTo(4)
})
test('%=', () => {
expect('x = 10; x %= 3; x').toEvaluateTo(1)
})
})
describe('import', () => {
test('imports single dict', () => {
expect(`import str; starts-with? abc a`).toEvaluateTo(true)
})
test('imports multiple dicts', () => {
expect(`import str math list; map [1 2 3] do x: x * 2 end`).toEvaluateTo([2, 4, 6])
})
test('imports non-prelude dicts', () => {
expect(`
abc = [a=true b=yes c=si]
import abc
abc.b
`).toEvaluateTo('yes')
})
test('can specify imports', () => {
expect(`import str only=ends-with?; ref ends-with? | function?`).toEvaluateTo(true)
expect(`import str only=ends-with?; ref starts-with? | function?`).toEvaluateTo(false)
expect(`
abc = [a=true b=yes c=si]
import abc only=[a c]
[a c]
`).toEvaluateTo([true, 'si'])
})
})

View File

@ -10,16 +10,12 @@ describe('single line function blocks', () => {
}) })
test('work with named args', () => { test('work with named args', () => {
expect( expect(`attach = do signal fn: [ signal (fn) ] end; attach signal='exit': true end`).toEvaluateTo(['exit', true])
`attach = do signal fn: [ signal (fn) ] end; attach signal='exit': true end`,
).toEvaluateTo(['exit', true])
}) })
test('work with dot-get', () => { test('work with dot-get', () => {
expect(`signals = [trap=do x y: [x (y)] end]; signals.trap 'EXIT': true end`).toEvaluateTo([ expect(`signals = [trap=do x y: [x (y)] end]; signals.trap 'EXIT': true end`).toEvaluateTo(['EXIT', true])
'EXIT',
true,
])
}) })
}) })
@ -27,15 +23,15 @@ describe('multi line function blocks', () => {
test('work with no args', () => { test('work with no args', () => {
expect(` expect(`
trap = do x: x end trap = do x: x end
trap: trap:
true true
end`).toEvaluateTo(true) end`).toEvaluateTo(true)
}) })
test('work with one arg', () => { test('work with one arg', () => {
expect(` expect(`
trap = do x y: [ x (y) ] end trap = do x y: [ x (y) ] end
trap EXIT: trap EXIT:
true true
end`).toEvaluateTo(['EXIT', true]) end`).toEvaluateTo(['EXIT', true])
}) })
@ -43,16 +39,17 @@ end`).toEvaluateTo(['EXIT', true])
test('work with named args', () => { test('work with named args', () => {
expect(` expect(`
attach = do signal fn: [ signal (fn) ] end attach = do signal fn: [ signal (fn) ] end
attach signal='exit': attach signal='exit':
true true
end`).toEvaluateTo(['exit', true]) end`).toEvaluateTo(['exit', true])
}) })
test('work with dot-get', () => { test('work with dot-get', () => {
expect(` expect(`
signals = [trap=do x y: [x (y)] end] signals = [trap=do x y: [x (y)] end]
signals.trap 'EXIT': signals.trap 'EXIT':
true true
end`).toEvaluateTo(['EXIT', true]) end`).toEvaluateTo(['EXIT', true])
}) })
}) })

View File

@ -1,55 +1,6 @@
import { describe } from 'bun:test' import { describe } from 'bun:test'
import { expect, test } from 'bun:test' import { expect, test } from 'bun:test'
describe('number literals', () => {
test('binary literals', () => {
expect('0b110').toEvaluateTo(6)
expect('0b1010').toEvaluateTo(10)
expect('0b11111111').toEvaluateTo(255)
expect('0b0').toEvaluateTo(0)
expect('0b1').toEvaluateTo(1)
})
test('hex literals', () => {
expect('0xdeadbeef').toEvaluateTo(0xdeadbeef)
expect('0xdeadbeef').toEvaluateTo(3735928559)
expect('0xFF').toEvaluateTo(255)
expect('0xff').toEvaluateTo(255)
expect('0x10').toEvaluateTo(16)
expect('0x0').toEvaluateTo(0)
expect('0xABCDEF').toEvaluateTo(0xabcdef)
})
test('octal literals', () => {
expect('0o644').toEvaluateTo(420)
expect('0o755').toEvaluateTo(493)
expect('0o777').toEvaluateTo(511)
expect('0o10').toEvaluateTo(8)
expect('0o0').toEvaluateTo(0)
expect('0o123').toEvaluateTo(83)
})
test('decimal literals still work', () => {
expect('42').toEvaluateTo(42)
expect('3.14').toEvaluateTo(3.14)
expect('0').toEvaluateTo(0)
expect('999999').toEvaluateTo(999999)
})
test('negative hex, binary, and octal', () => {
expect('-0xFF').toEvaluateTo(-255)
expect('-0b1010').toEvaluateTo(-10)
expect('-0o755').toEvaluateTo(-493)
})
test('positive prefix', () => {
expect('+0xFF').toEvaluateTo(255)
expect('+0b110').toEvaluateTo(6)
expect('+0o644').toEvaluateTo(420)
expect('+42').toEvaluateTo(42)
})
})
describe('array literals', () => { describe('array literals', () => {
test('work with numbers', () => { test('work with numbers', () => {
expect('[1 2 3]').toEvaluateTo([1, 2, 3]) expect('[1 2 3]').toEvaluateTo([1, 2, 3])
@ -151,22 +102,18 @@ describe('array literals', () => {
describe('dict literals', () => { describe('dict literals', () => {
test('work with numbers', () => { test('work with numbers', () => {
expect('[a=1 b=2 c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 }) expect('[a=1 b=2 c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 })
expect('[a = 1 b = 2 c = 3]').toEvaluateTo({ a: 1, b: 2, c: 3 })
}) })
test('work with strings', () => { test('work with strings', () => {
expect("[a='one' b='two' c='three']").toEvaluateTo({ a: 'one', b: 'two', c: 'three' }) expect("[a='one' b='two' c='three']").toEvaluateTo({ a: 'one', b: 'two', c: 'three' })
expect("[a = 'one' b = 'two' c = 'three']").toEvaluateTo({ a: 'one', b: 'two', c: 'three' })
}) })
test('work with identifiers', () => { test('work with identifiers', () => {
expect('[a=one b=two c=three]').toEvaluateTo({ a: 'one', b: 'two', c: 'three' }) expect('[a=one b=two c=three]').toEvaluateTo({ a: 'one', b: 'two', c: 'three' })
expect('[a = one b = two c = three]').toEvaluateTo({ a: 'one', b: 'two', c: 'three' })
}) })
test('can be nested', () => { test('can be nested', () => {
expect('[a=one b=[two [c=three]]]').toEvaluateTo({ a: 'one', b: ['two', { c: 'three' }] }) expect('[a=one b=[two [c=three]]]').toEvaluateTo({ a: 'one', b: ['two', { c: 'three' }] })
expect('[a = one b = [two [c = three]]]').toEvaluateTo({ a: 'one', b: ['two', { c: 'three' }] })
}) })
test('can span multiple lines', () => { test('can span multiple lines', () => {
@ -175,12 +122,6 @@ describe('dict literals', () => {
b=2 b=2
c=3 c=3
]`).toEvaluateTo({ a: 1, b: 2, c: 3 }) ]`).toEvaluateTo({ a: 1, b: 2, c: 3 })
expect(`[
a = 1
b = 2
c = 3
]`).toEvaluateTo({ a: 1, b: 2, c: 3 })
}) })
test('empty dict', () => { test('empty dict', () => {
@ -200,12 +141,10 @@ describe('dict literals', () => {
test('semicolons as separators', () => { test('semicolons as separators', () => {
expect('[a=1; b=2; c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 }) expect('[a=1; b=2; c=3]').toEvaluateTo({ a: 1, b: 2, c: 3 })
expect('[a = 1; b = 2; c = 3]').toEvaluateTo({ a: 1, b: 2, c: 3 })
}) })
test('expressions in dicts', () => { test('expressions in dicts', () => {
expect('[a=(1 + 2) b=(3 * 4)]').toEvaluateTo({ a: 3, b: 12 }) expect('[a=(1 + 2) b=(3 * 4)]').toEvaluateTo({ a: 3, b: 12 })
expect('[a = (1 + 2) b = (3 * 4)]').toEvaluateTo({ a: 3, b: 12 })
}) })
test('empty lines within dicts', () => { test('empty lines within dicts', () => {
@ -216,69 +155,3 @@ describe('dict literals', () => {
c=3]`).toEvaluateTo({ a: 1, b: 2, c: 3 }) c=3]`).toEvaluateTo({ a: 1, b: 2, c: 3 })
}) })
}) })
describe('curly strings', () => {
test('work on one line', () => {
expect('{ one two three }').toEvaluateTo(' one two three ')
})
test('work on multiple lines', () => {
expect(`{
one
two
three
}`).toEvaluateTo('\n one\n two\n three\n ')
})
test('can contain other curlies', () => {
expect(`{
{ one }
two
{ three }
}`).toEvaluateTo('\n { one }\n two\n { three }\n ')
})
test('interpolates variables', () => {
expect(`name = Bob; { Hello $name! }`).toEvaluateTo(` Hello Bob! `)
})
test("doesn't interpolate escaped variables ", () => {
expect(`name = Bob; { Hello \\$name }`).toEvaluateTo(` Hello $name `)
expect(`a = 1; b = 2; { sum is \\$(a + b)! }`).toEvaluateTo(` sum is $(a + b)! `)
})
test('interpolates expressions', () => {
expect(`a = 1; b = 2; { sum is $(a + b)! }`).toEvaluateTo(` sum is 3! `)
expect(`a = 1; b = 2; { sum is { $(a + b) }! }`).toEvaluateTo(` sum is { 3 }! `)
expect(`a = 1; b = 2; { sum is $(a + (b * b))! }`).toEvaluateTo(` sum is 5! `)
expect(`{ This is $({twisted}). }`).toEvaluateTo(` This is twisted. `)
expect(`{ This is $({{twisted}}). }`).toEvaluateTo(` This is {twisted}. `)
})
test('interpolation edge cases', () => {
expect(`{[a=1 b=2 c={wild}]}`).toEvaluateTo(`[a=1 b=2 c={wild}]`)
expect(`a = 1;b = 2;c = 3;{$a $b $c}`).toEvaluateTo(`1 2 3`)
expect(`a = 1;b = 2;c = 3;{$(a)$(b)$(c)}`).toEvaluateTo(`123`)
})
})
describe('double quoted strings', () => {
test('work', () => {
expect(`"hello world"`).toEvaluateTo('hello world')
})
test("don't interpolate", () => {
expect(`"hello $world"`).toEvaluateTo('hello $world')
expect(`"hello $(1 + 2)"`).toEvaluateTo('hello $(1 + 2)')
})
test('equal regular strings', () => {
expect(`"hello world" == 'hello world'`).toEvaluateTo(true)
})
test('can contain newlines', () => {
expect(`
"hello
world"`).toEvaluateTo('hello\n world')
})
})

View File

@ -40,7 +40,7 @@ describe('Native Function Exceptions', () => {
const vm = new VM(compiler.bytecode) const vm = new VM(compiler.bytecode)
vm.set('async-fail', async () => { vm.set('async-fail', async () => {
await new Promise((resolve) => setTimeout(resolve, 1)) await new Promise(resolve => setTimeout(resolve, 1))
throw new Error('async error') throw new Error('async error')
}) })
@ -237,7 +237,7 @@ describe('Native Function Exceptions', () => {
const result = await vm.run() const result = await vm.run()
expect(result).toEqual({ expect(result).toEqual({
type: 'string', type: 'string',
value: 'This is a very specific error message with details', value: 'This is a very specific error message with details'
}) })
}) })

View File

@ -93,6 +93,7 @@ describe('pipe expressions', () => {
`).toEvaluateTo(5) `).toEvaluateTo(5)
}) })
test('string literals can be piped', () => { test('string literals can be piped', () => {
expect(`'hey there' | str.to-upper`).toEvaluateTo('HEY THERE') expect(`'hey there' | str.to-upper`).toEvaluateTo('HEY THERE')
}) })
@ -117,42 +118,4 @@ describe('pipe expressions', () => {
test('dict literals can be piped', () => { test('dict literals can be piped', () => {
expect(`[a=1 b=2 c=3] | dict.values | list.sort | str.join '-'`).toEvaluateTo('1-2-3') expect(`[a=1 b=2 c=3] | dict.values | list.sort | str.join '-'`).toEvaluateTo('1-2-3')
}) })
test('pipe to array literal using _ in nested expressions', () => {
// _ should be accessible inside nested function calls within array literals
const code = `
double = do x: x * 2 end
triple = do x: x * 3 end
5 | [(double _) (triple _)]`
expect(code).toEvaluateTo([10, 15])
})
test('pipe to array literal using _ multiple times', () => {
expect(`10 | [_ _ _]`).toEvaluateTo([10, 10, 10])
})
test('pipe to parenthesized expression using _', () => {
const code = `
double = do x: x * 2 end
5 | (double _)`
expect(code).toEvaluateTo(10)
})
test('pipe chain with array literal receiver', () => {
// Pipe to array, then pipe that array to a function
const code = `
double = do x: x * 2 end
5 | [(double _) _] | list.sum`
expect(code).toEvaluateTo(15) // [10, 5] -> 15
})
test('_ in deeply nested expressions within pipe', () => {
// _ should work in nested function calls within function arguments
const code = `
add = do a b: a + b end
mul = do a b: a * b end
10 | add (mul _ 2) _`
// add(mul(10, 2), 10) = add(20, 10) = 30
expect(code).toEvaluateTo(30)
})
}) })

View File

@ -5,7 +5,7 @@ const buffer: string[] = []
const ribbitGlobals = { const ribbitGlobals = {
ribbit: async (cb: Function) => { ribbit: async (cb: Function) => {
await cb() await cb()
return buffer.join('\n') return buffer.join("\n")
}, },
tag: async (tagFn: Function, atDefaults = {}) => { tag: async (tagFn: Function, atDefaults = {}) => {
return (atNamed = {}, ...args: any[]) => tagFn(Object.assign({}, atDefaults, atNamed), ...args) return (atNamed = {}, ...args: any[]) => tagFn(Object.assign({}, atDefaults, atNamed), ...args)
@ -20,12 +20,10 @@ const ribbitGlobals = {
ul: (atNamed: {}, ...args: any[]) => tag('ul', atNamed, ...args), ul: (atNamed: {}, ...args: any[]) => tag('ul', atNamed, ...args),
li: (atNamed: {}, ...args: any[]) => tag('li', atNamed, ...args), li: (atNamed: {}, ...args: any[]) => tag('li', atNamed, ...args),
nospace: () => NOSPACE_TOKEN, nospace: () => NOSPACE_TOKEN,
echo: (...args: any[]) => console.log(...args), echo: (...args: any[]) => console.log(...args)
} }
function raw(fn: Function) { function raw(fn: Function) { (fn as any).raw = true }
;(fn as any).raw = true
}
const tagBlock = async (tagName: string, props = {}, fn: Function) => { const tagBlock = async (tagName: string, props = {}, fn: Function) => {
const attrs = Object.entries(props).map(([key, value]) => `${key}="${value}"`) const attrs = Object.entries(props).map(([key, value]) => `${key}="${value}"`)
@ -41,13 +39,14 @@ const tagCall = (tagName: string, atNamed = {}, ...args: any[]) => {
const space = attrs.length ? ' ' : '' const space = attrs.length ? ' ' : ''
const children = args const children = args
.reverse() .reverse()
.map((a) => (a === TAG_TOKEN ? buffer.pop() : a)) .map(a => a === TAG_TOKEN ? buffer.pop() : a)
.reverse() .reverse().join(' ')
.join(' ')
.replaceAll(` ${NOSPACE_TOKEN} `, '') .replaceAll(` ${NOSPACE_TOKEN} `, '')
if (SELF_CLOSING.includes(tagName)) buffer.push(`<${tagName}${space}${attrs.join(' ')} />`) if (SELF_CLOSING.includes(tagName))
else buffer.push(`<${tagName}${space}${attrs.join(' ')}>${children}</${tagName}>`) buffer.push(`<${tagName}${space}${attrs.join(' ')} />`)
else
buffer.push(`<${tagName}${space}${attrs.join(' ')}>${children}</${tagName}>`)
} }
const tag = async (tagName: string, atNamed = {}, ...args: any[]) => { const tag = async (tagName: string, atNamed = {}, ...args: any[]) => {
@ -61,25 +60,10 @@ const tag = async (tagName: string, atNamed = {}, ...args: any[]) => {
const NOSPACE_TOKEN = '!!ribbit-nospace!!' const NOSPACE_TOKEN = '!!ribbit-nospace!!'
const TAG_TOKEN = '!!ribbit-tag!!' const TAG_TOKEN = '!!ribbit-tag!!'
const SELF_CLOSING = [ const SELF_CLOSING = ["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"]
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'link',
'meta',
'param',
'source',
'track',
'wbr',
]
describe('ribbit', () => { describe('ribbit', () => {
beforeEach(() => (buffer.length = 0)) beforeEach(() => buffer.length = 0)
test('head tag', () => { test('head tag', () => {
expect(` expect(`
@ -90,14 +74,11 @@ ribbit:
meta name=viewport content='width=device-width, initial-scale=1, viewport-fit=cover' meta name=viewport content='width=device-width, initial-scale=1, viewport-fit=cover'
end end
end end
`).toEvaluateTo( `).toEvaluateTo(`<head>
`<head>
<title>What up</title> <title>What up</title>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" /> <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
</head>`, </head>`, ribbitGlobals)
ribbitGlobals,
)
}) })
test('custom tags', () => { test('custom tags', () => {
@ -109,14 +90,11 @@ ribbit:
li two li two
li three li three
end end
end`).toEvaluateTo( end`).toEvaluateTo(`<ul class="list">
`<ul class="list">
<li border-bottom="1px solid black">one</li> <li border-bottom="1px solid black">one</li>
<li>two</li> <li>two</li>
<li>three</li> <li>three</li>
</ul>`, </ul>`, ribbitGlobals)
ribbitGlobals,
)
}) })
test('inline expressions', () => { test('inline expressions', () => {
@ -132,8 +110,6 @@ end`).toEvaluateTo(
<h1 class="bright" style="font-family: helvetica">Heya</h1> <h1 class="bright" style="font-family: helvetica">Heya</h1>
<h2>man that is <b>wild</b>!</h2> <h2>man that is <b>wild</b>!</h2>
<p>Double the fun.</p> <p>Double the fun.</p>
</p>`, </p>`, ribbitGlobals)
ribbitGlobals,
)
}) })
}) })

View File

@ -10,7 +10,8 @@ describe('while', () => {
a = false a = false
b = done b = done
end end
b`).toEvaluateTo('done') b`)
.toEvaluateTo('done')
}) })
test('basic expression', () => { test('basic expression', () => {
@ -19,7 +20,8 @@ describe('while', () => {
while a < 10: while a < 10:
a += 1 a += 1
end end
a`).toEvaluateTo(10) a`)
.toEvaluateTo(10)
}) })
test('compound expression', () => { test('compound expression', () => {
@ -29,7 +31,8 @@ describe('while', () => {
while a > 0 and b < 100: while a > 0 and b < 100:
b += 1 b += 1
end end
b`).toEvaluateTo(100) b`)
.toEvaluateTo(100)
}) })
test('returns value', () => { test('returns value', () => {
@ -39,6 +42,7 @@ describe('while', () => {
a += 1 a += 1
done done
end end
ret`).toEvaluateTo('done') ret`)
.toEvaluateTo('done')
}) })
}) })

View File

@ -1,9 +1,9 @@
import { CompilerError } from '#compiler/compilerError.ts' import { CompilerError } from '#compiler/compilerError.ts'
import type { SyntaxNode, Tree } from '#parser/node' import * as terms from '#parser/shrimp.terms'
import type { SyntaxNode, Tree } from '@lezer/common'
export const checkTreeForErrors = (tree: Tree): CompilerError[] => { export const checkTreeForErrors = (tree: Tree): CompilerError[] => {
const errors: CompilerError[] = [] const errors: CompilerError[] = []
tree.iterate({ tree.iterate({
enter: (node) => { enter: (node) => {
if (node.type.isError) { if (node.type.isError) {
@ -23,7 +23,7 @@ export const getAllChildren = (node: SyntaxNode): SyntaxNode[] => {
child = child.nextSibling child = child.nextSibling
} }
return children.filter((n) => !n.type.is('Comment')) return children.filter((n) => n.type.id !== terms.Comment)
} }
export const getBinaryParts = (node: SyntaxNode) => { export const getBinaryParts = (node: SyntaxNode) => {
@ -45,23 +45,23 @@ export const getAssignmentParts = (node: SyntaxNode) => {
throw new CompilerError( throw new CompilerError(
`Assign expected 3 children, got ${children.length}`, `Assign expected 3 children, got ${children.length}`,
node.from, node.from,
node.to, node.to
) )
} }
// array destructuring // array destructuring
if (left && left.type.is('Array')) { if (left && left.type.id === terms.Array) {
const identifiers = getAllChildren(left).filter((child) => child.type.is('Identifier')) const identifiers = getAllChildren(left).filter((child) => child.type.id === terms.Identifier)
return { arrayPattern: identifiers, right } return { arrayPattern: identifiers, right }
} }
if (!left || !left.type.is('AssignableIdentifier')) { if (!left || left.type.id !== terms.AssignableIdentifier) {
throw new CompilerError( throw new CompilerError(
`Assign left child must be an AssignableIdentifier or Array, got ${ `Assign left child must be an AssignableIdentifier or Array, got ${
left ? left.type.name : 'none' left ? left.type.name : 'none'
}`, }`,
node.from, node.from,
node.to, node.to
) )
} }
@ -72,19 +72,19 @@ export const getCompoundAssignmentParts = (node: SyntaxNode) => {
const children = getAllChildren(node) const children = getAllChildren(node)
const [left, operator, right] = children const [left, operator, right] = children
if (!left || !left.type.is('AssignableIdentifier')) { if (!left || left.type.id !== terms.AssignableIdentifier) {
throw new CompilerError( throw new CompilerError(
`CompoundAssign left child must be an AssignableIdentifier, got ${ `CompoundAssign left child must be an AssignableIdentifier, got ${
left ? left.type.name : 'none' left ? left.type.name : 'none'
}`, }`,
node.from, node.from,
node.to, node.to
) )
} else if (!operator || !right) { } else if (!operator || !right) {
throw new CompilerError( throw new CompilerError(
`CompoundAssign expected 3 children, got ${children.length}`, `CompoundAssign expected 3 children, got ${children.length}`,
node.from, node.from,
node.to, node.to
) )
} }
@ -99,16 +99,16 @@ export const getFunctionDefParts = (node: SyntaxNode, input: string) => {
throw new CompilerError( throw new CompilerError(
`FunctionDef expected at least 4 children, got ${children.length}`, `FunctionDef expected at least 4 children, got ${children.length}`,
node.from, node.from,
node.to, node.to
) )
} }
const paramNames = getAllChildren(paramsNode).map((param) => { const paramNames = getAllChildren(paramsNode).map((param) => {
if (!param.type.is('Identifier') && !param.type.is('NamedParam')) { if (param.type.id !== terms.Identifier && param.type.id !== terms.NamedParam) {
throw new CompilerError( throw new CompilerError(
`FunctionDef params must be Identifier or NamedParam, got ${param.type.name}`, `FunctionDef params must be Identifier or NamedParam, got ${param.type.name}`,
param.from, param.from,
param.to, param.to
) )
} }
return input.slice(param.from, param.to) return input.slice(param.from, param.to)
@ -123,7 +123,7 @@ export const getFunctionDefParts = (node: SyntaxNode, input: string) => {
let finallyBody: SyntaxNode | undefined let finallyBody: SyntaxNode | undefined
for (const child of rest) { for (const child of rest) {
if (child.type.is('CatchExpr')) { if (child.type.id === terms.CatchExpr) {
catchExpr = child catchExpr = child
const catchChildren = getAllChildren(child) const catchChildren = getAllChildren(child)
const [_catchKeyword, identifierNode, _colon, body] = catchChildren const [_catchKeyword, identifierNode, _colon, body] = catchChildren
@ -131,12 +131,12 @@ export const getFunctionDefParts = (node: SyntaxNode, input: string) => {
throw new CompilerError( throw new CompilerError(
`CatchExpr expected identifier and body, got ${catchChildren.length} children`, `CatchExpr expected identifier and body, got ${catchChildren.length} children`,
child.from, child.from,
child.to, child.to
) )
} }
catchVariable = input.slice(identifierNode.from, identifierNode.to) catchVariable = input.slice(identifierNode.from, identifierNode.to)
catchBody = body catchBody = body
} else if (child.type.is('FinallyExpr')) { } else if (child.type.id === terms.FinallyExpr) {
finallyExpr = child finallyExpr = child
const finallyChildren = getAllChildren(child) const finallyChildren = getAllChildren(child)
const [_finallyKeyword, _colon, body] = finallyChildren const [_finallyKeyword, _colon, body] = finallyChildren
@ -144,7 +144,7 @@ export const getFunctionDefParts = (node: SyntaxNode, input: string) => {
throw new CompilerError( throw new CompilerError(
`FinallyExpr expected body, got ${finallyChildren.length} children`, `FinallyExpr expected body, got ${finallyChildren.length} children`,
child.from, child.from,
child.to, child.to
) )
} }
finallyBody = body finallyBody = body
@ -165,9 +165,9 @@ export const getFunctionCallParts = (node: SyntaxNode, input: string) => {
throw new CompilerError(`FunctionCall expected at least 1 child, got 0`, node.from, node.to) throw new CompilerError(`FunctionCall expected at least 1 child, got 0`, node.from, node.to)
} }
const namedArgs = args.filter((arg) => arg.type.is('NamedArg')) const namedArgs = args.filter((arg) => arg.type.id === terms.NamedArg)
const positionalArgs = args const positionalArgs = args
.filter((arg) => arg.type.is('PositionalArg')) .filter((arg) => arg.type.id === terms.PositionalArg)
.map((arg) => { .map((arg) => {
const child = arg.firstChild const child = arg.firstChild
if (!child) throw new CompilerError(`PositionalArg has no child`, arg.from, arg.to) if (!child) throw new CompilerError(`PositionalArg has no child`, arg.from, arg.to)
@ -199,7 +199,7 @@ export const getIfExprParts = (node: SyntaxNode, input: string) => {
throw new CompilerError( throw new CompilerError(
`IfExpr expected at least 4 children, got ${children.length}`, `IfExpr expected at least 4 children, got ${children.length}`,
node.from, node.from,
node.to, node.to
) )
} }
@ -208,13 +208,13 @@ export const getIfExprParts = (node: SyntaxNode, input: string) => {
rest.forEach((child) => { rest.forEach((child) => {
const parts = getAllChildren(child) const parts = getAllChildren(child)
if (child.type.is('ElseExpr')) { if (child.type.id === terms.ElseExpr) {
if (parts.length !== 3) { if (parts.length !== 3) {
const message = `ElseExpr expected 1 child, got ${parts.length}` const message = `ElseExpr expected 1 child, got ${parts.length}`
throw new CompilerError(message, child.from, child.to) throw new CompilerError(message, child.from, child.to)
} }
elseThenBlock = parts.at(-1) elseThenBlock = parts.at(-1)
} else if (child.type.is('ElseIfExpr')) { } else if (child.type.id === terms.ElseIfExpr) {
const [_else, _if, conditional, _colon, thenBlock] = parts const [_else, _if, conditional, _colon, thenBlock] = parts
if (!conditional || !thenBlock) { if (!conditional || !thenBlock) {
const names = parts.map((p) => p.type.name).join(', ') const names = parts.map((p) => p.type.name).join(', ')
@ -249,32 +249,32 @@ export const getStringParts = (node: SyntaxNode, input: string) => {
// The text is just between the quotes // The text is just between the quotes
const parts = children.filter((child) => { const parts = children.filter((child) => {
return ( return (
child.type.is('StringFragment') || child.type.id === terms.StringFragment ||
child.type.is('Interpolation') || child.type.id === terms.Interpolation ||
child.type.is('EscapeSeq') || child.type.id === terms.EscapeSeq
child.type.is('CurlyString')
) )
}) })
// Validate each part is the expected type // Validate each part is the expected type
parts.forEach((part) => { parts.forEach((part) => {
if ( if (
part.type.is('StringFragment') && part.type.id !== terms.StringFragment &&
part.type.is('Interpolation') && part.type.id !== terms.Interpolation &&
part.type.is('EscapeSeq') && part.type.id !== terms.EscapeSeq
part.type.is('CurlyString')
) { ) {
throw new CompilerError( throw new CompilerError(
`String child must be StringFragment, Interpolation, or EscapeSeq, got ${part.type.name}`, `String child must be StringFragment, Interpolation, or EscapeSeq, got ${part.type.name}`,
part.from, part.from,
part.to, part.to
) )
} }
}) })
// hasInterpolation means the string has interpolation ($var) or escape sequences (\n) // hasInterpolation means the string has interpolation ($var) or escape sequences (\n)
// A simple string like 'hello' has one StringFragment but no interpolation // A simple string like 'hello' has one StringFragment but no interpolation
const hasInterpolation = parts.some((p) => p.type.is('Interpolation') || p.type.is('EscapeSeq')) const hasInterpolation = parts.some(
(p) => p.type.id === terms.Interpolation || p.type.id === terms.EscapeSeq
)
return { parts, hasInterpolation } return { parts, hasInterpolation }
} }
@ -286,29 +286,29 @@ export const getDotGetParts = (node: SyntaxNode, input: string) => {
throw new CompilerError( throw new CompilerError(
`DotGet expected 2 identifier children, got ${children.length}`, `DotGet expected 2 identifier children, got ${children.length}`,
node.from, node.from,
node.to, node.to
) )
} }
if (!object.type.is('IdentifierBeforeDot')) { if (object.type.id !== terms.IdentifierBeforeDot) {
throw new CompilerError( throw new CompilerError(
`DotGet object must be an IdentifierBeforeDot, got ${object.type.name}`, `DotGet object must be an IdentifierBeforeDot, got ${object.type.name}`,
object.from, object.from,
object.to, object.to
) )
} }
if (!['Identifier', 'Number', 'ParenExpr', 'DotGet'].includes(property.type.name)) { if (![terms.Identifier, terms.Number, terms.ParenExpr].includes(property.type.id)) {
throw new CompilerError( throw new CompilerError(
`DotGet property must be an Identifier, Number, ParenExpr, or DotGet, got ${property.type.name}`, `DotGet property must be an Identifier or Number, got ${property.type.name}`,
property.from, property.from,
property.to, property.to
) )
} }
const objectName = input.slice(object.from, object.to) const objectName = input.slice(object.from, object.to)
return { object, objectName, property } return { objectName, property }
} }
export const getTryExprParts = (node: SyntaxNode, input: string) => { export const getTryExprParts = (node: SyntaxNode, input: string) => {
@ -321,7 +321,7 @@ export const getTryExprParts = (node: SyntaxNode, input: string) => {
throw new CompilerError( throw new CompilerError(
`TryExpr expected at least 3 children, got ${children.length}`, `TryExpr expected at least 3 children, got ${children.length}`,
node.from, node.from,
node.to, node.to
) )
} }
@ -332,7 +332,7 @@ export const getTryExprParts = (node: SyntaxNode, input: string) => {
let finallyBody: SyntaxNode | undefined let finallyBody: SyntaxNode | undefined
rest.forEach((child) => { rest.forEach((child) => {
if (child.type.is('CatchExpr')) { if (child.type.id === terms.CatchExpr) {
catchExpr = child catchExpr = child
const catchChildren = getAllChildren(child) const catchChildren = getAllChildren(child)
const [_catchKeyword, identifierNode, _colon, body] = catchChildren const [_catchKeyword, identifierNode, _colon, body] = catchChildren
@ -340,12 +340,12 @@ export const getTryExprParts = (node: SyntaxNode, input: string) => {
throw new CompilerError( throw new CompilerError(
`CatchExpr expected identifier and body, got ${catchChildren.length} children`, `CatchExpr expected identifier and body, got ${catchChildren.length} children`,
child.from, child.from,
child.to, child.to
) )
} }
catchVariable = input.slice(identifierNode.from, identifierNode.to) catchVariable = input.slice(identifierNode.from, identifierNode.to)
catchBody = body catchBody = body
} else if (child.type.is('FinallyExpr')) { } else if (child.type.id === terms.FinallyExpr) {
finallyExpr = child finallyExpr = child
const finallyChildren = getAllChildren(child) const finallyChildren = getAllChildren(child)
const [_finallyKeyword, _colon, body] = finallyChildren const [_finallyKeyword, _colon, body] = finallyChildren
@ -353,7 +353,7 @@ export const getTryExprParts = (node: SyntaxNode, input: string) => {
throw new CompilerError( throw new CompilerError(
`FinallyExpr expected body, got ${finallyChildren.length} children`, `FinallyExpr expected body, got ${finallyChildren.length} children`,
child.from, child.from,
child.to, child.to
) )
} }
finallyBody = body finallyBody = body

261
src/editor/commands.ts Normal file
View File

@ -0,0 +1,261 @@
export type CommandShape = {
command: string
description?: string
args: ArgShape[]
execute: string | ((...args: any[]) => any)
}
type ArgShape<T extends keyof ArgTypeMap = keyof ArgTypeMap> = {
name: string
type: T
description?: string
optional?: boolean
default?: ArgTypeMap[T]
}
type ArgTypeMap = {
string: string
number: number
boolean: boolean
}
const commandShapes: CommandShape[] = [
{
command: 'ls',
description: 'List the contents of a directory',
execute: './commands/ls.ts',
args: [
{ name: 'path', type: 'string', description: 'The path to list' },
{ name: 'all', type: 'boolean', description: 'Show hidden files', default: false },
{ name: 'long', type: 'boolean', description: 'List in long format', default: false },
{
name: 'short-names',
type: 'boolean',
description: 'Only print file names',
default: false,
},
{ name: 'full-paths', type: 'boolean', description: 'Display full paths', default: false },
],
},
{
command: 'cd',
description: 'Change the current working directory',
execute: './commands/cd.ts',
args: [{ name: 'path', type: 'string', description: 'The path to change to' }],
},
{
command: 'cp',
description: 'Copy files or directories',
execute: './commands/cp.ts',
args: [
{ name: 'source', type: 'string', description: 'Source file or directory' },
{ name: 'destination', type: 'string', description: 'Destination path' },
{ name: 'recursive', type: 'boolean', description: 'Copy recursively', default: false },
{ name: 'verbose', type: 'boolean', description: 'Verbose output', default: false },
],
},
{
command: 'mv',
description: 'Move files or directories',
execute: './commands/mv.ts',
args: [
{ name: 'source', type: 'string', description: 'Source file or directory' },
{ name: 'destination', type: 'string', description: 'Destination path' },
{ name: 'verbose', type: 'boolean', description: 'Verbose output', default: false },
],
},
{
command: 'rm',
description: 'Remove files or directories',
execute: './commands/rm.ts',
args: [
{ name: 'path', type: 'string', description: 'Path to remove' },
{ name: 'recursive', type: 'boolean', description: 'Remove recursively', default: false },
{ name: 'force', type: 'boolean', description: 'Force removal', default: false },
{ name: 'verbose', type: 'boolean', description: 'Verbose output', default: false },
],
},
{
command: 'mkdir',
description: 'Create directories',
execute: './commands/mkdir.ts',
args: [
{ name: 'path', type: 'string', description: 'Directory path to create' },
{ name: 'verbose', type: 'boolean', description: 'Verbose output', default: false },
],
},
{
command: 'touch',
description: 'Create empty files or update timestamps',
execute: './commands/touch.ts',
args: [
{ name: 'path', type: 'string', description: 'File path to touch' },
{ name: 'access', type: 'boolean', description: 'Update access time only', default: false },
{
name: 'modified',
type: 'boolean',
description: 'Update modified time only',
default: false,
},
],
},
{
command: 'echo',
description: 'Display a string',
execute: './commands/echo.ts',
args: [
{ name: 'text', type: 'string', description: 'Text to display' },
{ name: 'no-newline', type: 'boolean', description: "Don't append newline", default: false },
],
},
{
command: 'cat',
description: 'Display file contents',
execute: './commands/cat.ts',
args: [
{ name: 'path', type: 'string', description: 'File to display' },
{ name: 'numbered', type: 'boolean', description: 'Show line numbers', default: false },
],
},
{
command: 'head',
description: 'Show first lines of input',
execute: './commands/head.ts',
args: [
{ name: 'path', type: 'string', description: 'File to read from' },
{ name: 'lines', type: 'number', description: 'Number of lines', default: 10 },
],
},
{
command: 'tail',
description: 'Show last lines of input',
execute: './commands/tail.ts',
args: [
{ name: 'path', type: 'string', description: 'File to read from' },
{ name: 'lines', type: 'number', description: 'Number of lines', default: 10 },
{ name: 'follow', type: 'boolean', description: 'Follow file changes', default: false },
],
},
{
command: 'grep',
description: 'Search for patterns in text',
execute: './commands/grep.ts',
args: [
{ name: 'pattern', type: 'string', description: 'Pattern to search for' },
{
name: 'ignore-case',
type: 'boolean',
description: 'Case insensitive search',
default: false,
},
{ name: 'invert-match', type: 'boolean', description: 'Invert match', default: false },
{ name: 'line-number', type: 'boolean', description: 'Show line numbers', default: false },
],
},
{
command: 'sort',
description: 'Sort input',
execute: './commands/sort.ts',
args: [
{ name: 'reverse', type: 'boolean', description: 'Sort in reverse order', default: false },
{
name: 'ignore-case',
type: 'boolean',
description: 'Case insensitive sort',
default: false,
},
{ name: 'numeric', type: 'boolean', description: 'Numeric sort', default: false },
],
},
{
command: 'uniq',
description: 'Filter out repeated lines',
execute: './commands/uniq.ts',
args: [
{ name: 'count', type: 'boolean', description: 'Show count of occurrences', default: false },
{
name: 'repeated',
type: 'boolean',
description: 'Show only repeated lines',
default: false,
},
{ name: 'unique', type: 'boolean', description: 'Show only unique lines', default: false },
],
},
{
command: 'select',
description: 'Select specific columns from data',
execute: './commands/select.ts',
args: [{ name: 'columns', type: 'string', description: 'Columns to select' }],
},
{
command: 'where',
description: 'Filter data based on conditions',
execute: './commands/where.ts',
args: [{ name: 'condition', type: 'string', description: 'Filter condition' }],
},
{
command: 'group-by',
description: 'Group data by column values',
execute: './commands/group-by.ts',
args: [{ name: 'column', type: 'string', description: 'Column to group by' }],
},
{
command: 'ps',
description: 'List running processes',
execute: './commands/ps.ts',
args: [
{ name: 'long', type: 'boolean', description: 'Show detailed information', default: false },
],
},
{
command: 'sys',
description: 'Show system information',
execute: './commands/sys.ts',
args: [],
},
{
command: 'which',
description: 'Find the location of a command',
execute: './commands/which.ts',
args: [
{ name: 'command', type: 'string', description: 'Command to locate' },
{ name: 'all', type: 'boolean', description: 'Show all matches', default: false },
],
},
] as const
let commandSource = () => commandShapes
export const setCommandSource = (do: () => CommandShape[]) => {
commandSource = fn
}
export const resetCommandSource = () => {
commandSource = () => commandShapes
}
export const matchingCommands = (prefix: string) => {
const match = commandSource().find((cmd) => cmd.command === prefix)
const partialMatches = commandSource().filter((cmd) => cmd.command.startsWith(prefix))
return { match, partialMatches }
}

View File

@ -1,87 +0,0 @@
import { autocompletion, type CompletionContext, type Completion } from '@codemirror/autocomplete'
import { Shrimp } from '#/index'
const keywords = [
'import',
'end',
'do',
'if',
'else',
'while',
'try',
'catch',
'finally',
'throw',
'not',
'and',
'or',
'true',
'false',
'null',
]
const keywordCompletions: Completion[] = keywords.map((k) => ({
label: k,
type: 'keyword',
boost: -1,
}))
export const createShrimpCompletions = (shrimp: Shrimp) => {
// Build completions from all names in the shrimp scope
const scopeNames = shrimp.vm.scope.vars()
console.log(`🌭`, shrimp.vm.vars())
const functionCompletions: Completion[] = scopeNames.map((name) => {
const value = shrimp.vm.scope.get(name)
let type
if (value?.type === 'function' || value?.type === 'native') {
type = 'function'
} else if (value?.type === 'dict') {
type = 'namespace'
} else {
type = 'variable'
}
return { type, label: name }
})
const allCompletions = [...keywordCompletions, ...functionCompletions]
// Get methods for a module (e.g., math, str, list)
const getModuleMethods = (moduleName: string): Completion[] => {
const module = shrimp.get(moduleName)
if (!module || typeof module !== 'object') return []
return Object.keys(module).map((m) => ({ label: m, type: 'function' }))
}
const shrimpCompletionSource = (context: CompletionContext) => {
// Check for module.method pattern (e.g., "math.")
const dotMatch = context.matchBefore(/[\w\-]+\.[\w\-\?]*/)
if (dotMatch) {
const [moduleName, methodPrefix] = dotMatch.text.split('.')
const methods = getModuleMethods(moduleName!)
if (methods.length > 0) {
const dotPos = dotMatch.from + moduleName!.length + 1
return {
from: dotPos,
options: methods.filter((m) => m.label.startsWith(methodPrefix ?? '')),
}
}
}
// Regular completions
const word = context.matchBefore(/[\w\-\?\$]+/)
if (!word || (word.from === word.to && !context.explicit)) return null
return {
from: word.from,
options: allCompletions.filter((c) => c.label.startsWith(word.text)),
}
}
return autocompletion({
override: [shrimpCompletionSource],
})
}

View File

@ -1,24 +0,0 @@
import { linter, type Diagnostic } from '@codemirror/lint'
import { Shrimp } from '#/index'
import { CompilerError } from '#compiler/compilerError'
export const createShrimpDiagnostics = (shrimp: Shrimp) =>
linter((view) => {
const code = view.state.doc.toString()
const diagnostics: Diagnostic[] = []
try {
shrimp.parse(code)
} catch (err) {
if (err instanceof CompilerError) {
diagnostics.push({
from: err.from,
to: err.to,
severity: 'error',
message: err.message,
})
}
}
return diagnostics
})

View File

@ -1,78 +1,53 @@
:root { #output {
/* Background colors */ flex: 1;
--bg-editor: #011627; background: var(--bg-output);
--bg-output: #40318D; color: var(--text-output);
--bg-status-bar: #1E2A4A; padding: 20px;
--bg-status-border: #0E1A3A; overflow-y: auto;
--bg-selection: #1D3B53; white-space: pre-wrap;
/* Text colors */
--text-editor: #D6DEEB;
--text-output: #7C70DA;
--text-status: #B3A9FF55;
--caret: #80A4C2;
/* Syntax highlighting colors */
--color-keyword: #C792EA;
--color-function: #82AAFF;
--color-string: #C3E88D;
--color-number: #F78C6C;
--color-bool: #FF5370;
--color-operator: #89DDFF;
--color-paren: #676E95;
--color-function-call: #FF9CAC;
--color-variable-def: #FFCB6B;
--color-error: #FF6E6E;
--color-regex: #E1ACFF;
/* ANSI terminal colors */
--ansi-black: #011627;
--ansi-red: #FF5370;
--ansi-green: #C3E88D;
--ansi-yellow: #FFCB6B;
--ansi-blue: #82AAFF;
--ansi-magenta: #C792EA;
--ansi-cyan: #89DDFF;
--ansi-white: #D6DEEB;
/* ANSI bright colors (slightly more vibrant) */
--ansi-bright-black: #676E95;
--ansi-bright-red: #FF6E90;
--ansi-bright-green: #D4F6A8;
--ansi-bright-yellow: #FFE082;
--ansi-bright-blue: #A8C7FA;
--ansi-bright-magenta: #E1ACFF;
--ansi-bright-cyan: #A8F5FF;
--ansi-bright-white: #FFFFFF;
}
@font-face {
font-family: 'C64ProMono';
src: url('../../assets/C64_Pro_Mono-STYLE.woff2') format('woff2');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Pixeloid Mono';
src: url('../../assets/PixeloidMono.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: var(--bg-editor);
font-family: 'Pixeloid Mono', 'Courier New', monospace; font-family: 'Pixeloid Mono', 'Courier New', monospace;
font-size: 18px; font-size: 18px;
height: 100vh;
} }
#root { #status-bar {
height: 100vh; height: 30px;
background: var(--bg-status-bar);
color: var(--text-status);
display: flex;
align-items: center;
padding: 0 10px;
font-size: 14px;
border-top: 3px solid var(--bg-status-border);
border-bottom: 3px solid var(--bg-status-border);
display: flex;
justify-content: space-between;
}
#status-bar .left,
#status-bar .right {
display: flex;
justify-content: center;
gap: 2rem;
}
#status-bar .multiline {
display: flex;
.dot {
padding-top: 1px;
margin-right: 4px;
}
.active {
color: var(--color-string);
}
.inactive {
color: inherit;
}
}
.syntax-error {
text-decoration: underline dotted var(--color-error);
} }

View File

@ -1,70 +1,140 @@
import { EditorView, basicSetup } from 'codemirror' import { EditorView } from '@codemirror/view'
import { Shrimp } from '#/index' import { asciiEscapeToHtml, assertNever, log, toElement } from '#utils/utils'
import { Signal } from '#utils/signal'
import { getContent } from '#editor/plugins/persistence'
import type { HtmlEscapedString } from 'hono/utils/html'
import { connectToNose, noseSignals } from '#editor/noseClient'
import type { Value } from 'reefvm'
import { Compartment } from '@codemirror/state'
import { lineNumbers } from '@codemirror/view'
import { shrimpSetup } from '#editor/plugins/shrimpSetup'
import { shrimpTheme } from './theme' import '#editor/editor.css'
import { createShrimpDiagnostics } from './diagnostics'
import { shrimpHighlighter } from './highlighter'
import { createShrimpCompletions } from './completions'
import { shrimpKeymap } from './keymap'
import { getContent, persistence } from './persistence'
type EditorProps = { const lineNumbersCompartment = new Compartment()
initialCode?: string
onChange?: (code: string) => void
extensions?: import('@codemirror/state').Extension[]
shrimp?: Shrimp
}
export const Editor = ({ connectToNose()
initialCode = '',
onChange, export const outputSignal = new Signal<Value | string>()
extensions: customExtensions = [], export const errorSignal = new Signal<string>()
shrimp = new Shrimp(), export const multilineModeSignal = new Signal<boolean>()
}: EditorProps) => {
export const Editor = () => {
return ( return (
<div <>
ref={(el: Element) => { <div
if (!el?.querySelector('.cm-editor')) ref={(ref: Element) => {
createEditorView(el, getContent() ?? initialCode, onChange, customExtensions, shrimp) if (ref?.querySelector('.cm-editor')) return
}} const view = new EditorView({
/> parent: ref,
doc: getContent(),
extensions: shrimpSetup(lineNumbersCompartment),
})
multilineModeSignal.connect((isMultiline) => {
view.dispatch({
effects: lineNumbersCompartment.reconfigure(isMultiline ? lineNumbers() : []),
})
})
requestAnimationFrame(() => view.focus())
}}
/>
<div id="status-bar">
<div className="left"></div>
<div className="right"></div>
</div>
<div id="output"></div>
<div id="error"></div>
</>
) )
} }
const createEditorView = ( noseSignals.connect((message) => {
el: Element, if (message.type === 'error') {
initialCode: string, log.error(`Nose error: ${message.data}`)
onChange: ((code: string) => void) | undefined, errorSignal.emit(`Nose error: ${message.data}`)
customExtensions: import('@codemirror/state').Extension[], } else if (message.type === 'reef-output') {
shrimp: Shrimp const x = outputSignal.emit(message.data)
) => { } else if (message.type === 'connected') {
const extensions = [ outputSignal.emit(`╞ Connected to Nose VM`)
basicSetup,
shrimpTheme,
createShrimpDiagnostics(shrimp),
createShrimpCompletions(shrimp),
shrimpHighlighter,
shrimpKeymap,
persistence,
...customExtensions,
]
if (onChange) {
extensions.push(
EditorView.updateListener.of((update) => {
if (update.docChanged) {
onChange(update.state.doc.toString())
}
})
)
} }
})
new EditorView({ outputSignal.connect((value) => {
parent: el, const el = document.querySelector('#output')!
doc: initialCode, el.innerHTML = ''
extensions, el.innerHTML = asciiEscapeToHtml(valueToString(value))
})
errorSignal.connect((error) => {
const el = document.querySelector('#output')!
el.innerHTML = ''
el.classList.add('error')
el.innerHTML = asciiEscapeToHtml(error)
})
type StatusBarMessage = {
side: 'left' | 'right'
message: string | Promise<HtmlEscapedString>
className: string
order?: number
}
export const statusBarSignal = new Signal<StatusBarMessage>()
statusBarSignal.connect(async ({ side, message, className, order }) => {
document.querySelector(`#status-bar .${className}`)?.remove()
const sideEl = document.querySelector(`#status-bar .${side}`)!
const messageEl = (
<div data-order={order ?? 0} className={className}>
{await message}
</div>
)
// Now go through the nodes and put it in the right spot based on order. Higher number means further right
const nodes = Array.from(sideEl.childNodes)
const index = nodes.findIndex((node) => {
if (!(node instanceof HTMLElement)) return false
return Number(node.dataset.order) > (order ?? 0)
}) })
// Trigger onChange with initial content if (index === -1) {
onChange?.(initialCode) sideEl.appendChild(toElement(messageEl))
} else {
sideEl.insertBefore(toElement(messageEl), nodes[index]!)
}
})
const valueToString = (value: Value | string): string => {
if (typeof value === 'string') {
return value
}
switch (value.type) {
case 'null':
return 'null'
case 'boolean':
return value.value ? 'true' : 'false'
case 'number':
return value.value.toString()
case 'string':
return value.value
case 'array':
return `${value.value.map(valueToString).join('\n')}`
case 'dict': {
const entries = Array.from(value.value.entries()).map(
([key, val]) => `"${key}": ${valueToString(val)}`
)
return `{${entries.join(', ')}}`
}
case 'regex':
return `/${value.value.source}/`
case 'function':
return `<function>`
case 'native':
return `<function ${value.fn.name}>`
default:
assertNever(value)
return `<unknown value type: ${(value as any).type}>`
}
} }

View File

@ -1,6 +0,0 @@
import { Editor } from '#editor/editor'
import { render } from 'hono/jsx/dom'
if (typeof document !== 'undefined') {
render(<Editor initialCode={'# type some code'} />, document.getElementById('root')!)
}

View File

@ -1,11 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="../editor.css" />
</head>
<body>
<div id="root"></div>
<script type="module" src="./frontend.tsx"></script>
</body>
</html>

View File

@ -1,14 +0,0 @@
import index from './index.html'
const server = Bun.serve({
port: 3000,
routes: {
'/': index,
},
development: {
hmr: true,
console: true,
},
})
console.log(`Editor running at ${server.url}`)

View File

@ -1,73 +0,0 @@
import { EditorView, Decoration, DecorationSet, ViewPlugin, ViewUpdate } from '@codemirror/view'
import { RangeSetBuilder } from '@codemirror/state'
import { Shrimp } from '#/index'
import { type SyntaxNode } from '#parser/node'
import { log } from '#utils/utils'
const shrimp = new Shrimp()
export const shrimpHighlighter = ViewPlugin.fromClass(
class {
decorations: DecorationSet
constructor(view: EditorView) {
this.decorations = this.highlight(view)
}
update(update: ViewUpdate) {
if (!update.docChanged) return
this.decorations = this.highlight(update.view)
}
highlight(view: EditorView): DecorationSet {
const builder = new RangeSetBuilder<Decoration>()
const code = view.state.doc.toString()
try {
const tree = shrimp.parse(code)
const decorations: { from: number; to: number; class: string }[] = []
tree.iterate({
enter: (node) => {
const cls = tokenStyles[node.type.name]
const isLeaf = node.children.length === 0
if (cls && isLeaf) {
decorations.push({ from: node.from, to: node.to, class: cls })
}
},
})
// Sort by position (required by RangeSetBuilder)
decorations.sort((a, b) => a.from - b.from)
for (const d of decorations) {
builder.add(d.from, d.to, Decoration.mark({ class: d.class }))
}
} catch (error) {
log('Parsing error in highlighter', error)
}
return builder.finish()
}
},
{
decorations: (v) => v.decorations,
}
)
// Map node types to CSS classes
const tokenStyles: Record<string, string> = {
keyword: 'tok-keyword',
String: 'tok-string',
StringFragment: 'tok-string',
CurlyString: 'tok-string',
Number: 'tok-number',
Boolean: 'tok-bool',
Null: 'tok-null',
Identifier: 'tok-identifier',
AssignableIdentifier: 'tok-variable-def',
Comment: 'tok-comment',
operator: 'tok-operator',
Regex: 'tok-regex',
}

View File

@ -1 +0,0 @@
export { Editor } from './editor'

View File

@ -1,8 +0,0 @@
import { keymap } from '@codemirror/view'
import { acceptCompletion } from '@codemirror/autocomplete'
import { indentWithTab } from '@codemirror/commands'
export const shrimpKeymap = keymap.of([
{ key: 'Tab', run: acceptCompletion },
indentWithTab,
])

59
src/editor/noseClient.ts Normal file
View File

@ -0,0 +1,59 @@
import { Signal } from '#utils/signal'
import type { Bytecode, Value } from 'reefvm'
let ws: WebSocket
type IncomingMessage =
| { type: 'connected' }
| { type: 'ping'; data: number }
| { type: 'commands'; data: number }
| {
type: 'apps'
data: {
name: string
type: 'browser' | 'server'
}[]
}
| {
type: 'session:start'
data: {
NOSE_DIR: string
cwd: string
hostname: string
mode: string
project: string
}
}
| { type: 'reef-output'; data: Value }
| { type: 'error'; data: string }
export const noseSignals = new Signal<IncomingMessage>()
export const connectToNose = (url: string = 'ws://localhost:3000/ws') => {
ws = new WebSocket(url)
ws.onopen = () => noseSignals.emit({ type: 'connected' })
ws.onmessage = (event) => {
const message = JSON.parse(event.data)
noseSignals.emit(message)
}
ws.onerror = (event) => {
console.error(`💥WebSocket error:`, event)
}
ws.onclose = () => {
console.log(`🚪 Connection closed`)
}
}
let id = 0
export const sendToNose = (code: Bytecode) => {
if (!ws) {
throw new Error('WebSocket is not connected.')
} else if (ws.readyState !== WebSocket.OPEN) {
throw new Error(`WebSocket is not open, current status is ${ws.readyState}.`)
}
id += 1
ws.send(JSON.stringify({ type: 'reef-bytecode', data: code, id }))
}

View File

@ -0,0 +1,9 @@
import { errorSignal } from '#editor/editor'
import { EditorView } from '@codemirror/view'
export const catchErrors = EditorView.exceptionSink.of((exception) => {
console.error('CodeMirror error:', exception)
errorSignal.emit(
`Editor error: ${exception instanceof Error ? exception.message : String(exception)}`
)
})

View File

@ -0,0 +1,35 @@
import { EditorView, ViewPlugin, ViewUpdate } from '@codemirror/view'
import { syntaxTree } from '@codemirror/language'
import { statusBarSignal } from '#editor/editor'
export const debugTags = ViewPlugin.fromClass(
class {
update(update: ViewUpdate) {
if (update.docChanged || update.selectionSet || update.geometryChanged) {
this.updateStatusBar(update.view)
}
}
updateStatusBar(view: EditorView) {
const pos = view.state.selection.main.head + 1
const tree = syntaxTree(view.state)
let tags: string[] = []
let node = tree.resolveInner(pos, -1)
while (node) {
tags.push(node.type.name)
node = node.parent!
if (!node) break
}
const debugText = tags.length ? tags.reverse().slice(1).join(' > ') : 'No nodes'
statusBarSignal.emit({
side: 'right',
message: debugText,
className: 'debug-tags',
order: -1,
})
}
}
)

View File

@ -0,0 +1,62 @@
import { parser } from '#parser/shrimp'
import type { Timeout } from '#utils/utils'
import { Range } from '@codemirror/state'
import {
Decoration,
EditorView,
ViewPlugin,
ViewUpdate,
type DecorationSet,
} from '@codemirror/view'
export const shrimpErrors = ViewPlugin.fromClass(
class {
timeout?: Timeout
decorations: DecorationSet = Decoration.none
constructor(view: EditorView) {
this.updateErrors(view)
}
update(update: ViewUpdate) {
if (update.docChanged) {
this.debounceUpdate(update.view)
}
}
updateErrors(view: EditorView) {
this.decorations = Decoration.none
try {
const decorations: Range<Decoration>[] = []
const tree = parser.parse(view.state.doc.toString())
tree.iterate({
enter: (node) => {
if (!node.type.isError) return
// Skip empty error nodes
if (node.from === node.to) return
const decoration = Decoration.mark({
class: 'syntax-error',
attributes: { title: 'COREY REPLACE THIS' },
}).range(node.from, node.to)
decorations.push(decoration)
},
})
this.decorations = Decoration.set(decorations)
// requestAnimationFrame(() => view.dispatch({}))
} catch (e) {
console.error('🙈 Error parsing document', e)
}
}
debounceUpdate = (view: EditorView) => {
clearTimeout(this.timeout)
this.timeout = setTimeout(() => this.updateErrors(view), 250)
}
},
{
decorations: (v) => v.decorations,
}
)

View File

@ -0,0 +1,232 @@
import {
ViewPlugin,
ViewUpdate,
EditorView,
Decoration,
type DecorationSet,
} from '@codemirror/view'
import { syntaxTree } from '@codemirror/language'
import { type SyntaxNode } from '@lezer/common'
import { WidgetType } from '@codemirror/view'
import { toElement } from '#utils/utils'
import { matchingCommands } from '#editor/commands'
import * as Terms from '#parser/shrimp.terms'
const ghostTextTheme = EditorView.theme({
'.ghost-text': {
color: '#666',
opacity: '0.6',
fontStyle: 'italic',
},
})
type Hint = { cursor: number; hintText?: string; completionText?: string }
export const inlineHints = [
ViewPlugin.fromClass(
class {
decorations: DecorationSet = Decoration.none
currentHint?: Hint
update(update: ViewUpdate) {
if (!update.docChanged && !update.selectionSet) return
this.clearHints()
let hint = this.getContext(update.view)
this.currentHint = hint
this.showHint(hint)
}
handleTab(view: EditorView) {
if (!this.currentHint?.completionText) return false
this.decorations = Decoration.none
view.dispatch({
changes: {
from: this.currentHint.cursor,
insert: this.currentHint.completionText,
},
selection: {
anchor: this.currentHint.cursor + this.currentHint.completionText.length,
},
})
this.currentHint = undefined
return true
}
clearHints() {
this.currentHint = undefined
this.decorations = Decoration.none
}
getContext(view: EditorView): Hint {
const cursor = view.state.selection.main.head
const isCursorAtEnd = cursor === view.state.doc.length
if (!isCursorAtEnd) return { cursor }
const token = this.getCommandContextToken(view, cursor)
if (!token) return { cursor }
const text = view.state.doc.sliceString(token.from, token.to)
const tokenId = token.type.id
let completionText = ''
let hintText = ''
const justSpaces = view.state.doc.sliceString(cursor - 1, cursor) === ' '
if (tokenId === Terms.CommandPartial) {
const { partialMatches } = matchingCommands(text)
const match = partialMatches[0]
if (match) {
completionText = match.command.slice(text.length) + ' '
hintText = completionText
}
} else if (
tokenId === Terms.Identifier &&
token.parent?.type.id === Terms.Arg &&
!justSpaces
) {
const { availableArgs } = this.getCommandContext(view, token)
const matchingArgs = availableArgs.filter((arg) => arg.name.startsWith(text))
const match = matchingArgs[0]
if (match) {
hintText = `${match.name.slice(text.length)}=<${match.type}>`
completionText = `${match.name.slice(text.length)}=`
}
} else if (this.containedBy(token, Terms.PartialNamedArg)) {
const { availableArgs } = this.getCommandContext(view, token)
const textWithoutEquals = text.slice(0, -1)
const matchingArgs = availableArgs.filter((arg) => arg.name == textWithoutEquals)
const match = matchingArgs[0]
if (match) {
hintText = `<${match.type}>`
completionText = 'default' in match ? `${match.default}` : ''
}
} else {
const { availableArgs } = this.getCommandContext(view, token)
const nextArg = Array.from(availableArgs)[0]
const space = justSpaces ? '' : ' '
if (nextArg) {
hintText = `${space}${nextArg.name}=<${nextArg.type}>`
if (nextArg) {
completionText = `${space}${nextArg.name}=`
}
}
}
return { completionText, hintText, cursor }
}
getCommandContextToken(view: EditorView, cursor: number) {
const tree = syntaxTree(view.state)
let node = tree.resolveInner(cursor, -1)
// If we're in a CommandCall, return the token before cursor
if (this.containedBy(node, Terms.CommandCall)) {
return tree.resolveInner(cursor, -1)
}
// If we're in Program, look backward
while (node.name === 'Program' && cursor > 0) {
cursor -= 1
node = tree.resolveInner(cursor, -1)
if (this.containedBy(node, Terms.CommandCall)) {
return tree.resolveInner(cursor, -1)
}
}
}
containedBy(node: SyntaxNode, nodeId: number): SyntaxNode | undefined {
let current: SyntaxNode | undefined = node
while (current) {
if (current.type.id === nodeId) {
return current
}
current = current.parent ?? undefined
}
}
showHint(hint: Hint) {
if (!hint.hintText) return
const widget = new GhostTextWidget(hint.hintText)
const afterCursor = 1
const decoration = Decoration.widget({ widget, side: afterCursor }).range(hint.cursor)
this.decorations = Decoration.set([decoration])
}
getCommandContext(view: EditorView, currentToken: SyntaxNode) {
let commandCallNode = currentToken.parent
while (commandCallNode?.type.name !== 'CommandCall') {
if (!commandCallNode) {
throw new Error('No CommandCall parent found, must be an error in the grammar')
}
commandCallNode = commandCallNode.parent
}
const commandToken = commandCallNode.firstChild
if (!commandToken) {
throw new Error('CommandCall has no children, must be an error in the grammar')
}
const commandText = view.state.doc.sliceString(commandToken.from, commandToken.to)
const { match: commandShape } = matchingCommands(commandText)
if (!commandShape) {
throw new Error(`No command shape found for command "${commandText}"`)
}
let availableArgs = [...commandShape.args]
// Walk through all NamedArg children
let child = commandToken.nextSibling
while (child) {
console.log('child', child.type.name, child.to - child.from)
if (child.type.id === Terms.NamedArg) {
const argName = child.firstChild // Should be the Identifier
if (argName) {
const argText = view.state.doc.sliceString(argName.from, argName.to - 1)
availableArgs = availableArgs.filter((arg) => arg.name !== argText)
}
} else if (child.type.id == Terms.Arg) {
const hasSpaceAfter = view.state.doc.sliceString(child.to, child.to + 1) === ' '
if (hasSpaceAfter) {
availableArgs.shift()
}
}
child = child.nextSibling
}
return { commandShape, availableArgs }
}
},
{
decorations: (v) => v.decorations,
eventHandlers: {
keydown(event, view) {
if (event.key === 'Tab') {
event.preventDefault()
const plugin = view.plugin(inlineHints[0]! as ViewPlugin<any>)
plugin?.handleTab(view)
}
},
},
}
),
ghostTextTheme,
]
class GhostTextWidget extends WidgetType {
constructor(private text: string) {
super()
}
toDOM() {
const el = <span className="ghost-text">{this.text}</span>
return toElement(el)
}
}

View File

@ -0,0 +1,184 @@
import { multilineModeSignal, outputSignal } from '#editor/editor'
import { printBytecodeOutput, printParserOutput, runCode } from '#editor/runCode'
import { EditorState } from '@codemirror/state'
import { keymap } from '@codemirror/view'
let multilineMode = false
const customKeymap = keymap.of([
{
key: 'Enter',
run: (view) => {
if (multilineMode) return false
const input = view.state.doc.toString()
history.push(input)
runCode(input)
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: '' },
selection: { anchor: 0 },
})
return true
},
},
{
key: 'Shift-Enter',
run: (view) => {
if (multilineMode) {
const input = view.state.doc.toString()
runCode(input)
return true
} else {
outputSignal.emit('Press Shift+Enter to insert run the code.')
}
multilineModeSignal.emit(true)
multilineMode = true
view.dispatch({
changes: { from: view.state.doc.length, insert: '\n' },
selection: { anchor: view.state.doc.length + 1 },
})
return true
},
},
{
key: 'Tab',
preventDefault: true,
run: (view) => {
view.dispatch({
changes: { from: view.state.selection.main.from, insert: ' ' },
selection: { anchor: view.state.selection.main.from + 2 },
})
return true
},
},
{
key: 'ArrowUp',
run: (view) => {
if (multilineMode) return false
const command = history.previous()
if (command === undefined) return false
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: command },
selection: { anchor: command.length },
})
return true
},
},
{
key: 'ArrowDown',
run: (view) => {
if (multilineMode) return false
const command = history.next()
if (command === undefined) return false
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: command },
selection: { anchor: command.length },
})
return true
},
},
{
key: 'Mod-k 1',
preventDefault: true,
run: (view) => {
const input = view.state.doc.toString()
printParserOutput(input)
return true
},
},
{
key: 'Mod-k 2',
preventDefault: true,
run: (view) => {
const input = view.state.doc.toString()
printBytecodeOutput(input)
return true
},
},
])
let firstTime = true
const singleLineFilter = EditorState.transactionFilter.of((transaction) => {
if (multilineMode) return transaction // Allow everything in multiline mode
if (firstTime) {
firstTime = false
if (transaction.newDoc.toString().includes('\n')) {
multilineMode = true
return transaction
}
}
transaction.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
if (inserted.toString().includes('\n')) {
multilineMode = true
return
}
})
return transaction
})
export const shrimpKeymap = [customKeymap, singleLineFilter]
class History {
private commands: string[] = []
private index: number | undefined
private storageKey = 'shrimp-command-history'
constructor() {
try {
this.commands = JSON.parse(localStorage.getItem(this.storageKey) || '[]')
} catch {
console.warn('Failed to load command history from localStorage')
}
}
push(command: string) {
this.commands.push(command)
// Limit to last 50 commands
this.commands = this.commands.slice(-50)
localStorage.setItem(this.storageKey, JSON.stringify(this.commands))
this.index = undefined
}
previous(): string | undefined {
if (this.commands.length === 0) return
if (this.index === undefined) {
this.index = this.commands.length - 1
} else if (this.index > 0) {
this.index -= 1
}
return this.commands[this.index]
}
next(): string | undefined {
if (this.commands.length === 0 || this.index === undefined) return
if (this.index < this.commands.length - 1) {
this.index += 1
return this.commands[this.index]
} else {
this.index = undefined
return ''
}
}
}
const history = new History()

View File

@ -1,6 +1,6 @@
import { ViewPlugin, ViewUpdate } from '@codemirror/view' import { ViewPlugin, ViewUpdate } from '@codemirror/view'
export const persistence = ViewPlugin.fromClass( export const persistencePlugin = ViewPlugin.fromClass(
class { class {
saveTimeout?: ReturnType<typeof setTimeout> saveTimeout?: ReturnType<typeof setTimeout>
@ -24,6 +24,6 @@ export const getContent = () => {
return localStorage.getItem('shrimp-editor-content') || '' return localStorage.getItem('shrimp-editor-content') || ''
} }
export const setContent = (data: string) => { const setContent = (data: string) => {
localStorage.setItem('shrimp-editor-content', data) localStorage.setItem('shrimp-editor-content', data)
} }

View File

@ -0,0 +1,9 @@
import { parser } from '#/parser/shrimp'
import { LRLanguage, LanguageSupport } from '@codemirror/language'
import { highlighting } from '#/parser/highlight.js'
const language = LRLanguage.define({
parser: parser.configure({ props: [highlighting] }),
})
export const shrimpLanguage = new LanguageSupport(language)

View File

@ -0,0 +1,35 @@
import { history, defaultKeymap, historyKeymap } from '@codemirror/commands'
import { bracketMatching, indentOnInput } from '@codemirror/language'
import { highlightSpecialChars, drawSelection, dropCursor, keymap } from '@codemirror/view'
import { closeBrackets, autocompletion, completionKeymap } from '@codemirror/autocomplete'
import { EditorState, Compartment } from '@codemirror/state'
import { searchKeymap } from '@codemirror/search'
import { shrimpKeymap } from './keymap'
import { shrimpTheme, shrimpHighlighting } from './theme'
import { shrimpLanguage } from './shrimpLanguage'
import { shrimpErrors } from './errors'
import { persistencePlugin } from './persistence'
import { catchErrors } from './catchErrors'
export const shrimpSetup = (lineNumbersCompartment: Compartment) => {
return [
catchErrors,
shrimpKeymap,
highlightSpecialChars(),
history(),
drawSelection(),
dropCursor(),
EditorState.allowMultipleSelections.of(true),
bracketMatching(),
closeBrackets(),
autocompletion(),
indentOnInput(),
keymap.of([...defaultKeymap, ...historyKeymap, ...searchKeymap, ...completionKeymap]),
lineNumbersCompartment.of([]),
shrimpTheme,
shrimpLanguage,
shrimpHighlighting,
shrimpErrors,
persistencePlugin,
]
}

View File

@ -0,0 +1,60 @@
import { EditorView } from '@codemirror/view'
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language'
import { tags } from '@lezer/highlight'
const highlightStyle = HighlightStyle.define([
{ tag: tags.keyword, color: 'var(--color-keyword)' },
{ tag: tags.name, color: 'var(--color-function)' },
{ tag: tags.string, color: 'var(--color-string)' },
{ tag: tags.number, color: 'var(--color-number)' },
{ tag: tags.bool, color: 'var(--color-bool)' },
{ tag: tags.operator, color: 'var(--color-operator)' },
{ tag: tags.paren, color: 'var(--color-paren)' },
{ tag: tags.regexp, color: 'var(--color-regex)' },
{ tag: tags.function(tags.variableName), color: 'var(--color-function-call)' },
{ tag: tags.function(tags.invalid), color: 'white' },
{
tag: tags.definition(tags.variableName),
color: 'var(--color-variable-def)',
backgroundColor: 'var(--bg-variable-def)',
padding: '1px 2px',
borderRadius: '2px',
fontWeight: '500',
},
])
export const shrimpHighlighting = syntaxHighlighting(highlightStyle)
export const shrimpTheme = EditorView.theme(
{
'&': {
color: 'var(--text-editor)',
backgroundColor: 'var(--bg-editor)',
height: '100%',
fontSize: '18px',
},
'.cm-content': {
fontFamily: '"Pixeloid Mono", "Courier New", monospace',
caretColor: 'var(--caret)',
padding: '0px',
},
'&.cm-focused .cm-cursor': {
borderLeftColor: 'var(--caret)',
},
'&.cm-focused .cm-selectionBackground, ::selection': {
backgroundColor: 'var(--bg-selection)',
},
'.cm-editor': {
border: 'none',
outline: 'none',
height: '100%',
},
'.cm-matchingBracket': {
backgroundColor: 'var(--color-bool)',
},
'.cm-nonmatchingBracket': {
backgroundColor: 'var(--color-string)',
},
},
{ dark: true }
)

38
src/editor/runCode.tsx Normal file
View File

@ -0,0 +1,38 @@
import { outputSignal, errorSignal } from '#editor/editor'
import { Compiler } from '#compiler/compiler'
import { errorMessage, log } from '#utils/utils'
import { bytecodeToString } from 'reefvm'
import { parser } from '#parser/shrimp'
import { sendToNose } from '#editor/noseClient'
import { treeToString } from '#utils/tree'
export const runCode = async (input: string) => {
try {
const compiler = new Compiler(input)
sendToNose(compiler.bytecode)
} catch (error) {
log.error(error)
errorSignal.emit(`${errorMessage(error)}`)
}
}
export const printParserOutput = (input: string) => {
try {
const cst = parser.parse(input)
const string = treeToString(cst, input)
outputSignal.emit(string)
} catch (error) {
log.error(error)
errorSignal.emit(`${errorMessage(error)}`)
}
}
export const printBytecodeOutput = (input: string) => {
try {
const compiler = new Compiler(input)
outputSignal.emit(bytecodeToString(compiler.bytecode))
} catch (error) {
log.error(error)
errorSignal.emit(`${errorMessage(error)}`)
}
}

View File

@ -1,51 +0,0 @@
import { EditorView } from '@codemirror/view'
export const shrimpTheme = EditorView.theme(
{
'&': {
color: 'var(--text-editor)',
backgroundColor: 'var(--bg-editor)',
height: '100%',
fontSize: '18px',
},
'.cm-content': {
fontFamily: '"Pixeloid Mono", "Courier New", monospace',
caretColor: 'var(--caret)',
padding: '0px',
borderTop: '1px solid var(--bg-editor)',
},
'&.cm-focused .cm-cursor': {
borderLeftColor: 'var(--caret)',
},
'.cm-selectionLayer .cm-selectionBackground': {
backgroundColor: 'var(--bg-selection)',
},
'.cm-editor': {
border: 'none',
outline: 'none',
height: '100%',
},
'.cm-matchingBracket': {
backgroundColor: 'var(--color-bool)',
},
'.cm-nonmatchingBracket': {
backgroundColor: 'var(--color-string)',
},
'.cm-activeLine': {
backgroundColor: 'rgba(255, 255, 255, 0.03)',
},
// Token highlighting
'.tok-keyword': { color: 'var(--color-keyword)' },
'.tok-string': { color: 'var(--color-string)' },
'.tok-number': { color: 'var(--color-number)' },
'.tok-bool': { color: 'var(--color-bool)' },
'.tok-null': { color: 'var(--color-number)', fontStyle: 'italic' },
'.tok-identifier': { color: 'var(--color-function)' },
'.tok-variable-def': { color: 'var(--color-variable-def)' },
'.tok-comment': { color: 'var(--ansi-bright-black)', fontStyle: 'italic' },
'.tok-operator': { color: 'var(--color-operator)' },
'.tok-regex': { color: 'var(--color-regex)' },
},
{ dark: true }
)

View File

@ -1,114 +1,73 @@
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import { VM, fromValue, toValue, isValue, type Bytecode } from 'reefvm' import { VM, fromValue, type Bytecode } from 'reefvm'
import { Compiler } from '#compiler/compiler' import { Compiler } from '#compiler/compiler'
import { parse } from '#parser/parser2' import { globals as shrimpGlobals, colors } from '#prelude'
import { Tree } from '#parser/node'
import { globals as parserGlobals, setGlobals as setParserGlobals } from '#parser/parser2'
import { globals as prelude } from '#prelude'
export { Compiler } from '#compiler/compiler' export { Compiler } from '#compiler/compiler'
export { parse } from '#parser/parser2' export { parser } from '#parser/shrimp'
export { type SyntaxNode, Tree } from '#parser/node' export { globals } from '#prelude'
export { globals as prelude } from '#prelude'
export { type Value, type Bytecode } from 'reefvm'
export { toValue, fromValue, isValue, Scope, VM, bytecodeToString } from 'reefvm'
export class Shrimp { export class Shrimp {
vm: VM vm: VM
private globals?: Record<string, any> private globals?: Record<string, any>
constructor(globals?: Record<string, any>) { constructor(globals?: Record<string, any>) {
const emptyBytecode = { instructions: [], constants: [], labels: new Map() } const emptyBytecode = { instructions: [], constants: [], labels: new Map() }
this.vm = new VM(emptyBytecode, Object.assign({}, prelude, globals ?? {})) this.vm = new VM(emptyBytecode, Object.assign({}, shrimpGlobals, globals ?? {}))
this.globals = globals this.globals = globals
}
get(name: string): any {
const value = this.vm.scope.get(name)
return value ? fromValue(value, this.vm) : null
}
set(name: string, value: any) {
this.vm.scope.set(name, toValue(value, this.vm))
}
has(name: string): boolean {
return this.vm.scope.has(name)
}
async call(name: string, ...args: any[]): Promise<any> {
const result = await this.vm.call(name, ...args)
return isValue(result) ? fromValue(result, this.vm) : result
}
parse(code: string): Tree {
return parseCode(code, this.globals)
}
compile(code: string): Bytecode {
return compileCode(code, this.globals)
}
async run(code: string | Bytecode, locals?: Record<string, any>): Promise<any> {
let bytecode
if (typeof code === 'string') {
const compiler = new Compiler(
code,
Object.keys(Object.assign({}, prelude, this.globals ?? {}, locals ?? {})),
)
bytecode = compiler.bytecode
} else {
bytecode = code
} }
if (locals) this.vm.pushScope(locals) async run(code: string | Bytecode, locals?: Record<string, any>): Promise<any> {
this.vm.appendBytecode(bytecode) let bytecode
await this.vm.continue()
if (locals) this.vm.popScope()
return this.vm.stack.length ? fromValue(this.vm.stack.at(-1)!, this.vm) : null if (typeof code === 'string') {
} const compiler = new Compiler(code, Object.keys(Object.assign({}, shrimpGlobals, this.globals ?? {}, locals ?? {})))
bytecode = compiler.bytecode
} else {
bytecode = code
}
if (locals) this.vm.pushScope(locals)
this.vm.appendBytecode(bytecode)
await this.vm.continue()
if (locals) this.vm.popScope()
return this.vm.stack.length ? fromValue(this.vm.stack.at(-1)!) : null
}
get(name: string): any {
const value = this.vm.scope.get(name)
return value ? fromValue(value) : null
}
} }
export async function runFile(path: string, globals?: Record<string, any>): Promise<any> { export async function runFile(path: string, globals?: Record<string, any>): Promise<any> {
const code = readFileSync(path, 'utf-8') const code = readFileSync(path, 'utf-8')
return await runCode(code, globals) return await runCode(code, globals)
} }
export async function runCode(code: string, globals?: Record<string, any>): Promise<any> { export async function runCode(code: string, globals?: Record<string, any>): Promise<any> {
return await runBytecode(compileCode(code, globals), globals) return await runBytecode(compileCode(code, globals), globals)
} }
export async function runBytecode(bytecode: Bytecode, globals?: Record<string, any>): Promise<any> { export async function runBytecode(bytecode: Bytecode, globals?: Record<string, any>): Promise<any> {
const vm = new VM(bytecode, Object.assign({}, prelude, globals)) try {
await vm.run() const vm = new VM(bytecode, Object.assign({}, shrimpGlobals, globals))
return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]!, vm) : null await vm.run()
return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]!) : null
} catch (error: any) {
console.error(`${colors.red}Error:${colors.reset} ${error.message}`)
process.exit(1)
}
} }
export function compileFile(path: string, globals?: Record<string, any>): Bytecode { export function compileFile(path: string, globals?: Record<string, any>): Bytecode {
const code = readFileSync(path, 'utf-8') const code = readFileSync(path, 'utf-8')
return compileCode(code, globals) return compileCode(code, globals)
} }
export function compileCode(code: string, globals?: Record<string, any>): Bytecode { export function compileCode(code: string, globals?: Record<string, any>): Bytecode {
const globalNames = [...Object.keys(prelude), ...(globals ? Object.keys(globals) : [])] const globalNames = [...Object.keys(shrimpGlobals), ...(globals ? Object.keys(globals) : [])]
const compiler = new Compiler(code, globalNames) const compiler = new Compiler(code, globalNames)
return compiler.bytecode return compiler.bytecode
} }
export function parseFile(path: string, globals?: Record<string, any>): Tree {
const code = readFileSync(path, 'utf-8')
return parseCode(code, globals)
}
export function parseCode(code: string, globals?: Record<string, any>): Tree {
const oldGlobals = [...parserGlobals]
const globalNames = [...Object.keys(prelude), ...(globals ? Object.keys(globals) : [])]
setParserGlobals(globalNames)
const result = parse(code)
setParserGlobals(oldGlobals)
return new Tree(result)
}

View File

@ -1,61 +0,0 @@
import { parse } from '#parser/parser2'
import type { SyntaxNode } from '#parser/node'
import { isIdentStart, isIdentChar } from './tokenizer2'
// Turns a { curly string } into strings and nodes for interpolation
export const tokenizeCurlyString = (value: string): (string | [string, SyntaxNode])[] => {
let pos = 1
let start = 1
let char = value[pos]
const tokens: (string | [string, SyntaxNode])[] = []
while (pos < value.length) {
if (char === '$') {
// escaped \$
if (value[pos - 1] === '\\' && value[pos - 2] !== '\\') {
tokens.push(value.slice(start, pos - 1))
start = pos
char = value[++pos]
continue
}
tokens.push(value.slice(start, pos))
start = pos
if (value[pos + 1] === '(') {
pos++ // slip opening '('
char = value[++pos]
if (!char) break
let depth = 0
while (char) {
if (char === '(') depth++
if (char === ')') depth--
if (depth < 0) break
char = value[++pos]
}
const input = value.slice(start + 2, pos) // skip '$('
tokens.push([input, parse(input)])
start = pos + 1 // start after ')'
} else {
char = value[++pos]
if (!char) break
if (!isIdentStart(char.charCodeAt(0))) break
while (char && isIdentChar(char.charCodeAt(0))) char = value[++pos]
const input = value.slice(start + 1, pos) // skip '$'
tokens.push([input, parse(input)])
start = pos-- // backtrack and start over
}
}
char = value[++pos]
}
tokens.push(value.slice(start, pos - 1))
return tokens
}

View File

@ -1,264 +0,0 @@
import { type Token, TokenType } from './tokenizer2'
export type NodeType =
| 'Program'
| 'Block'
| 'FunctionCall'
| 'FunctionCallOrIdentifier'
| 'FunctionCallWithBlock'
| 'PositionalArg'
| 'NamedArg'
| 'NamedArgPrefix'
| 'FunctionDef'
| 'Params'
| 'NamedParam'
| 'Null'
| 'Boolean'
| 'Number'
| 'String'
| 'StringFragment'
| 'CurlyString'
| 'DoubleQuote'
| 'EscapeSeq'
| 'Interpolation'
| 'Regex'
| 'Identifier'
| 'AssignableIdentifier'
| 'IdentifierBeforeDot'
| 'Word'
| 'Array'
| 'Dict'
| 'Comment'
| 'BinOp'
| 'ConditionalOp'
| 'ParenExpr'
| 'Assign'
| 'CompoundAssign'
| 'DotGet'
| 'PipeExpr'
| 'IfExpr'
| 'ElseIfExpr'
| 'ElseExpr'
| 'WhileExpr'
| 'TryExpr'
| 'CatchExpr'
| 'FinallyExpr'
| 'Throw'
| 'Not'
| 'Eq'
| 'Modulo'
| 'Plus'
| 'Star'
| 'Slash'
| 'Import'
| 'Do'
| 'Underscore'
| 'colon'
| 'keyword'
| 'operator'
// TODO: remove this when we switch from lezer
export const operators: Record<string, any> = {
// Logic
and: 'And',
or: 'Or',
// Bitwise
band: 'Band',
bor: 'Bor',
bxor: 'Bxor',
'>>>': 'Ushr',
'>>': 'Shr',
'<<': 'Shl',
// Comparison
'>=': 'Gte',
'<=': 'Lte',
'>': 'Gt',
'<': 'Lt',
'!=': 'Neq',
'==': 'EqEq',
// Compound assignment operators
'??=': 'NullishEq',
'+=': 'PlusEq',
'-=': 'MinusEq',
'*=': 'StarEq',
'/=': 'SlashEq',
'%=': 'ModuloEq',
// Nullish coalescing
'??': 'NullishCoalesce',
// Math
'*': 'Star',
'**': 'StarStar',
'=': 'Eq',
'/': 'Slash',
'+': 'Plus',
'-': 'Minus',
'%': 'Modulo',
// Dotget
'.': 'Dot',
// Pipe
'|': 'operator',
}
export class Tree {
constructor(public topNode: SyntaxNode) {}
get length(): number {
return this.topNode.to
}
cursor() {
return {
type: this.topNode.type,
from: this.topNode.from,
to: this.topNode.to,
node: this.topNode,
}
}
iterate(options: { enter: (node: SyntaxNode) => void }) {
const iter = (node: SyntaxNode) => {
for (const n of node.children) iter(n)
options.enter(node)
}
iter(this.topNode)
}
}
export class SyntaxNode {
#type: NodeType
#isError = false
from: number
to: number
parent: SyntaxNode | null
children: SyntaxNode[] = []
constructor(type: NodeType, from: number, to: number, parent: SyntaxNode | null = null) {
this.#type = type
this.from = from
this.to = to
this.parent = parent
}
static from(token: Token, parent?: SyntaxNode): SyntaxNode {
return new SyntaxNode(TokenType[token.type] as NodeType, token.from, token.to, parent ?? null)
}
get type(): {
type: NodeType
name: NodeType
isError: boolean
is: (other: NodeType) => boolean
} {
return {
type: this.#type,
name: this.#type,
isError: this.#isError,
is: (other: NodeType) => other === this.#type,
}
}
set type(name: NodeType) {
this.#type = name
}
get name(): string {
return this.type.name
}
get isError(): boolean {
return this.#isError
}
set isError(err: boolean) {
this.#isError = err
}
get firstChild(): SyntaxNode | null {
return this.children[0] ?? null
}
get lastChild(): SyntaxNode | null {
return this.children.at(-1) ?? null
}
get nextSibling(): SyntaxNode | null {
if (!this.parent) return null
const siblings = this.parent.children
const index = siblings.indexOf(this)
return index >= 0 && index < siblings.length - 1 ? siblings[index + 1]! : null
}
get prevSibling(): SyntaxNode | null {
if (!this.parent) return null
const siblings = this.parent.children
const index = siblings.indexOf(this)
return index > 0 ? siblings[index - 1]! : null
}
add(node: SyntaxNode) {
node.parent = this
this.children.push(node)
}
push(...nodes: SyntaxNode[]): SyntaxNode {
nodes.forEach((child) => (child.parent = this))
this.children.push(...nodes)
return this
}
toString(): string {
return this.type.name
}
}
// Operator precedence (binding power) - higher = tighter binding
export const precedence: Record<string, number> = {
// Logical
or: 10,
and: 20,
// Comparison
'==': 30,
'!=': 30,
'<': 30,
'>': 30,
'<=': 30,
'>=': 30,
// Nullish coalescing
'??': 35,
// Bitwise shifts (lower precedence than addition)
'<<': 37,
'>>': 37,
'>>>': 37,
// Addition/Subtraction
'+': 40,
'-': 40,
// Bitwise AND/OR/XOR (higher precedence than addition)
band: 45,
bor: 45,
bxor: 45,
// Multiplication/Division/Modulo
'*': 50,
'/': 50,
'%': 50,
// Exponentiation (right-associative)
'**': 60,
}
export const conditionals = new Set(['==', '!=', '<', '>', '<=', '>=', '??', 'and', 'or'])
export const compounds = ['??=', '+=', '-=', '*=', '/=', '%=']

View File

@ -0,0 +1,89 @@
import { ExternalTokenizer, InputStream } from '@lezer/lr'
import * as terms from './shrimp.terms'
type Operator = { str: string; tokenName: keyof typeof terms }
const operators: Array<Operator> = [
{ str: 'and', tokenName: 'And' },
{ str: 'or', tokenName: 'Or' },
{ str: '>=', tokenName: 'Gte' },
{ str: '<=', tokenName: 'Lte' },
{ str: '!=', tokenName: 'Neq' },
{ str: '==', tokenName: 'EqEq' },
// Compound assignment operators (must come before single-char operators)
{ str: '+=', tokenName: 'PlusEq' },
{ str: '-=', tokenName: 'MinusEq' },
{ str: '*=', tokenName: 'StarEq' },
{ str: '/=', tokenName: 'SlashEq' },
{ str: '%=', tokenName: 'ModuloEq' },
// Single-char operators
{ str: '*', tokenName: 'Star' },
{ str: '=', tokenName: 'Eq' },
{ str: '/', tokenName: 'Slash' },
{ str: '+', tokenName: 'Plus' },
{ str: '-', tokenName: 'Minus' },
{ str: '>', tokenName: 'Gt' },
{ str: '<', tokenName: 'Lt' },
{ str: '%', tokenName: 'Modulo' },
]
export const operatorTokenizer = new ExternalTokenizer((input: InputStream) => {
for (let operator of operators) {
if (!matchesString(input, 0, operator.str)) continue
const afterOpPos = operator.str.length
const charAfterOp = input.peek(afterOpPos)
if (!isWhitespace(charAfterOp)) continue
// Accept the operator token
const token = terms[operator.tokenName]
if (token === undefined) {
throw new Error(`Unknown token name: ${operator.tokenName}`)
}
input.advance(afterOpPos)
input.acceptToken(token)
return
}
})
const isWhitespace = (ch: number): boolean => {
return matchesChar(ch, [' ', '\t', '\n'])
}
const matchesChar = (ch: number, chars: (string | number)[]): boolean => {
for (const c of chars) {
if (typeof c === 'number') {
if (ch === c) {
return true
}
} else if (ch === c.charCodeAt(0)) {
return true
}
}
return false
}
const matchesString = (input: InputStream, pos: number, str: string): boolean => {
for (let i = 0; i < str.length; i++) {
if (input.peek(pos + i) !== str.charCodeAt(i)) {
return false
}
}
return true
}
const peek = (numChars: number, input: InputStream): string => {
let result = ''
for (let i = 0; i < numChars; i++) {
const ch = input.peek(i)
if (ch === -1) {
result += 'EOF'
break
} else {
result += String.fromCharCode(ch)
}
}
return result
}

View File

@ -1,968 +0,0 @@
import { CompilerError } from '#compiler/compilerError'
import { Scanner, type Token, TokenType } from './tokenizer2'
import { SyntaxNode, operators, precedence, conditionals, compounds } from './node'
import { parseString } from './stringParser'
const $T = TokenType
// tell the dotGet searcher about builtin globals
export const globals: string[] = []
export const setGlobals = (newGlobals: string[] | Record<string, any>) => {
globals.length = 0
globals.push(...(Array.isArray(newGlobals) ? newGlobals : Object.keys(newGlobals)))
}
export const parse = (input: string): SyntaxNode => {
const parser = new Parser()
return parser.parse(input)
}
class Scope {
parent?: Scope
set = new Set<string>()
constructor(parent?: Scope) {
this.parent = parent
// no parent means this is global scope
if (!parent) for (const name of globals) this.add(name)
}
add(key: string) {
this.set.add(key)
}
has(key: string): boolean {
return this.set.has(key) || this.parent?.has(key) || false
}
}
export class Parser {
tokens: Token[] = []
pos = 0
inParens = 0
input = ''
scope = new Scope()
inTestExpr = false
parse(input: string): SyntaxNode {
const scanner = new Scanner()
this.tokens = scanner.tokenize(input)
this.pos = 0
this.input = input
this.scope = new Scope()
this.inTestExpr = false
const node = new SyntaxNode('Program', 0, input.length)
while (!this.isEOF()) {
if (this.is($T.Newline) || this.is($T.Semicolon)) {
this.next()
continue
}
const prevPos = this.pos
const stmt = this.statement()
if (stmt) node.add(stmt)
if (this.pos === prevPos && !this.isEOF())
throw `parser didn't advance - you need to call next()\n\n ${this.input}\n`
}
return node
}
//
// parse foundation nodes - statements, expressions
//
// statement is a line of code
statement(): SyntaxNode | null {
if (this.is($T.Comment)) return this.comment()
while (this.is($T.Newline) || this.is($T.Semicolon)) this.next()
if (this.isEOF() || this.isExprEndKeyword()) return null
return this.expression()
}
// expressions can be found in four places:
// 1. line of code
// 2. right side of assignment
// 3. if/while conditions
// 4. inside (parens)
expression(allowPipe = true): SyntaxNode {
let expr
// x = value
if (
this.is($T.Identifier) &&
(this.nextIs($T.Operator, '=') || compounds.some((x) => this.nextIs($T.Operator, x)))
)
expr = this.assign()
// if, while, do, etc
else if (this.is($T.Keyword)) expr = this.keywords()
// dotget
else if (this.nextIs($T.Operator, '.')) expr = this.dotGetFunctionCall()
// echo hello world
else if (this.is($T.Identifier) && !this.nextIs($T.Operator) && !this.nextIsExprEnd())
expr = this.functionCall()
// bare-function-call
else if (this.is($T.Identifier) && this.nextIsExprEnd()) expr = this.functionCallOrIdentifier()
// everything else
else expr = this.exprWithPrecedence()
// check for destructuring
if (expr.type.is('Array') && this.is($T.Operator, '=')) return this.destructure(expr)
// check for parens function call
// ex: (ref my-func) my-arg
if (expr.type.is('ParenExpr') && !this.isExprEnd()) expr = this.functionCall(expr)
// if dotget is followed by binary operator, continue parsing as binary expression
if (expr.type.is('DotGet') && this.is($T.Operator) && !this.is($T.Operator, '|'))
expr = this.dotGetBinOp(expr)
// one | echo
if (allowPipe && this.isPipe()) return this.pipe(expr)
// regular
else return expr
}
// piping | stuff | is | cool
pipe(left: SyntaxNode): SyntaxNode {
const canLookPastNewlines = this.inParens === 0
const parts: SyntaxNode[] = [left]
while (this.isPipe()) {
// consume newlines before pipe (only if not in parens)
if (canLookPastNewlines) {
while (this.is($T.Newline)) this.next()
}
const pipeOp = this.op('|')
pipeOp.type = 'operator'
parts.push(pipeOp)
// consume newlines after pipe (only if not in parens)
if (canLookPastNewlines) {
while (this.is($T.Newline)) this.next()
}
// parse right side - don't allow nested pipes
parts.push(this.expression(false))
}
const node = new SyntaxNode('PipeExpr', parts[0]!.from, parts.at(-1)!.to)
return node.push(...parts)
}
// Pratt parser - parses expressions with precedence climbing
// bp = binding precedence
exprWithPrecedence(minBp = 0): SyntaxNode {
let left = this.value()
// infix operators with precedence
while (this.is($T.Operator)) {
const op = this.current().value!
const bp = precedence[op]
// operator has lower precedence than required, stop
if (bp === undefined || bp < minBp) break
const opNode = this.op()
// right-associative operators (like **) use same bp, others use bp + 1
const nextMinBp = op === '**' ? bp : bp + 1
// parse right-hand side with higher precedence
const right = this.exprWithPrecedence(nextMinBp)
const nodeType = conditionals.has(op) ? 'ConditionalOp' : 'BinOp'
const node = new SyntaxNode(nodeType, left.from, right.to)
node.push(left, opNode, right)
left = node
}
return left
}
// if, while, do, etc
keywords(): SyntaxNode {
if (this.is($T.Keyword, 'if')) return this.if()
if (this.is($T.Keyword, 'while')) return this.while()
if (this.is($T.Keyword, 'do')) return this.do()
if (this.is($T.Keyword, 'try')) return this.try()
if (this.is($T.Keyword, 'throw')) return this.throw()
if (this.is($T.Keyword, 'not')) return this.not()
if (this.is($T.Keyword, 'import')) return this.import()
return this.expect($T.Keyword, 'if/while/do/import') as never
}
// value can be an atom or a (parens that gets turned into an atom)
// values are used in a few places:
// 1. function arguments
// 2. array/dict members
// 3. binary operations
// 4. anywhere an expression can be used
value(): SyntaxNode {
if (this.is($T.OpenParen)) return this.parens()
if (this.is($T.OpenBracket)) return this.arrayOrDict()
// dotget
if (this.nextIs($T.Operator, '.')) return this.dotGet()
return this.atom()
}
//
// parse specific nodes
//
// raw determines whether we just want the SyntaxNodes or we want to
// wrap them in a PositionalArg
arg(raw = false): SyntaxNode {
// 'do' is a special function arg - it doesn't need to be wrapped
// in parens. otherwise, args are regular value()s
const val = this.is($T.Keyword, 'do') ? this.do() : this.value()
if (raw) {
return val
} else {
const arg = new SyntaxNode('PositionalArg', val.from, val.to)
if (val.isError) arg.isError = true
arg.add(val)
return arg
}
}
// [ 1 2 3 ]
array(): SyntaxNode {
const open = this.expect($T.OpenBracket)
const values = []
while (!this.is($T.CloseBracket) && !this.isEOF()) {
if (this.is($T.Semicolon) || this.is($T.Newline)) {
this.next()
continue
}
if (this.is($T.Comment)) {
values.push(this.comment())
continue
}
values.push(this.value())
}
const close = this.expect($T.CloseBracket)
const node = new SyntaxNode('Array', open.from, close.to)
return node.push(...values)
}
// which are we dealing with? ignores leading newlines and comments
arrayOrDict(): SyntaxNode {
let peek = 1
let curr = this.peek(peek++)
let isDict = false
while (curr && curr.type !== $T.CloseBracket) {
// definitely a dict
if (curr.type === $T.NamedArgPrefix) {
isDict = true
break
}
// empty dict
if (curr.type === $T.Operator && curr.value === '=') {
isDict = true
break
}
// [ a = true ]
const next = this.peek(peek)
if (next?.type === $T.Operator && next.value === '=') {
isDict = true
break
}
// probably an array
if (curr.type !== $T.Comment && curr.type !== $T.Semicolon && curr.type !== $T.Newline) break
curr = this.peek(peek++)
}
return isDict ? this.dict() : this.array()
}
// x = true
assign(): SyntaxNode {
const ident = this.assignableIdentifier()
const opToken = this.current()!
const op = this.op()
const expr = this.expression()
const node = new SyntaxNode(
opToken.value === '=' ? 'Assign' : 'CompoundAssign',
ident.from,
expr.to,
)
return node.push(ident, op, expr)
}
// identifier used in assignment (TODO: legacy lezer quirk)
assignableIdentifier(): SyntaxNode {
const token = this.expect($T.Identifier)
this.scope.add(token.value!)
const node = SyntaxNode.from(token)
node.type = 'AssignableIdentifier'
return node
}
// atoms are the basic building blocks: literals, identifiers, words
atom(): SyntaxNode {
if (this.is($T.String)) return this.string()
if (this.isAny($T.Null, $T.Boolean, $T.Number, $T.Identifier, $T.Word, $T.Regex, $T.Underscore))
return SyntaxNode.from(this.next())
const next = this.next()
throw new CompilerError(`Unexpected token: ${TokenType[next.type]}`, next.from, next.to)
}
// blocks in if, do, special calls, etc
// `: something end`
//
// `blockNode` determines whether we return [colon, BlockNode, end] or
// just a list of statements like [colon, stmt1, stmt2, end]
block(blockNode = true): SyntaxNode[] {
const stmts: SyntaxNode[] = []
const colon = this.colon()
while (!this.isExprEndKeyword() && !this.isEOF()) {
const stmt = this.statement()
if (stmt) stmts.push(stmt)
}
const out = [colon]
if (blockNode) {
const block = new SyntaxNode('Block', stmts[0]!.from, stmts.at(-1)!.to)
block.push(...stmts)
out.push(block)
} else {
out.push(...stmts)
}
return out
}
// catch err: block
catch(): SyntaxNode {
const keyword = this.keyword('catch')
let catchVar
if (this.is($T.Identifier)) catchVar = this.identifier()
const block = this.block()
const node = new SyntaxNode('CatchExpr', keyword.from, block.at(-1)!.to)
node.push(keyword)
if (catchVar) node.push(catchVar)
return node.push(...block)
}
// colon
colon(): SyntaxNode {
const colon = SyntaxNode.from(this.expect($T.Colon))
colon.type = 'colon' // TODO lezer legacy
return colon
}
// # comment
comment(): SyntaxNode {
return SyntaxNode.from(this.expect($T.Comment))
}
// [ a b c ] = [ 1 2 3 ]
destructure(array: SyntaxNode): SyntaxNode {
const eq = this.op('=')
const val = this.expression()
for (const ident of array.children) {
const varName = this.input.slice(ident.from, ident.to)
this.scope.add(varName)
}
const node = new SyntaxNode('Assign', array.from, val.to)
return node.push(array, eq, val)
}
// [ a=1 b=true c='three' ]
dict(): SyntaxNode {
const open = this.expect($T.OpenBracket)
let isError = false
// empty dict [=] or [ = ]
if (this.is($T.Operator, '=') && this.nextIs($T.CloseBracket)) {
const _op = this.next()
const close = this.next()
return new SyntaxNode('Dict', open.from, close.to)
}
const values = []
while (!this.is($T.CloseBracket) && !this.isEOF()) {
if (this.is($T.Semicolon) || this.is($T.Newline)) {
this.next()
continue
}
if (this.is($T.Comment)) {
values.push(this.comment())
continue
}
// check for named arg with space after it (vs connected)
if (this.nextIs($T.Operator, '=')) {
const ident = this.identifier()
const op = this.op('=')
const prefix = new SyntaxNode('NamedArgPrefix', ident.from, op.to)
if (this.is($T.CloseBracket) || this.is($T.Semicolon) || this.is($T.Newline)) {
const node = new SyntaxNode('NamedArg', ident.from, op.to)
node.isError = true
isError = true
values.push(node.push(prefix))
} else {
const val = this.arg(true)
const node = new SyntaxNode('NamedArg', ident.from, val.to)
values.push(node.push(prefix, val))
}
} else {
const arg = this.is($T.NamedArgPrefix) ? this.namedArg() : this.arg()
if (arg.isError) isError = true
values.push(arg)
}
}
const close = this.expect($T.CloseBracket)
const node = new SyntaxNode('Dict', open.from, close.to)
node.isError = isError
return node.push(...values)
}
// FunctionDef `do x y: something end`
do(): SyntaxNode {
const doNode = this.keyword('do')
doNode.type = 'Do'
this.scope = new Scope(this.scope)
const params = []
while (!this.is($T.Colon) && !this.isExprEnd()) {
let varName = this.current().value!
if (varName.endsWith('=')) varName = varName.slice(0, varName.length - 1)
this.scope.add(varName)
let arg
if (this.is($T.Identifier)) arg = this.identifier()
else if (this.is($T.NamedArgPrefix)) arg = this.namedParam()
else
throw new CompilerError(
`Expected Identifier or NamedArgPrefix, got ${TokenType[this.current().type]}`,
this.current().from,
this.current().to,
)
params.push(arg)
}
const block = this.block(false)
let catchNode, finalNode
if (this.is($T.Keyword, 'catch')) catchNode = this.catch()
if (this.is($T.Keyword, 'finally')) finalNode = this.finally()
const end = this.keyword('end')
let last = block.at(-1)
if (finalNode) last = finalNode.children.at(-1)!
else if (catchNode) last = catchNode.children.at(-1)!
const node = new SyntaxNode('FunctionDef', doNode.from, last!.to)
node.add(doNode)
const paramsNode = new SyntaxNode('Params', params[0]?.from ?? 0, params.at(-1)?.to ?? 0)
if (params.length) paramsNode.push(...params)
node.add(paramsNode)
this.scope = this.scope.parent!
node.push(...block)
if (catchNode) node.push(catchNode)
if (finalNode) node.push(finalNode)
return node.push(end)
}
// config.path
dotGet(): SyntaxNode {
const left = this.identifier()
const ident = this.input.slice(left.from, left.to)
// not in scope, just return Word
if (!this.scope.has(ident)) return this.word(left)
if (left.type.is('Identifier')) left.type = 'IdentifierBeforeDot'
let parts = []
while (this.is($T.Operator, '.')) {
this.next()
parts.push(this.is($T.OpenParen) ? this.parens() : this.atom())
}
// TODO lezer legacy - we can do a flat DotGet if we remove this
const nodes = parts.length > 1 ? collapseDotGets(parts) : undefined
const node = new SyntaxNode('DotGet', left.from, parts.at(-1)!.to)
return nodes ? node.push(left, nodes!) : node.push(left, ...parts)
}
// continue parsing dotget/word binary operation
dotGetBinOp(left: SyntaxNode): SyntaxNode {
while (this.is($T.Operator) && !this.is($T.Operator, '|')) {
const op = this.current().value!
const bp = precedence[op]
if (bp === undefined) break
const opNode = this.op()
const right = this.exprWithPrecedence(bp + 1)
const nodeType = conditionals.has(op) ? 'ConditionalOp' : 'BinOp'
const node = new SyntaxNode(nodeType, left.from, right.to)
node.push(left, opNode, right)
left = node
}
return left
}
// dotget in a statement/expression (something.blah) or (something.blah arg1)
dotGetFunctionCall(): SyntaxNode {
const dotGet = this.dotGet()
// if followed by a binary operator (not pipe), return dotGet/Word as-is for expression parser
if (this.is($T.Operator) && !this.is($T.Operator, '|')) return dotGet
// dotget not in scope, regular Word
if (dotGet.type.is('Word')) return dotGet
if (this.isExprEnd()) return this.functionCallOrIdentifier(dotGet)
else return this.functionCall(dotGet)
}
// can be used in functions or try block
finally(): SyntaxNode {
const keyword = this.keyword('finally')
const block = this.block()
const node = new SyntaxNode('FinallyExpr', keyword.from, block.at(-1)!.to)
return node.push(keyword, ...block)
}
// you're lookin at it
functionCall(fn?: SyntaxNode): SyntaxNode {
const ident = fn ?? this.identifier()
let isError = false
const args: SyntaxNode[] = []
while (!this.isExprEnd()) {
const arg = this.is($T.NamedArgPrefix) ? this.namedArg() : this.arg()
if (arg.isError) isError = true
args.push(arg)
}
const node = new SyntaxNode('FunctionCall', ident.from, (args.at(-1) || ident).to)
node.push(ident, ...args)
if (isError) node.isError = true
if (!this.inTestExpr && this.is($T.Colon)) {
const block = this.block()
const end = this.keyword('end')
const blockNode = new SyntaxNode('FunctionCallWithBlock', node.from, end.to)
return blockNode.push(node, ...block, end)
}
return node
}
// bare identifier in an expression
functionCallOrIdentifier(inner?: SyntaxNode) {
if (!inner && this.nextIs($T.Operator, '.')) {
inner = this.dotGet()
// if the dotGet was just a Word, bail
if (inner.type.is('Word')) return inner
}
inner ??= this.identifier()
const wrapper = new SyntaxNode('FunctionCallOrIdentifier', inner.from, inner.to)
wrapper.push(inner)
if (!this.inTestExpr && this.is($T.Colon)) {
const block = this.block()
const end = this.keyword('end')
const node = new SyntaxNode('FunctionCallWithBlock', wrapper.from, end.to)
return node.push(wrapper, ...block, end)
}
return wrapper
}
// function and variable names
identifier(): SyntaxNode {
return SyntaxNode.from(this.expect($T.Identifier))
}
// if something: blah end
// if something: blah else: blah end
// if something: blah else if something: blah else: blah end
if(): SyntaxNode {
const ifNode = this.keyword('if')
const test = this.testExpr()
const ifBlock = this.block()
const node = new SyntaxNode('IfExpr', ifNode.from, ifBlock.at(-1)!.to)
node.push(ifNode, test)
node.push(...ifBlock)
while (this.is($T.Keyword, 'else') && this.nextIs($T.Keyword, 'if')) {
const elseWord = this.keyword('else')
const ifWord = this.keyword('if')
const elseIfTest = this.testExpr()
const elseIfBlock = this.block()
const elseIfNode = new SyntaxNode('ElseIfExpr', elseWord.from, elseIfBlock.at(-1)!.to)
elseIfNode.push(elseWord, ifWord, elseIfTest)
elseIfNode.push(...elseIfBlock)
node.push(elseIfNode)
}
if (this.is($T.Keyword, 'else') && this.nextIs($T.Colon)) {
const elseWord = this.keyword('else')
const elseBlock = this.block()
const elseNode = new SyntaxNode('ElseExpr', elseWord.from, elseBlock.at(-1)!.to)
elseNode.push(elseWord)
elseNode.push(...elseBlock)
node.push(elseNode)
}
return node.push(this.keyword('end'))
}
import(): SyntaxNode {
const keyword = this.keyword('import')
const args: SyntaxNode[] = []
while (!this.isExprEnd()) {
if (this.is($T.NamedArgPrefix)) {
const prefix = SyntaxNode.from(this.next())
const val = this.value()
const arg = new SyntaxNode('NamedArg', prefix.from, val.to)
arg.push(prefix, val)
args.push(arg)
} else {
args.push(this.identifier())
}
}
const node = new SyntaxNode('Import', keyword.from, args.at(-1)!.to)
node.add(keyword)
return node.push(...args)
}
// if, while, do, etc
keyword(name: string): SyntaxNode {
const node = SyntaxNode.from(this.expect($T.Keyword, name))
node.type = 'keyword' // TODO lezer legacy
return node
}
// abc= true
namedArg(): SyntaxNode {
const prefix = SyntaxNode.from(this.expect($T.NamedArgPrefix))
if (this.isExprEnd()) {
const node = new SyntaxNode('NamedArg', prefix.from, prefix.to)
node.isError = true
return node.push(prefix)
}
const val = this.arg(true)
const node = new SyntaxNode('NamedArg', prefix.from, val.to)
return node.push(prefix, val)
}
// abc= null|true|123|'hi'
namedParam(): SyntaxNode {
const prefix = SyntaxNode.from(this.expect($T.NamedArgPrefix))
const val = this.value()
if (!['Null', 'Boolean', 'Number', 'String'].includes(val.type.name))
throw new CompilerError(
`Default value must be null, boolean, number, or string, got ${val.type.name}`,
val.from,
val.to,
)
const node = new SyntaxNode('NamedParam', prefix.from, val.to)
return node.push(prefix, val)
}
// not blah
not(): SyntaxNode {
const keyword = this.keyword('not')
const val = this.expression()
const node = new SyntaxNode('Not', keyword.from, val.to)
return node.push(keyword, val)
}
// operators like + - =
op(op?: string): SyntaxNode {
const token = op ? this.expect($T.Operator, op) : this.expect($T.Operator)
const name = operators[token.value!]
if (!name)
throw new CompilerError(`Operator not registered: ${token.value!}`, token.from, token.to)
return new SyntaxNode(name, token.from, token.to)
}
// ( expressions in parens )
parens(): SyntaxNode {
this.inParens++
const open = this.expect($T.OpenParen)
const child = this.expression()
const close = this.expect($T.CloseParen)
this.inParens--
const node = new SyntaxNode('ParenExpr', open.from, close.to)
node.add(child)
return node
}
// 'hell yes' "hell no" { hell if i know }
string(): SyntaxNode {
const token = this.expect($T.String)
return parseString(this.input, token.from, token.to, this)
}
// if TEST: blah end
testExpr(): SyntaxNode {
this.inTestExpr = true
const expr = this.expression()
this.inTestExpr = false
return expr
}
// throw blah
throw(): SyntaxNode {
const keyword = this.keyword('throw')
const val = this.expression()
const node = new SyntaxNode('Throw', keyword.from, val.to)
return node.push(keyword, val)
}
// try: blah catch e: blah end
try(): SyntaxNode {
const tryNode = this.keyword('try')
const tryBlock = this.block()
let last = tryBlock.at(-1)
let catchNode, finalNode
if (this.is($T.Keyword, 'catch')) catchNode = this.catch()
if (this.is($T.Keyword, 'finally')) finalNode = this.finally()
const end = this.keyword('end')
if (finalNode) last = finalNode.children.at(-1)
else if (catchNode) last = catchNode.children.at(-1)
const node = new SyntaxNode('TryExpr', tryNode.from, last!.to)
node.push(tryNode, ...tryBlock)
if (catchNode) node.push(catchNode)
if (finalNode) node.push(finalNode)
return node.push(end)
}
// while test: blah end
while(): SyntaxNode {
const keyword = this.keyword('while')
const test = this.testExpr()
const block = this.block()
const end = this.keyword('end')
const node = new SyntaxNode('WhileExpr', keyword.from, end.to)
return node.push(keyword, test, ...block, end)
}
// readme.txt (when `readme` isn't in scope)
word(start?: SyntaxNode): SyntaxNode {
const parts = [start ?? this.expect($T.Word)]
while (this.is($T.Operator, '.')) {
this.next()
if (this.isAny($T.Word, $T.Identifier, $T.Number)) parts.push(this.next())
}
return new SyntaxNode('Word', parts[0]!.from, parts.at(-1)!.to)
}
//
// helpers
//
current(): Token {
return this.tokens[this.pos] || { type: TokenType.Newline, from: 0, to: 0 }
}
peek(offset = 1): Token | undefined {
return this.tokens[this.pos + offset]
}
// look past newlines to check for a specific token
peekPastNewlines(type: TokenType, value?: string): boolean {
let offset = 1
let peek = this.peek(offset)
while (peek && peek.type === $T.Newline) peek = this.peek(++offset)
if (!peek || peek.type !== type) return false
if (value !== undefined && peek.value !== value) return false
return true
}
next(): Token {
const token = this.current()
this.pos++
return token
}
is(type: TokenType, value?: string): boolean {
const token = this.current()
if (!token || token.type !== type) return false
if (value !== undefined && token.value !== value) return false
return true
}
isAny(...type: TokenType[]): boolean {
return type.some((x) => this.is(x))
}
nextIs(type: TokenType, value?: string): boolean {
const token = this.peek()
if (!token || token.type !== type) return false
if (value !== undefined && token.value !== value) return false
return true
}
nextIsAny(...type: TokenType[]): boolean {
return type.some((x) => this.nextIs(x))
}
isExprEnd(): boolean {
return (
this.isAny($T.Colon, $T.Semicolon, $T.Newline, $T.CloseParen, $T.CloseBracket) ||
this.is($T.Operator, '|') ||
this.isExprEndKeyword() ||
!this.current()
)
}
nextIsExprEnd(): boolean {
// pipes act like expression end for function arg parsing
if (this.nextIs($T.Operator, '|')) return true
return (
this.nextIsAny($T.Colon, $T.Semicolon, $T.Newline, $T.CloseBracket, $T.CloseParen) ||
this.nextIs($T.Keyword, 'end') ||
this.nextIs($T.Keyword, 'else') ||
this.nextIs($T.Keyword, 'catch') ||
this.nextIs($T.Keyword, 'finally') ||
!this.peek()
)
}
isExprEndKeyword(): boolean {
return (
this.is($T.Keyword, 'end') ||
this.is($T.Keyword, 'else') ||
this.is($T.Keyword, 'catch') ||
this.is($T.Keyword, 'finally')
)
}
isPipe(): boolean {
// inside parens, only look for pipes on same line (don't look past newlines)
const canLookPastNewlines = this.inParens === 0
return (
this.is($T.Operator, '|') || (canLookPastNewlines && this.peekPastNewlines($T.Operator, '|'))
)
}
expect(type: TokenType, value?: string): Token | never {
if (!this.is(type, value)) {
const token = this.current()
throw new CompilerError(
`Expected ${TokenType[type]}${value ? ` "${value}"` : ''}, got ${TokenType[token?.type || 0]}${token?.value ? ` "${token.value}"` : ''} at position ${this.pos}`,
token.from,
token.to,
)
}
return this.next()
}
isEOF(): boolean {
return this.pos >= this.tokens.length
}
}
// TODO lezer legacy
function collapseDotGets(origNodes: SyntaxNode[]): SyntaxNode {
const nodes = [...origNodes]
let right = nodes.pop()!
while (nodes.length > 0) {
const left = nodes.pop()!
if (left.type.is('Identifier')) left.type = 'IdentifierBeforeDot'
const dot = new SyntaxNode('DotGet', left.from, right.to)
dot.push(left, right)
right = dot
}
return right
}

View File

@ -0,0 +1,129 @@
import { ContextTracker, InputStream } from '@lezer/lr'
import * as terms from './shrimp.terms'
export class Scope {
constructor(public parent: Scope | null, public vars = new Set<string>()) { }
has(name: string): boolean {
return this.vars.has(name) || (this.parent?.has(name) ?? false)
}
hash(): number {
let h = 0
for (const name of this.vars) {
for (let i = 0; i < name.length; i++) {
h = (h << 5) - h + name.charCodeAt(i)
h |= 0
}
}
if (this.parent) {
h = (h << 5) - h + this.parent.hash()
h |= 0
}
return h
}
// Static methods that return new Scopes (immutable operations)
static add(scope: Scope, ...names: string[]): Scope {
const newVars = new Set(scope.vars)
names.forEach((name) => newVars.add(name))
return new Scope(scope.parent, newVars)
}
push(): Scope {
return new Scope(this, new Set())
}
pop(): Scope {
return this.parent ?? this
}
}
// Tracker context that combines Scope with temporary pending identifiers
class TrackerContext {
constructor(public scope: Scope, public pendingIds: string[] = []) { }
}
// Extract identifier text from input stream
const readIdentifierText = (input: InputStream, start: number, end: number): string => {
let text = ''
for (let i = start; i < end; i++) {
const offset = i - input.pos
const ch = input.peek(offset)
if (ch === -1) break
text += String.fromCharCode(ch)
}
return text
}
let inParams = false
export const trackScope = new ContextTracker<TrackerContext>({
start: new TrackerContext(new Scope(null, new Set())),
shift(context, term, stack, input) {
if (term == terms.Do) inParams = true
if (term === terms.AssignableIdentifier) {
const text = readIdentifierText(input, input.pos, stack.pos)
return new TrackerContext(Scope.add(context.scope, text), context.pendingIds)
}
if (inParams && term === terms.Identifier) {
const text = readIdentifierText(input, input.pos, stack.pos)
return new TrackerContext(context.scope, [...context.pendingIds, text])
}
// Track identifiers in array destructuring: [ a b ] = ...
if (!inParams && term === terms.Identifier && isArrayDestructuring(input)) {
const text = readIdentifierText(input, input.pos, stack.pos)
return new TrackerContext(Scope.add(context.scope, text), context.pendingIds)
}
return context
},
reduce(context, term) {
if (term === terms.Params) {
inParams = false
let newScope = context.scope.push()
if (context.pendingIds.length > 0) {
newScope = Scope.add(newScope, ...context.pendingIds)
}
return new TrackerContext(newScope, [])
}
// Pop scope when exiting function
if (term === terms.FunctionDef) {
return new TrackerContext(context.scope.pop(), [])
}
return context
},
hash: (context) => context.scope.hash(),
})
// Check if we're parsing array destructuring: [ a b ] = ...
const isArrayDestructuring = (input: InputStream): boolean => {
let pos = 0
// Find closing bracket
while (pos < 200 && input.peek(pos) !== 93 /* ] */) {
if (input.peek(pos) === -1) return false // EOF
pos++
}
if (input.peek(pos) !== 93 /* ] */) return false
pos++
// Skip whitespace
while (input.peek(pos) === 32 /* space */ ||
input.peek(pos) === 9 /* tab */ ||
input.peek(pos) === 10 /* \n */) {
pos++
}
return input.peek(pos) === 61 /* = */
}

249
src/parser/shrimp.grammar Normal file
View File

@ -0,0 +1,249 @@
@external propSource highlighting from "./highlight"
@context trackScope from "./parserScopeContext"
@skip { space | Comment }
@top Program { item* }
@external tokens operatorTokenizer from "./operatorTokenizer" { Star, Slash, Plus, Minus, And, Or, Eq, EqEq, Neq, Lt, Lte, Gt, Gte, Modulo, PlusEq, MinusEq, StarEq, SlashEq, ModuloEq }
@tokens {
@precedence { Number Regex }
StringFragment { !['\\$]+ }
NamedArgPrefix { $[a-z-]+ "=" }
Number { ("-" | "+")? $[0-9]+ ('.' $[0-9]+)? }
Boolean { "true" | "false" }
newlineOrSemicolon { "\n" | ";" }
eof { @eof }
space { " " | "\t" }
Comment { "#" " " ![\n]* }
leftParen { "(" }
rightParen { ")" }
colon[closedBy="end", @name="colon"] { ":" }
Underscore { "_" }
Regex { "//" (![/\\\n[] | "\\" ![\n] | "[" (![\n\\\]] | "\\" ![\n])* "]")+ ("//" $[gimsuy]*)? } // Stolen from the lezer JavaScript grammar
"|"[@name=operator]
}
end { @specialize[@name=keyword]<Identifier, "end"> }
while { @specialize[@name=keyword]<Identifier, "while"> }
if { @specialize[@name=keyword]<Identifier, "if"> }
else { @specialize[@name=keyword]<Identifier, "else"> }
try { @specialize[@name=keyword]<Identifier, "try"> }
catch { @specialize[@name=keyword]<Identifier, "catch"> }
finally { @specialize[@name=keyword]<Identifier, "finally"> }
throw { @specialize[@name=keyword]<Identifier, "throw"> }
null { @specialize[@name=Null]<Identifier, "null"> }
@external tokens tokenizer from "./tokenizer" { Identifier, AssignableIdentifier, Word, IdentifierBeforeDot }
@external specialize {Identifier} specializeKeyword from "./tokenizer" { Do }
@precedence {
pipe @left,
or @left,
and @left,
comparison @left,
multiplicative @left,
additive @left,
call
}
item {
consumeToTerminator newlineOrSemicolon |
consumeToTerminator eof |
newlineOrSemicolon // allow blank lines
}
consumeToTerminator {
PipeExpr |
WhileExpr |
FunctionCallWithBlock |
ambiguousFunctionCall |
TryExpr |
Throw |
IfExpr |
FunctionDef |
CompoundAssign |
Assign |
BinOp |
ConditionalOp |
expressionWithoutIdentifier
}
PipeExpr {
pipeOperand (!pipe "|" pipeOperand)+
}
pipeOperand {
consumeToTerminator
}
WhileExpr {
while (ConditionalOp | expression) colon Block end
}
Block {
consumeToTerminator | newlineOrSemicolon block
}
FunctionCallWithBlock {
ambiguousFunctionCall colon Block CatchExpr? FinallyExpr? end
}
FunctionCallOrIdentifier {
DotGet | Identifier
}
ambiguousFunctionCall {
FunctionCall | FunctionCallOrIdentifier
}
FunctionCall {
(DotGet | Identifier | ParenExpr) arg+
}
arg {
PositionalArg | NamedArg
}
PositionalArg {
expression | FunctionDef | Underscore
}
NamedArg {
NamedArgPrefix (expression | FunctionDef | Underscore)
}
FunctionDef {
Do Params colon (consumeToTerminator | newlineOrSemicolon block) CatchExpr? FinallyExpr? end
}
ifTest {
ConditionalOp | expression | FunctionCall
}
IfExpr {
if ifTest colon Block ElseIfExpr* ElseExpr? end
}
ElseIfExpr {
else if ifTest colon Block
}
ElseExpr {
else colon Block
}
TryExpr {
try colon Block CatchExpr? FinallyExpr? end
}
CatchExpr {
catch Identifier colon Block
}
FinallyExpr {
finally colon Block
}
Throw {
throw (BinOp | ConditionalOp | expression)
}
ConditionalOp {
expression !comparison EqEq expression |
expression !comparison Neq expression |
expression !comparison Lt expression |
expression !comparison Lte expression |
expression !comparison Gt expression |
expression !comparison Gte expression |
(expression | ConditionalOp) !and And (expression | ConditionalOp) |
(expression | ConditionalOp) !or Or (expression | ConditionalOp)
}
Params {
Identifier* NamedParam*
}
NamedParam {
NamedArgPrefix (String | Number | Boolean | null)
}
Assign {
(AssignableIdentifier | Array) Eq consumeToTerminator
}
CompoundAssign {
AssignableIdentifier (PlusEq | MinusEq | StarEq | SlashEq | ModuloEq) consumeToTerminator
}
BinOp {
expression !multiplicative Modulo expression |
(expression | BinOp) !multiplicative Star (expression | BinOp) |
(expression | BinOp) !multiplicative Slash (expression | BinOp) |
(expression | BinOp) !additive Plus (expression | BinOp) |
(expression | BinOp) !additive Minus (expression | BinOp)
}
ParenExpr {
leftParen (IfExpr | ambiguousFunctionCall | BinOp | expressionWithoutIdentifier | ConditionalOp | PipeExpr | FunctionDef) rightParen
}
expression {
expressionWithoutIdentifier | DotGet | Identifier
}
@local tokens {
dot { "." }
}
@skip {} {
DotGet {
IdentifierBeforeDot dot (Number | Identifier | ParenExpr)
}
String { "'" stringContent* "'" }
}
stringContent {
StringFragment |
Interpolation |
EscapeSeq
}
Interpolation {
"$" Identifier |
"$" ParenExpr
}
EscapeSeq {
"\\" ("$" | "n" | "t" | "r" | "\\" | "'")
}
Dict {
"[=]" |
"[" newlineOrSemicolon* NamedArg (newlineOrSemicolon | NamedArg)* "]"
}
Array {
"[" newlineOrSemicolon* (expression (newlineOrSemicolon | expression)*)? "]"
}
// We need expressionWithoutIdentifier to avoid conflicts in consumeToTerminator.
// Without this, when parsing "my-var" at statement level, the parser can't decide:
// - ambiguousFunctionCall → FunctionCallOrIdentifier → Identifier
// - expression → Identifier
// Both want the same Identifier token! So we use expressionWithoutIdentifier
// to remove Identifier from the second path, forcing standalone identifiers
// to go through ambiguousFunctionCall (which is what we want semantically).
// Yes, it is annoying and I gave up trying to use GLR to fix it.
expressionWithoutIdentifier {
ParenExpr | Word | String | Number | Boolean | Regex | Dict | Array | null
}
block {
(consumeToTerminator? newlineOrSemicolon)*
}

4
src/parser/shrimp.grammar.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
declare module '*.grammar' {
const content: string
export default content
}

View File

@ -0,0 +1,66 @@
// This file was generated by lezer-generator. You probably shouldn't edit it.
export const
Star = 1,
Slash = 2,
Plus = 3,
Minus = 4,
And = 5,
Or = 6,
Eq = 7,
EqEq = 8,
Neq = 9,
Lt = 10,
Lte = 11,
Gt = 12,
Gte = 13,
Modulo = 14,
PlusEq = 15,
MinusEq = 16,
StarEq = 17,
SlashEq = 18,
ModuloEq = 19,
Identifier = 20,
AssignableIdentifier = 21,
Word = 22,
IdentifierBeforeDot = 23,
Do = 24,
Comment = 25,
Program = 26,
PipeExpr = 27,
WhileExpr = 29,
keyword = 70,
ConditionalOp = 31,
ParenExpr = 32,
IfExpr = 33,
FunctionCall = 35,
DotGet = 36,
Number = 37,
PositionalArg = 38,
FunctionDef = 39,
Params = 40,
NamedParam = 41,
NamedArgPrefix = 42,
String = 43,
StringFragment = 44,
Interpolation = 45,
EscapeSeq = 46,
Boolean = 47,
Null = 48,
colon = 49,
CatchExpr = 50,
Block = 52,
FinallyExpr = 53,
Underscore = 56,
NamedArg = 57,
ElseIfExpr = 58,
ElseExpr = 60,
FunctionCallOrIdentifier = 61,
BinOp = 62,
Regex = 63,
Dict = 64,
Array = 65,
FunctionCallWithBlock = 66,
TryExpr = 67,
Throw = 69,
CompoundAssign = 71,
Assign = 72

27
src/parser/shrimp.ts Normal file
View File

@ -0,0 +1,27 @@
// This file was generated by lezer-generator. You probably shouldn't edit it.
import {LRParser, LocalTokenGroup} from "@lezer/lr"
import {operatorTokenizer} from "./operatorTokenizer"
import {tokenizer, specializeKeyword} from "./tokenizer"
import {trackScope} from "./parserScopeContext"
import {highlighting} from "./highlight"
const spec_Identifier = {__proto__:null,while:60, if:68, null:96, catch:102, finally:108, end:110, else:118, try:136, throw:140}
export const parser = LRParser.deserialize({
version: 14,
states: "9UQYQbOOO!dOpO'#DQO!iOSO'#DXO$_QcO'#DkO&rQcO'#EYOOQ`'#Eh'#EhO'uQRO'#DlO)[QcO'#EWO)lQbO'#C|OOQa'#Dn'#DnO+nQbO'#DoOOQa'#EY'#EYO+uQcO'#EYO+|QcO'#EXO,yQcO'#EWO-TQRO'#DuOOQ`'#EW'#EWO-iQbO'#EWO-pQQO'#EVOOQ`'#EV'#EVOOQ`'#Dw'#DwQYQbOOO-{QbO'#DTO.WQbO'#C}O.{QbO'#CyO/pQQO'#DqO.{QbO'#DsO/uObO,59lO0QQbO'#DZO0YQWO'#D[OOOO'#E`'#E`OOOO'#D|'#D|O0nOSO,59sOOQa,59s,59sOOQ`'#DS'#DSO0|QbO'#DgOOQ`'#E^'#E^OOQ`'#Dy'#DyO1WQbO,59kOOQa'#EX'#EXO.{QbO,5:WO.{QbO,5:WO.{QbO,5:WO.{QbO,59gO.{QbO,59gO.{QbO,59gO2QQRO,59hO2^QQO,59hO2fQQO,59hO2qQRO,59hO3[QRO,59hO3jQQO'#CwOOQ`'#EP'#EPO3oQbO,5:ZO3vQQO,5:YOOQa,5:Z,5:ZO4RQbO,5:ZO)lQbO,5:bO)lQbO,5:aO4]QbO,5:[O4dQbO,59cOOQ`,5:q,5:qO)lQbO'#DxOOQ`-E7u-E7uOOQ`'#Dz'#DzO5OQbO'#DUO5ZQbO'#DVOOQO'#D{'#D{O5RQQO'#DUO5iQQO,59oO5nQcO'#EXO5uQRO'#E[O6lQRO'#E[OOQO'#E['#E[O6sQQO,59iO6xQRO,59eO7PQRO,59eO4]QbO,5:]O7[QcO,5:_O8dQcO,5:_O8tQcO,5:_OOQa1G/W1G/WOOOO,59u,59uOOOO,59v,59vOOOO-E7z-E7zOOQa1G/_1G/_OOQ`,5:R,5:ROOQ`-E7w-E7wOOQa1G/r1G/rO9sQcO1G/rO9}QcO1G/rO:XQcO1G/rOOQa1G/R1G/RO<TQcO1G/RO<[QcO1G/RO<cQcO1G/ROOQa1G/S1G/SOOQ`-E7}-E7}O<jQQO1G/tOOQa1G/u1G/uO<uQbO1G/uOOQO'#EQ'#EQO<jQQO1G/tOOQa1G/t1G/tOOQ`'#ER'#ERO<uQbO1G/uO=PQbO1G/|O=kQbO1G/{O>VQbO'#DbO>hQbO'#DbO>{QbO1G/vOOQ`-E7v-E7vOOQ`,5:d,5:dOOQ`-E7x-E7xO?WQQO,59pOOQO,59q,59qOOQO-E7y-E7yO?`QbO1G/ZO4]QbO1G/TO4]QbO1G/PO?gQbO1G/wO?rQQO7+%`OOQa7+%`7+%`O?}QbO7+%aOOQa7+%a7+%aOOQO-E8O-E8OOOQ`-E8P-E8POOQ`'#D}'#D}O@XQQO'#D}O@aQbO'#EgOOQ`,59|,59|O@tQbO'#D`O@yQQO'#DcOOQ`7+%b7+%bOAOQbO7+%bOATQbO7+%bOA]QbO7+$uOAkQbO7+$uOA{QbO7+$oOBTQbO7+$kOOQ`7+%c7+%cOBYQbO7+%cOB_QbO7+%cOOQa<<Hz<<HzOOQa<<H{<<H{OOQ`,5:i,5:iOOQ`-E7{-E7{OBgQQO,59zO4]QbO,59}OOQ`<<H|<<H|OBlQbO<<H|OOQ`<<Ha<<HaOBqQbO<<HaOBvQbO<<HaOCOQbO<<HaOOQ`'#EO'#EOOCZQbO<<HZOCcQbO'#DjOOQ`<<HZ<<HZOCkQbO<<HZOOQ`<<HV<<HVOOQ`<<H}<<H}OCpQbO<<H}O4]QbO1G/fOOQ`1G/i1G/iOOQ`AN>hAN>hOOQ`AN={AN={OCuQbOAN={OCzQbOAN={OOQ`-E7|-E7|OOQ`AN=uAN=uODSQbOAN=uO.WQbO,5:SO4]QbO,5:UOOQ`AN>iAN>iOOQ`7+%Q7+%QOOQ`G23gG23gODXQbOG23gPD^QbO'#DhOOQ`G23aG23aODcQQO1G/nOOQ`1G/p1G/pOOQ`LD)RLD)RO4]QbO7+%YOOQ`<<Ht<<Ht",
stateData: "Dk~O!xOSiOS~OdROe_OfZOgPOhfOnhOrgOuZO!PZO!QZO!aZO!fiO!hjO!}WO#RQO#YcO#^XO#_YO~O#PkO~O|nO#RqO#TlO#UmO~OdwOfZOgPOhfOuZOzsO!PZO!QZO!YrO!aZO!}WO#RQO#^XO#_YOT!{XU!{XW!{XX!{XY!{XZ!{X[!{X]!{X~OP!{XQ!{XR!{XS!{X^!{Xl!_X!R!_X#Y!_X#a!_X#]!_X!T!_X!W!_X!X!_X!]!_X~P!wOP!|XQ!|XR!|XS!|XT!|XU!|XW!|XX!|XY!|XZ!|X[!|X]!|X^!|Xl!|X#Y!|X#a!|X#]!|X!T!|X!W!|X!X!|X!]!|X~OdwOfZOgPOhfOuZOzsO!PZO!QZO!YrO!aZO!}WO#RQO#^XO#_YO!R!|X~P%_OPyOQyORzOSzOT|OU}OW{OX{OY{OZ{O[{O]{O^xO~Ol!zX#Y!zX#a!zX!T!zX!W!zX!X!zX#]!zX!]!zX~OPyOQyORzOSzO~P(pOdROe_OfZOgPOhfOnhOrgOuZO!PZO!QZO!aZO!fiO!hjO!}WO#RQO#^XO#_YO~OdwOfZOgPOuZOzsO!PZO!QZO!aZO!}WO#RQO#Y!UO#^XO#_YO~O#`!XO~P*sOV!ZO~P%_OP!{XQ!{XR!{XS!{XT!{XU!{XW!{XX!{XY!{XZ!{X[!{X]!{X^!{X~P(pOT|OU}O~P(pOV!ZO_![O`![Oa![Ob![Oc![O~O!R!]O~P(pOl!`O#Y!_O#a!_O~Od!bOz!dO!RxP~Od!hOfZOgPOuZO!PZO!QZO!aZO!}WO#RQO#^XO#_YO~OdwOfZOgPOuZO!PZO!QZO!aZO!}WO#RQO#^XO#_YO~O!R!oO~Od!sOu!sO!}WO~Od!tO!}WO~O#R!uO#T!uO#U!uO#V!uO#W!uO#X!uO~O|nO#R!wO#TlO#UmO~OhfO!Y!xO~P.{OhfOzsO!YrOlsa!Rsa#Ysa#asa#]sa!Tsa!Wsa!Xsa!]sa~P.{OPyOQyORzOSzO#]#SOl!zX~O!R!]O#]#SOl!zX~O#]#SOP!{XQ!{XR!{XS!{X^!{Xl!zX~P#sOT|OU}O#]#SOl!zX~Ol!`O~O#`#VO~P*sOzsO#Y#XO#`#ZO~O#Y#[O#`#VO~P.{O#Y#aO~P)lOl!`O#Yka#aka#]ka!Tka!Wka!Xka!]ka~Od!bOz!dO!RxX~Ou#gO!P#gO!Q#gO#RQO~O!R#iO~O!R!{X~P!wOT|OU}O!R#OX~OT|OU}OW{OX{OY{OZ{O[{O]{O~O!R#OX~P6QO!R#jO~O!R#kO~P6QOT|OU}O!R#kO~Ol!ga#Y!ga#a!ga!T!ga!W!ga!X!ga#]!ga!]!ga~P'uOl!ga#Y!ga#a!ga!T!ga!W!ga!X!ga#]!ga!]!ga~OPyOQyORzOSzO~P7xOT|OU}O~P7xO^xOR!`iS!`il!`i#Y!`i#a!`i#]!`i!T!`i!W!`i!X!`i!]!`i~OP!`iQ!`i~P9OOPyOQyO~P9OOPyOQyOR!`iS!`il!`i#Y!`i#a!`i#]!`i!T!`i!W!`i!X!`i!]!`i~OW{OX{OY{OZ{O[{O]{OToiloi#Yoi#aoi#]oi!Roi!Toi!Woi!Xoi!]oi~OU}O~P;POU}O~P;cOUoi~P;POzsO#Y#XO#`#nO~O#Y#[O#`#pO~P.{Ol!`O#Y!ji#a!ji!T!ji!W!ji!X!ji#]!ji!]!ji~Ol!`O#Y!ii#a!ii!T!ii!W!ii!X!ii#]!ii!]!ii~Ol!`O!T!UX!W!UX!X!UX!]!UX~O#Y#sO!T#ZP!W#ZP!X#ZP!]#ZP~P)lO!T#wO!W#xO!X#yO~Oz!dO!Rxa~O#Y#}O~P)lO!T#wO!W#xO!X$QO~OzsO#Y#XO#`$TO~O#Y#[O#`$UO~P.{Ol!`O#Y$VO~O#Y#sO!T#ZX!W#ZX!X#ZX!]#ZX~P)lOd$XO~O!R$YO~O!X$ZO~O!W#xO!X$ZO~Ol!`O!T#wO!W#xO!X$]O~O#Y#sO!T#ZP!W#ZP!X#ZP~P)lO!X$dO!]$cO~O!X$fO~O!X$gO~O!W#xO!X$gO~O!R$iO~O!X$kO~O!X$lO~O!W#xO!X$lO~O!T#wO!W#xO!X$lO~O!X$pO!]$cO~Or$rO!R$sO~O!X$pO~O!X$tO~O!X$vO~O!W#xO!X$vO~O!X$yO~O!X$|O~Or$rO~O!R$}O~Ou!a~",
goto: "4l#]PPPPPPPPPPPPPPPPPPPPPPPPPPP#^P#tP$Y%Q#^P&T&mP'l'r(c(fP(lP)j)jPPP)nP)z*dPPP*z+^P+b+h+|P,m-h#t#tP#tP#t#t.e.k.w/P/V/a/g/n/t/z0UPPP0`0d1W2oP3nP3tP3zPPPPPP4O4Ur`Oe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}R!PWu`OWe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}r^Oe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}Q!SWS!ig$rQ!nhQ!rjQ#O}R#Q|xSOWeg!Z![!]!`!o#a#i#j#k#u#}$Y$i$r$s$}vZRSYhjsvxyz{|}!V!Y!h#W#]#oQ!skR!tltTOWe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}T!kg$rtROWe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}vwRSYhjsvxyz{|}!V!Y!h#W#]#oT!hg$rXtRSv!hr`Oe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}WrRSv!hQ!PWR!xsR!gfX!ef!c!f#f!pZORSWYeghjsvxyz{|}!V!Y!Z![!]!`!h!o#W#]#a#i#j#k#o#u#}$Y$i$r$s$}R#g!dTnQpQ#{#bQ$S#lQ$_#|R$n$`Q#b!]Q#l!oQ$O#jQ$P#kQ$j$YQ$u$iQ${$sR%O$}Q#z#bQ$R#lQ$[#{Q$^#|Q$h$SS$m$_$`R$w$nWtRSv!hQ!WYQ#U!VX#X!W#U#Y#mT$a$O$bQ$e$OR$q$buTOWe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}rVOe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}Q!OWQ!qjQ!zyR!}z!qZORSWYeghjsvxyz{|}!V!Y!Z![!]!`!h!o#W#]#a#i#j#k#o#u#}$Y$i$r$s$}zZRSYghjsvxyz{|}!V!Y!h#W#]#o$ru[OWe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}QeOR!ae^!^b!T#^#_#`#t#|R#c!^UvRS!hR!yvQ!cfR#e!cQ!ffQ#f!cT#h!f#fQpQR!vpS#u#a#}R$W#uQ$b$OR$o$bQ!VYR#T!VQ#Y!WQ#m#UT#q#Y#mQ#]!YQ#o#WT#r#]#oTdOeSbOeQ!TWQ#^!ZQ#_![`#`!]!o#j#k$Y$i$s$}Q#d!`U#t#a#u#}R#|#itUOWe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}WrRSv!hQ!YYS!jg$rQ!mhQ!pjQ!xsQ!zxQ!{yQ!|zQ#O{Q#P|Q#R}Q#W!VX#[!Y#W#]#or]Oe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}zwRSYghjsvxyz{|}!V!Y!h#W#]#o$rR!RWQ!lgR$z$rXuRSv!hToQpQ#v#aR$`#}raOe!Z![!]!`!o#a#i#j#k#u#}$Y$i$s$}R!QW",
nodeNames: "⚠ Star Slash Plus Minus And Or Eq EqEq Neq Lt Lte Gt Gte Modulo PlusEq MinusEq StarEq SlashEq ModuloEq Identifier AssignableIdentifier Word IdentifierBeforeDot Do Comment Program PipeExpr operator WhileExpr keyword ConditionalOp ParenExpr IfExpr keyword FunctionCall DotGet Number PositionalArg FunctionDef Params NamedParam NamedArgPrefix String StringFragment Interpolation EscapeSeq Boolean Null colon CatchExpr keyword Block FinallyExpr keyword keyword Underscore NamedArg ElseIfExpr keyword ElseExpr FunctionCallOrIdentifier BinOp Regex Dict Array FunctionCallWithBlock TryExpr keyword Throw keyword CompoundAssign Assign",
maxTerm: 109,
context: trackScope,
nodeProps: [
["closedBy", 49,"end"]
],
propSources: [highlighting],
skippedNodes: [0,25],
repeatNodeCount: 11,
tokenData: "C|~R|OX#{XY$jYZ%TZp#{pq$jqs#{st%ntu'tuw#{wx'yxy(Oyz(iz{#{{|)S|}#{}!O+v!O!P#{!P!Q.]!Q![)q![!]6x!]!^%T!^!}#{!}#O7c#O#P9X#P#Q9^#Q#R#{#R#S9w#S#T#{#T#Y,w#Y#Z:b#Z#b,w#b#c?`#c#f,w#f#g@]#g#h,w#h#iAY#i#o,w#o#p#{#p#qC^#q;'S#{;'S;=`$d<%l~#{~O#{~~CwS$QU|SOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{S$gP;=`<%l#{^$qU|S!xYOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U%[U|S#YQOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{^%sW|SOp#{pq&]qt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{^&dZiY|SOY&]YZ#{Zt&]tu'Vuw&]wx'Vx#O&]#O#P'V#P;'S&];'S;=`'n<%lO&]Y'[SiYOY'VZ;'S'V;'S;=`'h<%lO'VY'kP;=`<%l'V^'qP;=`<%l&]~'yO#T~~(OO#R~U(VU|S!}QOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U(pU|S#]QOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U)XW|SOt#{uw#{x!Q#{!Q![)q![#O#{#P;'S#{;'S;=`$d<%lO#{U)xY|SuQOt#{uw#{x!O#{!O!P*h!P!Q#{!Q![)q![#O#{#P;'S#{;'S;=`$d<%lO#{U*mW|SOt#{uw#{x!Q#{!Q![+V![#O#{#P;'S#{;'S;=`$d<%lO#{U+^W|SuQOt#{uw#{x!Q#{!Q![+V![#O#{#P;'S#{;'S;=`$d<%lO#{U+{^|SOt#{uw#{x}#{}!O,w!O!Q#{!Q![)q![!_#{!_!`-r!`#O#{#P#T#{#T#o,w#o;'S#{;'S;=`$d<%lO#{U,|[|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#o,w#o;'S#{;'S;=`$d<%lO#{U-yUzQ|SOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U.bW|SOt#{uw#{x!P#{!P!Q.z!Q#O#{#P;'S#{;'S;=`$d<%lO#{U/P^|SOY/{YZ#{Zt/{tu1Ouw/{wx1Ox!P/{!P!Q#{!Q!}/{!}#O5q#O#P3^#P;'S/{;'S;=`6r<%lO/{U0S^|S!aQOY/{YZ#{Zt/{tu1Ouw/{wx1Ox!P/{!P!Q3s!Q!}/{!}#O5q#O#P3^#P;'S/{;'S;=`6r<%lO/{Q1TX!aQOY1OZ!P1O!P!Q1p!Q!}1O!}#O2_#O#P3^#P;'S1O;'S;=`3m<%lO1OQ1sP!P!Q1vQ1{U!aQ#Z#[1v#]#^1v#a#b1v#g#h1v#i#j1v#m#n1vQ2bVOY2_Z#O2_#O#P2w#P#Q1O#Q;'S2_;'S;=`3W<%lO2_Q2zSOY2_Z;'S2_;'S;=`3W<%lO2_Q3ZP;=`<%l2_Q3aSOY1OZ;'S1O;'S;=`3m<%lO1OQ3pP;=`<%l1OU3xW|SOt#{uw#{x!P#{!P!Q4b!Q#O#{#P;'S#{;'S;=`$d<%lO#{U4ib|S!aQOt#{uw#{x#O#{#P#Z#{#Z#[4b#[#]#{#]#^4b#^#a#{#a#b4b#b#g#{#g#h4b#h#i#{#i#j4b#j#m#{#m#n4b#n;'S#{;'S;=`$d<%lO#{U5v[|SOY5qYZ#{Zt5qtu2_uw5qwx2_x#O5q#O#P2w#P#Q/{#Q;'S5q;'S;=`6l<%lO5qU6oP;=`<%l5qU6uP;=`<%l/{U7PU|S!RQOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U7jW#_Q|SOt#{uw#{x!_#{!_!`8S!`#O#{#P;'S#{;'S;=`$d<%lO#{U8XV|SOt#{uw#{x#O#{#P#Q8n#Q;'S#{;'S;=`$d<%lO#{U8uU#^Q|SOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{~9^O#U~U9eU#`Q|SOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U:OU|S!YQOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{U:g]|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#U;`#U#o,w#o;'S#{;'S;=`$d<%lO#{U;e^|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#`,w#`#a<a#a#o,w#o;'S#{;'S;=`$d<%lO#{U<f^|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#g,w#g#h=b#h#o,w#o;'S#{;'S;=`$d<%lO#{U=g^|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#X,w#X#Y>c#Y#o,w#o;'S#{;'S;=`$d<%lO#{U>j[!PQ|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#o,w#o;'S#{;'S;=`$d<%lO#{^?g[#VW|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#o,w#o;'S#{;'S;=`$d<%lO#{^@d[#XW|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#o,w#o;'S#{;'S;=`$d<%lO#{^Aa^#WW|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#f,w#f#gB]#g#o,w#o;'S#{;'S;=`$d<%lO#{UBb^|SOt#{uw#{x}#{}!O,w!O!_#{!_!`-r!`#O#{#P#T#{#T#i,w#i#j=b#j#o,w#o;'S#{;'S;=`$d<%lO#{UCeUlQ|SOt#{uw#{x#O#{#P;'S#{;'S;=`$d<%lO#{~C|O#a~",
tokenizers: [operatorTokenizer, 1, 2, 3, tokenizer, new LocalTokenGroup("[~RP!O!PU~ZO#P~~", 11)],
topRules: {"Program":[0,26]},
specialized: [{term: 20, get: (value: any, stack: any) => (specializeKeyword(value, stack) << 1), external: specializeKeyword},{term: 20, get: (value: keyof typeof spec_Identifier) => spec_Identifier[value] || -1}],
tokenPrec: 1634
})

View File

@ -1,275 +0,0 @@
import { SyntaxNode } from './node'
/**
* Parse string contents into fragments, interpolations, and escape sequences.
*
* Input: full string including quotes, e.g. "'hello $name'"
* Output: SyntaxNode tree with StringFragment, Interpolation, EscapeSeq children
*/
export const parseString = (input: string, from: number, to: number, parser: any): SyntaxNode => {
const stringNode = new SyntaxNode('String', from, to)
const content = input.slice(from, to)
// Determine string type
const firstChar = content[0]
// Double-quoted strings: no interpolation or escapes
if (firstChar === '"') {
const fragment = new SyntaxNode('DoubleQuote', from, to)
stringNode.add(fragment)
return stringNode
}
// Curly strings: interpolation but no escapes
if (firstChar === '{') {
parseCurlyString(stringNode, input, from, to, parser)
return stringNode
}
// Single-quoted strings: interpolation and escapes
if (firstChar === "'") {
parseSingleQuoteString(stringNode, input, from, to, parser)
return stringNode
}
throw `Unknown string type starting with: ${firstChar}`
}
/**
* Parse single-quoted string: 'hello $name\n'
* Supports: interpolation ($var, $(expr)), escape sequences (\n, \$, etc)
*/
const parseSingleQuoteString = (
stringNode: SyntaxNode,
input: string,
from: number,
to: number,
parser: any,
) => {
let pos = from + 1 // Skip opening '
let fragmentStart = pos
while (pos < to - 1) {
// -1 to skip closing '
const char = input[pos]
// Escape sequence
if (char === '\\' && pos + 1 < to - 1) {
// Push accumulated fragment
if (pos > fragmentStart) {
const frag = new SyntaxNode('StringFragment', fragmentStart, pos)
stringNode.add(frag)
}
// Add escape sequence node
const escNode = new SyntaxNode('EscapeSeq', pos, pos + 2)
stringNode.add(escNode)
pos += 2
fragmentStart = pos
continue
}
// Interpolation
if (char === '$') {
// Push accumulated fragment
if (pos > fragmentStart) {
const frag = new SyntaxNode('StringFragment', fragmentStart, pos)
stringNode.add(frag)
}
pos++ // Skip $
// Parse interpolation content
if (input[pos] === '(') {
// Expression interpolation: $(expr)
const interpStart = pos - 1 // Include the $
const exprResult = parseInterpolationExpr(input, pos, parser)
const interpNode = new SyntaxNode('Interpolation', interpStart, exprResult.endPos)
interpNode.add(exprResult.node)
stringNode.add(interpNode)
pos = exprResult.endPos
} else {
// Variable interpolation: $name
const interpStart = pos - 1
const identEnd = findIdentifierEnd(input, pos, to - 1)
const identNode = new SyntaxNode('FunctionCallOrIdentifier', pos, identEnd)
const innerIdent = new SyntaxNode('Identifier', pos, identEnd)
identNode.add(innerIdent)
const interpNode = new SyntaxNode('Interpolation', interpStart, identEnd)
interpNode.add(identNode)
stringNode.add(interpNode)
pos = identEnd
}
fragmentStart = pos
continue
}
pos++
}
// Push final fragment
if (pos > fragmentStart && fragmentStart < to - 1) {
const frag = new SyntaxNode('StringFragment', fragmentStart, pos)
stringNode.add(frag)
}
}
/**
* Parse curly string: { hello $name }
* Supports: interpolation ($var, $(expr)), nested braces
* Does NOT support: escape sequences (raw content)
*/
const parseCurlyString = (
stringNode: SyntaxNode,
input: string,
from: number,
to: number,
parser: any,
) => {
let pos = from + 1 // Skip opening {
let fragmentStart = from // Include the opening { in the fragment
let depth = 1
while (pos < to && depth > 0) {
const char = input[pos]
// Track brace nesting
if (char === '{') {
depth++
pos++
continue
}
if (char === '}') {
depth--
if (depth === 0) {
// Push final fragment including closing }
const frag = new SyntaxNode('CurlyString', fragmentStart, pos + 1)
stringNode.add(frag)
break
}
pos++
continue
}
// Interpolation
if (char === '$') {
// Push accumulated fragment
if (pos > fragmentStart) {
const frag = new SyntaxNode('CurlyString', fragmentStart, pos)
stringNode.add(frag)
}
pos++ // Skip $
// Parse interpolation content
if (input[pos] === '(') {
// Expression interpolation: $(expr)
const interpStart = pos - 1
const exprResult = parseInterpolationExpr(input, pos, parser)
const interpNode = new SyntaxNode('Interpolation', interpStart, exprResult.endPos)
interpNode.add(exprResult.node)
stringNode.add(interpNode)
pos = exprResult.endPos
} else {
// Variable interpolation: $name
const interpStart = pos - 1
const identEnd = findIdentifierEnd(input, pos, to)
const identNode = new SyntaxNode('FunctionCallOrIdentifier', pos, identEnd)
const innerIdent = new SyntaxNode('Identifier', pos, identEnd)
identNode.add(innerIdent)
const interpNode = new SyntaxNode('Interpolation', interpStart, identEnd)
interpNode.add(identNode)
stringNode.add(interpNode)
pos = identEnd
}
fragmentStart = pos
continue
}
pos++
}
}
/**
* Parse a parenthesized expression interpolation: $(a + b)
* Returns the parsed expression node and the position after the closing )
* pos is position of the opening ( in the full input string
*/
const parseInterpolationExpr = (
input: string,
pos: number,
parser: any,
): { node: SyntaxNode; endPos: number } => {
// Find matching closing paren
let depth = 1
let start = pos
let end = pos + 1 // Start after opening (
while (end < input.length && depth > 0) {
if (input[end] === '(') depth++
if (input[end] === ')') {
depth--
if (depth === 0) break
}
end++
}
const exprContent = input.slice(start + 1, end) // Content between ( and )
const closeParen = end
end++ // Move past closing )
// Use the main parser to parse the expression
const exprNode = parser.parse(exprContent)
// Get the first real node (skip Program wrapper)
const innerNode = exprNode.firstChild || exprNode
// Adjust node positions: they're relative to exprContent, need to offset to full input
const offset = start + 1 // Position where exprContent starts in full input
adjustNodePositions(innerNode, offset)
// Wrap in ParenExpr - use positions in the full string
const parenNode = new SyntaxNode('ParenExpr', start, closeParen + 1)
parenNode.add(innerNode)
return { node: parenNode, endPos: end }
}
/**
* Recursively adjust all node positions by adding an offset
*/
const adjustNodePositions = (node: SyntaxNode, offset: number) => {
node.from += offset
node.to += offset
for (const child of node.children) {
adjustNodePositions(child, offset)
}
}
/**
* Find the end position of an identifier starting at pos
* Identifiers: lowercase letter or emoji, followed by letters/digits/dashes/emoji
*/
const findIdentifierEnd = (input: string, pos: number, maxPos: number): number => {
let end = pos
while (end < maxPos) {
const char = input[end]!
// Stop at non-identifier characters
if (!/[a-z0-9\-?]/.test(char)) {
break
}
end++
}
return end
}

View File

@ -1,5 +1,7 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('null', () => { describe('null', () => {
test('parses null', () => { test('parses null', () => {
expect('null').toMatchTree(`Null null`) expect('null').toMatchTree(`Null null`)
@ -366,138 +368,6 @@ describe('Parentheses', () => {
PositionalArg PositionalArg
Number 3`) Number 3`)
}) })
test('function call with named args on multiple lines in parens', () => {
expect(`(tail
arg1=true
arg2=30
)`).toMatchTree(`
ParenExpr
FunctionCall
Identifier tail
NamedArg
NamedArgPrefix arg1=
Boolean true
NamedArg
NamedArgPrefix arg2=
Number 30
`)
expect(`(
tail
arg1=true
arg2=30
)`).toMatchTree(`
ParenExpr
FunctionCall
Identifier tail
NamedArg
NamedArgPrefix arg1=
Boolean true
NamedArg
NamedArgPrefix arg2=
Number 30
`)
})
test('binop with newlines in parens', () => {
expect(`(
1 + 2
)`).toMatchTree(`
ParenExpr
BinOp
Number 1
Plus +
Number 2`)
})
test('comparison with newlines in parens', () => {
expect(`(
1 < 2
)`).toMatchTree(`
ParenExpr
ConditionalOp
Number 1
Lt <
Number 2`)
})
test('function call with multiple identifiers on separate lines in parens', () => {
expect(`(echo
arg1
arg2
arg3
)`).toMatchTree(`
ParenExpr
FunctionCall
Identifier echo
PositionalArg
Identifier arg1
PositionalArg
Identifier arg2
PositionalArg
Identifier arg3`)
})
test('function call with mulitline identifiers starting separate lines in parens', () => {
expect(`(
echo
arg1
arg2
arg3
)`).toMatchTree(`
ParenExpr
FunctionCall
Identifier echo
PositionalArg
Identifier arg1
PositionalArg
Identifier arg2
PositionalArg
Identifier arg3`)
})
})
describe('Number literals', () => {
test('allows underscores in integer literals', () => {
expect('10_000').toMatchTree(`Number 10_000`)
expect('1_000_000').toMatchTree(`Number 1_000_000`)
expect('100_000').toMatchTree(`Number 100_000`)
})
test('allows underscores in decimal literals', () => {
expect('3.14_159').toMatchTree(`Number 3.14_159`)
expect('1_000.50').toMatchTree(`Number 1_000.50`)
expect('0.000_001').toMatchTree(`Number 0.000_001`)
})
test('allows underscores in negative numbers', () => {
expect('-10_000').toMatchTree(`Number -10_000`)
expect('-3.14_159').toMatchTree(`Number -3.14_159`)
})
test('allows underscores in positive numbers with explicit sign', () => {
expect('+10_000').toMatchTree(`Number +10_000`)
expect('+3.14_159').toMatchTree(`Number +3.14_159`)
})
test('works in expressions', () => {
expect('1_000 + 2_000').toMatchTree(`
BinOp
Number 1_000
Plus +
Number 2_000`)
})
test('works in function calls', () => {
expect('echo 10_000').toMatchTree(`
FunctionCall
Identifier echo
PositionalArg
Number 10_000`)
})
}) })
describe('BinOp', () => { describe('BinOp', () => {
@ -725,86 +595,43 @@ describe('CompoundAssign', () => {
PositionalArg PositionalArg
Number 3`) Number 3`)
}) })
test('parses ??= operator', () => {
expect('x ??= 5').toMatchTree(`
CompoundAssign
AssignableIdentifier x
NullishEq ??=
Number 5`)
})
test('parses ??= with expression', () => {
expect('config ??= get-default').toMatchTree(`
CompoundAssign
AssignableIdentifier config
NullishEq ??=
FunctionCallOrIdentifier
Identifier get-default`)
})
}) })
describe('Nullish coalescing operator', () => { describe('DotGet whitespace sensitivity', () => {
test('? can still end an identifier', () => { test('no whitespace - DotGet works when identifier in scope', () => {
expect('what?').toMatchTree(` expect('basename = 5; basename.prop').toMatchTree(`
Assign
AssignableIdentifier basename
Eq =
Number 5
FunctionCallOrIdentifier FunctionCallOrIdentifier
Identifier what?`) DotGet
IdentifierBeforeDot basename
Identifier prop`)
}) })
test('?? can still end an identifier', () => { test('space before dot - NOT DotGet, parses as division', () => {
expect('what??').toMatchTree(` expect('basename = 5; basename / prop').toMatchTree(`
FunctionCallOrIdentifier Assign
Identifier what??`) AssignableIdentifier basename
Eq =
Number 5
BinOp
Identifier basename
Slash /
Identifier prop`)
}) })
test('?? can still be in a word', () => { test('dot followed by slash is Word, not DotGet', () => {
expect('what??the').toMatchTree(` expect('basename ./cool').toMatchTree(`
FunctionCallOrIdentifier FunctionCall
Identifier what??the`) Identifier basename
PositionalArg
Word ./cool`)
}) })
test('?? can still start a word', () => { test('identifier not in scope with dot becomes Word', () => {
expect('??what??the').toMatchTree(` expect('readme.txt').toMatchTree(`Word readme.txt`)
Word ??what??the`)
})
test('parses ?? operator', () => {
expect('x ?? 5').toMatchTree(`
ConditionalOp
Identifier x
NullishCoalesce ??
Number 5`)
})
test('parses chained ?? operators', () => {
expect('a ?? b ?? c').toMatchTree(`
ConditionalOp
ConditionalOp
Identifier a
NullishCoalesce ??
Identifier b
NullishCoalesce ??
Identifier c`)
})
test('parses ?? with expressions', () => {
expect('get-value ?? default-value').toMatchTree(`
ConditionalOp
Identifier get-value
NullishCoalesce ??
Identifier default-value`)
})
test('parses ?? with parenthesized function call', () => {
expect('get-value ?? (default 10)').toMatchTree(`
ConditionalOp
Identifier get-value
NullishCoalesce ??
ParenExpr
FunctionCall
Identifier default
PositionalArg
Number 10`)
}) })
}) })
@ -812,7 +639,7 @@ describe('Comments', () => {
test('are greedy', () => { test('are greedy', () => {
expect(` expect(`
x = 5 # one banana x = 5 # one banana
y = 2 #two bananas`).toMatchTree(` y = 2 # two bananas`).toMatchTree(`
Assign Assign
AssignableIdentifier x AssignableIdentifier x
Eq = Eq =
@ -822,7 +649,7 @@ y = 2 #two bananas`).toMatchTree(`
AssignableIdentifier y AssignableIdentifier y
Eq = Eq =
Number 2 Number 2
Comment #two bananas`) Comment # two bananas`)
expect(` expect(`
# some comment # some comment
@ -843,11 +670,11 @@ basename = 5 # very astute
}) })
test('words with # are not considered comments', () => { test('words with # are not considered comments', () => {
expect('find my#hashtag-file.txt').toMatchTree(` expect('find #hashtag-file.txt').toMatchTree(`
FunctionCall FunctionCall
Identifier find Identifier find
PositionalArg PositionalArg
Word my#hashtag-file.txt`) Word #hashtag-file.txt`)
}) })
test('hastags in strings are not comments', () => { test('hastags in strings are not comments', () => {
@ -857,6 +684,61 @@ basename = 5 # very astute
}) })
}) })
describe('Array destructuring', () => {
test('parses array pattern with two variables', () => {
expect('[ a b ] = [ 1 2 3 4]').toMatchTree(`
Assign
Array
Identifier a
Identifier b
Eq =
Array
Number 1
Number 2
Number 3
Number 4`)
})
test('parses array pattern with one variable', () => {
expect('[ x ] = [ 42 ]').toMatchTree(`
Assign
Array
Identifier x
Eq =
Array
Number 42`)
})
test('parses array pattern with emoji identifiers', () => {
expect('[ 🚀 💎 ] = [ 1 2 ]').toMatchTree(`
Assign
Array
Identifier 🚀
Identifier 💎
Eq =
Array
Number 1
Number 2`)
})
test('works with dotget', () => {
expect('[ a ] = [ [1 2 3] ]; a.1').toMatchTree(`
Assign
Array
Identifier a
Eq =
Array
Array
Number 1
Number 2
Number 3
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot a
Number 1`)
})
})
describe('Conditional ops', () => { describe('Conditional ops', () => {
test('or can be chained', () => { test('or can be chained', () => {
expect(` expect(`

View File

@ -1,70 +0,0 @@
import { expect, describe, test } from 'bun:test'
describe('bitwise operators - grammar', () => {
test('parses band (bitwise AND)', () => {
expect('5 band 3').toMatchTree(`
BinOp
Number 5
Band band
Number 3`)
})
test('parses bor (bitwise OR)', () => {
expect('5 bor 3').toMatchTree(`
BinOp
Number 5
Bor bor
Number 3`)
})
test('parses bxor (bitwise XOR)', () => {
expect('5 bxor 3').toMatchTree(`
BinOp
Number 5
Bxor bxor
Number 3`)
})
test('parses << (left shift)', () => {
expect('5 << 2').toMatchTree(`
BinOp
Number 5
Shl <<
Number 2`)
})
test('parses >> (signed right shift)', () => {
expect('20 >> 2').toMatchTree(`
BinOp
Number 20
Shr >>
Number 2`)
})
test('parses >>> (unsigned right shift)', () => {
expect('-1 >>> 1').toMatchTree(`
BinOp
Number -1
Ushr >>>
Number 1`)
})
test('parses bnot (bitwise NOT) as function call', () => {
expect('bnot 5').toMatchTree(`
FunctionCall
Identifier bnot
PositionalArg
Number 5`)
})
test('bitwise operators work in expressions', () => {
expect('x = 5 band 3').toMatchTree(`
Assign
AssignableIdentifier x
Eq =
BinOp
Number 5
Band band
Number 3`)
})
})

View File

@ -1,5 +1,7 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('if/else if/else', () => { describe('if/else if/else', () => {
test('parses single line if', () => { test('parses single line if', () => {
expect(`if y == 1: 'cool' end`).toMatchTree(` expect(`if y == 1: 'cool' end`).toMatchTree(`
@ -22,8 +24,7 @@ describe('if/else if/else', () => {
Eq = Eq =
IfExpr IfExpr
keyword if keyword if
FunctionCallOrIdentifier Identifier x
Identifier x
colon : colon :
Block Block
Number 2 Number 2
@ -58,8 +59,7 @@ describe('if/else if/else', () => {
end`).toMatchTree(` end`).toMatchTree(`
IfExpr IfExpr
keyword if keyword if
FunctionCallOrIdentifier Identifier with-else
Identifier with-else
colon : colon :
Block Block
FunctionCallOrIdentifier FunctionCallOrIdentifier
@ -82,8 +82,7 @@ describe('if/else if/else', () => {
end`).toMatchTree(` end`).toMatchTree(`
IfExpr IfExpr
keyword if keyword if
FunctionCallOrIdentifier Identifier with-else-if
Identifier with-else-if
colon : colon :
Block Block
FunctionCallOrIdentifier FunctionCallOrIdentifier
@ -91,8 +90,7 @@ describe('if/else if/else', () => {
ElseIfExpr ElseIfExpr
keyword else keyword else
keyword if keyword if
FunctionCallOrIdentifier Identifier another-condition
Identifier another-condition
colon : colon :
Block Block
FunctionCallOrIdentifier FunctionCallOrIdentifier
@ -113,8 +111,7 @@ describe('if/else if/else', () => {
end`).toMatchTree(` end`).toMatchTree(`
IfExpr IfExpr
keyword if keyword if
FunctionCallOrIdentifier Identifier with-else-if-else
Identifier with-else-if-else
colon : colon :
Block Block
FunctionCallOrIdentifier FunctionCallOrIdentifier
@ -122,8 +119,7 @@ describe('if/else if/else', () => {
ElseIfExpr ElseIfExpr
keyword else keyword else
keyword if keyword if
FunctionCallOrIdentifier Identifier another-condition
Identifier another-condition
colon : colon :
Block Block
FunctionCallOrIdentifier FunctionCallOrIdentifier
@ -131,8 +127,7 @@ describe('if/else if/else', () => {
ElseIfExpr ElseIfExpr
keyword else keyword else
keyword if keyword if
FunctionCallOrIdentifier Identifier yet-another-condition
Identifier yet-another-condition
colon : colon :
Block Block
FunctionCallOrIdentifier FunctionCallOrIdentifier
@ -178,7 +173,7 @@ describe('if/else if/else', () => {
`) `)
}) })
test("parses paren'd function calls in if tests", () => { test('parses function calls in if tests', () => {
expect(`if (var? 'abc'): true end`).toMatchTree(` expect(`if (var? 'abc'): true end`).toMatchTree(`
IfExpr IfExpr
keyword if keyword if
@ -195,6 +190,7 @@ describe('if/else if/else', () => {
`) `)
}) })
test('parses function calls in else-if tests', () => { test('parses function calls in else-if tests', () => {
expect(`if false: true else if var? 'abc': true end`).toMatchTree(` expect(`if false: true else if var? 'abc': true end`).toMatchTree(`
IfExpr IfExpr
@ -218,7 +214,7 @@ describe('if/else if/else', () => {
`) `)
}) })
test("parses paren'd function calls in else-if tests", () => { test('parses function calls in else-if tests', () => {
expect(`if false: true else if (var? 'abc'): true end`).toMatchTree(` expect(`if false: true else if (var? 'abc'): true end`).toMatchTree(`
IfExpr IfExpr
keyword if keyword if
@ -285,6 +281,7 @@ describe('while', () => {
keyword end`) keyword end`)
}) })
test('compound expression', () => { test('compound expression', () => {
expect(`while a > 0 and b < 100 and c < 1000: true end`).toMatchTree(` expect(`while a > 0 and b < 100 and c < 1000: true end`).toMatchTree(`
WhileExpr WhileExpr
@ -313,8 +310,8 @@ describe('while', () => {
test('multiline infinite loop', () => { test('multiline infinite loop', () => {
expect(` expect(`
while true: while true:
true true
end`).toMatchTree(` end`).toMatchTree(`
WhileExpr WhileExpr
keyword while keyword while
@ -327,7 +324,7 @@ describe('while', () => {
test('multiline basic expression', () => { test('multiline basic expression', () => {
expect(` expect(`
while a > 0: while a > 0:
true true
end`).toMatchTree(` end`).toMatchTree(`
WhileExpr WhileExpr
@ -342,10 +339,11 @@ describe('while', () => {
keyword end`) keyword end`)
}) })
test('multiline compound expression', () => { test('multiline compound expression', () => {
expect(` expect(`
while a > 0 and b < 100 and c < 1000: while a > 0 and b < 100 and c < 1000:
true true
end`).toMatchTree(` end`).toMatchTree(`
WhileExpr WhileExpr
keyword while keyword while
@ -370,4 +368,4 @@ describe('while', () => {
Boolean true Boolean true
keyword end`) keyword end`)
}) })
}) })

View File

@ -1,56 +0,0 @@
import { expect, describe, test } from 'bun:test'
describe('Array destructuring', () => {
test('parses array pattern with two variables', () => {
expect('[ a b ] = [ 1 2 3 4]').toMatchTree(`
Assign
Array
Identifier a
Identifier b
Eq =
Array
Number 1
Number 2
Number 3
Number 4`)
})
test('parses array pattern with one variable', () => {
expect('[ x ] = [ 42 ]').toMatchTree(`
Assign
Array
Identifier x
Eq =
Array
Number 42`)
})
test('parses array pattern with emoji identifiers', () => {
expect('[ 🚀 💎 ] = [ 1 2 ]').toMatchTree(`
Assign
Array
Identifier 🚀
Identifier 💎
Eq =
Array
Number 1
Number 2`)
})
test('works with dotget', () => {
expect('[ a ] = [ [1 2 3] ]; a.1').toMatchTree(`
Assign
Array
Identifier a
Eq =
Array
Array
Number 1
Number 2
Number 3
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot a
Number 1`)
})
})

View File

@ -1,44 +1,6 @@
import { describe, test, expect } from 'bun:test' import { describe, test, expect } from 'bun:test'
import '../../testSetup' import '../../testSetup'
describe('DotGet whitespace sensitivity', () => {
test('no whitespace - DotGet works when identifier in scope', () => {
expect('basename = 5; basename.prop').toMatchTree(`
Assign
AssignableIdentifier basename
Eq =
Number 5
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot basename
Identifier prop`)
})
test('space before dot - NOT DotGet, parses as division', () => {
expect('basename = 5; basename / prop').toMatchTree(`
Assign
AssignableIdentifier basename
Eq =
Number 5
BinOp
Identifier basename
Slash /
Identifier prop`)
})
test('dot followed by slash is Word, not DotGet', () => {
expect('basename ./cool').toMatchTree(`
FunctionCall
Identifier basename
PositionalArg
Word ./cool`)
})
test('identifier not in scope with dot becomes Word', () => {
expect('readme.txt').toMatchTree(`Word readme.txt`)
})
})
describe('DotGet', () => { describe('DotGet', () => {
test('readme.txt is Word when readme not in scope', () => { test('readme.txt is Word when readme not in scope', () => {
expect('readme.txt').toMatchTree(`Word readme.txt`) expect('readme.txt').toMatchTree(`Word readme.txt`)
@ -237,7 +199,7 @@ end`).toMatchTree(`
`) `)
}) })
test.skip("dot get doesn't work with spaces", () => { test("dot get doesn't work with spaces", () => {
expect('obj . prop').toMatchTree(` expect('obj . prop').toMatchTree(`
FunctionCall FunctionCall
Identifier obj Identifier obj
@ -336,163 +298,4 @@ end`).toMatchTree(`
Number 2 Number 2
`) `)
}) })
// NOTE: these are parsed as DotGet(meta, DotGet(script, name)) because that's easiest,
// but the compiler flattens them
test('chained dot get: meta.script.name', () => {
expect('meta = 42; meta.script.name').toMatchTree(`
Assign
AssignableIdentifier meta
Eq =
Number 42
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot meta
DotGet
IdentifierBeforeDot script
Identifier name
`)
})
test('chained dot get: a.b.c.d', () => {
expect('a = 1; a.b.c.d').toMatchTree(`
Assign
AssignableIdentifier a
Eq =
Number 1
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot a
DotGet
IdentifierBeforeDot b
DotGet
IdentifierBeforeDot c
Identifier d
`)
})
test('chained dot get in function call', () => {
expect('config = 1; echo config.db.host').toMatchTree(`
Assign
AssignableIdentifier config
Eq =
Number 1
FunctionCall
Identifier echo
PositionalArg
DotGet
IdentifierBeforeDot config
DotGet
IdentifierBeforeDot db
Identifier host
`)
})
test('chained dot get with numeric index at end', () => {
expect('obj = 1; obj.items.0').toMatchTree(`
Assign
AssignableIdentifier obj
Eq =
Number 1
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot obj
DotGet
IdentifierBeforeDot items
Number 0
`)
})
test('chained dot get with ParenExpr at end', () => {
expect('obj = 1; obj.items.(i)').toMatchTree(`
Assign
AssignableIdentifier obj
Eq =
Number 1
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot obj
DotGet
IdentifierBeforeDot items
ParenExpr
FunctionCallOrIdentifier
Identifier i
`)
})
test('not in scope remains Word with chained dots', () => {
expect('readme.md.bak').toMatchTree(`Word readme.md.bak`)
})
test('chained dot get in nested functions', () => {
expect(`do cfg:
do inner:
cfg.db.host
end
end`).toMatchTree(`
FunctionDef
Do do
Params
Identifier cfg
colon :
FunctionDef
Do do
Params
Identifier inner
colon :
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot cfg
DotGet
IdentifierBeforeDot db
Identifier host
keyword end
keyword end
`)
})
test('mixed simple and chained dot get', () => {
expect('obj = 1; obj.a; obj.b.c').toMatchTree(`
Assign
AssignableIdentifier obj
Eq =
Number 1
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot obj
Identifier a
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot obj
DotGet
IdentifierBeforeDot b
Identifier c
`)
})
test.skip('chained numeric dot get: row.2.1.b', () => {
expect('row = []; row.2.1').toMatchTree(`
Assign
AssignableIdentifier row
Eq =
Array []
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot row
DotGet
Number 2
DotGet
Number 1
Identifier b
`)
test('parses $.pid just fine', () => {
expect(`$.pid`).toMatchTree(`
FunctionCallOrIdentifier
DotGet
Dollar $
Identifier pid
`)
})
})
}) })

View File

@ -1,5 +1,7 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('try/catch/finally/throw', () => { describe('try/catch/finally/throw', () => {
test('parses try with catch', () => { test('parses try with catch', () => {
expect(`try: expect(`try:
@ -137,24 +139,11 @@ describe('try/catch/finally/throw', () => {
`) `)
}) })
test('parses throw statement with BinOp', () => {
expect("throw 'error message:' + msg").toMatchTree(`
Throw
keyword throw
BinOp
String
StringFragment error message:
Plus +
Identifier msg
`)
})
test('parses throw statement with identifier', () => { test('parses throw statement with identifier', () => {
expect('throw error-object').toMatchTree(` expect('throw error-object').toMatchTree(`
Throw Throw
keyword throw keyword throw
FunctionCallOrIdentifier Identifier error-object
Identifier error-object
`) `)
}) })

View File

@ -1,5 +1,7 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('single line function blocks', () => { describe('single line function blocks', () => {
test('work with no args', () => { test('work with no args', () => {
expect(`trap: echo bye bye end`).toMatchTree(` expect(`trap: echo bye bye end`).toMatchTree(`
@ -14,7 +16,8 @@ describe('single line function blocks', () => {
Identifier bye Identifier bye
PositionalArg PositionalArg
Identifier bye Identifier bye
keyword end`) keyword end`
)
}) })
test('work with one arg', () => { test('work with one arg', () => {
@ -32,7 +35,8 @@ describe('single line function blocks', () => {
Identifier bye Identifier bye
PositionalArg PositionalArg
Identifier bye Identifier bye
keyword end`) keyword end`
)
}) })
test('work with named args', () => { test('work with named args', () => {
@ -52,9 +56,11 @@ describe('single line function blocks', () => {
Identifier bye Identifier bye
PositionalArg PositionalArg
Identifier bye Identifier bye
keyword end`) keyword end`
)
}) })
test('work with dot-get', () => { test('work with dot-get', () => {
expect(`signals = [=]; signals.trap 'EXIT': echo bye bye end`).toMatchTree(` expect(`signals = [=]; signals.trap 'EXIT': echo bye bye end`).toMatchTree(`
Assign Assign
@ -77,15 +83,16 @@ describe('single line function blocks', () => {
Identifier bye Identifier bye
PositionalArg PositionalArg
Identifier bye Identifier bye
keyword end`) keyword end`
)
}) })
}) })
describe('multi line function blocks', () => { describe('multi line function blocks', () => {
test('work with no args', () => { test('work with no args', () => {
expect(` expect(`
trap: trap:
echo bye bye echo bye bye
end end
`).toMatchTree(` `).toMatchTree(`
FunctionCallWithBlock FunctionCallWithBlock
@ -99,13 +106,14 @@ end
Identifier bye Identifier bye
PositionalArg PositionalArg
Identifier bye Identifier bye
keyword end`) keyword end`
)
}) })
test('work with one arg', () => { test('work with one arg', () => {
expect(` expect(`
trap EXIT: trap EXIT:
echo bye bye echo bye bye
end`).toMatchTree(` end`).toMatchTree(`
FunctionCallWithBlock FunctionCallWithBlock
FunctionCall FunctionCall
@ -120,13 +128,14 @@ end`).toMatchTree(`
Identifier bye Identifier bye
PositionalArg PositionalArg
Identifier bye Identifier bye
keyword end`) keyword end`
)
}) })
test('work with named args', () => { test('work with named args', () => {
expect(` expect(`
attach signal='exit' code=1: attach signal='exit' code=1:
echo bye bye echo bye bye
end`).toMatchTree(` end`).toMatchTree(`
FunctionCallWithBlock FunctionCallWithBlock
FunctionCall FunctionCall
@ -146,14 +155,16 @@ end`).toMatchTree(`
Identifier bye Identifier bye
PositionalArg PositionalArg
Identifier bye Identifier bye
keyword end`) keyword end`
)
}) })
test('work with dot-get', () => { test('work with dot-get', () => {
expect(` expect(`
signals = [=] signals = [=]
signals.trap 'EXIT': signals.trap 'EXIT':
echo bye bye echo bye bye
end`).toMatchTree(` end`).toMatchTree(`
Assign Assign
AssignableIdentifier signals AssignableIdentifier signals
@ -175,7 +186,8 @@ end`).toMatchTree(`
Identifier bye Identifier bye
PositionalArg PositionalArg
Identifier bye Identifier bye
keyword end`) keyword end`
)
}) })
}) })
@ -252,7 +264,8 @@ end`).toMatchTree(`
p: p:
h1 class=bright style='font-family: helvetica' Heya h1 class=bright style='font-family: helvetica' Heya
h2 man that is (b wild)! h2 man that is (b wild)!
end`).toMatchTree(` end`)
.toMatchTree(`
FunctionCallWithBlock FunctionCallWithBlock
FunctionCallOrIdentifier FunctionCallOrIdentifier
Identifier p Identifier p
@ -287,4 +300,4 @@ end`).toMatchTree(`
Word ! Word !
keyword end`) keyword end`)
}) })
}) })

View File

@ -1,5 +1,7 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('calling functions', () => { describe('calling functions', () => {
test('call with no args', () => { test('call with no args', () => {
expect('tail').toMatchTree(` expect('tail').toMatchTree(`
@ -41,57 +43,6 @@ describe('calling functions', () => {
`) `)
}) })
test('call with function', () => {
expect(`tail do x: x end`).toMatchTree(`
FunctionCall
Identifier tail
PositionalArg
FunctionDef
Do do
Params
Identifier x
colon :
FunctionCallOrIdentifier
Identifier x
keyword end
`)
})
test('call with arg and function', () => {
expect(`tail true do x: x end`).toMatchTree(`
FunctionCall
Identifier tail
PositionalArg
Boolean true
PositionalArg
FunctionDef
Do do
Params
Identifier x
colon :
FunctionCallOrIdentifier
Identifier x
keyword end
`)
})
test('call with function in named arg', () => {
expect(`tail callback=do x: x end`).toMatchTree(`
FunctionCall
Identifier tail
NamedArg
NamedArgPrefix callback=
FunctionDef
Do do
Params
Identifier x
colon :
FunctionCallOrIdentifier
Identifier x
keyword end
`)
})
test('command with arg that is also a command', () => { test('command with arg that is also a command', () => {
expect('tail tail').toMatchTree(` expect('tail tail').toMatchTree(`
FunctionCall FunctionCall
@ -112,8 +63,8 @@ describe('calling functions', () => {
Identifier tail Identifier tail
NamedArg NamedArg
NamedArgPrefix lines= NamedArgPrefix lines=
`) `)
}) })
}) })
@ -122,7 +73,7 @@ describe('Do', () => {
expect('do: 1 end').toMatchTree(` expect('do: 1 end').toMatchTree(`
FunctionDef FunctionDef
Do do Do do
Params Params
colon : colon :
Number 1 Number 1
keyword end`) keyword end`)
@ -280,4 +231,4 @@ describe('default params', () => {
Identifier y Identifier y
keyword end`) keyword end`)
}) })
}) })

View File

@ -1,32 +0,0 @@
import { expect, describe, test } from 'bun:test'
describe('import', () => {
test('parses single import', () => {
expect(`import str`).toMatchTree(`
Import
keyword import
Identifier str
`)
})
test('parses multiple imports', () => {
expect(`import str math list`).toMatchTree(`
Import
keyword import
Identifier str
Identifier math
Identifier list
`)
})
test('parses named args', () => {
expect(`import str only=ends-with?`).toMatchTree(`
Import
keyword import
Identifier str
NamedArg
NamedArgPrefix only=
Identifier ends-with?
`)
})
})

View File

@ -1,86 +1,6 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
describe('number literals', () => { import '../shrimp.grammar' // Importing this so changes cause it to retest!
test('binary numbers', () => {
expect('0b110').toMatchTree(`
Number 0b110
`)
})
test('hex numbers', () => {
expect('0xdeadbeef').toMatchTree(`
Number 0xdeadbeef
`)
})
test('hex numbers uppercase', () => {
expect('0xFF').toMatchTree(`
Number 0xFF
`)
})
test('octal numbers', () => {
expect('0o644').toMatchTree(`
Number 0o644
`)
expect('0o055').toMatchTree(`
Number 0o055
`)
})
test('decimal numbers still work', () => {
expect('42').toMatchTree(`
Number 42
`)
})
test('negative binary', () => {
expect('-0b110').toMatchTree(`
Number -0b110
`)
})
test('negative hex', () => {
expect('-0xFF').toMatchTree(`
Number -0xFF
`)
})
test('negative octal', () => {
expect('-0o755').toMatchTree(`
Number -0o755
`)
})
test('positive prefix binary', () => {
expect('+0b110').toMatchTree(`
Number +0b110
`)
})
test('positive prefix hex', () => {
expect('+0xFF').toMatchTree(`
Number +0xFF
`)
})
test('positive prefix octal', () => {
expect('+0o644').toMatchTree(`
Number +0o644
`)
})
test('hex, binary, and octal in arrays', () => {
expect('[0xFF 0b110 0o644 42]').toMatchTree(`
Array
Number 0xFF
Number 0b110
Number 0o644
Number 42
`)
})
})
describe('array literals', () => { describe('array literals', () => {
test('work with numbers', () => { test('work with numbers', () => {
@ -334,22 +254,6 @@ describe('dict literals', () => {
`) `)
}) })
test('work with functions', () => {
expect(`[trap=do x: x end]`).toMatchTree(`
Dict
NamedArg
NamedArgPrefix trap=
FunctionDef
Do do
Params
Identifier x
colon :
FunctionCallOrIdentifier
Identifier x
keyword end
`)
})
test('can be nested', () => { test('can be nested', () => {
expect('[a=one b=[two [c=three]]]').toMatchTree(` expect('[a=one b=[two [c=three]]]').toMatchTree(`
Dict Dict
@ -385,35 +289,14 @@ describe('dict literals', () => {
Number 3 Number 3
`) `)
}) })
test('can have spaces between equals', () => {
expect(`[
a = 1
b = 2
c = 3
]`).toMatchTree(`
Dict
NamedArg
NamedArgPrefix a =
Number 1
NamedArg
NamedArgPrefix b =
Number 2
NamedArg
NamedArgPrefix c =
Number 3
`)
})
test('empty dict', () => { test('empty dict', () => {
expect('[=]').toMatchTree(` expect('[=]').toMatchTree(`
Dict [=] Dict [=]
`) `)
})
test('empty dict w whitespace', () => {
expect('[ = ]').toMatchTree(` expect('[ = ]').toMatchTree(`
Dict [ = ] Array
Word =
`) `)
}) })

View File

@ -1,5 +1,7 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('multiline', () => { describe('multiline', () => {
test('parses multiline strings', () => { test('parses multiline strings', () => {
expect(`'first'\n'second'`).toMatchTree(` expect(`'first'\n'second'`).toMatchTree(`
@ -74,12 +76,12 @@ end
expect(` expect(`
do: do:
2 2
end end
`).toMatchTree(` `).toMatchTree(`
FunctionDef FunctionDef
Do do Do do
Params Params
colon : colon :
Number 2 Number 2
keyword end keyword end

View File

@ -1,5 +1,7 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('pipe expressions', () => { describe('pipe expressions', () => {
test('simple pipe expression', () => { test('simple pipe expression', () => {
expect('echo hello | grep h').toMatchTree(` expect('echo hello | grep h').toMatchTree(`
@ -173,235 +175,4 @@ describe('pipe expressions', () => {
Identifier echo Identifier echo
`) `)
}) })
test('parenthesized expressions can be piped', () => {
expect(`(1 + 2) | echo`).toMatchTree(`
PipeExpr
ParenExpr
BinOp
Number 1
Plus +
Number 2
operator |
FunctionCallOrIdentifier
Identifier echo
`)
})
test('complex parenthesized expressions with pipes', () => {
expect(`((math.random) * 10 + 1) | math.floor`).toMatchTree(`
PipeExpr
ParenExpr
BinOp
BinOp
ParenExpr
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot math
Identifier random
Star *
Number 10
Plus +
Number 1
operator |
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot math
Identifier floor
`)
})
})
describe('pipe continuation', () => {
test('pipe on next line', () => {
expect(`hello
| echo`).toMatchTree(`
PipeExpr
FunctionCallOrIdentifier
Identifier hello
operator |
FunctionCallOrIdentifier
Identifier echo
`)
expect(`echo hello
| grep h`).toMatchTree(`
PipeExpr
FunctionCall
Identifier echo
PositionalArg
Identifier hello
operator |
FunctionCall
Identifier grep
PositionalArg
Identifier h
`)
})
test('pipe on next non-empty line', () => {
expect(`hello
| echo`).toMatchTree(`
PipeExpr
FunctionCallOrIdentifier
Identifier hello
operator |
FunctionCallOrIdentifier
Identifier echo
`)
})
test('multi-line pipe chain', () => {
expect(`echo hello
| grep h
| sort`).toMatchTree(`
PipeExpr
FunctionCall
Identifier echo
PositionalArg
Identifier hello
operator |
FunctionCall
Identifier grep
PositionalArg
Identifier h
operator |
FunctionCallOrIdentifier
Identifier sort
`)
})
test('pipe with indentation', () => {
expect(`echo hello
| grep h
| sort`).toMatchTree(`
PipeExpr
FunctionCall
Identifier echo
PositionalArg
Identifier hello
operator |
FunctionCall
Identifier grep
PositionalArg
Identifier h
operator |
FunctionCallOrIdentifier
Identifier sort
`)
})
test('pipe after operand on next line (trailing pipe style)', () => {
expect(`echo hello |
grep h`).toMatchTree(`
PipeExpr
FunctionCall
Identifier echo
PositionalArg
Identifier hello
operator |
FunctionCall
Identifier grep
PositionalArg
Identifier h
`)
})
test('same-line pipes still work', () => {
expect('echo hello | grep h | sort').toMatchTree(`
PipeExpr
FunctionCall
Identifier echo
PositionalArg
Identifier hello
operator |
FunctionCall
Identifier grep
PositionalArg
Identifier h
operator |
FunctionCallOrIdentifier
Identifier sort
`)
})
test('lots of pipes', () => {
expect(`
'this should help readability in long chains'
| split ' '
| map (ref str.to-upper)
| join '-'
| echo
`).toMatchTree(`
PipeExpr
String
StringFragment this should help readability in long chains
operator |
FunctionCall
Identifier split
PositionalArg
String
StringFragment (space)
operator |
FunctionCall
Identifier map
PositionalArg
ParenExpr
FunctionCall
Identifier ref
PositionalArg
DotGet
IdentifierBeforeDot str
Identifier to-upper
operator |
FunctionCall
Identifier join
PositionalArg
String
StringFragment -
operator |
FunctionCallOrIdentifier
Identifier echo
`)
})
})
describe('Underscore', () => {
test('works in pipes', () => {
expect(`sub 3 1 | div (sub 110 9 | sub 1) _ | div 5`).toMatchTree(`
PipeExpr
FunctionCall
Identifier sub
PositionalArg
Number 3
PositionalArg
Number 1
operator |
FunctionCall
Identifier div
PositionalArg
ParenExpr
PipeExpr
FunctionCall
Identifier sub
PositionalArg
Number 110
PositionalArg
Number 9
operator |
FunctionCall
Identifier sub
PositionalArg
Number 1
PositionalArg
Underscore _
operator |
FunctionCall
Identifier div
PositionalArg
Number 5
`)
})
}) })

View File

@ -1,13 +1,14 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('string interpolation', () => { describe('string interpolation', () => {
test('string with variable interpolation', () => { test('string with variable interpolation', () => {
expect("'hello $name'").toMatchTree(` expect("'hello $name'").toMatchTree(`
String String
StringFragment ${'hello '} StringFragment ${'hello '}
Interpolation Interpolation
FunctionCallOrIdentifier Identifier name
Identifier name
`) `)
}) })
@ -43,8 +44,7 @@ describe('string interpolation', () => {
String String
StringFragment x/ StringFragment x/
Interpolation Interpolation
FunctionCallOrIdentifier Identifier y
Identifier y
StringFragment /z StringFragment /z
`) `)
}) })
@ -122,58 +122,8 @@ describe('string escape sequences', () => {
String String
StringFragment value: StringFragment value:
Interpolation Interpolation
FunctionCallOrIdentifier Identifier x
Identifier x
EscapeSeq \\n EscapeSeq \\n
`) `)
}) })
}) })
describe('curly strings', () => {
test('work on one line', () => {
expect('{ one two three }').toMatchTree(`
String
CurlyString { one two three }
`)
})
test('work on multiple lines', () => {
expect(`{
one
two
three }`).toMatchTree(`
String
CurlyString {
one
two
three }`)
})
test('can contain other curlies', () => {
expect(`{ { one }
two
{ three } }`).toMatchTree(`
String
CurlyString { { one }
two
{ three } }`)
})
})
describe('double quoted strings', () => {
test('work', () => {
expect(`"hello world"`).toMatchTree(`
String
DoubleQuote "hello world"`)
})
test("don't interpolate", () => {
expect(`"hello $world"`).toMatchTree(`
String
DoubleQuote "hello $world"`)
expect(`"hello $(1 + 2)"`).toMatchTree(`
String
DoubleQuote "hello $(1 + 2)"`)
})
})

View File

@ -1,745 +0,0 @@
import { expect, describe, test } from 'bun:test'
describe('constant types', () => {
test('null', () => {
expect(`null`).toBeToken('Null')
})
test('boolean', () => {
expect(`true`).toMatchToken('Boolean', 'true')
expect(`false`).toMatchToken('Boolean', 'false')
})
})
describe('numbers', () => {
test('non-numbers', () => {
expect(`1st`).toMatchToken('Word', '1st')
expect(`1_`).toMatchToken('Word', '1_')
expect(`100.`).toMatchTokens({ type: 'Number', value: '100' }, { type: 'Operator', value: '.' })
})
test('simple numbers', () => {
expect(`1`).toMatchToken('Number', '1')
expect(`200`).toMatchToken('Number', '200')
expect(`5.20`).toMatchToken('Number', '5.20')
expect(`0.20`).toMatchToken('Number', '0.20')
expect(`-20`).toMatchToken('Number', '-20')
expect(`+20`).toMatchToken('Number', '+20')
expect(`-2134.34`).toMatchToken('Number', '-2134.34')
expect(`+20.5325`).toMatchToken('Number', '+20.5325')
expect(`1_000`).toMatchToken('Number', '1_000')
expect(`53_232_220`).toMatchToken('Number', '53_232_220')
})
test('binary numbers', () => {
expect('0b110').toMatchToken('Number', '0b110')
})
test('hex numbers', () => {
expect('0xdeadbeef').toMatchToken('Number', '0xdeadbeef')
expect('0x02d3f4').toMatchToken('Number', '0x02d3f4')
})
test('hex numbers uppercase', () => {
expect('0xFF').toMatchToken('Number', '0xFF')
})
test('octal numbers', () => {
expect('0o644').toMatchToken('Number', '0o644')
expect('0o055').toMatchToken('Number', '0o055')
})
test('negative binary', () => {
expect('-0b110').toMatchToken('Number', '-0b110')
})
test('negative hex', () => {
expect('-0xFF').toMatchToken('Number', '-0xFF')
})
test('negative octal', () => {
expect('-0o755').toMatchToken('Number', '-0o755')
})
test('positive prefix binary', () => {
expect('+0b110').toMatchToken('Number', '+0b110')
})
test('positive prefix hex', () => {
expect('+0xFF').toMatchToken('Number', '+0xFF')
})
test('positive prefix octal', () => {
expect('+0o644').toMatchToken('Number', '+0o644')
})
test('underscores in number', () => {
expect(`1_000`).toMatchToken('Number', '1_000')
expect(`1_0`).toMatchToken('Number', '1_0')
expect('0b11_0').toMatchToken('Number', '0b11_0')
expect('0xdead_beef').toMatchToken('Number', '0xdead_beef')
expect('0o64_4').toMatchToken('Number', '0o64_4')
})
})
describe('identifiers', () => {
test('regular', () => {
expect('name').toBeToken('Identifier')
expect('bobby-mcgee').toBeToken('Identifier')
expect('starts-with?').toBeToken('Identifier')
expect('📢').toMatchToken('Identifier', '📢')
expect(' 📢 ').toMatchToken('Identifier', '📢')
expect(' oink-🐷-oink').toMatchToken('Identifier', 'oink-🐷-oink')
expect('$').toMatchToken('Identifier', '$')
expect('$cool').toMatchToken('Identifier', '$cool')
})
test('one character identifiers', () => {
expect('a').toMatchToken('Identifier', 'a')
expect('z').toMatchToken('Identifier', 'z')
expect('$').toMatchToken('Identifier', '$')
expect('📢').toMatchToken('Identifier', '📢')
expect('?').toBeToken('Word') // ? alone is not valid identifier start
})
test('two character identifiers', () => {
expect('ab').toMatchToken('Identifier', 'ab')
expect('a1').toMatchToken('Identifier', 'a1')
expect('a-').toMatchToken('Identifier', 'a-')
expect('a?').toMatchToken('Identifier', 'a?') // ? valid at end
expect('ab?').toMatchToken('Identifier', 'ab?')
})
test('three+ character identifiers', () => {
expect('abc').toMatchToken('Identifier', 'abc')
expect('a-b').toMatchToken('Identifier', 'a-b')
expect('a1b').toMatchToken('Identifier', 'a1b')
expect('abc?').toMatchToken('Identifier', 'abc?') // ? valid at end
expect('a-b-c?').toMatchToken('Identifier', 'a-b-c?')
})
test('edge cases', () => {
expect('-bobby-mcgee').toBeToken('Word')
expect('starts-with??').toMatchToken('Identifier', 'starts-with??')
expect('starts?with?').toMatchToken('Identifier', 'starts?with?')
expect('a??b').toMatchToken('Identifier', 'a??b')
expect('oink-oink!').toBeToken('Word')
expect('dog#pound').toMatchToken('Word', 'dog#pound')
expect('http://website.com').toMatchToken('Word', 'http://website.com')
expect('school$cool').toMatchToken('Identifier', 'school$cool')
expect('EXIT:').toMatchTokens({ type: 'Word', value: 'EXIT' }, { type: 'Colon' })
expect(`if y == 1: 'cool' end`).toMatchTokens(
{ type: 'Keyword', value: 'if' },
{ type: 'Identifier', value: 'y' },
{ type: 'Operator', value: '==' },
{ type: 'Number', value: '1' },
{ type: 'Colon' },
{ type: 'String', value: `'cool'` },
{ type: 'Keyword', value: 'end' },
)
})
})
describe('paths', () => {
test('starting with ./', () => {
expect('./tmp').toMatchToken('Word', './tmp')
})
test('starting with /', () => {
expect('/home/chris/dev').toMatchToken('Word', '/home/chris/dev')
})
test('identifiers with dots tokenize separately', () => {
expect('readme.txt').toMatchTokens(
{ type: 'Identifier', value: 'readme' },
{ type: 'Operator', value: '.' },
{ type: 'Identifier', value: 'txt' },
)
})
test('words (non-identifiers) consume dots', () => {
expect('README.md').toMatchToken('Word', 'README.md')
})
test('all sorts of weird stuff', () => {
expect('dog#pound').toMatchToken('Word', 'dog#pound')
expect('my/kinda/place').toMatchToken('my/kinda/place')
expect('file://%/$##/@40!/index.php').toMatchToken('Word', 'file://%/$##/@40!/index.php')
})
})
describe('strings', () => {
test('single quoted', () => {
expect(`'hello world'`).toMatchToken('String', `'hello world'`)
expect(`'it\\'s a beautiful world'`).toMatchToken("'it\\'s a beautiful world'")
})
test('double quoted', () => {
expect(`"hello world"`).toMatchToken('String', `"hello world"`)
expect(`"it's a beautiful world"`).toMatchToken('String', `"it's a beautiful world"`)
})
test('empty strings', () => {
expect(`''`).toMatchToken('String', `''`)
expect(`""`).toMatchToken('String', `""`)
})
test('escape sequences', () => {
expect(`'hello\\nworld'`).toMatchToken('String', `'hello\\nworld'`)
expect(`'tab\\there'`).toMatchToken('String', `'tab\\there'`)
expect(`'quote\\''`).toMatchToken('String', `'quote\\''`)
expect(`'backslash\\\\'`).toMatchToken('String', `'backslash\\\\'`)
expect(`'dollar\\$sign'`).toMatchToken('String', `'dollar\\$sign'`)
})
test('unclosed strings - error case', () => {
// These should either fail or produce unexpected results
expect(`'hello`).toMatchToken('String', `'hello`)
expect(`"world`).toMatchToken('String', `"world`)
})
})
describe('curly strings', () => {
test('curly quoted', () => {
expect('{ one two three }').toMatchToken('String', `{ one two three }`)
})
test('work on multiple lines', () => {
expect(`{
one
two
three }`).toMatchToken(
'String',
`{
one
two
three }`,
)
})
test('can contain other curlies', () => {
expect(`{ { one }
two
{ three } }`).toMatchToken(
'String',
`{ { one }
two
{ three } }`,
)
})
test('empty curly string', () => {
expect('{}').toMatchToken('String', '{}')
})
test('unclosed curly string - error case', () => {
// Should either fail or produce unexpected results
expect('{ hello').toMatchToken('String', '{ hello')
expect('{ nested { unclosed }').toMatchToken('String', '{ nested { unclosed }')
})
})
describe('operators', () => {
test('math operators', () => {
// assignment
expect('=').toMatchToken('Operator', '=')
// logic
expect('or').toMatchToken('Operator', 'or')
expect('and').toMatchToken('Operator', 'and')
// bitwise
expect('band').toMatchToken('Operator', 'band')
expect('bor').toMatchToken('Operator', 'bor')
expect('bxor').toMatchToken('Operator', 'bxor')
expect('>>>').toMatchToken('Operator', '>>>')
expect('>>').toMatchToken('Operator', '>>')
expect('<<').toMatchToken('Operator', '<<')
// compound assignment
expect('??=').toMatchToken('Operator', '??=')
expect('+=').toMatchToken('Operator', '+=')
expect('-=').toMatchToken('Operator', '-=')
expect('*=').toMatchToken('Operator', '*=')
expect('/=').toMatchToken('Operator', '/=')
expect('%=').toMatchToken('Operator', '%=')
// nullish
expect('??').toMatchToken('Operator', '??')
// math
expect('**').toMatchToken('Operator', '**')
expect('*').toMatchToken('Operator', '*')
expect('/').toMatchToken('Operator', '/')
expect('+').toMatchToken('Operator', '+')
expect('-').toMatchToken('Operator', '-')
expect('%').toMatchToken('Operator', '%')
// comparison
expect('>=').toMatchToken('Operator', '>=')
expect('<=').toMatchToken('Operator', '<=')
expect('!=').toMatchToken('Operator', '!=')
expect('==').toMatchToken('Operator', '==')
expect('>').toMatchToken('Operator', '>')
expect('<').toMatchToken('Operator', '<')
// property access
expect('.').toMatchToken('Operator', '.')
})
})
describe('keywords', () => {
test('keywords', () => {
expect(`import`).toMatchToken('Keyword', 'import')
expect(`end`).toMatchToken('Keyword', 'end')
expect(`do`).toMatchToken('Keyword', 'do')
expect(`while`).toMatchToken('Keyword', 'while')
expect(`if`).toMatchToken('Keyword', 'if')
expect(`else`).toMatchToken('Keyword', 'else')
expect(`try`).toMatchToken('Keyword', 'try')
expect(`catch`).toMatchToken('Keyword', 'catch')
expect(`finally`).toMatchToken('Keyword', 'finally')
expect(`throw`).toMatchToken('Keyword', 'throw')
expect(`not`).toMatchToken('Keyword', 'not')
})
})
describe('regex', () => {
test('use double slash', () => {
expect(`//[0-9]+//`).toMatchToken('Regex', '//[0-9]+//')
})
})
describe('punctuation', () => {
test('underscore', () => {
expect(`_`).toBeToken('Underscore')
expect(`__`).toMatchToken('Word', '__')
})
test('semicolon', () => {
expect(`;`).toBeToken('Semicolon')
})
test('newline', () => {
expect('\n').toBeToken('Newline')
})
test('colon', () => {
expect(':').toBeToken('Colon')
})
})
describe('comments', () => {
test('comments', () => {
expect(`# hey friends`).toMatchToken('Comment', '# hey friends')
expect(`#hey-friends`).toMatchToken('Comment', '#hey-friends')
})
})
describe('brackets', () => {
test('parens', () => {
expect(`(`).toBeToken('OpenParen')
expect(`)`).toBeToken('CloseParen')
})
test('staples', () => {
expect(`[`).toBeToken('OpenBracket')
expect(`]`).toBeToken('CloseBracket')
})
})
describe('multiple tokens', () => {
test('constants work fine', () => {
expect(`null true false`).toMatchTokens(
{ type: 'Null' },
{ type: 'Boolean', value: 'true' },
{ type: 'Boolean', value: 'false' },
)
})
test('numbers', () => {
expect(`100 -400.42 null`).toMatchTokens(
{ type: 'Number', value: '100' },
{ type: 'Number', value: '-400.42' },
{ type: 'Null' },
)
})
test('whitespace', () => {
expect(`
'hello world'
'goodbye world'
`).toMatchTokens(
{ type: 'Newline' },
{ type: 'String', value: "'hello world'" },
{ type: 'Newline' },
{ type: 'Newline' },
{ type: 'String', value: "'goodbye world'" },
{ type: 'Newline' },
)
})
test('newline in parens is ignored', () => {
expect(`(
'hello world'
'goodbye world'
)`).toMatchTokens(
{ type: 'OpenParen' },
{ type: 'String', value: "'hello world'" },
{ type: 'String', value: "'goodbye world'" },
{ type: 'CloseParen' },
)
})
test('newline in brackets is ignored', () => {
expect(`[
a b
c d
e
f
]`).toMatchTokens(
{ type: 'OpenBracket' },
{ type: 'Identifier', value: 'a' },
{ type: 'Identifier', value: 'b' },
{ type: 'Identifier', value: 'c' },
{ type: 'Identifier', value: 'd' },
{ type: 'Identifier', value: 'e' },
{ type: 'Identifier', value: 'f' },
{ type: 'CloseBracket' },
)
})
test('function call', () => {
expect('echo hello world').toMatchTokens(
{ type: 'Identifier', value: 'echo' },
{ type: 'Identifier', value: 'hello' },
{ type: 'Identifier', value: 'world' },
)
})
test('function call w/ parens', () => {
expect('echo(bold hello world)').toMatchTokens(
{ type: 'Identifier', value: 'echo' },
{ type: 'OpenParen' },
{ type: 'Identifier', value: 'bold' },
{ type: 'Identifier', value: 'hello' },
{ type: 'Identifier', value: 'world' },
{ type: 'CloseParen' },
)
expect('echo (bold hello world)').toMatchTokens(
{ type: 'Identifier', value: 'echo' },
{ type: 'OpenParen' },
{ type: 'Identifier', value: 'bold' },
{ type: 'Identifier', value: 'hello' },
{ type: 'Identifier', value: 'world' },
{ type: 'CloseParen' },
)
})
test('assignment', () => {
expect('x = 5').toMatchTokens(
{ type: 'Identifier', value: 'x' },
{ type: 'Operator', value: '=' },
{ type: 'Number', value: '5' },
)
})
test('math expression', () => {
expect('1 + 2 * 3').toMatchTokens(
{ type: 'Number', value: '1' },
{ type: 'Operator', value: '+' },
{ type: 'Number', value: '2' },
{ type: 'Operator', value: '*' },
{ type: 'Number', value: '3' },
)
})
test('inline comment', () => {
expect('x = 5 # set x').toMatchTokens(
{ type: 'Identifier', value: 'x' },
{ type: 'Operator', value: '=' },
{ type: 'Number', value: '5' },
{ type: 'Comment', value: '# set x' },
)
})
test('line comment', () => {
expect('x = 5 \n# hello\n set x').toMatchTokens(
{ type: 'Identifier', value: 'x' },
{ type: 'Operator', value: '=' },
{ type: 'Number', value: '5' },
{ type: 'Newline' },
{ type: 'Comment', value: '# hello' },
{ type: 'Newline' },
{ type: 'Identifier', value: 'set' },
{ type: 'Identifier', value: 'x' },
)
})
test('colons separate tokens', () => {
expect('x do: y').toMatchTokens(
{ type: 'Identifier', value: 'x' },
{ type: 'Keyword', value: 'do' },
{ type: 'Colon' },
{ type: 'Identifier', value: 'y' },
)
expect('x: y').toMatchTokens(
{ type: 'Identifier', value: 'x' },
{ type: 'Colon' },
{ type: 'Identifier', value: 'y' },
)
expect('5: y').toMatchTokens(
{ type: 'Number', value: '5' },
{ type: 'Colon' },
{ type: 'Identifier', value: 'y' },
)
expect(`if (var? 'abc'): y`).toMatchTokens(
{ type: 'Keyword', value: 'if' },
{ type: 'OpenParen' },
{ type: 'Identifier', value: 'var?' },
{ type: 'String', value: `'abc'` },
{ type: 'CloseParen' },
{ type: 'Colon' },
{ type: 'Identifier', value: 'y' },
)
expect(`
do x:
y
end`).toMatchTokens(
{ type: 'Newline' },
{ type: 'Keyword', value: 'do' },
{ type: 'Identifier', value: 'x' },
{ type: 'Colon' },
{ type: 'Newline' },
{ type: 'Identifier', value: 'y' },
{ type: 'Newline' },
{ type: 'Keyword', value: 'end' },
)
})
test('semicolons separate statements', () => {
expect('x; y').toMatchTokens(
{ type: 'Identifier', value: 'x' },
{ type: 'Semicolon' },
{ type: 'Identifier', value: 'y' },
)
})
test('semicolons in parens', () => {
expect('(x; y)').toMatchTokens(
{ type: 'OpenParen' },
{ type: 'Identifier', value: 'x' },
{ type: 'Semicolon' },
{ type: 'Identifier', value: 'y' },
{ type: 'CloseParen' },
)
})
test('dot operator beginning word with slash', () => {
expect(`(basename ./cool)`).toMatchTokens(
{ type: 'OpenParen' },
{ type: 'Identifier', value: 'basename' },
{ type: 'Word', value: './cool' },
{ type: 'CloseParen' },
)
})
test('dot word after identifier with space', () => {
expect(`expand-path .git`).toMatchTokens(
{ type: 'Identifier', value: 'expand-path' },
{ type: 'Word', value: '.git' },
)
})
test('dot operator after identifier without space', () => {
expect(`config.path`).toMatchTokens(
{ type: 'Identifier', value: 'config' },
{ type: 'Operator', value: '.' },
{ type: 'Identifier', value: 'path' },
)
})
})
describe('nesting edge cases', () => {
test('deeply nested parens', () => {
expect('((nested))').toMatchTokens(
{ type: 'OpenParen' },
{ type: 'OpenParen' },
{ type: 'Identifier', value: 'nested' },
{ type: 'CloseParen' },
{ type: 'CloseParen' },
)
})
test('mixed nesting', () => {
expect('([combo])').toMatchTokens(
{ type: 'OpenParen' },
{ type: 'OpenBracket' },
{ type: 'Identifier', value: 'combo' },
{ type: 'CloseBracket' },
{ type: 'CloseParen' },
)
})
})
describe('invalid numbers that should be words', () => {
test('invalid binary', () => {
expect('0b2').toMatchToken('Word', '0b2')
expect('0b123').toMatchToken('Word', '0b123')
})
test('invalid octal', () => {
expect('0o8').toMatchToken('Word', '0o8')
expect('0o999').toMatchToken('Word', '0o999')
})
test('invalid hex', () => {
expect('0xGGG').toMatchToken('Word', '0xGGG')
expect('0xZZZ').toMatchToken('Word', '0xZZZ')
})
test('multiple decimal points', () => {
expect('1.2.3').toMatchToken('Word', '1.2.3')
})
})
describe('unicode and emoji', () => {
test('greek letters', () => {
expect('αβγ').toMatchToken('Identifier', 'αβγ')
expect('delta-δ').toMatchToken('Identifier', 'delta-δ')
})
test('math symbols', () => {
expect('∑').toMatchToken('Identifier', '∑')
expect('∏').toMatchToken('Identifier', '∏')
})
test('CJK characters', () => {
expect('你好').toMatchToken('Identifier', '你好')
expect('こんにちは').toMatchToken('Identifier', 'こんにちは')
})
})
describe('empty and whitespace input', () => {
test('empty string', () => {
expect('').toMatchTokens()
})
test('only whitespace', () => {
expect(' ').toMatchTokens()
})
test('only tabs', () => {
expect('\t\t\t').toMatchTokens()
})
test('only newlines', () => {
expect('\n\n\n').toMatchTokens({ type: 'Newline' }, { type: 'Newline' }, { type: 'Newline' })
})
})
describe('named args', () => {
test("don't need spaces", () => {
expect(`named=arg`).toMatchTokens(
{ type: 'NamedArgPrefix', value: 'named=' },
{ type: 'Identifier', value: 'arg' },
)
})
test('can have spaces', () => {
expect(`named= arg`).toMatchTokens(
{ type: 'NamedArgPrefix', value: 'named=' },
{ type: 'Identifier', value: 'arg' },
)
})
test('can include numbers', () => {
expect(`named123= arg`).toMatchTokens(
{ type: 'NamedArgPrefix', value: 'named123=' },
{ type: 'Identifier', value: 'arg' },
)
})
})
describe('dot operator', () => {
test('standalone dot', () => {
expect('.').toMatchToken('Operator', '.')
})
test('dot between identifiers tokenizes as separate tokens', () => {
expect('config.path').toMatchTokens(
{ type: 'Identifier', value: 'config' },
{ type: 'Operator', value: '.' },
{ type: 'Identifier', value: 'path' },
)
})
test('dot with number', () => {
expect('array.0').toMatchTokens(
{ type: 'Identifier', value: 'array' },
{ type: 'Operator', value: '.' },
{ type: 'Number', value: '0' },
)
})
test('chained dots', () => {
expect('a.b.c').toMatchTokens(
{ type: 'Identifier', value: 'a' },
{ type: 'Operator', value: '.' },
{ type: 'Identifier', value: 'b' },
{ type: 'Operator', value: '.' },
{ type: 'Identifier', value: 'c' },
)
})
test('identifier-like paths tokenize separately', () => {
expect('readme.txt').toMatchTokens(
{ type: 'Identifier', value: 'readme' },
{ type: 'Operator', value: '.' },
{ type: 'Identifier', value: 'txt' },
)
})
test('word-like paths remain as single token', () => {
expect('./file.txt').toMatchToken('Word', './file.txt')
expect('README.TXT').toMatchToken('Word', 'README.TXT')
})
test('dot with paren expression', () => {
expect('obj.(1 + 2)').toMatchTokens(
{ type: 'Identifier', value: 'obj' },
{ type: 'Operator', value: '.' },
{ type: 'OpenParen' },
{ type: 'Number', value: '1' },
{ type: 'Operator', value: '+' },
{ type: 'Number', value: '2' },
{ type: 'CloseParen' },
)
})
test('chained dot with paren expression', () => {
expect('obj.items.(i)').toMatchTokens(
{ type: 'Identifier', value: 'obj' },
{ type: 'Operator', value: '.' },
{ type: 'Identifier', value: 'items' },
{ type: 'Operator', value: '.' },
{ type: 'OpenParen' },
{ type: 'Identifier', value: 'i' },
{ type: 'CloseParen' },
)
})
})

307
src/parser/tokenizer.ts Normal file
View File

@ -0,0 +1,307 @@
import { ExternalTokenizer, InputStream, Stack } from '@lezer/lr'
import { Identifier, AssignableIdentifier, Word, IdentifierBeforeDot, Do } from './shrimp.terms'
// doobie doobie do (we need the `do` keyword to know when we're defining params)
export function specializeKeyword(ident: string) {
return ident === 'do' ? Do : -1
}
// tell the dotGet searcher about builtin globals
export const globals: string[] = []
export const setGlobals = (newGlobals: string[]) => {
globals.length = 0
globals.push(...newGlobals)
}
// The only chars that can't be words are whitespace, apostrophes, closing parens, and EOF.
export const tokenizer = new ExternalTokenizer(
(input: InputStream, stack: Stack) => {
const ch = getFullCodePoint(input, 0)
if (!isWordChar(ch)) return
// Don't consume things that start with digits - let Number token handle it
if (isDigit(ch)) return
// Don't consume things that start with - or + followed by a digit (negative/positive numbers)
if ((ch === 45 /* - */ || ch === 43) /* + */ && isDigit(input.peek(1))) return
const isValidStart = isLowercaseLetter(ch) || isEmojiOrUnicode(ch)
const canBeWord = stack.canShift(Word)
// Consume all word characters, tracking if it remains a valid identifier
const { pos, isValidIdentifier, stoppedAtDot } = consumeWordToken(
input,
isValidStart,
canBeWord
)
// Check if we should emit IdentifierBeforeDot for property access
if (stoppedAtDot) {
const dotGetToken = checkForDotGet(input, stack, pos)
if (dotGetToken) {
input.advance(pos)
input.acceptToken(dotGetToken)
} else {
// Not in scope - continue consuming the dot as part of the word
const afterDot = consumeRestOfWord(input, pos + 1, canBeWord)
input.advance(afterDot)
input.acceptToken(Word)
}
return
}
// Advance past the token we consumed
input.advance(pos)
// Choose which token to emit
if (isValidIdentifier) {
const token = chooseIdentifierToken(input, stack)
input.acceptToken(token)
} else {
input.acceptToken(Word)
}
},
{ contextual: true }
)
// Build identifier text from input stream, handling surrogate pairs for emoji
const buildIdentifierText = (input: InputStream, length: number): string => {
let text = ''
for (let i = 0; i < length; i++) {
const charCode = input.peek(i)
if (charCode === -1) break
// Handle surrogate pairs for emoji (UTF-16 encoding)
if (charCode >= 0xd800 && charCode <= 0xdbff && i + 1 < length) {
const low = input.peek(i + 1)
if (low >= 0xdc00 && low <= 0xdfff) {
text += String.fromCharCode(charCode, low)
i++ // Skip the low surrogate
continue
}
}
text += String.fromCharCode(charCode)
}
return text
}
// Consume word characters, tracking if it remains a valid identifier
// Returns the position after consuming, whether it's a valid identifier, and if we stopped at a dot
const consumeWordToken = (
input: InputStream,
isValidStart: boolean,
canBeWord: boolean
): { pos: number; isValidIdentifier: boolean; stoppedAtDot: boolean } => {
let pos = getCharSize(getFullCodePoint(input, 0))
let isValidIdentifier = isValidStart
let stoppedAtDot = false
while (true) {
const ch = getFullCodePoint(input, pos)
// Stop at dot if we have a valid identifier (might be property access)
if (ch === 46 /* . */ && isValidIdentifier) {
stoppedAtDot = true
break
}
// Stop if we hit a non-word character
if (!isWordChar(ch)) break
// Context-aware termination: semicolon/colon can end a word if followed by whitespace
// This allows `hello; 2` to parse correctly while `hello;world` stays as one word
if (canBeWord && (ch === 59 /* ; */ || ch === 58) /* : */) {
const nextCh = getFullCodePoint(input, pos + 1)
if (!isWordChar(nextCh)) break
}
// Track identifier validity: must be lowercase, digit, dash, or emoji/unicode
if (
!isLowercaseLetter(ch) &&
!isDigit(ch) &&
ch !== 45 /* - */ &&
ch !== 63 /* ? */ &&
!isEmojiOrUnicode(ch)
) {
if (!canBeWord) break
isValidIdentifier = false
}
pos += getCharSize(ch)
}
return { pos, isValidIdentifier, stoppedAtDot }
}
// Consume the rest of a word after we've decided not to treat a dot as DotGet
// Used when we have "file.txt" - we already consumed "file", now consume ".txt"
const consumeRestOfWord = (input: InputStream, startPos: number, canBeWord: boolean): number => {
let pos = startPos
while (true) {
const ch = getFullCodePoint(input, pos)
// Stop if we hit a non-word character
if (!isWordChar(ch)) break
// Context-aware termination for semicolon/colon
if (canBeWord && (ch === 59 /* ; */ || ch === 58) /* : */) {
const nextCh = getFullCodePoint(input, pos + 1)
if (!isWordChar(nextCh)) break
}
pos += getCharSize(ch)
}
return pos
}
// Check if this identifier is in scope (for property access detection)
// Returns IdentifierBeforeDot token if in scope, null otherwise
const checkForDotGet = (input: InputStream, stack: Stack, pos: number): number | null => {
const identifierText = buildIdentifierText(input, pos)
const context = stack.context as { scope: { has(name: string): boolean } } | undefined
// If identifier is in scope, this is property access (e.g., obj.prop)
// If not in scope, it should be consumed as a Word (e.g., file.txt)
return context?.scope.has(identifierText) || globals.includes(identifierText)
? IdentifierBeforeDot
: null
}
// Decide between AssignableIdentifier and Identifier using grammar state + peek-ahead
const chooseIdentifierToken = (input: InputStream, stack: Stack): number => {
const canAssignable = stack.canShift(AssignableIdentifier)
const canRegular = stack.canShift(Identifier)
// Only one option is valid - use it
if (canAssignable && !canRegular) return AssignableIdentifier
if (canRegular && !canAssignable) return Identifier
// Both possible (ambiguous context) - peek ahead for '=' to disambiguate
// This happens at statement start where both `x = 5` (assign) and `echo x` (call) are valid
let peekPos = 0
while (true) {
const ch = getFullCodePoint(input, peekPos)
if (isWhiteSpace(ch)) {
peekPos += getCharSize(ch)
} else {
break
}
}
const nextCh = getFullCodePoint(input, peekPos)
const nextCh2 = getFullCodePoint(input, peekPos + 1)
// Check for compound assignment operators: +=, -=, *=, /=, %=
if (
[43 /* + */, 45 /* - */, 42 /* * */, 47 /* / */, 37 /* % */].includes(nextCh) &&
nextCh2 === 61 /* = */
) {
// Found compound operator, check if it's followed by whitespace
const charAfterOp = getFullCodePoint(input, peekPos + 2)
if (isWhiteSpace(charAfterOp) || charAfterOp === -1 /* EOF */) {
return AssignableIdentifier
}
}
if (nextCh === 61 /* = */) {
// Found '=', but check if it's followed by whitespace
// If '=' is followed by non-whitespace (like '=cool*'), it won't be tokenized as Eq
// In that case, this should be Identifier (for function call), not AssignableIdentifier
const charAfterEquals = getFullCodePoint(input, peekPos + 1)
if (isWhiteSpace(charAfterEquals) || charAfterEquals === -1 /* EOF */) {
return AssignableIdentifier
}
}
return Identifier
}
// Character classification helpers
const isWhiteSpace = (ch: number): boolean => {
return ch === 32 /* space */ || ch === 9 /* tab */ || ch === 13 /* \r */
}
const isWordChar = (ch: number): boolean => {
return (
!isWhiteSpace(ch) &&
ch !== 10 /* \n */ &&
ch !== 41 /* ) */ &&
ch !== 93 /* ] */ &&
ch !== -1 /* EOF */
)
}
const isLowercaseLetter = (ch: number): boolean => {
return ch >= 97 && ch <= 122 // a-z
}
const isDigit = (ch: number): boolean => {
return ch >= 48 && ch <= 57 // 0-9
}
const getFullCodePoint = (input: InputStream, pos: number): number => {
const ch = input.peek(pos)
// Check if this is a high surrogate (0xD800-0xDBFF)
if (ch >= 0xd800 && ch <= 0xdbff) {
const low = input.peek(pos + 1)
// Check if next is low surrogate (0xDC00-0xDFFF)
if (low >= 0xdc00 && low <= 0xdfff) {
// Combine surrogate pair into full code point
return 0x10000 + ((ch & 0x3ff) << 10) + (low & 0x3ff)
}
}
return ch
}
const isEmojiOrUnicode = (ch: number): boolean => {
return (
// Basic Emoticons
(ch >= 0x1f600 && ch <= 0x1f64f) ||
// Miscellaneous Symbols and Pictographs
(ch >= 0x1f300 && ch <= 0x1f5ff) ||
// Transport and Map Symbols
(ch >= 0x1f680 && ch <= 0x1f6ff) ||
// Regional Indicator Symbols (flags)
(ch >= 0x1f1e6 && ch <= 0x1f1ff) ||
// Miscellaneous Symbols (hearts, stars, weather)
(ch >= 0x2600 && ch <= 0x26ff) ||
// Dingbats (scissors, pencils, etc)
(ch >= 0x2700 && ch <= 0x27bf) ||
// Supplemental Symbols and Pictographs (newer emojis)
(ch >= 0x1f900 && ch <= 0x1f9ff) ||
// Symbols and Pictographs Extended-A (newest emojis)
(ch >= 0x1fa70 && ch <= 0x1faff) ||
// Various Asian Characters with emoji presentation
(ch >= 0x1f018 && ch <= 0x1f270) ||
// Variation Selectors (for emoji presentation)
(ch >= 0xfe00 && ch <= 0xfe0f) ||
// Additional miscellaneous items
(ch >= 0x238c && ch <= 0x2454) ||
// Combining Diacritical Marks for Symbols
(ch >= 0x20d0 && ch <= 0x20ff) ||
// Latin-1 Supplement (includes ², ³, ¹ and other special chars)
(ch >= 0x00a0 && ch <= 0x00ff) ||
// Greek and Coptic (U+0370-U+03FF)
(ch >= 0x0370 && ch <= 0x03ff) ||
// Mathematical Alphanumeric Symbols (U+1D400-U+1D7FF)
(ch >= 0x1d400 && ch <= 0x1d7ff) ||
// Mathematical Operators (U+2200-U+22FF)
(ch >= 0x2200 && ch <= 0x22ff) ||
// Superscripts and Subscripts (U+2070-U+209F)
(ch >= 0x2070 && ch <= 0x209f) ||
// Arrows (U+2190-U+21FF)
(ch >= 0x2190 && ch <= 0x21ff) ||
// Hiragana (U+3040-U+309F)
(ch >= 0x3040 && ch <= 0x309f) ||
// Katakana (U+30A0-U+30FF)
(ch >= 0x30a0 && ch <= 0x30ff) ||
// CJK Unified Ideographs (U+4E00-U+9FFF)
(ch >= 0x4e00 && ch <= 0x9fff)
)
}
const getCharSize = (ch: number) => (ch > 0xffff ? 2 : 1) // emoji takes 2 UTF-16 code units

View File

@ -1,596 +0,0 @@
import { isDebug } from '#utils/utils'
export type Token = {
type: TokenType
value?: string
from: number
to: number
}
export enum TokenType {
Comment,
Keyword,
Operator,
Newline,
Semicolon,
Colon,
Underscore,
OpenParen,
CloseParen,
OpenBracket,
CloseBracket,
Identifier,
Word,
NamedArgPrefix,
Null,
Boolean,
Number,
String,
Regex,
}
const valueTokens = new Set([
TokenType.Comment,
TokenType.Keyword,
TokenType.Operator,
TokenType.Identifier,
TokenType.Word,
TokenType.NamedArgPrefix,
TokenType.Boolean,
TokenType.Number,
TokenType.String,
TokenType.Regex,
TokenType.Underscore,
])
const operators = new Set([
// assignment
'=',
// logic
'or',
'and',
// bitwise
'band',
'bor',
'bxor',
'>>>',
'>>',
'<<',
// compound assignment
'??=',
'+=',
'-=',
'*=',
'/=',
'%=',
// nullish
'??',
// math
'**',
'*',
'/',
'+',
'-',
'%',
// comparison
'>=',
'<=',
'!=',
'==',
'>',
'<',
// property access
'.',
// pipe
'|',
])
const keywords = new Set([
'import',
'end',
'do',
'if',
'while',
'if',
'else',
'try',
'catch',
'finally',
'throw',
'not',
])
// helper
function c(strings: TemplateStringsArray, ...values: any[]) {
return strings.reduce((result, str, i) => result + str + (values[i] ?? ''), '').charCodeAt(0)
}
function s(c: number): string {
return String.fromCharCode(c)
}
export class Scanner {
input = ''
pos = 0
start = 0
char = 0
prev = 0
inParen = 0
inBracket = 0
tokens: Token[] = []
prevIsWhitespace = true
reset() {
this.input = ''
this.pos = 0
this.start = 0
this.char = 0
this.prev = 0
this.tokens.length = 0
this.prevIsWhitespace = true
}
peek(count = 0): number {
return getFullCodePoint(this.input, this.pos + count)
}
next(): number {
this.prevIsWhitespace = isWhitespace(this.char)
this.prev = this.char
this.char = this.peek()
this.pos += getCharSize(this.char)
return this.char
}
push(type: TokenType, from?: number, to?: number) {
from ??= this.start
to ??= this.pos - getCharSize(this.char)
if (to < from) to = from
this.tokens.push(
Object.assign(
{},
{
type,
from,
to,
},
valueTokens.has(type) ? { value: this.input.slice(from, to) } : {}
)
)
if (isDebug()) {
const tok = this.tokens.at(-1)
console.log(`≫ PUSH(${from},${to})`, TokenType[tok?.type || 0], '—', tok?.value)
}
this.start = this.pos
}
pushChar(type: TokenType) {
this.push(type, this.pos - 1, this.pos)
}
// turn shrimp code into shrimp tokens that get fed into the parser
tokenize(input: string): Token[] {
this.reset()
this.input = input
this.next()
while (this.char > 0) {
const char = this.char
if (char === c`#`) {
this.readComment()
continue
}
if (isBracket(char)) {
this.readBracket()
continue
}
if (isStringDelim(char)) {
this.readString(char)
continue
}
if (char === c`{`) {
this.readCurlyString()
continue
}
if (isIdentStart(char)) {
this.readWordOrIdent(true) // true = started with identifier char
continue
}
if (isDigit(char) || ((char === c`-` || char === c`+`) && isDigit(this.peek()))) {
this.readNumber()
continue
}
if (char === c`:`) {
this.pushChar(TokenType.Colon)
this.next()
continue
}
// whitespace-sensitive dot as operator (property access) only after identifier/number
if (char === c`.`) {
if (this.canBeDotGet(this.tokens.at(-1))) {
this.pushChar(TokenType.Operator)
this.next()
continue
}
}
if (char === c`/` && this.peek() === c`/`) {
this.readRegex()
continue
}
if (isWordChar(char)) {
this.readWordOrIdent(false) // false = didn't start with identifier char
continue
}
if (char === c`\n`) {
if (this.inParen === 0 && this.inBracket === 0) this.pushChar(TokenType.Newline)
this.next()
continue
}
if (char === c`;`) {
this.pushChar(TokenType.Semicolon)
this.next()
continue
}
this.next()
}
return this.tokens
}
readComment() {
this.start = this.pos - 1
while (this.char !== c`\n` && this.char > 0) this.next()
this.push(TokenType.Comment)
}
readBracket() {
switch (this.char) {
case c`(`:
this.inParen++
this.pushChar(TokenType.OpenParen)
break
case c`)`:
this.inParen--
this.pushChar(TokenType.CloseParen)
break
case c`[`:
this.inBracket++
this.pushChar(TokenType.OpenBracket)
break
case c`]`:
this.inBracket--
this.pushChar(TokenType.CloseBracket)
break
}
this.next()
}
readString(delim: number) {
this.start = this.pos - 1
this.next() // skip opening delim
while (this.char > 0 && (this.char !== delim || (this.char === delim && this.prev === c`\\`)))
this.next()
this.next() // skip closing delim
this.push(TokenType.String)
}
readCurlyString() {
this.start = this.pos - 1
let depth = 1
this.next()
while (depth > 0 && this.char > 0) {
if (this.char === c`{`) depth++
if (this.char === c`}`) depth--
this.next()
}
this.push(TokenType.String)
}
readWordOrIdent(startedWithIdentChar: boolean) {
this.start = this.pos - getCharSize(this.char)
while (isWordChar(this.char)) {
// stop at colon if followed by whitespace (e.g., 'do x: echo x end')
if (this.char === c`:`) {
const nextCh = this.peek()
if (isWhitespace(nextCh) || nextCh === 0) break
}
// stop at equal sign (named arg) - but only if what we've read so far is an identifier
if (this.char === c`=`) {
const soFar = this.input.slice(this.start, this.pos - getCharSize(this.char))
if (isIdentifer(soFar)) {
this.next()
break
}
}
// stop at dot only if it would create a valid property access
// AND only if we started with an identifier character (not for Words like README.txt)
if (startedWithIdentChar && this.char === c`.`) {
const nextCh = this.peek()
if (isIdentStart(nextCh) || isDigit(nextCh) || nextCh === c`(`) {
const soFar = this.input.slice(this.start, this.pos - getCharSize(this.char))
if (isIdentifer(soFar)) break
}
}
this.next()
}
const word = this.input.slice(this.start, this.pos - getCharSize(this.char))
// classify the token based on what we read
if (word === '_') this.push(TokenType.Underscore)
else if (word === 'null') this.push(TokenType.Null)
else if (word === 'true' || word === 'false') this.push(TokenType.Boolean)
else if (isKeyword(word)) this.push(TokenType.Keyword)
else if (isOperator(word)) this.push(TokenType.Operator)
else if (isIdentifer(word)) this.push(TokenType.Identifier)
else if (word.endsWith('=')) this.push(TokenType.NamedArgPrefix)
else this.push(TokenType.Word)
}
readNumber() {
this.start = this.pos - 1
while (isWordChar(this.char)) {
// stop at dot unless it's part of the number
if (this.char === c`.`) {
const nextCh = this.peek()
if (!isDigit(nextCh)) break
}
// stop at colon
if (this.char === c`:`) {
const nextCh = this.peek()
if (isWhitespace(nextCh) || nextCh === 0) break
}
this.next()
}
const ident = this.input.slice(this.start, this.pos - 1)
this.push(isNumber(ident) ? TokenType.Number : TokenType.Word)
}
readRegex() {
this.start = this.pos - 1
this.next() // skip 2nd /
while (this.char > 0) {
if (this.char === c`/` && this.peek() === c`/`) {
this.next() // skip /
this.next() // skip /
// read regex flags
while (this.char > 0 && isIdentStart(this.char)) this.next()
// validate regex
const to = this.pos - getCharSize(this.char)
const regexText = this.input.slice(this.start, to)
const [_, pattern, flags] = regexText.match(/^\/\/(.*)\/\/([gimsuy]*)$/) || []
if (pattern) {
try {
new RegExp(pattern, flags)
this.push(TokenType.Regex)
break
} catch (e) {
// invalid regex - fall through to Word
}
}
// invalid regex is treated as Word
this.push(TokenType.Word)
break
}
this.next()
}
}
canBeDotGet(lastToken?: Token): boolean {
return (
!this.prevIsWhitespace &&
!!lastToken &&
(lastToken.type === TokenType.Identifier ||
lastToken.type === TokenType.Number ||
lastToken.type === TokenType.CloseParen ||
lastToken.type === TokenType.CloseBracket)
)
}
}
const isNumber = (word: string): boolean => {
// regular number
if (/^[+-]?\d+(_?\d+)*(\.(\d+(_?\d+)*))?$/.test(word)) return true
// binary
if (/^[+-]?0b[01]+(_?[01]+)*(\.[01](_?[01]*))?$/.test(word)) return true
// octal
if (/^[+-]?0o[0-7]+(_?[0-7]+)*(\.[0-7](_?[0-7]*))?$/.test(word)) return true
// hex
if (/^[+-]?0x[0-9a-f]+([0-9a-f]_?[0-9a-f]+)*(\.([0-9a-f]_?[0-9a-f]*))?$/i.test(word)) return true
return false
}
const isIdentifer = (s: string): boolean => {
if (s.length === 0) return false
let pos = 0
const chars = []
while (pos < s.length) {
const out = getFullCodePoint(s, pos)
pos += getCharSize(out)
chars.push(out)
}
if (chars.length === 1) return isIdentStart(chars[0]!)
else if (chars.length === 2) return isIdentStart(chars[0]!) && isIdentEnd(chars[1]!)
else
return (
isIdentStart(chars[0]!) &&
chars.slice(1, chars.length - 1).every(isIdentChar) &&
isIdentEnd(chars.at(-1)!)
)
}
const isStringDelim = (ch: number): boolean => {
return ch === c`'` || ch === c`"`
}
export const isIdentStart = (char: number | string): boolean => {
let ch = typeof char === 'string' ? char.charCodeAt(0) : char
return isLowercaseLetter(ch) || isEmojiOrUnicode(ch) || ch === 36 /* $ */
}
export const isIdentChar = (char: number | string): boolean => {
let ch = typeof char === 'string' ? char.charCodeAt(0) : char
return isIdentStart(ch) || isDigit(ch) || ch === 45 /* - */ || ch === 63 /* ? */
}
const isIdentEnd = (char: number | string): boolean => {
return isIdentChar(char)
}
const isLowercaseLetter = (ch: number): boolean => {
return ch >= 97 && ch <= 122 // a-z
}
const isDigit = (ch: number): boolean => {
return ch >= 48 && ch <= 57 // 0-9
}
const isWhitespace = (ch: number): boolean => {
return (
ch === 32 /* space */ ||
ch === 9 /* tab */ ||
ch === 13 /* \r */ ||
ch === 10 /* \n */ ||
ch === -1 ||
ch === 0
) /* EOF */
}
const isWordChar = (ch: number): boolean => {
return (
!isWhitespace(ch) &&
ch !== 10 /* \n */ &&
ch !== 59 /* ; */ &&
ch !== 40 /* ( */ &&
ch !== 41 /* ) */ &&
ch !== 93 /* ] */ &&
ch !== -1 /* EOF */
)
}
const isOperator = (word: string): boolean => {
return operators.has(word)
}
const isKeyword = (word: string): boolean => {
return keywords.has(word)
}
const isBracket = (char: number): boolean => {
return char === c`(` || char === c`)` || char === c`[` || char === c`]`
}
const getCharSize = (ch: number) => (ch > 0xffff ? 2 : 1) // emoji takes 2 UTF-16 code units
const getFullCodePoint = (input: string, pos: number): number => {
const ch = input[pos]?.charCodeAt(0) || 0
// Check if this is a high surrogate (0xD800-0xDBFF)
if (ch >= 0xd800 && ch <= 0xdbff) {
const low = input[pos + 1]?.charCodeAt(0) || 0
// Check if next is low surrogate (0xDC00-0xDFFF)
if (low >= 0xdc00 && low <= 0xdfff) {
// Combine surrogate pair into full code point
return 0x10000 + ((ch & 0x3ff) << 10) + (low & 0x3ff)
}
}
return ch
}
const isEmojiOrUnicode = (ch: number): boolean => {
return (
// Basic Emoticons
(ch >= 0x1f600 && ch <= 0x1f64f) ||
// Miscellaneous Symbols and Pictographs
(ch >= 0x1f300 && ch <= 0x1f5ff) ||
// Transport and Map Symbols
(ch >= 0x1f680 && ch <= 0x1f6ff) ||
// Regional Indicator Symbols (flags)
(ch >= 0x1f1e6 && ch <= 0x1f1ff) ||
// Miscellaneous Symbols (hearts, stars, weather)
(ch >= 0x2600 && ch <= 0x26ff) ||
// Dingbats (scissors, pencils, etc)
(ch >= 0x2700 && ch <= 0x27bf) ||
// Supplemental Symbols and Pictographs (newer emojis)
(ch >= 0x1f900 && ch <= 0x1f9ff) ||
// Symbols and Pictographs Extended-A (newest emojis)
(ch >= 0x1fa70 && ch <= 0x1faff) ||
// Various Asian Characters with emoji presentation
(ch >= 0x1f018 && ch <= 0x1f270) ||
// Variation Selectors (for emoji presentation)
(ch >= 0xfe00 && ch <= 0xfe0f) ||
// Additional miscellaneous items
(ch >= 0x238c && ch <= 0x2454) ||
// Combining Diacritical Marks for Symbols
(ch >= 0x20d0 && ch <= 0x20ff) ||
// Latin-1 Supplement (includes ², ³, ¹ and other special chars)
(ch >= 0x00a0 && ch <= 0x00ff) ||
// Greek and Coptic (U+0370-U+03FF)
(ch >= 0x0370 && ch <= 0x03ff) ||
// Mathematical Alphanumeric Symbols (U+1D400-U+1D7FF)
(ch >= 0x1d400 && ch <= 0x1d7ff) ||
// Mathematical Operators (U+2200-U+22FF)
(ch >= 0x2200 && ch <= 0x22ff) ||
// Superscripts and Subscripts (U+2070-U+209F)
(ch >= 0x2070 && ch <= 0x209f) ||
// Arrows (U+2190-U+21FF)
(ch >= 0x2190 && ch <= 0x21ff) ||
// Hiragana (U+3040-U+309F)
(ch >= 0x3040 && ch <= 0x309f) ||
// Katakana (U+30A0-U+30FF)
(ch >= 0x30a0 && ch <= 0x30ff) ||
// CJK Unified Ideographs (U+4E00-U+9FFF)
(ch >= 0x4e00 && ch <= 0x9fff)
)
}

View File

@ -1,12 +0,0 @@
export const date = {
now: () => Date.now(),
year: (time: number) => new Date(time).getFullYear(),
month: (time: number) => new Date(time).getMonth(),
date: (time: number) => new Date(time).getDate(),
hour: (time: number) => new Date(time).getHours(),
minute: (time: number) => new Date(time).getMinutes(),
second: (time: number) => new Date(time).getSeconds(),
ms: (time: number) => new Date(time).getMilliseconds(),
new: (year: number, month: number, day: number, hour = 0, minute = 0, second = 0, ms = 0) =>
new Date(year, month, day, hour, minute, second, ms).getTime(),
}

View File

@ -1,13 +1,11 @@
import { type Value, toString } from 'reefvm' import { type Value, toString, toValue } from 'reefvm'
export const dict = { export const dict = {
keys: (dict: Record<string, any>) => Object.keys(dict), keys: (dict: Record<string, any>) => Object.keys(dict),
values: (dict: Record<string, any>) => Object.values(dict), values: (dict: Record<string, any>) => Object.values(dict),
entries: (dict: Record<string, any>) => entries: (dict: Record<string, any>) => Object.entries(dict).map(([k, v]) => ({ key: k, value: v })),
Object.entries(dict).map(([k, v]) => ({ key: k, value: v })),
'has?': (dict: Record<string, any>, key: string) => key in dict, 'has?': (dict: Record<string, any>, key: string) => key in dict,
get: (dict: Record<string, any>, key: string, defaultValue: any = null) => get: (dict: Record<string, any>, key: string, defaultValue: any = null) => dict[key] ?? defaultValue,
dict[key] ?? defaultValue,
set: (dict: Value, key: Value, value: Value) => { set: (dict: Value, key: Value, value: Value) => {
const map = dict.value as Map<string, Value> const map = dict.value as Map<string, Value>
map.set(toString(key), value) map.set(toString(key), value)
@ -32,6 +30,6 @@ export const dict = {
'from-entries': (entries: [string, any][]) => Object.fromEntries(entries), 'from-entries': (entries: [string, any][]) => Object.fromEntries(entries),
} }
// raw functions deal directly in Value types, meaning we can modify collection // raw functions deal directly in Value types, meaning we can modify collection
// careful - they MUST return a Value! // careful - they MUST return a Value!
;(dict.set as any).raw = true ; (dict.set as any).raw = true

View File

@ -1,146 +0,0 @@
import { join, resolve, basename, dirname, extname } from 'path'
import {
readdirSync,
mkdirSync,
rmdirSync,
readFileSync,
writeFileSync,
appendFileSync,
rmSync,
copyFileSync,
statSync,
lstatSync,
chmodSync,
symlinkSync,
readlinkSync,
watch,
} from 'fs'
export const fs = {
// Directory operations
ls: (path: string) => readdirSync(path),
mkdir: (path: string) => mkdirSync(path, { recursive: true }),
rmdir: (path: string) =>
rmdirSync(path === '/' || path === '' ? '/tmp/*' : path, { recursive: true }),
pwd: () => process.cwd(),
cd: (path: string) => process.chdir(path),
// Reading
read: (path: string) => readFileSync(path, 'utf-8'),
cat: (path: string) => {}, // added below
'read-bytes': (path: string) => [...readFileSync(path)],
// Writing
write: (path: string, content: string) => writeFileSync(path, content),
append: (path: string, content: string) => appendFileSync(path, content),
// File operations
delete: (path: string) => rmSync(path),
rm: (path: string) => {}, // added below
copy: (from: string, to: string) => copyFileSync(from, to),
move: (from: string, to: string) => {
fs.copy(from, to)
fs.rm(from)
},
mv: (from: string, to: string) => {}, // added below
// Path operations
basename: (path: string) => basename(path),
dirname: (path: string) => dirname(path),
extname: (path: string) => extname(path),
join: (...paths: string[]) => join(...paths),
resolve: (...paths: string[]) => resolve(...paths),
// File info
stat: (path: string) => {
try {
const stats = statSync(path)
const record = Object.fromEntries(Object.entries(stats))
record['atime'] = record['atimeMs']
record['ctime'] = record['ctimeMs']
record['mtime'] = record['mtimeMs']
delete record['atimeMs']
delete record['ctimeMs']
delete record['mtimeMs']
return record
} catch {
return {}
}
},
'exists?': (path: string) => {
try {
statSync(path)
return true
} catch {
return false
}
},
'file?': (path: string) => {
try {
return statSync(path).isFile()
} catch {
return false
}
},
'dir?': (path: string) => {
try {
return statSync(path).isDirectory()
} catch {
return false
}
},
'symlink?': (path: string) => {
try {
return lstatSync(path).isSymbolicLink()
} catch {
return false
}
},
'exec?': (path: string) => {
try {
const stats = statSync(path)
return !!(stats.mode & 0o111)
} catch {
return false
}
},
size: (path: string) => {
try {
return statSync(path).size
} catch {
return 0
}
},
// Permissions
chmod: (path: string, mode: number | string) => {
const numMode = typeof mode === 'string' ? parseInt(mode, 8) : mode
chmodSync(path, numMode)
},
// Symlinks
symlink: (target: string, path: string) => symlinkSync(target, path),
readlink: (path: string) => readlinkSync(path, 'utf-8'),
// Other
glob: (pattern: string) => {
const dir = pattern.substring(0, pattern.lastIndexOf('/'))
const match = pattern.substring(pattern.lastIndexOf('/') + 1)
if (!match.includes('*')) throw new Error('only * patterns supported')
const ext = match.split('*').pop()!
return readdirSync(dir)
.filter((f) => f.endsWith(ext))
.map((f) => join(dir, f))
},
watch: (path: string, callback: Function) =>
watch(path, (event, filename) => callback(event, filename)),
}
;(fs as any).cat = fs.read
;(fs as any).mv = fs.move
;(fs as any).cp = fs.copy
;(fs as any).rm = fs.delete

View File

@ -1,69 +1,29 @@
// The prelude creates all the builtin Shrimp functions. // The prelude creates all the builtin Shrimp functions.
import { join, resolve } from 'path'
import { import {
type Value, type Value, type VM, toValue,
type VM, extractParamInfo, isWrapped, getOriginalFunction,
toValue,
extractParamInfo,
isWrapped,
getOriginalFunction,
} from 'reefvm' } from 'reefvm'
import { date } from './date'
import { dict } from './dict' import { dict } from './dict'
import { fs } from './fs'
import { json } from './json'
import { load } from './load' import { load } from './load'
import { list } from './list' import { list } from './list'
import { math } from './math' import { math } from './math'
import { str } from './str' import { str } from './str'
import { types } from './types'
import { runningInBrowser } from '#utils/utils'
export const globals: Record<string, any> = { export const globals = {
date,
dict, dict,
fs,
json,
load, load,
list, list,
math, math,
str, str,
// shrimp runtime info
$: runningInBrowser
? {
args: [],
argv: [],
env: {},
pid: 0,
cwd: '',
script: {
name: '',
path: '.',
},
}
: {
args: Bun.argv.slice(3),
argv: Bun.argv.slice(1),
env: process.env,
pid: process.pid,
cwd: process.env.PWD,
script: {
name: Bun.argv[2] || '(shrimp)',
path: resolve(join('.', Bun.argv[2] ?? '')),
},
},
// hello // hello
echo: (...args: any[]) => { echo: (...args: any[]) => {
console.log( console.log(...args.map(a => {
...args.map((a) => { const v = toValue(a)
const v = toValue(a) return ['array', 'dict'].includes(v.type) ? formatValue(v, true) : v.value
return ['array', 'dict'].includes(v.type) ? formatValue(v, true) : v.value }))
})
)
return toValue(null) return toValue(null)
}, },
@ -80,34 +40,22 @@ export const globals: Record<string, any> = {
'var?': function (this: VM, v: string) { 'var?': function (this: VM, v: string) {
return typeof v !== 'string' || this.scope.has(v) return typeof v !== 'string' || this.scope.has(v)
}, },
ref: (fn: Function) => fn,
import: function (this: VM, atNamed: Record<any, string | string[]> = {}, ...idents: string[]) {
const onlyArray = Array.isArray(atNamed.only) ? atNamed.only : [atNamed.only].filter((a) => a)
const only = new Set(onlyArray)
const wantsOnly = only.size > 0
for (const ident of idents) {
const module = this.get(ident)
if (!module) throw new Error(`import: can't find ${ident}`)
if (module.type !== 'dict') throw new Error(`import: can't import ${module.type}`)
for (const [name, value] of module.value.entries()) {
if (value.type === 'dict') throw new Error(`import: can't import dicts in dicts`)
if (wantsOnly && !only.has(name)) continue
this.set(name, value)
}
}
},
// env
exit: (num: number) => process.exit(num ?? 0),
// type predicates // type predicates
'string?': (v: any) => toValue(v).type === 'string',
'number?': (v: any) => toValue(v).type === 'number',
'boolean?': (v: any) => toValue(v).type === 'boolean',
'array?': (v: any) => toValue(v).type === 'array',
'dict?': (v: any) => toValue(v).type === 'dict',
'function?': (v: any) => {
const t = toValue(v).type
return t === 'function' || t === 'native'
},
'null?': (v: any) => toValue(v).type === 'null',
'some?': (v: any) => toValue(v).type !== 'null', 'some?': (v: any) => toValue(v).type !== 'null',
// boolean/logic // boolean/logic
bnot: (n: number) => ~(n | 0), not: (v: any) => !v,
// utilities // utilities
inc: (n: number) => n + 1, inc: (n: number) => n + 1,
@ -118,13 +66,9 @@ export const globals: Record<string, any> = {
length: (v: any) => { length: (v: any) => {
const value = toValue(v) const value = toValue(v)
switch (value.type) { switch (value.type) {
case 'string': case 'string': case 'array': return value.value.length
case 'array': case 'dict': return value.value.size
return value.value.length default: throw new Error(`length: expected string, array, or dict, got ${value.type}`)
case 'dict':
return value.value.size
default:
throw new Error(`length: expected string, array, or dict, got ${value.type}`)
} }
}, },
at: (collection: any, index: number | string) => { at: (collection: any, index: number | string) => {
@ -132,9 +76,7 @@ export const globals: Record<string, any> = {
if (value.type === 'string' || value.type === 'array') { if (value.type === 'string' || value.type === 'array') {
const idx = typeof index === 'number' ? index : parseInt(index as string) const idx = typeof index === 'number' ? index : parseInt(index as string)
if (idx < 0 || idx >= value.value.length) { if (idx < 0 || idx >= value.value.length) {
throw new Error( throw new Error(`at: index ${idx} out of bounds for ${value.type} of length ${value.value.length}`)
`at: index ${idx} out of bounds for ${value.type} of length ${value.value.length}`
)
} }
return value.value[idx] return value.value[idx]
} else if (value.type === 'dict') { } else if (value.type === 'dict') {
@ -161,8 +103,7 @@ export const globals: Record<string, any> = {
'empty?': (v: any) => { 'empty?': (v: any) => {
const value = toValue(v) const value = toValue(v)
switch (value.type) { switch (value.type) {
case 'string': case 'string': case 'array':
case 'array':
return value.value.length === 0 return value.value.length === 0
case 'dict': case 'dict':
return value.value.size === 0 return value.value.size === 0
@ -176,6 +117,7 @@ export const globals: Record<string, any> = {
for (const value of list) await cb(value) for (const value of list) await cb(value)
return list return list
}, },
} }
export const colors = { export const colors = {
@ -188,7 +130,7 @@ export const colors = {
red: '\x1b[31m', red: '\x1b[31m',
blue: '\x1b[34m', blue: '\x1b[34m',
magenta: '\x1b[35m', magenta: '\x1b[35m',
pink: '\x1b[38;2;255;105;180m', pink: '\x1b[38;2;255;105;180m'
} }
export function formatValue(value: Value, inner = false): string { export function formatValue(value: Value, inner = false): string {
@ -202,15 +144,15 @@ export function formatValue(value: Value, inner = false): string {
case 'null': case 'null':
return `${colors.dim}null${colors.reset}` return `${colors.dim}null${colors.reset}`
case 'array': { case 'array': {
const items = value.value.map((x) => formatValue(x, true)).join(' ') const items = value.value.map(x => formatValue(x, true)).join(' ')
return `${colors.blue}[${colors.reset}${items}${colors.blue}]${colors.reset}` return `${colors.blue}[${colors.reset}${items}${colors.blue}]${colors.reset}`
} }
case 'dict': { case 'dict': {
const entries = Array.from(value.value.entries()) const entries = Array.from(value.value.entries())
.reverse() .map(([k, v]) => `${k}${colors.blue}=${colors.reset}${formatValue(v, true)}`)
.map(([k, v]) => `${k.trim()}${colors.blue}=${colors.reset}${formatValue(v, true)}`)
.join(' ') .join(' ')
if (entries.length === 0) return `${colors.blue}[=]${colors.reset}` if (entries.length === 0)
return `${colors.blue}[=]${colors.reset}`
return `${colors.blue}[${colors.reset}${entries}${colors.blue}]${colors.reset}` return `${colors.blue}[${colors.reset}${entries}${colors.blue}]${colors.reset}`
} }
case 'function': { case 'function': {
@ -227,7 +169,4 @@ export function formatValue(value: Value, inner = false): string {
default: default:
return String(value) return String(value)
} }
} }
// add types functions to top-level namespace
for (const [key, value] of Object.entries(types)) globals[key] = value

View File

@ -1,6 +0,0 @@
export const json = {
encode: (s: any) => JSON.stringify(s),
decode: (s: string) => JSON.parse(s),
}
;(json as any).parse = json.decode
;(json as any).stringify = json.encode

View File

@ -1,7 +1,7 @@
import { type Value, toValue, toNull } from 'reefvm' import { type Value, toValue, toNull } from 'reefvm'
export const list = { export const list = {
slice: (list: any[], start: number, end?: number) => list.slice(start, end ? end : undefined), slice: (list: any[], start: number, end?: number) => list.slice(start, end),
map: async (list: any[], cb: Function) => { map: async (list: any[], cb: Function) => {
let acc: any[] = [] let acc: any[] = []
for (const value of list) acc.push(await cb(value)) for (const value of list) acc.push(await cb(value))
@ -14,13 +14,6 @@ export const list = {
} }
return acc return acc
}, },
reject: async (list: any[], cb: Function) => {
let acc: any[] = []
for (const value of list) {
if (!(await cb(value))) acc.push(value)
}
return acc
},
reduce: async (list: any[], cb: Function, initial: any) => { reduce: async (list: any[], cb: Function, initial: any) => {
let acc = initial let acc = initial
for (const value of list) acc = await cb(acc, value) for (const value of list) acc = await cb(acc, value)
@ -36,8 +29,6 @@ export const list = {
// predicates // predicates
'empty?': (list: any[]) => list.length === 0, 'empty?': (list: any[]) => list.length === 0,
'contains?': (list: any[], item: any) => list.includes(item), 'contains?': (list: any[], item: any) => list.includes(item),
'includes?': (list: any[], item: any) => list.includes(item),
'has?': (list: any[], item: any) => list.includes(item),
'any?': async (list: any[], cb: Function) => { 'any?': async (list: any[], cb: Function) => {
for (const value of list) { for (const value of list) {
if (await cb(value)) return true if (await cb(value)) return true
@ -46,7 +37,7 @@ export const list = {
}, },
'all?': async (list: any[], cb: Function) => { 'all?': async (list: any[], cb: Function) => {
for (const value of list) { for (const value of list) {
if (!(await cb(value))) return false if (!await cb(value)) return false
} }
return true return true
}, },
@ -72,14 +63,8 @@ export const list = {
const realList = list.value as any[] const realList = list.value as any[]
const realStart = start.value as number const realStart = start.value as number
const realDeleteCount = deleteCount.value as number const realDeleteCount = deleteCount.value as number
return toValue(realList.splice(realStart, realDeleteCount, ...items)) const realItems = items.map(item => item.value)
}, return toValue(realList.splice(realStart, realDeleteCount, ...realItems))
insert: (list: Value, index: Value, item: Value) => {
if (list.type !== 'array') return toNull()
const realList = list.value as any[]
const realIndex = index.value as number
realList.splice(realIndex, 0, item)
return toValue(realList.length)
}, },
// sequence operations // sequence operations
@ -131,7 +116,7 @@ export const list = {
} }
return [truthy, falsy] return [truthy, falsy]
}, },
compact: (list: any[]) => list.filter((x) => x != null), compact: (list: any[]) => list.filter(x => x != null),
'group-by': async (list: any[], cb: Function) => { 'group-by': async (list: any[], cb: Function) => {
const groups: Record<string, any[]> = {} const groups: Record<string, any[]> = {}
for (const value of list) { for (const value of list) {
@ -143,11 +128,11 @@ export const list = {
}, },
} }
// raw functions deal directly in Value types, meaning we can modify collection
// careful - they MUST return a Value! // raw functions deal directly in Value types, meaning we can modify collection
;(list.splice as any).raw = true // careful - they MUST return a Value!
;(list.push as any).raw = true ; (list.splice as any).raw = true
;(list.pop as any).raw = true ; (list.push as any).raw = true
;(list.shift as any).raw = true ; (list.pop as any).raw = true
;(list.unshift as any).raw = true ; (list.shift as any).raw = true
;(list.insert as any).raw = true ; (list.unshift as any).raw = true

View File

@ -7,9 +7,7 @@ export const load = async function (this: VM, path: string): Promise<Record<stri
const scope = this.scope const scope = this.scope
const pc = this.pc const pc = this.pc
let fullPath = resolve(path) const fullPath = resolve(path) + '.sh'
if (!path.includes('.')) fullPath += '.sh'
const code = readFileSync(fullPath, 'utf-8') const code = readFileSync(fullPath, 'utf-8')
this.pc = this.instructions.length this.pc = this.instructions.length
@ -20,11 +18,12 @@ export const load = async function (this: VM, path: string): Promise<Record<stri
await this.continue() await this.continue()
const module: Record<string, Value> = {} const module: Record<string, Value> = {}
for (const [name, value] of this.scope.locals.entries()) module[name] = value for (const [name, value] of this.scope.locals.entries())
module[name] = value
this.scope = scope this.scope = scope
this.pc = pc this.pc = pc
this.stopped = false this.stopped = false
return module return module
} }

View File

@ -16,10 +16,7 @@ export const math = {
if (n < 0) throw new Error(`sqrt: cannot take square root of negative number ${n}`) if (n < 0) throw new Error(`sqrt: cannot take square root of negative number ${n}`)
return Math.sqrt(n) return Math.sqrt(n)
}, },
random: (min = 0, max = 1) => { random: () => Math.random(),
if (min === 0 && max === 1) return Math.random()
return Math.floor(Math.random() * (max - min + 1)) + min
},
clamp: (n: number, min: number, max: number) => { clamp: (n: number, min: number, max: number) => {
if (min > max) throw new Error(`clamp: min (${min}) must be less than or equal to max (${max})`) if (min > max) throw new Error(`clamp: min (${min}) must be less than or equal to max (${max})`)
return Math.min(Math.max(n, min), max) return Math.min(Math.max(n, min), max)
@ -33,4 +30,4 @@ export const math = {
'positive?': (n: number) => n > 0, 'positive?': (n: number) => n > 0,
'negative?': (n: number) => n < 0, 'negative?': (n: number) => n < 0,
'zero?': (n: number) => n === 0, 'zero?': (n: number) => n === 0,
} }

View File

@ -17,37 +17,21 @@ export const str = {
'last-index-of': (str: string, search: string) => String(str ?? '').lastIndexOf(search), 'last-index-of': (str: string, search: string) => String(str ?? '').lastIndexOf(search),
// transformations // transformations
replace: (str: string, search: string, replacement: string) => replace: (str: string, search: string, replacement: string) => String(str ?? '').replace(search, replacement),
String(str ?? '').replace(search, replacement), 'replace-all': (str: string, search: string, replacement: string) => String(str ?? '').replaceAll(search, replacement),
'replace-all': (str: string, search: string, replacement: string) => slice: (str: string, start: number, end?: number | null) => String(str ?? '').slice(start, end ?? undefined),
String(str ?? '').replaceAll(search, replacement), substring: (str: string, start: number, end?: number | null) => String(str ?? '').substring(start, end ?? undefined),
slice: (str: string, start: number, end?: number | null) =>
String(str ?? '').slice(start, end ?? undefined),
substring: (str: string, start: number, end?: number | null) =>
String(str ?? '').substring(start, end ?? undefined),
repeat: (str: string, count: number) => { repeat: (str: string, count: number) => {
if (count < 0) throw new Error(`repeat: count must be non-negative, got ${count}`) if (count < 0) throw new Error(`repeat: count must be non-negative, got ${count}`)
if (!Number.isInteger(count)) throw new Error(`repeat: count must be an integer, got ${count}`) if (!Number.isInteger(count)) throw new Error(`repeat: count must be an integer, got ${count}`)
return String(str ?? '').repeat(count) return String(str ?? '').repeat(count)
}, },
'pad-start': (str: string, length: number, pad: string = ' ') => 'pad-start': (str: string, length: number, pad: string = ' ') => String(str ?? '').padStart(length, pad),
String(str ?? '').padStart(length, pad), 'pad-end': (str: string, length: number, pad: string = ' ') => String(str ?? '').padEnd(length, pad),
'pad-end': (str: string, length: number, pad: string = ' ') =>
String(str ?? '').padEnd(length, pad),
capitalize: (str: string) => {
const s = String(str ?? '')
return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase()
},
titlecase: (s: string) => {
return String(s ?? '')
.split(' ')
.map(str.capitalize)
.join(' ')
},
lines: (str: string) => String(str ?? '').split('\n'), lines: (str: string) => String(str ?? '').split('\n'),
chars: (str: string) => String(str ?? '').split(''), chars: (str: string) => String(str ?? '').split(''),
// regex // regex
match: (str: string, regex: RegExp) => String(str ?? '').match(regex), match: (str: string, regex: RegExp) => String(str ?? '').match(regex),
'test?': (str: string, regex: RegExp) => regex.test(String(str ?? '')), 'test?': (str: string, regex: RegExp) => regex.test(String(str ?? '')),
} }

View File

@ -1,170 +0,0 @@
import { expect, describe, test } from 'bun:test'
describe('date', () => {
test('date.now returns current timestamp', () => {
expect(`date.now | number?`).toEvaluateTo(true)
expect(`(date.now) > 1577836800000`).toEvaluateTo(true)
})
test('date.new creates timestamp from components', () => {
expect(`
t = date.new 2024 0 1 12 0 0 500
[
(date.year t)
(date.month t)
(date.date t)
(date.hour t)
(date.minute t)
(date.second t)
(date.ms t)
]
`).toEvaluateTo([2024, 0, 1, 12, 0, 0, 500])
})
test('date.new with minimal arguments', () => {
expect(`
t = date.new 2024 5 15
[
(date.year t)
(date.month t)
(date.date t)
(date.hour t)
(date.minute t)
(date.second t)
(date.ms t)
]
`).toEvaluateTo([2024, 5, 15, 0, 0, 0, 0])
})
test('date.year extracts year', () => {
expect(`
t = date.new 2024 0 1
date.year t
`).toEvaluateTo(2024)
expect(`
t = date.new 1999 11 31
date.year t
`).toEvaluateTo(1999)
})
test('date.month extracts month (0-indexed)', () => {
// January = 0, December = 11
expect(`
jan = date.new 2024 0 1
dec = date.new 2024 11 31
[(date.month jan) (date.month dec)]
`).toEvaluateTo([0, 11])
})
test('date.date extracts day of month', () => {
expect(`
t = date.new 2024 5 15
date.date t
`).toEvaluateTo(15)
expect(`
date.new 2024 0 1 | date.date
`).toEvaluateTo(1)
})
test('date.hour extracts hour', () => {
expect(`
t = date.new 2024 0 1 14 30 45
date.hour t
`).toEvaluateTo(14)
expect(`
t = date.new 2024 0 1 0 0 0
date.hour t
`).toEvaluateTo(0)
})
test('date.minute extracts minute', () => {
expect(`
t = date.new 2024 0 1 14 30 45
date.minute t
`).toEvaluateTo(30)
})
test('date.second extracts second', () => {
expect(`
t = date.new 2024 0 1 14 30 45
date.second t
`).toEvaluateTo(45)
})
test('date.ms extracts milliseconds', () => {
expect(`
t = date.new 2024 0 1 14 30 45 250
date.ms t
`).toEvaluateTo(250)
})
test('round-trip: create and extract components', () => {
expect(`
t = date.new 2024 6 4 15 30 45 123
year = date.year t
month = date.month t
day = date.date t
hour = date.hour t
min = date.minute t
sec = date.second t
ms = date.ms t
[year month day hour min sec ms]
`).toEvaluateTo([2024, 6, 4, 15, 30, 45, 123])
})
test('edge cases - midnight', () => {
expect(`
t = date.new 2024 0 1 0 0 0 0
[
(date.hour t)
(date.minute t)
(date.second t)
(date.ms t)
]
`).toEvaluateTo([0, 0, 0, 0])
})
test('edge cases - end of day', () => {
expect(`
t = date.new 2024 0 1 23 59 59 999
[
(date.hour t)
(date.minute t)
(date.second t)
(date.ms t)
]
`).toEvaluateTo([23, 59, 59, 999])
})
test('edge cases - leap year', () => {
expect(`
t = date.new 2024 1 29
[
(date.year t)
(date.month t)
(date.date t)
]
`).toEvaluateTo([2024, 1, 29])
})
test('combining date functions with arithmetic', () => {
expect(`
t = date.new 2024 5 15 10 30 0
next-hour = date.new 2024 5 15 11 30 0
(date.hour next-hour) - (date.hour t)
`).toEvaluateTo(1)
})
test('using date.now in calculations', () => {
// Check that date.now is in the past compared to a future timestamp
expect(`
now = (date.now)
future = date.new 2030 0 1
future > now
`).toEvaluateTo(true)
})
})

View File

@ -1,331 +0,0 @@
import { expect, describe, test, beforeEach, afterEach } from 'bun:test'
import { mkdirSync, writeFileSync, rmSync, existsSync } from 'fs'
import { join, resolve } from 'path'
import { fs } from '../fs'
const TEST_DIR = resolve('./tmp/shrimp-fs-test')
const CWD = process.cwd()
beforeEach(() => {
if (existsSync(TEST_DIR)) {
rmSync(TEST_DIR, { recursive: true })
}
mkdirSync(TEST_DIR, { recursive: true })
})
afterEach(() => {
process.chdir(CWD)
if (existsSync(TEST_DIR)) {
rmSync(TEST_DIR, { recursive: true })
}
})
describe('fs - directory operations', () => {
test('fs.ls lists directory contents', () => {
writeFileSync(join(TEST_DIR, 'file1.txt'), 'content1')
writeFileSync(join(TEST_DIR, 'file2.txt'), 'content2')
const result = fs.ls(TEST_DIR)
expect(result).toContain('file1.txt')
expect(result).toContain('file2.txt')
})
test('fs.mkdir creates directory', () => {
const newDir = join(TEST_DIR, 'newdir')
fs.mkdir(newDir)
expect(existsSync(newDir)).toBe(true)
})
test('fs.rmdir removes empty directory', () => {
const dir = join(TEST_DIR, 'toremove')
mkdirSync(dir)
fs.rmdir(dir)
expect(existsSync(dir)).toBe(false)
})
test('fs.pwd returns current working directory', () => {
const result = fs.pwd()
expect(typeof result).toBe('string')
expect(result.length).toBeGreaterThan(0)
})
test('fs.cd changes current working directory', () => {
const originalCwd = process.cwd()
fs.cd(TEST_DIR)
expect(process.cwd()).toBe(TEST_DIR)
process.chdir(originalCwd) // restore
})
})
describe('fs - reading', () => {
test('fs.read reads file contents as string', () => {
const file = join(TEST_DIR, 'test.txt')
writeFileSync(file, 'hello world')
const result = fs.read(file)
expect(result).toBe('hello world')
})
test('fs.cat is alias for fs.read', () => {
const file = join(TEST_DIR, 'test.txt')
writeFileSync(file, 'hello world')
const result = fs.cat(file)
expect(result).toBe('hello world')
})
test('fs.read-bytes reads file as buffer', () => {
const file = join(TEST_DIR, 'test.bin')
writeFileSync(file, Buffer.from([1, 2, 3, 4]))
const result = fs['read-bytes'](file)
expect(result).toBeInstanceOf(Array)
expect(result).toEqual([1, 2, 3, 4])
})
})
describe('fs - writing', () => {
test('fs.write writes string to file', async () => {
const file = join(TEST_DIR, 'output.txt')
fs.write(file, 'test content')
const content = Bun.file(file).text()
expect(await content).toBe('test content')
})
test('fs.append appends to existing file', async () => {
const file = join(TEST_DIR, 'append.txt')
writeFileSync(file, 'first')
fs.append(file, ' second')
const content = await Bun.file(file).text()
expect(content).toBe('first second')
})
})
describe('fs - file operations', () => {
test('fs.rm removes file', () => {
const file = join(TEST_DIR, 'remove.txt')
writeFileSync(file, 'content')
fs.rm(file)
expect(existsSync(file)).toBe(false)
})
test('fs.delete is alias for fs.rm', () => {
const file = join(TEST_DIR, 'delete.txt')
writeFileSync(file, 'content')
fs.delete(file)
expect(existsSync(file)).toBe(false)
})
test('fs.copy copies file', async () => {
const src = join(TEST_DIR, 'source.txt')
const dest = join(TEST_DIR, 'dest.txt')
writeFileSync(src, 'content')
fs.copy(src, dest)
expect(await Bun.file(dest).text()).toBe('content')
})
test('fs.cp is alias for fs.copy', async () => {
const src = join(TEST_DIR, 'source2.txt')
const dest = join(TEST_DIR, 'dest2.txt')
writeFileSync(src, 'content')
fs.cp(src, dest)
expect(await Bun.file(dest).text()).toBe('content')
})
test('fs.move moves file', async () => {
const src = join(TEST_DIR, 'source.txt')
const dest = join(TEST_DIR, 'moved.txt')
writeFileSync(src, 'content')
fs.move(src, dest)
expect(existsSync(src)).toBe(false)
expect(await Bun.file(dest).text()).toBe('content')
})
test('fs.mv is alias for fs.move', async () => {
const src = join(TEST_DIR, 'source2.txt')
const dest = join(TEST_DIR, 'moved2.txt')
writeFileSync(src, 'content')
fs.mv(src, dest)
expect(existsSync(src)).toBe(false)
expect(await Bun.file(dest).text()).toBe('content')
})
})
describe('fs - path operations', () => {
test('fs.basename extracts filename from path', () => {
expect(fs.basename('/path/to/file.txt')).toBe('file.txt')
expect(fs.basename('/path/to/dir/')).toBe('dir')
})
test('fs.dirname extracts directory from path', () => {
expect(fs.dirname('/path/to/file.txt')).toBe('/path/to')
expect(fs.dirname('/path/to/dir/')).toBe('/path/to')
})
test('fs.extname extracts file extension', () => {
expect(fs.extname('file.txt')).toBe('.txt')
expect(fs.extname('file.tar.gz')).toBe('.gz')
expect(fs.extname('noext')).toBe('')
})
test('fs.join joins path segments', () => {
expect(fs.join('path', 'to', 'file.txt')).toBe('path/to/file.txt')
expect(fs.join('/absolute', 'path')).toBe('/absolute/path')
})
test('fs.resolve resolves to absolute path', () => {
const result = fs.resolve('relative', 'path')
expect(result.startsWith('/')).toBe(true)
expect(result).toContain('relative')
})
})
describe('fs - file info', () => {
test('fs.stat returns file stats', () => {
const file = join(TEST_DIR, 'stat.txt')
writeFileSync(file, 'content')
const stats = fs.stat(file)
expect(stats).toHaveProperty('size')
expect(stats).toHaveProperty('mtime')
expect(stats.size).toBe(7) // 'content' is 7 bytes
})
test('fs.exists? checks if path exists', () => {
const file = join(TEST_DIR, 'exists.txt')
expect(fs['exists?'](file)).toBe(false)
writeFileSync(file, 'content')
expect(fs['exists?'](file)).toBe(true)
})
test('fs.file? checks if path is a file', () => {
const file = join(TEST_DIR, 'isfile.txt')
writeFileSync(file, 'content')
expect(fs['file?'](file)).toBe(true)
expect(fs['file?'](TEST_DIR)).toBe(false)
})
test('fs.dir? checks if path is a directory', () => {
const dir = join(TEST_DIR, 'isdir')
mkdirSync(dir)
expect(fs['dir?'](dir)).toBe(true)
expect(fs['dir?'](join(TEST_DIR, 'isfile.txt'))).toBe(false)
})
test('fs.symlink? checks if path is a symbolic link', () => {
const file = join(TEST_DIR, 'target.txt')
const link = join(TEST_DIR, 'link.txt')
writeFileSync(file, 'content')
fs.symlink(file, link)
expect(fs['symlink?'](link)).toBe(true)
expect(fs['symlink?'](file)).toBe(false)
})
test('fs.exec? checks if file is executable', () => {
const file = join(TEST_DIR, 'script.sh')
writeFileSync(file, '#!/bin/bash\necho hello')
fs.chmod(file, 0o755)
expect(fs['exec?'](file)).toBe(true)
fs.chmod(file, 0o644)
expect(fs['exec?'](file)).toBe(false)
})
test('fs.size returns file size in bytes', () => {
const file = join(TEST_DIR, 'sizeme.txt')
writeFileSync(file, 'content')
expect(fs.size(file)).toBe(7) // 'content' is 7 bytes
})
})
describe('fs - permissions', () => {
test('fs.chmod changes file permissions with octal number', () => {
const file = join(TEST_DIR, 'perms.txt')
writeFileSync(file, 'content')
fs.chmod(file, 0o755)
expect(fs['exec?'](file)).toBe(true)
fs.chmod(file, 0o644)
expect(fs['exec?'](file)).toBe(false)
})
test('fs.chmod changes file permissions with string', () => {
const file = join(TEST_DIR, 'perms2.txt')
writeFileSync(file, 'content')
fs.chmod(file, '755')
expect(fs['exec?'](file)).toBe(true)
fs.chmod(file, '644')
expect(fs['exec?'](file)).toBe(false)
})
})
describe('fs - symlinks', () => {
test('fs.symlink creates symbolic link', () => {
const target = join(TEST_DIR, 'target.txt')
const link = join(TEST_DIR, 'link.txt')
writeFileSync(target, 'content')
fs.symlink(target, link)
expect(fs['symlink?'](link)).toBe(true)
expect(fs.read(link)).toBe('content')
})
test('fs.readlink reads symbolic link target', () => {
const target = join(TEST_DIR, 'target.txt')
const link = join(TEST_DIR, 'link.txt')
writeFileSync(target, 'content')
fs.symlink(target, link)
expect(fs.readlink(link)).toBe(target)
})
})
describe('fs - other', () => {
test('fs.glob matches file patterns', () => {
writeFileSync(join(TEST_DIR, 'file1.txt'), '')
writeFileSync(join(TEST_DIR, 'file2.txt'), '')
writeFileSync(join(TEST_DIR, 'file3.md'), '')
const result = fs.glob(join(TEST_DIR, '*.txt'))
expect(result).toHaveLength(2)
expect(result).toContain(join(TEST_DIR, 'file1.txt'))
expect(result).toContain(join(TEST_DIR, 'file2.txt'))
})
test('fs.watch calls callback on file change', async () => {
const file = join(TEST_DIR, 'watch.txt')
writeFileSync(file, 'initial')
let called = false
const watcher = fs.watch(file, () => {
called = true
})
// Trigger change
await new Promise((resolve) => setTimeout(resolve, 100))
writeFileSync(file, 'updated')
// Wait for watcher
await new Promise((resolve) => setTimeout(resolve, 500))
expect(called).toBe(true)
watcher.close?.()
})
})

View File

@ -1,139 +1,79 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import { globals } from '#prelude'
describe('var and var?', () => { describe('var and var?', () => {
test('var? checks if a variable exists', async () => { test('var? checks if a variable exists', async () => {
await expect(`var? 'nada'`).toEvaluateTo(false) await expect(`var? 'nada'`).toEvaluateTo(false, globals)
await expect(`var? 'info'`).toEvaluateTo(false) await expect(`var? 'info'`).toEvaluateTo(false, globals)
await expect(`abc = abc; var? 'abc'`).toEvaluateTo(true) await expect(`abc = abc; var? 'abc'`).toEvaluateTo(true, globals)
await expect(`var? 'var?'`).toEvaluateTo(true) await expect(`var? 'var?'`).toEvaluateTo(true, globals)
await expect(`var? 'dict'`).toEvaluateTo(true) await expect(`var? 'dict'`).toEvaluateTo(true, globals)
await expect(`var? dict`).toEvaluateTo(true) await expect(`var? dict`).toEvaluateTo(true, globals)
}) })
test('var returns a value or null', async () => { test('var returns a value or null', async () => {
await expect(`var 'nada'`).toEvaluateTo(null) await expect(`var 'nada'`).toEvaluateTo(null, globals)
await expect(`var nada`).toEvaluateTo(null) await expect(`var nada`).toEvaluateTo(null, globals)
await expect(`var 'info'`).toEvaluateTo(null) await expect(`var 'info'`).toEvaluateTo(null, globals)
await expect(`abc = my-string; var 'abc'`).toEvaluateTo('my-string') await expect(`abc = my-string; var 'abc'`).toEvaluateTo('my-string', globals)
await expect(`abc = my-string; var abc`).toEvaluateTo(null) await expect(`abc = my-string; var abc`).toEvaluateTo(null, globals)
}) })
}) })
describe('type predicates', () => { describe('type predicates', () => {
test('string? checks for string type', async () => { test('string? checks for string type', async () => {
await expect(`string? 'hello'`).toEvaluateTo(true) await expect(`string? 'hello'`).toEvaluateTo(true, globals)
await expect(`string? 42`).toEvaluateTo(false) await expect(`string? 42`).toEvaluateTo(false, globals)
}) })
test('number? checks for number type', async () => { test('number? checks for number type', async () => {
await expect(`number? 42`).toEvaluateTo(true) await expect(`number? 42`).toEvaluateTo(true, globals)
await expect(`number? 'hello'`).toEvaluateTo(false) await expect(`number? 'hello'`).toEvaluateTo(false, globals)
}) })
test('boolean? checks for boolean type', async () => { test('boolean? checks for boolean type', async () => {
await expect(`boolean? true`).toEvaluateTo(true) await expect(`boolean? true`).toEvaluateTo(true, globals)
await expect(`boolean? 42`).toEvaluateTo(false) await expect(`boolean? 42`).toEvaluateTo(false, globals)
}) })
test('array? checks for array type', async () => { test('array? checks for array type', async () => {
await expect(`array? [1 2 3]`).toEvaluateTo(true) await expect(`array? [1 2 3]`).toEvaluateTo(true, globals)
await expect(`array? 42`).toEvaluateTo(false) await expect(`array? 42`).toEvaluateTo(false, globals)
}) })
test('dict? checks for dict type', async () => { test('dict? checks for dict type', async () => {
await expect(`dict? [a=1]`).toEvaluateTo(true) await expect(`dict? [a=1]`).toEvaluateTo(true, globals)
await expect(`dict? []`).toEvaluateTo(false) await expect(`dict? []`).toEvaluateTo(false, globals)
}) })
test('null? checks for null type', async () => { test('null? checks for null type', async () => {
await expect(`null? null`).toEvaluateTo(true) await expect(`null? null`).toEvaluateTo(true, globals)
await expect(`null? 42`).toEvaluateTo(false) await expect(`null? 42`).toEvaluateTo(false, globals)
}) })
test('some? checks for non-null', async () => { test('some? checks for non-null', async () => {
await expect(`some? 42`).toEvaluateTo(true) await expect(`some? 42`).toEvaluateTo(true, globals)
await expect(`some? null`).toEvaluateTo(false) await expect(`some? null`).toEvaluateTo(false, globals)
}) })
}) })
describe('introspection', () => { describe('introspection', () => {
test('type returns proper types', async () => { test('type returns proper types', async () => {
await expect(`type 'hello'`).toEvaluateTo('string') await expect(`type 'hello'`).toEvaluateTo('string', globals)
await expect(`type 42`).toEvaluateTo('number') await expect(`type 42`).toEvaluateTo('number', globals)
await expect(`type true`).toEvaluateTo('boolean') await expect(`type true`).toEvaluateTo('boolean', globals)
await expect(`type false`).toEvaluateTo('boolean') await expect(`type false`).toEvaluateTo('boolean', globals)
await expect(`type null`).toEvaluateTo('null') await expect(`type null`).toEvaluateTo('null', globals)
await expect(`type [1 2 3]`).toEvaluateTo('array') await expect(`type [1 2 3]`).toEvaluateTo('array', globals)
await expect(`type [a=1 b=2]`).toEvaluateTo('dict') await expect(`type [a=1 b=2]`).toEvaluateTo('dict', globals)
}) })
test('inspect formats values', async () => { test('inspect formats values', async () => {
await expect(`inspect 'hello'`).toEvaluateTo("\u001b[32m'hello\u001b[32m'\u001b[0m") await expect(`inspect 'hello'`).toEvaluateTo("\u001b[32m'hello\u001b[32m'\u001b[0m", globals)
}) })
test('describe describes values', async () => { test('describe describes values', async () => {
await expect(`describe 'hello'`).toEvaluateTo("#<string: \u001b[32m'hello\u001b[32m'\u001b[0m>") await expect(`describe 'hello'`).toEvaluateTo("#<string: \u001b[32m'hello\u001b[32m'\u001b[0m>", globals)
})
})
describe('environment', () => {
test('args is an array', async () => {
await expect(`array? $.args`).toEvaluateTo(true)
})
test('args can be accessed', async () => {
await expect(`type $.args`).toEvaluateTo('array')
})
test('argv includes more than just the args', async () => {
await expect(`list.first $.argv | str.ends-with? 'shrimp.test.ts'`).toEvaluateTo(true)
})
})
describe('ref', () => {
expect(`rnd = do x: true end; rnd | type`).toEvaluateTo('boolean')
expect(`rnd = do x: true end; ref rnd | type`).toEvaluateTo('function')
expect(`math.random | type`).toEvaluateTo('number')
expect(`ref math.random | type`).toEvaluateTo('native')
expect(`rnd = math.random; rnd | type`).toEvaluateTo('number')
expect(`rnd = ref math.random; rnd | type`).toEvaluateTo('number')
expect(`rnd = ref math.random; ref rnd | type`).toEvaluateTo('native')
})
describe('$ global dictionary', () => {
test('$.args is an array', async () => {
await expect(`$.args | array?`).toEvaluateTo(true)
})
test('$.args can be accessed', async () => {
await expect(`$.args | type`).toEvaluateTo('array')
})
test('$.script.name is a string', async () => {
await expect(`$.script.name | string?`).toEvaluateTo(true)
})
test('$.script.path is a string', async () => {
await expect(`$.script.path | string?`).toEvaluateTo(true)
})
test('$.env is a dict', async () => {
await expect(`$.env | dict?`).toEvaluateTo(true)
})
test('$.pid is a number', async () => {
await expect(`$.pid | number?`).toEvaluateTo(true)
await expect(`$.pid > 0`).toEvaluateTo(true)
})
test('$.cwd is a string', async () => {
await expect(`$.cwd | string?`).toEvaluateTo(true)
})
test('$.cwd returns current working directory', async () => {
await expect(`$.cwd`).toEvaluateTo(process.cwd())
}) })
}) })

View File

@ -1,96 +0,0 @@
import { expect, describe, test } from 'bun:test'
describe('json', () => {
test('json.decode', () => {
expect(`json.decode '[1,2,3]'`).toEvaluateTo([1, 2, 3])
expect(`json.decode '"heya"'`).toEvaluateTo('heya')
expect(`json.decode '[true, false, null]'`).toEvaluateTo([true, false, null])
expect(`json.decode '{"a": true, "b": false, "c": "yeah"}'`).toEvaluateTo({
a: true,
b: false,
c: 'yeah',
})
})
test('json.encode', () => {
expect(`json.encode [1 2 3]`).toEvaluateTo('[1,2,3]')
expect(`json.encode 'heya'`).toEvaluateTo('"heya"')
expect(`json.encode [true false null]`).toEvaluateTo('[true,false,null]')
expect(`json.encode [a=true b=false c='yeah'] | json.decode`).toEvaluateTo({
a: true,
b: false,
c: 'yeah',
})
})
test('edge cases - empty structures', () => {
expect(`json.decode '[]'`).toEvaluateTo([])
expect(`json.decode '{}'`).toEvaluateTo({})
expect(`json.encode []`).toEvaluateTo('[]')
expect(`json.encode [=]`).toEvaluateTo('{}')
})
test('edge cases - special characters in strings', () => {
expect(`json.decode '"hello\\\\nworld"'`).toEvaluateTo('hello\nworld')
expect(`json.decode '"tab\\\\there"'`).toEvaluateTo('tab\there')
expect(`json.decode '"forward/slash"'`).toEvaluateTo('forward/slash')
expect(`json.decode '"with\\\\\\\\backslash"'`).toEvaluateTo('with\\backslash')
})
test('numbers - integers and floats', () => {
expect(`json.decode '42'`).toEvaluateTo(42)
expect(`json.decode '0'`).toEvaluateTo(0)
expect(`json.decode '-17'`).toEvaluateTo(-17)
expect(`json.decode '3.14159'`).toEvaluateTo(3.14159)
expect(`json.decode '-0.5'`).toEvaluateTo(-0.5)
})
test('numbers - scientific notation', () => {
expect(`json.decode '1e10'`).toEvaluateTo(1e10)
expect(`json.decode '2.5e-3'`).toEvaluateTo(2.5e-3)
expect(`json.decode '1.23E+5'`).toEvaluateTo(1.23e5)
})
test('unicode - emoji and special characters', () => {
expect(`json.decode '"hello 👋"'`).toEvaluateTo('hello 👋')
expect(`json.decode '"🎉🚀✨"'`).toEvaluateTo('🎉🚀✨')
expect(`json.encode '你好'`).toEvaluateTo('"你好"')
expect(`json.encode 'café'`).toEvaluateTo('"café"')
})
test('nested structures - arrays', () => {
expect(`json.decode '[[1,2],[3,4],[5,6]]'`).toEvaluateTo([
[1, 2],
[3, 4],
[5, 6],
])
expect(`json.decode '[1,[2,[3,[4]]]]'`).toEvaluateTo([1, [2, [3, [4]]]])
})
test('nested structures - objects', () => {
expect(`json.decode '{"user":{"name":"Alice","age":30}}'`).toEvaluateTo({
user: { name: 'Alice', age: 30 },
})
expect(`json.decode '{"a":{"b":{"c":"deep"}}}'`).toEvaluateTo({
a: { b: { c: 'deep' } },
})
})
test('nested structures - mixed arrays and objects', () => {
expect(`json.decode '[{"id":1,"tags":["a","b"]},{"id":2,"tags":["c"]}]'`).toEvaluateTo([
{ id: 1, tags: ['a', 'b'] },
{ id: 2, tags: ['c'] },
])
expect(`json.decode '{"items":[1,2,3],"meta":{"count":3}}'`).toEvaluateTo({
items: [1, 2, 3],
meta: { count: 3 },
})
})
test('error handling - invalid json', () => {
expect(`json.decode '{invalid}'`).toFailEvaluation()
expect(`json.decode '[1,2,3'`).toFailEvaluation()
expect(`json.decode 'undefined'`).toFailEvaluation()
expect(`json.decode ''`).toFailEvaluation()
})
})

View File

@ -1,41 +0,0 @@
import { expect, describe, test } from 'bun:test'
describe('loading a file', () => {
test(`imports all a file's functions`, async () => {
expect(`
math = load ./src/prelude/tests/math.sh
math.double 4
`).toEvaluateTo(8)
expect(`
math = load ./src/prelude/tests/math.sh
math.double (math.double 4)
`).toEvaluateTo(16)
expect(`
math = load ./src/prelude/tests/math.sh
dbl = ref math.double
dbl (dbl 2)
`).toEvaluateTo(8)
expect(`
math = load ./src/prelude/tests/math.sh
math.pi
`).toEvaluateTo(3.14)
expect(`
math = load ./src/prelude/tests/math.sh
math | at 🥧
`).toEvaluateTo(3.14159265359)
expect(`
math = load ./src/prelude/tests/math.sh
math.🥧
`).toEvaluateTo(3.14159265359)
expect(`
math = load ./src/prelude/tests/math.sh
math.add1 5
`).toEvaluateTo(6)
})
})

42
src/prelude/tests/load.ts Normal file
View File

@ -0,0 +1,42 @@
import { expect, describe, test } from 'bun:test'
import { globals } from '#prelude'
describe('use', () => {
test(`imports all a file's functions`, async () => {
expect(`
math = load ./src/prelude/tests/math
math.double 4
`).toEvaluateTo(8, globals)
expect(`
math = load ./src/prelude/tests/math
math.double (math.double 4)
`).toEvaluateTo(16, globals)
expect(`
math = load ./src/prelude/tests/math
dbl = math.double
dbl (dbl 2)
`).toEvaluateTo(8, globals)
expect(`
math = load ./src/prelude/tests/math
math.pi
`).toEvaluateTo(3.14, globals)
expect(`
math = load ./src/prelude/tests/math
math | at 🥧
`).toEvaluateTo(3.14159265359, globals)
expect(`
math = load ./src/prelude/tests/math
math.🥧
`).toEvaluateTo(3.14159265359, globals)
expect(`
math = load ./src/prelude/tests/math
math.add1 5
`).toEvaluateTo(6, globals)
})
})

View File

@ -1,4 +1,5 @@
import { expect, describe, test } from 'bun:test' import { expect, describe, test } from 'bun:test'
import { globals } from '#prelude'
describe('string operations', () => { describe('string operations', () => {
test('to-upper converts to uppercase', async () => { test('to-upper converts to uppercase', async () => {
@ -16,18 +17,6 @@ describe('string operations', () => {
await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello') await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello')
}) })
test('capitalize makes first char uppercase', async () => {
await expect(`str.capitalize 'hello'`).toEvaluateTo('Hello')
await expect(`str.capitalize 'HELLO'`).toEvaluateTo('Hello')
await expect(`str.capitalize 'hello world'`).toEvaluateTo('Hello world')
})
test('titlecase capitalizes each word', async () => {
await expect(`str.titlecase 'hello world'`).toEvaluateTo('Hello World')
await expect(`str.titlecase 'HELLO WORLD'`).toEvaluateTo('Hello World')
await expect(`str.titlecase 'the quick brown fox'`).toEvaluateTo('The Quick Brown Fox')
})
test('split divides string by separator', async () => { test('split divides string by separator', async () => {
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c']) await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'])
await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o']) await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'])
@ -77,7 +66,6 @@ describe('string operations', () => {
test('slice extracts substring', async () => { test('slice extracts substring', async () => {
await expect(`str.slice 'hello' 1 3`).toEvaluateTo('el') await expect(`str.slice 'hello' 1 3`).toEvaluateTo('el')
await expect(`str.slice 'hello' 2 null`).toEvaluateTo('llo') await expect(`str.slice 'hello' 2 null`).toEvaluateTo('llo')
await expect(`str.slice 'hello' 2`).toEvaluateTo('llo')
}) })
test('repeat repeats string', async () => { test('repeat repeats string', async () => {
@ -117,17 +105,6 @@ describe('boolean logic', () => {
await expect(`not 42`).toEvaluateTo(false) await expect(`not 42`).toEvaluateTo(false)
await expect(`not null`).toEvaluateTo(true) await expect(`not null`).toEvaluateTo(true)
}) })
test('not works with function calls', async () => {
await expect(`equals = do x y: x == y end; not equals 5 5`).toEvaluateTo(false)
await expect(`equals = do x y: x == y end; not equals 5 10`).toEvaluateTo(true)
})
test('not works with binary operations and comparisons', async () => {
await expect(`not 5 > 10`).toEvaluateTo(true)
await expect(`not 10 > 5`).toEvaluateTo(false)
await expect(`not true and false`).toEvaluateTo(true)
})
}) })
describe('utilities', () => { describe('utilities', () => {
@ -216,15 +193,6 @@ describe('collections', () => {
`).toEvaluateTo([3, 4, 5]) `).toEvaluateTo([3, 4, 5])
}) })
test('list.reject doesnt keep matching elements', async () => {
await expect(`
is-even = do x:
(x % 2) == 0
end
list.reject [1 2 3 4 5] is-even
`).toEvaluateTo([1, 3, 5])
})
test('list.reduce accumulates values', async () => { test('list.reduce accumulates values', async () => {
await expect(` await expect(`
add = do acc x: add = do acc x:
@ -277,10 +245,7 @@ describe('collections', () => {
}) })
test('list.zip combines two arrays', async () => { test('list.zip combines two arrays', async () => {
await expect(`list.zip [1 2] [3 4]`).toEvaluateTo([ await expect(`list.zip [1 2] [3 4]`).toEvaluateTo([[1, 3], [2, 4]])
[1, 3],
[2, 4],
])
}) })
test('list.first returns first element', async () => { test('list.first returns first element', async () => {
@ -374,22 +339,6 @@ describe('collections', () => {
await expect(`arr = [1 2 3 4 5]; list.splice arr 3 2; arr`).toEvaluateTo([1, 2, 3]) await expect(`arr = [1 2 3 4 5]; list.splice arr 3 2; arr`).toEvaluateTo([1, 2, 3])
}) })
test('list.insert adds element at index and mutates array', async () => {
await expect(`arr = [1 2 4 5]; list.insert arr 2 3; arr`).toEvaluateTo([1, 2, 3, 4, 5])
})
test('list.insert returns array length', async () => {
await expect(`list.insert [1 2 4] 2 3`).toEvaluateTo(4)
})
test('list.insert at start', async () => {
await expect(`arr = [2 3]; list.insert arr 0 1; arr`).toEvaluateTo([1, 2, 3])
})
test('list.insert at end', async () => {
await expect(`arr = [1 2]; list.insert arr 2 99; arr`).toEvaluateTo([1, 2, 99])
})
test('list.sort with no callback sorts ascending', async () => { test('list.sort with no callback sorts ascending', async () => {
await expect(`list.sort [3 1 4 1 5] null`).toEvaluateTo([1, 1, 3, 4, 5]) await expect(`list.sort [3 1 4 1 5] null`).toEvaluateTo([1, 1, 3, 4, 5])
}) })
@ -450,10 +399,7 @@ describe('collections', () => {
await expect(` await expect(`
gt-two = do x: x > 2 end gt-two = do x: x > 2 end
list.partition [1 2 3 4 5] gt-two list.partition [1 2 3 4 5] gt-two
`).toEvaluateTo([ `).toEvaluateTo([[3, 4, 5], [1, 2]])
[3, 4, 5],
[1, 2],
])
}) })
test('list.compact removes null values', async () => { test('list.compact removes null values', async () => {

View File

@ -1,143 +0,0 @@
import { expect, describe, test } from 'bun:test'
describe('type predicates', () => {
test('boolean? checks if value is boolean', async () => {
await expect(`boolean? true`).toEvaluateTo(true)
await expect(`boolean? false`).toEvaluateTo(true)
await expect(`boolean? 42`).toEvaluateTo(false)
await expect(`boolean? 'hello'`).toEvaluateTo(false)
await expect(`boolean? null`).toEvaluateTo(false)
await expect(`boolean? [1 2 3]`).toEvaluateTo(false)
})
test('number? checks if value is number', async () => {
await expect(`number? 42`).toEvaluateTo(true)
await expect(`number? 3.14`).toEvaluateTo(true)
await expect(`number? 0`).toEvaluateTo(true)
await expect(`number? -5`).toEvaluateTo(true)
await expect(`number? 'hello'`).toEvaluateTo(false)
await expect(`number? true`).toEvaluateTo(false)
await expect(`number? null`).toEvaluateTo(false)
})
test('string? checks if value is string', async () => {
await expect(`string? 'hello'`).toEvaluateTo(true)
await expect(`string? ''`).toEvaluateTo(true)
await expect(`string? world`).toEvaluateTo(true)
await expect(`string? 42`).toEvaluateTo(false)
await expect(`string? true`).toEvaluateTo(false)
await expect(`string? null`).toEvaluateTo(false)
await expect(`string? [1 2 3]`).toEvaluateTo(false)
})
test('array? checks if value is array', async () => {
await expect(`array? [1 2 3]`).toEvaluateTo(true)
await expect(`array? []`).toEvaluateTo(true)
await expect(`array? ['a' 'b']`).toEvaluateTo(true)
await expect(`array? [a=1 b=2]`).toEvaluateTo(false)
await expect(`array? 42`).toEvaluateTo(false)
await expect(`array? 'hello'`).toEvaluateTo(false)
await expect(`array? null`).toEvaluateTo(false)
})
test('list? is alias for array?', async () => {
await expect(`list? [1 2 3]`).toEvaluateTo(true)
await expect(`list? []`).toEvaluateTo(true)
await expect(`list? [a=1 b=2]`).toEvaluateTo(false)
})
test('dict? checks if value is dict', async () => {
await expect(`dict? [a=1 b=2]`).toEvaluateTo(true)
await expect(`dict? [=]`).toEvaluateTo(true)
await expect(`dict? [1 2 3]`).toEvaluateTo(false)
await expect(`dict? []`).toEvaluateTo(false)
await expect(`dict? 42`).toEvaluateTo(false)
await expect(`dict? 'hello'`).toEvaluateTo(false)
})
test('function? checks if value is function', async () => {
await expect(`
my-fn = do x: x * 2 end
function? my-fn
`).toEvaluateTo(true)
await expect(`function? inc`).toEvaluateTo(true)
await expect(`function? list.map`).toEvaluateTo(true)
await expect(`function? 42`).toEvaluateTo(false)
await expect(`function? 'hello'`).toEvaluateTo(false)
await expect(`function? [1 2 3]`).toEvaluateTo(false)
})
test('null? checks if value is null', async () => {
await expect(`null? null`).toEvaluateTo(true)
await expect(`null? 0`).toEvaluateTo(false)
await expect(`null? false`).toEvaluateTo(false)
await expect(`null? ''`).toEvaluateTo(false)
await expect(`null? []`).toEvaluateTo(false)
})
})
describe('type coercion', () => {
test('boolean coerces to boolean', async () => {
await expect(`boolean true`).toEvaluateTo(true)
await expect(`boolean false`).toEvaluateTo(false)
await expect(`boolean 1`).toEvaluateTo(true)
await expect(`boolean 0`).toEvaluateTo(false)
await expect(`boolean 'hello'`).toEvaluateTo(true)
await expect(`boolean ''`).toEvaluateTo(false)
await expect(`boolean null`).toEvaluateTo(false)
await expect(`boolean [1 2 3]`).toEvaluateTo(true)
})
test('number coerces to number', async () => {
await expect(`number 42`).toEvaluateTo(42)
await expect(`number '42'`).toEvaluateTo(42)
await expect(`number '3.14'`).toEvaluateTo(3.14)
await expect(`number true`).toEvaluateTo(1)
await expect(`number false`).toEvaluateTo(0)
})
test('string coerces to string', async () => {
await expect(`string 'hello'`).toEvaluateTo('hello')
await expect(`string 42`).toEvaluateTo('42')
await expect(`string true`).toEvaluateTo('true')
await expect(`string false`).toEvaluateTo('false')
await expect(`string null`).toEvaluateTo('null')
})
})
describe('type predicates in conditionals', () => {
test('using type predicates in if statements', async () => {
await expect(`
x = 42
if (number? x):
'is-num'
else:
'not-num'
end
`).toEvaluateTo('is-num')
})
test('filtering by type', async () => {
await expect(`
items = [1 'hello' 2 'world' 3]
list.filter items number?
`).toEvaluateTo([1, 2, 3])
})
test('filtering strings', async () => {
await expect(`
items = [1 'hello' 2 'world' 3]
list.filter items string?
`).toEvaluateTo(['hello', 'world'])
})
test('checking for functions', async () => {
await expect(`
double = do x: x * 2 end
not-fn = 42
is-fn = function? double
is-not-fn = function? not-fn
is-fn and (not is-not-fn)
`).toEvaluateTo(true)
})
})

View File

@ -1,21 +0,0 @@
import { toValue } from 'reefvm'
export const types = {
'boolean?': (v: any) => toValue(v).type === 'boolean',
boolean: (v: any) => Boolean(v),
'number?': (v: any) => toValue(v).type === 'number',
number: (v: any) => Number(v),
'string?': (v: any) => toValue(v).type === 'string',
string: (v: any) => String(v),
'array?': (v: any) => toValue(v).type === 'array',
'list?': (v: any) => toValue(v).type === 'array',
'dict?': (v: any) => toValue(v).type === 'dict',
'function?': (v: any) => ['function', 'native'].includes(toValue(v).type),
'null?': (v: any) => toValue(v).type === 'null',
}

10
src/server/app.tsx Normal file
View File

@ -0,0 +1,10 @@
import { Editor } from '#/editor/editor'
import { render } from 'hono/jsx/dom'
import './index.css'
const App = () => {
return <Editor />
}
const root = document.getElementById('root')!
render(<App />, root)

84
src/server/index.css Normal file
View File

@ -0,0 +1,84 @@
:root {
/* Background colors */
--bg-editor: #011627;
--bg-output: #40318D;
--bg-status-bar: #1E2A4A;
--bg-status-border: #0E1A3A;
--bg-selection: #1D3B53;
--bg-variable-def: #1E2A4A;
/* Text colors */
--text-editor: #D6DEEB;
--text-output: #7C70DA;
--text-status: #B3A9FF55;
--caret: #80A4C2;
/* Syntax highlighting colors */
--color-keyword: #C792EA;
--color-function: #82AAFF;
--color-string: #C3E88D;
--color-number: #F78C6C;
--color-bool: #FF5370;
--color-operator: #89DDFF;
--color-paren: #676E95;
--color-function-call: #FF9CAC;
--color-variable-def: #FFCB6B;
--color-error: #FF6E6E;
--color-regex: #E1ACFF;
/* ANSI terminal colors */
--ansi-black: #011627;
--ansi-red: #FF5370;
--ansi-green: #C3E88D;
--ansi-yellow: #FFCB6B;
--ansi-blue: #82AAFF;
--ansi-magenta: #C792EA;
--ansi-cyan: #89DDFF;
--ansi-white: #D6DEEB;
/* ANSI bright colors (slightly more vibrant) */
--ansi-bright-black: #676E95;
--ansi-bright-red: #FF6E90;
--ansi-bright-green: #D4F6A8;
--ansi-bright-yellow: #FFE082;
--ansi-bright-blue: #A8C7FA;
--ansi-bright-magenta: #E1ACFF;
--ansi-bright-cyan: #A8F5FF;
--ansi-bright-white: #FFFFFF;
}
@font-face {
font-family: 'C64ProMono';
src: url('../../assets/C64_Pro_Mono-STYLE.woff2') format('woff2');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Pixeloid Mono';
src: url('../../assets/PixeloidMono.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: var(--bg-output);
color: var(--text-output);
font-family: 'Pixeloid Mono', 'Courier New', monospace;
font-size: 18px;
height: 100vh;
overflow: hidden;
}
#root {
height: 100vh;
background: var(--bg-output);
display: flex;
flex-direction: column;
}

12
src/server/index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Shrimp</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="./app.tsx"></script>
</body>
</html>

29
src/server/server.tsx Normal file
View File

@ -0,0 +1,29 @@
import index from './index.html'
const server = Bun.serve({
port: process.env.PORT ? Number(process.env.PORT) : 3001,
routes: {
'/*': index,
'/api/hello': {
async GET(req) {
return Response.json({
message: 'Hello, world!',
method: 'GET',
})
},
async PUT(req) {
return Response.json({
message: 'Hello, world!',
method: 'PUT',
})
},
},
},
development: process.env.NODE_ENV !== 'production' && {
hmr: true,
console: true,
},
})
console.log(`🚀 Server running at ${server.url}`)

View File

@ -1,14 +1,33 @@
import { expect } from 'bun:test' import { expect } from 'bun:test'
import { diffLines } from 'diff' import { parser } from '#parser/shrimp'
import color from 'kleur' import { setGlobals } from '#parser/tokenizer'
import { Scanner, TokenType, type Token } from '#parser/tokenizer2'
import { parse, setGlobals } from '#parser/parser2'
import { Tree } from '#parser/node'
import { globals as prelude } from '#prelude' import { globals as prelude } from '#prelude'
import { $ } from 'bun'
import { assert, errorMessage } from '#utils/utils' import { assert, errorMessage } from '#utils/utils'
import { Compiler } from '#compiler/compiler' import { Compiler } from '#compiler/compiler'
import { run, VM } from 'reefvm' import { run, VM } from 'reefvm'
import { treeToString2, VMResultToValue } from '#utils/tree' import { treeToString, VMResultToValue } from '#utils/tree'
const regenerateParser = async () => {
let generate = true
try {
const grammarStat = await Bun.file('./src/parser/shrimp.grammar').stat()
const tokenizerStat = await Bun.file('./src/parser/tokenizer.ts').stat()
const parserStat = await Bun.file('./src/parser/shrimp.ts').stat()
if (grammarStat.mtime <= parserStat.mtime && tokenizerStat.mtime <= parserStat.mtime) {
generate = false
}
} catch (e) {
console.error('Error checking or regenerating parser:', e)
} finally {
if (generate) {
await $`bun generate-parser`
}
}
}
await regenerateParser()
// Type declaration for TypeScript // Type declaration for TypeScript
declare module 'bun:test' { declare module 'bun:test' {
@ -18,9 +37,6 @@ declare module 'bun:test' {
toFailParse(): T toFailParse(): T
toEvaluateTo(expected: unknown, globals?: Record<string, any>): Promise<T> toEvaluateTo(expected: unknown, globals?: Record<string, any>): Promise<T>
toFailEvaluation(): Promise<T> toFailEvaluation(): Promise<T>
toBeToken(expected: string): T
toMatchToken(typeOrValue: string, value?: string): T
toMatchTokens(...tokens: { type: string; value?: string }[]): T
} }
} }
@ -30,8 +46,8 @@ expect.extend({
const allGlobals = { ...prelude, ...(globals || {}) } const allGlobals = { ...prelude, ...(globals || {}) }
setGlobals(Object.keys(allGlobals)) setGlobals(Object.keys(allGlobals))
const tree = parse(received) const tree = parser.parse(received)
const actual = treeToString2(tree, received) const actual = treeToString(tree, received)
const normalizedExpected = trimWhitespace(expected) const normalizedExpected = trimWhitespace(expected)
try { try {
@ -50,8 +66,7 @@ expect.extend({
assert(typeof received === 'string', 'toFailParse can only be used with string values') assert(typeof received === 'string', 'toFailParse can only be used with string values')
try { try {
const node = parse(received) const tree = parser.parse(received)
const tree = new Tree(node)
let hasErrors = false let hasErrors = false
tree.iterate({ tree.iterate({
enter(n) { enter(n) {
@ -68,7 +83,7 @@ expect.extend({
pass: true, pass: true,
} }
} else { } else {
const actual = treeToString2(node, received) const actual = treeToString(tree, received)
return { return {
message: () => `Expected input to fail parsing, but it parsed successfully:\n${actual}`, message: () => `Expected input to fail parsing, but it parsed successfully:\n${actual}`,
pass: false, pass: false,
@ -129,109 +144,8 @@ expect.extend({
} }
} }
}, },
toBeToken(received: unknown, expected: string) {
assert(typeof received === 'string', 'toBeToken can only be used with string values')
try {
const tokens = tokenize(received)
const value = tokens[0] as Token
const target = TokenType[expected as keyof typeof TokenType]
if (!value) {
return {
message: () => `Expected token type to be ${expected}, but got ${value}`,
pass: false,
}
}
return {
message: () => `Expected token type to be ${expected}, but got ${TokenType[value.type]}`,
pass: value.type === target,
}
} catch (error) {
return {
message: () => `Tokenization failed: ${errorMessage(error)}`,
pass: false,
}
}
},
toMatchToken(received: unknown, typeOrValue: string, value?: string) {
assert(typeof received === 'string', 'toMatchToken can only be used with string values')
const expectedValue = value ? value : typeOrValue
const expectedType = value ? typeOrValue : undefined
try {
const tokens = tokenize(received)
const token = tokens[0] as Token
if (!token) {
return {
message: () =>
`Expected token to be ${expectedValue.replaceAll('\n', '\\n')}, got ${token}`,
pass: false,
}
}
if (expectedType && TokenType[expectedType as keyof typeof TokenType] !== token.type) {
return {
message: () => `Expected token to be ${expectedType}, but got ${TokenType[token.type]}`,
pass: false,
}
}
return {
message: () =>
`Expected token to be ${expectedValue.replaceAll('\n', '\\n')}, but got ${token.value}`,
pass: token.value === expectedValue,
}
} catch (error) {
return {
message: () => `Tokenization failed: ${errorMessage(error)} `,
pass: false,
}
}
},
toMatchTokens(received: unknown, ...tokens: { type: string; value?: string }[]) {
assert(typeof received === 'string', 'toMatchTokens can only be used with string values')
try {
const result = tokenize(received).map((t) => toHumanToken(t))
if (result.length === 0 && tokens.length > 0) {
return {
message: () => `Expected tokens ${JSON.stringify(tokens)}, got nothing`,
pass: false,
}
}
const expected = JSON.stringify(tokens, null, 2)
const actual = JSON.stringify(result, null, 2)
return {
message: () => `Tokens don't match: \n\n${diff(actual, expected)}`,
pass: expected == actual,
}
} catch (error) {
return {
message: () => `Tokenization failed: ${errorMessage(error)} `,
pass: false,
}
}
},
}) })
const tokenize = (code: string): Token[] => {
const scanner = new Scanner()
return scanner.tokenize(code)
}
const toHumanToken = (tok: Token): { type: string; value?: string } => {
return {
type: TokenType[tok.type],
value: tok.value,
}
}
const trimWhitespace = (str: string): string => { const trimWhitespace = (str: string): string => {
const lines = str.split('\n').filter((line) => line.trim().length > 0) const lines = str.split('\n').filter((line) => line.trim().length > 0)
const firstLine = lines[0] const firstLine = lines[0]
@ -243,33 +157,10 @@ const trimWhitespace = (str: string): string => {
if (!line.startsWith(leadingWhitespace)) { if (!line.startsWith(leadingWhitespace)) {
let foundWhitespace = line.match(/^(\s*)/)?.[1] || '' let foundWhitespace = line.match(/^(\s*)/)?.[1] || ''
throw new Error( throw new Error(
`Line has inconsistent leading whitespace: "${line}"(found "${foundWhitespace}", expected "${leadingWhitespace}")`, `Line has inconsistent leading whitespace: "${line}" (found "${foundWhitespace}", expected "${leadingWhitespace}")`
) )
} }
return line.slice(leadingWhitespace.length) return line.slice(leadingWhitespace.length)
}) })
.join('\n') .join('\n')
} }
const diff = (a: string, b: string): string => {
const expected = a.trim()
const actual = b.trim()
const lines = []
if (expected !== actual) {
const changes = diffLines(actual, expected)
for (const part of changes) {
const sign = part.added ? '+' : part.removed ? '-' : ' '
let line = sign + part.value
if (part.added) {
line = color.green(line)
} else if (part.removed) {
line = color.red(line)
}
lines.push(line.endsWith('\n') || line.endsWith('\n\u001b[39m') ? line : line + '\n')
}
}
return lines.join('\n')
}

View File

@ -1,6 +1,6 @@
import { describe } from 'bun:test' import { describe } from 'bun:test'
import { expect, test } from 'bun:test' import { expect, test } from 'bun:test'
import { Shrimp, runCode, compileCode, parseCode, bytecodeToString } from '..' import { Shrimp } from '..'
describe('Shrimp', () => { describe('Shrimp', () => {
test('allows running Shrimp code', async () => { test('allows running Shrimp code', async () => {
@ -50,403 +50,4 @@ describe('Shrimp', () => {
await shrimp.run('abc = nothing') await shrimp.run('abc = nothing')
expect(shrimp.get('abc')).toEqual('nothing') expect(shrimp.get('abc')).toEqual('nothing')
}) })
describe('set()', () => {
test('allows setting variables', async () => {
const shrimp = new Shrimp()
shrimp.set('foo', 42)
expect(shrimp.get('foo')).toEqual(42)
shrimp.set('bar', 'hello')
expect(shrimp.get('bar')).toEqual('hello')
})
test('set variables are accessible in code', async () => {
const shrimp = new Shrimp()
shrimp.set('x', 10)
shrimp.set('y', 20)
const result = await shrimp.run('x + y')
expect(result).toEqual(30)
})
test('allows setting functions', async () => {
const shrimp = new Shrimp()
shrimp.set('double', (n: number) => n * 2)
const result = await shrimp.run('double 21')
expect(result).toEqual(42)
})
test('overwrites existing variables', async () => {
const shrimp = new Shrimp()
await shrimp.run('x = 100')
expect(shrimp.get('x')).toEqual(100)
shrimp.set('x', 200)
expect(shrimp.get('x')).toEqual(200)
})
})
describe('has()', () => {
test('returns true for existing variables', async () => {
const shrimp = new Shrimp()
await shrimp.run('x = 5')
expect(shrimp.has('x')).toEqual(true)
})
test('returns false for non-existing variables', () => {
const shrimp = new Shrimp()
expect(shrimp.has('nonexistent')).toEqual(false)
})
test('returns true for globals', () => {
const shrimp = new Shrimp({ myGlobal: 42 })
expect(shrimp.has('myGlobal')).toEqual(true)
})
test('returns true for prelude functions', () => {
const shrimp = new Shrimp()
expect(shrimp.has('echo')).toEqual(true)
expect(shrimp.has('type')).toEqual(true)
})
})
describe('call()', () => {
test('calls Shrimp functions with positional args', async () => {
const shrimp = new Shrimp()
await shrimp.run(`add = do x y:
x + y
end`)
const result = await shrimp.call('add', 5, 10)
expect(result).toEqual(15)
})
test('calls Shrimp functions with named args', async () => {
const shrimp = new Shrimp()
await shrimp.run(`greet = do name:
str.join [ 'Hello ' name ] ''
end`)
const result = await shrimp.call('greet', { name: 'World' })
expect(result).toEqual('Hello World')
})
test('calls native functions', async () => {
const shrimp = new Shrimp()
shrimp.set('multiply', (a: number, b: number) => a * b)
const result = await shrimp.call('multiply', 6, 7)
expect(result).toEqual(42)
})
test('calls prelude functions', async () => {
const shrimp = new Shrimp()
const result = await shrimp.call('type', 42)
expect(result).toEqual('number')
})
test('calls async functions', async () => {
const shrimp = new Shrimp()
shrimp.set('fetchData', async () => {
return await Promise.resolve('async data')
})
const result = await shrimp.call('fetchData')
expect(result).toEqual('async data')
})
})
describe('compile()', () => {
test('compiles code to bytecode', () => {
const shrimp = new Shrimp()
const bytecode = shrimp.compile('x = 5')
expect(bytecode).toHaveProperty('instructions')
expect(bytecode).toHaveProperty('constants')
expect(bytecode).toHaveProperty('labels')
expect(bytecode.instructions.length).toBeGreaterThan(0)
})
test('respects globals when compiling', () => {
const shrimp = new Shrimp({ customGlobal: 42 })
const bytecode = shrimp.compile('x = customGlobal')
expect(bytecode.instructions.length).toBeGreaterThan(0)
})
test('compiled bytecode can be run', async () => {
const shrimp = new Shrimp()
const bytecode = shrimp.compile('2 * 21')
const result = await shrimp.run(bytecode)
expect(result).toEqual(42)
})
})
describe('parse()', () => {
test('parses code to syntax tree', () => {
const shrimp = new Shrimp()
const tree = shrimp.parse('x = 5')
expect(tree).toHaveProperty('length')
expect(tree).toHaveProperty('cursor')
expect(tree.length).toBeGreaterThan(0)
})
test('respects globals when parsing', () => {
const shrimp = new Shrimp({ myVar: 42 })
const tree = shrimp.parse('x = myVar + 10')
// Should parse without errors
expect(tree).toHaveProperty('length')
expect(tree.length).toBeGreaterThan(0)
})
test('parses function definitions', () => {
const shrimp = new Shrimp()
const tree = shrimp.parse(`add = do x y:
x + y
end`)
expect(tree.length).toBeGreaterThan(0)
})
})
describe('get()', () => {
test('returns null for undefined variables', () => {
const shrimp = new Shrimp()
expect(shrimp.get('undefined')).toEqual(null)
})
test('returns values from code execution', async () => {
const shrimp = new Shrimp()
await shrimp.run('x = 42')
expect(shrimp.get('x')).toEqual(42)
})
test('returns arrays', async () => {
const shrimp = new Shrimp()
await shrimp.run('arr = [1 2 3]')
expect(shrimp.get('arr')).toEqual([1, 2, 3])
})
test('returns dicts', async () => {
const shrimp = new Shrimp()
await shrimp.run('dict = [a=1 b=2]')
expect(shrimp.get('dict')).toEqual({ a: 1, b: 2 })
})
})
describe('running bytecode directly', () => {
test('can run pre-compiled bytecode', async () => {
const shrimp = new Shrimp()
const bytecode = shrimp.compile('x = 100')
const result = await shrimp.run(bytecode)
expect(result).toEqual(100)
expect(shrimp.get('x')).toEqual(100)
})
test('maintains state across bytecode runs', async () => {
const shrimp = new Shrimp()
const bytecode1 = shrimp.compile('x = 10')
const bytecode2 = shrimp.compile('x + 5')
await shrimp.run(bytecode1)
const result = await shrimp.run(bytecode2)
expect(result).toEqual(15)
})
})
})
describe('Functional API', () => {
describe('runCode()', () => {
test('runs code and returns result', async () => {
const result = await runCode('1 + 1')
expect(result).toEqual(2)
})
test('works with globals', async () => {
const result = await runCode('greet', { greet: () => 'hello' })
expect(result).toEqual('hello')
})
test('has access to prelude', async () => {
const result = await runCode('type 42')
expect(result).toEqual('number')
})
test('returns null for empty code', async () => {
const result = await runCode('')
expect(result).toEqual(null)
})
})
describe('compileCode()', () => {
test('compiles code to bytecode', () => {
const bytecode = compileCode('x = 5')
expect(bytecode).toHaveProperty('instructions')
expect(bytecode).toHaveProperty('constants')
expect(bytecode.instructions.length).toBeGreaterThan(0)
})
test('respects globals', () => {
const bytecode = compileCode('x = myGlobal', { myGlobal: 42 })
expect(bytecode.instructions.length).toBeGreaterThan(0)
})
test('compiled bytecode is usable', async () => {
const bytecode = compileCode('21 * 2')
const result = await runCode('21 * 2')
expect(result).toEqual(42)
})
})
describe('parseCode()', () => {
test('parses code to syntax tree', () => {
const tree = parseCode('x = 5')
expect(tree).toHaveProperty('length')
expect(tree.length).toBeGreaterThan(0)
})
test('respects globals', () => {
const tree = parseCode('x = myGlobal', { myGlobal: 42 })
expect(tree.length).toBeGreaterThan(0)
})
test('handles complex expressions', () => {
const tree = parseCode(`add = do x y:
x + y
end
result = add 5 10`)
expect(tree.length).toBeGreaterThan(0)
})
})
describe('bytecodeToString()', () => {
test('converts bytecode to human-readable format', () => {
const bytecode = compileCode('x = 42')
const str = bytecodeToString(bytecode)
expect(typeof str).toEqual('string')
expect(str.length).toBeGreaterThan(0)
})
test('shows instructions', () => {
const bytecode = compileCode('1 + 1')
const str = bytecodeToString(bytecode)
// Should contain some opcodes
expect(str).toContain('PUSH')
})
})
})
describe('Integration tests', () => {
test('complex REPL-like workflow', async () => {
const shrimp = new Shrimp()
// Define a function
await shrimp.run(`double = do x:
x * 2
end`)
expect(shrimp.has('double')).toEqual(true)
// Use the function
const result1 = await shrimp.run('double 21')
expect(result1).toEqual(42)
// Call it from TypeScript
const result2 = await shrimp.call('double', 50)
expect(result2).toEqual(100)
// Define another function using the first
await shrimp.run(`quadruple = do x:
double (double x)
end`)
const result3 = await shrimp.run('quadruple 5')
expect(result3).toEqual(20)
})
test('mixing native and Shrimp functions', async () => {
const shrimp = new Shrimp({
log: (msg: string) => `Logged: ${msg}`,
multiply: (a: number, b: number) => a * b,
})
await shrimp.run(`greet = do name:
log name
end`)
const result1 = await shrimp.run('greet Alice')
expect(result1).toEqual('Logged: Alice')
await shrimp.run(`calc = do x:
multiply x 3
end`)
const result2 = await shrimp.run('calc 7')
expect(result2).toEqual(21)
})
test('working with arrays and dicts', async () => {
const shrimp = new Shrimp()
await shrimp.run('nums = [1 2 3 4 5]')
expect(shrimp.get('nums')).toEqual([1, 2, 3, 4, 5])
await shrimp.run("config = [host='localhost' port=3000]")
expect(shrimp.get('config')).toEqual({ host: 'localhost', port: 3000 })
const result = await shrimp.run('length nums')
expect(result).toEqual(5)
})
test('compile once, run multiple times', async () => {
const bytecode = compileCode('x * 2')
const shrimp1 = new Shrimp()
shrimp1.set('x', 10)
const result1 = await shrimp1.run(bytecode)
expect(result1).toEqual(20)
const shrimp2 = new Shrimp()
shrimp2.set('x', 100)
const result2 = await shrimp2.run(bytecode)
expect(result2).toEqual(200)
})
}) })

68
src/utils/signal.ts Normal file
View File

@ -0,0 +1,68 @@
/**
* How to use a Signal:
*
* Create a signal with primitives:
* const nameSignal = new Signal<string>()
* const countSignal = new Signal<number>()
*
* Create a signal with objects:
* const chatSignal = new Signal<{ username: string, message: string }>()
*
* Create a signal with no data (void):
* const clickSignal = new Signal<void>()
* const clickSignal2 = new Signal() // Defaults to void
*
* Connect to the signal:
* const disconnect = chatSignal.connect((data) => {
* const {username, message} = data;
* console.log(`${username} said "${message}"`);
* })
*
* Emit a signal:
* nameSignal.emit("Alice")
* countSignal.emit(42)
* chatSignal.emit({ username: "Chad", message: "Hey everyone, how's it going?" });
* clickSignal.emit() // No argument for void signals
*
* Forward a signal:
* const relaySignal = new Signal<{ username: string, message: string }>()
* const disconnectRelay = chatSignal.connect(relaySignal)
* // Now, when chatSignal emits, relaySignal will also emit the same data
*
* Disconnect a single listener:
* disconnect(); // The disconnect function is returned when you connect to a signal
*
* Disconnect all listeners:
* chatSignal.disconnect()
*/
export class Signal<T = void> {
private listeners: Array<(data: T) => void> = []
connect(listenerOrSignal: Signal<T> | ((data: T) => void)) {
let listener: (data: T) => void
// If it is a signal, forward the data to the signal
if (listenerOrSignal instanceof Signal) {
listener = (data: T) => listenerOrSignal.emit(data)
} else {
listener = listenerOrSignal
}
this.listeners.push(listener)
return () => {
this.listeners = this.listeners.filter((l) => l !== listener)
}
}
emit(data: T) {
for (const listener of this.listeners) {
listener(data)
}
}
disconnect() {
this.listeners = []
}
}

View File

@ -1,50 +1,5 @@
import { Tree, TreeCursor } from '@lezer/common' import { Tree, TreeCursor } from '@lezer/common'
import { type Value, fromValue } from 'reefvm' import { type Value, fromValue } from 'reefvm'
import { SyntaxNode } from '#parser/node'
const nodeToString = (node: SyntaxNode, input: string, depth = 0): string => {
const indent = ' '.repeat(depth)
const text = input.slice(node.from, node.to)
const nodeName = node.name
if (node.firstChild) {
return `${indent}${nodeName}`
} else {
// Only strip quotes from whole String nodes (legacy DoubleQuote), not StringFragment/EscapeSeq/CurlyString
let cleanText = nodeName === 'String' ? text.slice(1, -1) : text
if (cleanText === ' ') cleanText = '(space)'
return cleanText ? `${indent}${nodeName} ${cleanText}` : `${indent}${nodeName}`
}
}
export const treeToString2 = (tree: SyntaxNode, input: string, depth = 0): string => {
let lines = []
let node: SyntaxNode | null = tree
if (node.name === 'Program') node = node.firstChild
while (node) {
// If this node is an error, print ⚠ instead of its content
if (node.isError && !node.firstChild) {
lines.push(' '.repeat(depth) + '⚠')
} else {
lines.push(nodeToString(node, input, depth))
if (node.firstChild) {
lines.push(treeToString2(node.firstChild, input, depth + 1))
}
// If this node has an error, add ⚠ after its children
if (node.isError && node.firstChild) {
lines.push(' '.repeat(depth === 0 ? 0 : depth + 1) + '⚠')
}
}
node = node.nextSibling
}
return lines.join('\n')
}
export const treeToString = (tree: Tree, input: string): string => { export const treeToString = (tree: Tree, input: string): string => {
const lines: string[] = [] const lines: string[] = []

View File

@ -136,12 +136,3 @@ export const asciiEscapeToHtml = (str: string): HtmlEscapedString => {
return result as HtmlEscapedString return result as HtmlEscapedString
} }
export const isDebug = (): boolean => {
if (typeof process !== 'undefined' && process.env) {
return !!process.env.DEBUG
}
return false
}
export const runningInBrowser = typeof window !== 'undefined'

0
today.md Normal file
View File

Some files were not shown because too many files have changed in this diff Show More