Compare commits

..

42 Commits

Author SHA1 Message Date
69bbe17992 "add double quoted strings" 2025-11-08 16:14:08 -08:00
2d4c79b30f topNode.topNode 2025-11-08 16:13:40 -08:00
238af9affc fix edge case 2025-11-08 16:13:40 -08:00
a6c283759d interpolation in { curly strings } 2025-11-08 16:13:40 -08:00
63ee57e7f0 curly -> Curly 2025-11-08 16:13:39 -08:00
503ca41155 { curly strings } 2025-11-08 16:13:28 -08:00
a156d24a91 Merge pull request 'Add ?? and ??= operators' (#42) from null-check-operators into main
Reviewed-on: #42
2025-11-09 00:12:34 +00:00
019f7d84b1 Add ?? and ??= operators 2025-11-08 16:12:20 -08:00
4c794944ef Merge pull request 'bitwise' (#36) from bitwise into main
Reviewed-on: #36
2025-11-09 00:11:19 +00:00
99a5aa5312 update reef, fix precedence test 2025-11-08 16:11:05 -08:00
7bbf43a725 bitwise operators 2025-11-08 16:10:13 -08:00
4c15526d1b Merge pull request 'hex and binary numbers' (#40) from hex-and-binary-numbers into main
Reviewed-on: #40
2025-11-09 00:05:46 +00:00
c741cfee51 hex and binary numbers 2025-11-08 16:05:28 -08:00
012b8c8cf1 Merge pull request 'ref keyword' (#38) from ref-keyword into main
Reviewed-on: #38
2025-11-09 00:03:44 +00:00
4c3f7a8bfc add ref for grabbing a function 2025-11-08 16:03:25 -08:00
fe6f54b402 Merge pull request 'Passing null to a function triggers default value' (#41) from null-triggers-default-values into main
Reviewed-on: #41
2025-11-09 00:02:27 +00:00
49f3f3e09f Merge pull request 'Fix calling no-arg dotget functions' (#37) from no-args-bug into main
Reviewed-on: #37
2025-11-09 00:01:57 +00:00
0d1dce4868 fix calling no-arg dotget functions 2025-11-08 16:01:40 -08:00
d18ab2507c Merge pull request 'Allow _ in numbers (10_000_000)' (#34) from underscore-in-numbers into main
Reviewed-on: #34
2025-11-09 00:00:40 +00:00
7e69356f79 allow _ in numbers (10_000_000) 2025-11-08 16:00:25 -08:00
9863f46f38 Merge pull request 'JSON!' (#33) from json into main
Reviewed-on: #33
2025-11-08 23:59:01 +00:00
45f31d0678 allow newlines in (some) parens expressions 2025-11-08 11:26:46 -08:00
49a6320fef add list.insert 2025-11-08 11:17:45 -08:00
51f67ac908 Passing null to a function triggers default value 2025-11-08 10:53:54 -08:00
7da437212d Merge pull request ''#comments are comments too, but not hash#in#words'' (#39) from no-comment-space into main
Reviewed-on: #39
2025-11-08 16:45:57 +00:00
740379d7b2 fix dotgets in repl 2025-11-08 08:42:43 -08:00
19c4fb5033 args and exit 2025-11-08 08:22:41 -08:00
f57452ece2 list.reject 2025-11-08 08:16:39 -08:00
4590d66105 '#comments are comments too, but not hash#in#words' 2025-11-08 08:09:58 -08:00
3aa40ae2c2 fix fromValue 2025-11-07 21:50:01 -08:00
da0af799d8 add more Shrimp API, tests 2025-11-07 21:43:55 -08:00
9f45252522 cli: don't re-implement stuff 2025-11-07 21:10:25 -08:00
bae0da31c2 globals accepts {} too 2025-11-07 21:05:42 -08:00
4258503c0e shrimp cli wants the prelude too 2025-11-07 20:50:48 -08:00
d4a772e88b json.encode & json.decode 2025-11-07 19:42:04 -08:00
68ec6f9f3e Merge pull request 'atoms-can-be-piped' (#31) from atoms-can-be-piped into main
Reviewed-on: #31
Reviewed-by: probablycorey <probablycorey@gmail.com>
2025-11-08 02:48:11 +00:00
Chris Wanstrath
59cf459d74 cli:install and cli:remove 2025-11-07 15:48:35 -08:00
Chris Wanstrath
890eb811b9 pipe atoms/literals to functions 2025-11-07 15:17:29 -08:00
Chris Wanstrath
fd3c5da59b coerce values to string in str prelude functions 2025-11-07 15:16:59 -08:00
afaedeea23 probably using list.sort is okay? 2025-11-06 21:39:51 -08:00
3ac606d0b2 prelude is now preloaded 2025-11-06 21:38:04 -08:00
62e42328e1 fix test issues 2025-11-06 21:34:35 -08:00
27 changed files with 1827 additions and 360 deletions

View File

@ -145,7 +145,7 @@ async function repl() {
}
try {
const compiler = new Compiler(trimmed, Object.keys(globals))
const compiler = new Compiler(trimmed, [...Object.keys(globals), ...vm.vars()])
// Save VM state before appending bytecode, in case execution fails
const savedInstructions = [...vm.instructions]
@ -235,7 +235,7 @@ async function loadFile(filePath: string): Promise<{ vm: VM; codeHistory: string
if (!trimmed) continue
try {
const compiler = new Compiler(trimmed)
const compiler = new Compiler(trimmed, [...Object.keys(globals), ...vm.vars()])
vm.appendBytecode(compiler.bytecode)
await vm.continue()
codeHistory.push(trimmed)

View File

@ -1,50 +1,14 @@
#!/usr/bin/env bun
import { Compiler } from '../src/compiler/compiler'
import { colors, globals } from '../src/prelude'
import { parser } from '../src/parser/shrimp'
import { colors } from '../src/prelude'
import { treeToString } from '../src/utils/tree'
import { VM, fromValue, bytecodeToString } from 'reefvm'
import { runFile, compileFile, parseCode } from '../src'
import { bytecodeToString } from 'reefvm'
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
import { randomUUID } from 'crypto'
import { spawn } from 'child_process'
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() {
console.log(`${colors.bright}${colors.magenta}🦐 Shrimp${colors.reset} is a scripting language in a shell.
@ -112,7 +76,7 @@ async function main() {
console.log(`${colors.bright}usage: shrimp bytecode <file>${colors.reset}`)
process.exit(1)
}
console.log(await compileFile(file))
console.log(bytecodeToString(compileFile(file)))
return
}
@ -122,7 +86,8 @@ async function main() {
console.log(`${colors.bright}usage: shrimp parse <file>${colors.reset}`)
process.exit(1)
}
console.log(await parseFile(file))
const input = readFileSync(file, 'utf-8')
console.log(treeToString(parseCode(input), input))
return
}

View File

@ -44,7 +44,7 @@
"@marijn/find-cluster-break": ["@marijn/find-cluster-break@1.0.2", "", {}, "sha512-l0h88YhZFyKdXIFNfSWpyjStDjGHwZ/U7iobcK1cQQD8sejsONdQtTVU+1wVN1PBw40PiiHB1vA5S7VTfQiP9g=="],
"@types/bun": ["@types/bun@1.3.1", "", { "dependencies": { "bun-types": "1.3.1" } }, "sha512-4jNMk2/K9YJtfqwoAa28c8wK+T7nvJFOjxI4h/7sORWcypRNxBpr+TPNaCfVWq70tLCJsqoFwcf0oI0JU/fvMQ=="],
"@types/bun": ["@types/bun@1.3.2", "", { "dependencies": { "bun-types": "1.3.2" } }, "sha512-t15P7k5UIgHKkxwnMNkJbWlh/617rkDGEdSsDbu+qNHTaz9SKf7aC8fiIlUdD5RPpH6GEkP0cK7WlvmrEBRtWg=="],
"@types/node": ["@types/node@24.10.0", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-qzQZRBqkFsYyaSWXuEHc2WR9c0a0CXwiE5FWUvn7ZM+vdy1uZLfCunD38UzhuB7YN/J11ndbDBcTmOdxJo9Q7A=="],
@ -52,7 +52,7 @@
"bun-plugin-tailwind": ["bun-plugin-tailwind@0.0.15", "", { "peerDependencies": { "typescript": "^5.0.0" } }, "sha512-qtAXMNGG4R0UGGI8zWrqm2B7BdXqx48vunJXBPzfDOHPA5WkRUZdTSbE7TFwO4jLhYqSE23YMWsM9NhE6ovobw=="],
"bun-types": ["bun-types@1.3.1", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-NMrcy7smratanWJ2mMXdpatalovtxVggkj11bScuWuiOoXTiKIu2eVS1/7qbyI/4yHedtsn175n4Sm4JcdHLXw=="],
"bun-types": ["bun-types@1.3.2", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-i/Gln4tbzKNuxP70OWhJRZz1MRfvqExowP7U6JKoI8cntFrtxg7RJK3jvz7wQW54UuvNC8tbKHHri5fy74FVqg=="],
"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,11 +62,11 @@
"hono": ["hono@4.10.4", "", {}, "sha512-YG/fo7zlU3KwrBL5vDpWKisLYiM+nVstBQqfr7gCPbSYURnNEP9BDxEMz8KfsDR9JX0lJWDRNc6nXX31v7ZEyg=="],
"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=="],
"tailwindcss": ["tailwindcss@4.1.16", "", {}, "sha512-pONL5awpaQX4LN5eiv7moSiSPd/DLDzKVRJz8Q9PgzmAdd1R4307GQS2ZpfiN7ZmekdQrfhZZiSE5jkLR4WNaA=="],
"tailwindcss": ["tailwindcss@4.1.17", "", {}, "sha512-j9Ee2YjuQqYT9bbRTfTZht9W/ytp5H+jJpZKiYdP/bpnXARAuELt9ofP0lPnmHjbga7SNQIxdTAXCmtKVYjN+Q=="],
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],

View File

@ -8,7 +8,9 @@
"dev": "bun generate-parser && bun --hot src/server/server.tsx",
"generate-parser": "lezer-generator src/parser/shrimp.grammar --typeScript -o src/parser/shrimp.ts",
"repl": "bun generate-parser && bun bin/repl",
"update-reef": "rm -rf ~/.bun/install/cache/ && rm bun.lock && bun update reefvm"
"update-reef": "rm -rf ~/.bun/install/cache/ && rm bun.lock && bun update reefvm",
"cli:install": "ln -s \"$(pwd)/bin/shrimp\" ~/.bun/bin/shrimp",
"cli:remove": "rm ~/.bun/bin/shrimp"
},
"dependencies": {
"@codemirror/view": "^6.38.3",

View File

@ -52,6 +52,7 @@ function processEscapeSeq(escapeSeq: string): string {
export class Compiler {
instructions: ProgramItem[] = []
labelCount = 0
fnLabelCount = 0
ifLabelCount = 0
tryLabelCount = 0
@ -59,9 +60,9 @@ export class Compiler {
bytecode: Bytecode
pipeCounter = 0
constructor(public input: string, globals?: string[]) {
constructor(public input: string, globals?: string[] | Record<string, any>) {
try {
if (globals) setGlobals(globals)
if (globals) setGlobals(Array.isArray(globals) ? globals : Object.keys(globals))
const cst = parser.parse(input)
const errors = checkTreeForErrors(cst)
@ -106,11 +107,21 @@ export class Compiler {
switch (node.type.id) {
case terms.Number:
const number = Number(value)
if (Number.isNaN(number))
// Handle sign prefix for hex and binary literals
// Number() doesn't parse '-0xFF' or '+0xFF' correctly
let numberValue: number
if (value.startsWith('-') && (value.includes('0x') || value.includes('0b'))) {
numberValue = -Number(value.slice(1))
} else if (value.startsWith('+') && (value.includes('0x') || value.includes('0b'))) {
numberValue = Number(value.slice(1))
} else {
numberValue = Number(value)
}
if (Number.isNaN(numberValue))
throw new CompilerError(`Invalid number literal: ${value}`, node.from, node.to)
return [[`PUSH`, number]]
return [[`PUSH`, numberValue]]
case terms.String: {
if (node.firstChild?.type.id === terms.CurlyString)
@ -236,6 +247,24 @@ export class Compiler {
case '%':
instructions.push(['MOD'])
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:
throw new CompilerError(`Unsupported binary operator: ${opValue}`, op.from, op.to)
}
@ -278,13 +307,31 @@ export class Compiler {
const { identifier, operator, right } = getCompoundAssignmentParts(node)
const identifierName = input.slice(identifier.from, identifier.to)
const instructions: ProgramItem[] = []
const opValue = input.slice(operator.from, operator.to)
// will throw if undefined
instructions.push(['LOAD', identifierName])
// Special handling for ??= since it needs conditional evaluation
if (opValue === '??=') {
instructions.push(['LOAD', identifierName])
const rightInstructions = this.#compileNode(right, input)
instructions.push(['DUP'])
instructions.push(['PUSH', null])
instructions.push(['NEQ'])
instructions.push(['JUMP_IF_TRUE', rightInstructions.length + 1])
instructions.push(['POP'])
instructions.push(...rightInstructions)
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))
const opValue = input.slice(operator.from, operator.to)
switch (opValue) {
case '+=':
instructions.push(['ADD'])
@ -371,7 +418,29 @@ export class Compiler {
case terms.FunctionCallOrIdentifier: {
if (node.firstChild?.type.id === terms.DotGet) {
return this.#compileNode(node.firstChild, input)
const instructions: ProgramItem[] = []
const callLabel = `.call_dotget_${++this.labelCount}`
const afterLabel = `.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]]
@ -591,6 +660,18 @@ export class Compiler {
break
case '??':
// Nullish coalescing: return left if not null, else right
instructions.push(...leftInstructions)
instructions.push(['DUP'])
instructions.push(['PUSH', null])
instructions.push(['NEQ'])
instructions.push(['JUMP_IF_TRUE', rightInstructions.length + 1])
instructions.push(['POP'])
instructions.push(...rightInstructions)
break
default:
throw new CompilerError(`Unsupported conditional operator: ${opValue}`, op.from, op.to)
}

View File

@ -0,0 +1,178 @@
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,7 +110,10 @@ describe('compiler', () => {
})
test('function call with no args', () => {
expect(`bloop = do: 'bloop' end; bloop`).toEvaluateTo('bloop')
expect(`bloop = do: 'bleep' end; bloop`).toEvaluateTo('bleep')
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', () => {
@ -298,6 +301,23 @@ describe('default params', () => {
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', () => {
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'])
@ -313,3 +333,118 @@ describe('default params', () => {
).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)
})
})

View File

@ -1,6 +1,44 @@
import { describe } 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('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 and binary', () => {
expect('-0xFF').toEvaluateTo(-255)
expect('-0b1010').toEvaluateTo(-10)
})
test('positive prefix', () => {
expect('+0xFF').toEvaluateTo(255)
expect('+0b110').toEvaluateTo(6)
expect('+42').toEvaluateTo(42)
})
})
describe('array literals', () => {
test('work with numbers', () => {
expect('[1 2 3]').toEvaluateTo([1, 2, 3])

View File

@ -92,4 +92,30 @@ describe('pipe expressions', () => {
get-msg | length
`).toEvaluateTo(5)
})
test('string literals can be piped', () => {
expect(`'hey there' | str.to-upper`).toEvaluateTo('HEY THERE')
})
test('number literals can be piped', () => {
expect(`42 | str.trim`).toEvaluateTo('42')
expect(`4.22 | str.trim`).toEvaluateTo('4.22')
})
test('null literals can be piped', () => {
expect(`null | type`).toEvaluateTo('null')
})
test('boolean literals can be piped', () => {
expect(`true | str.to-upper`).toEvaluateTo('TRUE')
})
test('array literals can be piped', () => {
expect(`[1 2 3] | str.join '-'`).toEvaluateTo('1-2-3')
})
test('dict literals can be piped', () => {
expect(`[a=1 b=2 c=3] | dict.values | list.sort | str.join '-'`).toEvaluateTo('1-2-3')
})
})

View File

@ -1,11 +1,17 @@
import { readFileSync } from 'fs'
import { VM, fromValue, type Bytecode } from 'reefvm'
import { VM, fromValue, toValue, isValue, type Bytecode } from 'reefvm'
import { type Tree } from '@lezer/common'
import { Compiler } from '#compiler/compiler'
import { globals as shrimpGlobals, colors } from '#prelude'
import { parser } from '#parser/shrimp'
import { globals as parserGlobals, setGlobals as setParserGlobals } from '#parser/tokenizer'
import { globals as shrimpGlobals } from '#prelude'
export { Compiler } from '#compiler/compiler'
export { parser } from '#parser/shrimp'
export { globals } from '#prelude'
export { globals as prelude } from '#prelude'
export type { Tree } from '@lezer/common'
export { type Value, type Bytecode } from 'reefvm'
export { toValue, fromValue, isValue, Scope, VM, bytecodeToString } from 'reefvm'
export class Shrimp {
vm: VM
@ -17,6 +23,32 @@ export class Shrimp {
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
@ -32,13 +64,9 @@ export class Shrimp {
await this.vm.continue()
if (locals) this.vm.popScope()
return this.vm.stack.length ? fromValue(this.vm.stack.at(-1)!) : null
return this.vm.stack.length ? fromValue(this.vm.stack.at(-1)!, this.vm) : 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> {
@ -51,14 +79,9 @@ export async function runCode(code: string, globals?: Record<string, any>): Prom
}
export async function runBytecode(bytecode: Bytecode, globals?: Record<string, any>): Promise<any> {
try {
const vm = new VM(bytecode, Object.assign({}, shrimpGlobals, 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)
}
const vm = new VM(bytecode, Object.assign({}, shrimpGlobals, globals))
await vm.run()
return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]!, vm) : null
}
export function compileFile(path: string, globals?: Record<string, any>): Bytecode {
@ -70,4 +93,20 @@ export function compileCode(code: string, globals?: Record<string, any>): Byteco
const globalNames = [...Object.keys(shrimpGlobals), ...(globals ? Object.keys(globals) : [])]
const compiler = new Compiler(code, globalNames)
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(shrimpGlobals), ...(globals ? Object.keys(globals) : [])]
setParserGlobals(globalNames)
const result = parser.parse(code)
setParserGlobals(oldGlobals)
return result
}

View File

@ -5,18 +5,28 @@ type Operator = { str: string; tokenName: keyof typeof terms }
const operators: Array<Operator> = [
{ str: 'and', tokenName: 'And' },
{ str: 'or', tokenName: 'Or' },
{ str: 'band', tokenName: 'Band' },
{ str: 'bor', tokenName: 'Bor' },
{ str: 'bxor', tokenName: 'Bxor' },
{ str: '>>>', tokenName: 'Ushr' }, // Must come before >>
{ str: '>>', tokenName: 'Shr' },
{ str: '<<', tokenName: 'Shl' },
{ str: '>=', tokenName: 'Gte' },
{ str: '<=', tokenName: 'Lte' },
{ str: '!=', tokenName: 'Neq' },
{ str: '==', tokenName: 'EqEq' },
// Compound assignment operators (must come before single-char operators)
{ str: '??=', tokenName: 'NullishEq' },
{ str: '+=', tokenName: 'PlusEq' },
{ str: '-=', tokenName: 'MinusEq' },
{ str: '*=', tokenName: 'StarEq' },
{ str: '/=', tokenName: 'SlashEq' },
{ str: '%=', tokenName: 'ModuloEq' },
// Nullish coalescing (must come before it could be mistaken for other tokens)
{ str: '??', tokenName: 'NullishCoalesce' },
// Single-char operators
{ str: '*', tokenName: 'Star' },
{ str: '=', tokenName: 'Eq' },

View File

@ -6,20 +6,24 @@
@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 }
@external tokens operatorTokenizer from "./operatorTokenizer" { Star, Slash, Plus, Minus, And, Or, Eq, EqEq, Neq, Lt, Lte, Gt, Gte, Modulo, PlusEq, MinusEq, StarEq, SlashEq, ModuloEq, Band, Bor, Bxor, Shl, Shr, Ushr, NullishCoalesce, NullishEq }
@tokens {
@precedence { Number Regex }
StringFragment { !['\\$]+ }
DoubleQuote { '"' !["]* '"' }
NamedArgPrefix { $[a-z-]+ "=" }
Number { ("-" | "+")? $[0-9]+ ('.' $[0-9]+)? }
NamedArgPrefix { $[a-z] $[a-z0-9-]* "=" }
Number {
("-" | "+")? "0x" $[0-9a-fA-F]+ |
("-" | "+")? "0b" $[01]+ |
("-" | "+")? $[0-9]+ ("_"? $[0-9]+)* ('.' $[0-9]+ ("_"? $[0-9]+)*)?
}
Boolean { "true" | "false" }
newlineOrSemicolon { "\n" | ";" }
eof { @eof }
space { " " | "\t" }
Comment { "#" " " ![\n]* }
Comment { "#" ![\n]* }
leftParen { "(" }
rightParen { ")" }
colon[closedBy="end", @name="colon"] { ":" }
@ -45,13 +49,16 @@ null { @specialize[@name=Null]<Identifier, "null"> }
pipe @left,
or @left,
and @left,
nullish @left,
comparison @left,
multiplicative @left,
additive @left,
call
bitwise @left,
call,
functionWithNewlines
}
item {
item {
consumeToTerminator newlineOrSemicolon |
consumeToTerminator eof |
newlineOrSemicolon // allow blank lines
@ -78,7 +85,7 @@ PipeExpr {
}
pipeOperand {
FunctionCall | FunctionCallOrIdentifier
consumeToTerminator
}
WhileExpr {
@ -161,7 +168,8 @@ ConditionalOp {
expression !comparison Gt expression |
expression !comparison Gte expression |
(expression | ConditionalOp) !and And (expression | ConditionalOp) |
(expression | ConditionalOp) !or Or (expression | ConditionalOp)
(expression | ConditionalOp) !or Or (expression | ConditionalOp) |
(expression | ConditionalOp) !nullish NullishCoalesce (expression | ConditionalOp)
}
Params {
@ -177,7 +185,7 @@ Assign {
}
CompoundAssign {
AssignableIdentifier (PlusEq | MinusEq | StarEq | SlashEq | ModuloEq) consumeToTerminator
AssignableIdentifier (PlusEq | MinusEq | StarEq | SlashEq | ModuloEq | NullishEq) consumeToTerminator
}
BinOp {
@ -185,11 +193,31 @@ BinOp {
(expression | BinOp) !multiplicative Star (expression | BinOp) |
(expression | BinOp) !multiplicative Slash (expression | BinOp) |
(expression | BinOp) !additive Plus (expression | BinOp) |
(expression | BinOp) !additive Minus (expression | BinOp)
(expression | BinOp) !additive Minus (expression | BinOp) |
(expression | BinOp) !bitwise Band (expression | BinOp) |
(expression | BinOp) !bitwise Bor (expression | BinOp) |
(expression | BinOp) !bitwise Bxor (expression | BinOp) |
(expression | BinOp) !bitwise Shl (expression | BinOp) |
(expression | BinOp) !bitwise Shr (expression | BinOp) |
(expression | BinOp) !bitwise Ushr (expression | BinOp)
}
ParenExpr {
leftParen (IfExpr | ambiguousFunctionCall | BinOp | expressionWithoutIdentifier | ConditionalOp | PipeExpr | FunctionDef) rightParen
leftParen newlineOrSemicolon* (
FunctionCallWithNewlines |
IfExpr |
ambiguousFunctionCall |
BinOp newlineOrSemicolon* |
expressionWithoutIdentifier |
ConditionalOp newlineOrSemicolon* |
PipeExpr |
FunctionDef
)
rightParen
}
FunctionCallWithNewlines[@name=FunctionCall] {
(DotGet | Identifier | ParenExpr) newlineOrSemicolon+ arg !functionWithNewlines (newlineOrSemicolon+ arg)* newlineOrSemicolon*
}
expression {
@ -238,7 +266,7 @@ Array {
// 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
// - 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).

View File

@ -19,50 +19,59 @@ export const
StarEq = 17,
SlashEq = 18,
ModuloEq = 19,
Identifier = 20,
AssignableIdentifier = 21,
Word = 22,
IdentifierBeforeDot = 23,
CurlyString = 24,
Do = 25,
Comment = 26,
Program = 27,
PipeExpr = 28,
FunctionCall = 29,
DotGet = 30,
Number = 31,
ParenExpr = 32,
IfExpr = 33,
keyword = 72,
ConditionalOp = 35,
String = 36,
StringFragment = 37,
Interpolation = 38,
EscapeSeq = 39,
DoubleQuote = 40,
Boolean = 41,
Regex = 42,
Dict = 43,
NamedArg = 44,
NamedArgPrefix = 45,
Band = 20,
Bor = 21,
Bxor = 22,
Shl = 23,
Shr = 24,
Ushr = 25,
NullishCoalesce = 26,
NullishEq = 27,
Identifier = 28,
AssignableIdentifier = 29,
Word = 30,
IdentifierBeforeDot = 31,
CurlyString = 32,
Do = 33,
Comment = 34,
Program = 35,
PipeExpr = 36,
WhileExpr = 38,
keyword = 81,
ConditionalOp = 40,
ParenExpr = 41,
FunctionCallWithNewlines = 42,
DotGet = 43,
Number = 44,
PositionalArg = 45,
FunctionDef = 46,
Params = 47,
NamedParam = 48,
Null = 49,
colon = 50,
CatchExpr = 51,
Block = 53,
FinallyExpr = 54,
Underscore = 57,
Array = 58,
ElseIfExpr = 59,
ElseExpr = 61,
FunctionCallOrIdentifier = 62,
BinOp = 63,
PositionalArg = 64,
WhileExpr = 66,
FunctionCallWithBlock = 68,
TryExpr = 69,
Throw = 71,
CompoundAssign = 73,
Assign = 74
NamedArgPrefix = 49,
String = 50,
StringFragment = 51,
Interpolation = 52,
EscapeSeq = 53,
DoubleQuote = 54,
Boolean = 55,
Null = 56,
colon = 57,
CatchExpr = 58,
Block = 60,
FinallyExpr = 61,
Underscore = 64,
NamedArg = 65,
IfExpr = 66,
FunctionCall = 68,
ElseIfExpr = 69,
ElseExpr = 71,
FunctionCallOrIdentifier = 72,
BinOp = 73,
Regex = 74,
Dict = 75,
Array = 76,
FunctionCallWithBlock = 77,
TryExpr = 78,
Throw = 80,
CompoundAssign = 82,
Assign = 83

View File

@ -4,24 +4,24 @@ import {operatorTokenizer} from "./operatorTokenizer"
import {tokenizer, specializeKeyword} from "./tokenizer"
import {trackScope} from "./parserScopeContext"
import {highlighting} from "./highlight"
const spec_Identifier = {__proto__:null,if:68, null:98, catch:104, finally:110, end:112, else:120, while:134, try:140, throw:144}
const spec_Identifier = {__proto__:null,while:78, null:112, catch:118, finally:124, end:126, if:134, else:140, try:158, throw:162}
export const parser = LRParser.deserialize({
version: 14,
states: "9bQYQbOOO!jOSO'#DQOOQa'#DQ'#DQOOQa'#DX'#DXO#yQbO'#DhO%_QcO'#E`OOQa'#E`'#E`O&kQcO'#E`O'mQcO'#E_O(TQcO'#E_O)vQRO'#DPO+VQc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$du_O]f}![!^!_!o#d#k#l#m#w$P$[$k$u%Pp[Of}!^!_!o#d#k#l#m#w$P$[$k$u%PQ!W]Q!rkQ#X!SR#[!T]zTX|!h#_#`qbOf}!^!_!o#d#k#l#m#w$P$[$k$u%PQfOR!afQpPR!upQsSR!wsQ!cgR#g!cQ!fgQ#h!cT#j!f#hS#w#d$PR$Y#wQ!}uQ#o!xT#s!}#oQ#QwQ#q!zT#t#Q#qQ$d$QR$q$dY|TX!h#_#`R#R|S!]`!YR#b!]TeOfScOfQ#S}`#c!^!o#l#m$[$k$u%PQ#f!_U#v#d#w$PR$O#kp`Of}!^!_!o#d#k#l#m#w$P$[$k$u%PQ!Y]R#a![Q!lhR$|$trYO]f}!^!_!o#d#k#l#m#w$P$[$k$u%PQwS[yTX|!h#_#`S!ih$tQ!miQ!pkQ!zsQ!{tW#Pw!z#Q#qQ#T!OQ#U!PQ#W!QQ#X!RQ#Y!SR#Z!TpWOf}!^!_!o#d#k#l#m#w$P$[$k$u%P!OxSTXhikstw|!O!P!Q!R!S!T!h!z#Q#_#`#q$tR!U]ToPpQ#x#dR$b$P]{TX|!h#_#`",
nodeNames: "⚠ Star Slash Plus Minus And Or Eq EqEq Neq Lt Lte Gt Gte Modulo PlusEq MinusEq StarEq SlashEq ModuloEq Identifier AssignableIdentifier Word IdentifierBeforeDot CurlyString Do Comment Program PipeExpr FunctionCall DotGet Number ParenExpr IfExpr keyword ConditionalOp String StringFragment Interpolation EscapeSeq DoubleQuote Boolean Regex Dict NamedArg NamedArgPrefix FunctionDef Params NamedParam Null colon CatchExpr keyword Block FinallyExpr keyword keyword Underscore Array ElseIfExpr keyword ElseExpr FunctionCallOrIdentifier BinOp PositionalArg operator WhileExpr keyword FunctionCallWithBlock TryExpr keyword Throw keyword CompoundAssign Assign",
maxTerm: 111,
states: "<zQYQbOOO!jOpO'#DXO!oOSO'#D`OOQa'#D`'#D`O%jQcO'#DvO(jQcO'#EfOOQ`'#Et'#EtO)TQRO'#DwO+YQcO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gt Gte Modulo PlusEq MinusEq StarEq SlashEq ModuloEq Band Bor Bxor Shl Shr Ushr NullishCoalesce NullishEq Identifier AssignableIdentifier Word IdentifierBeforeDot CurlyString Do Comment Program PipeExpr operator WhileExpr keyword ConditionalOp ParenExpr FunctionCall DotGet Number PositionalArg FunctionDef Params NamedParam NamedArgPrefix String StringFragment Interpolation EscapeSeq DoubleQuote Boolean Null colon CatchExpr keyword Block FinallyExpr keyword keyword Underscore NamedArg IfExpr keyword FunctionCall ElseIfExpr keyword ElseExpr FunctionCallOrIdentifier BinOp Regex Dict Array FunctionCallWithBlock TryExpr keyword Throw keyword CompoundAssign Assign",
maxTerm: 121,
context: trackScope,
nodeProps: [
["closedBy", 50,"end"]
["closedBy", 57,"end"]
],
propSources: [highlighting],
skippedNodes: [0,26],
repeatNodeCount: 11,
tokenData: "FV~R}OX$OXY$mYZ%WZp$Opq$mqr$Ors%qst'wtu)}uw$Owx*Sxy*Xyz*rz{$O{|+]|}$O}!O.P!O!P$O!P!Q0f!Q![+z![!]9R!]!^%W!^!}$O!}#O9l#O#P;b#P#Q;g#Q#R$O#R#S<Q#S#T$O#T#Y/Q#Y#Z<k#Z#b/Q#b#cAi#c#f/Q#f#gBf#g#h/Q#h#iCc#i#o/Q#o#p$O#p#qEg#q;'S$O;'S;=`$g<%l~$O~O$O~~FQS$TUuSOt$Ouw$Ox#O$O#P;'S$O;'S;=`$g<%lO$OS$jP;=`<%l$O^$tUuS!zYOt$Ouw$Ox#O$O#P;'S$O;'S;=`$g<%lO$OU%_UuS#^QOt$Ouw$Ox#O$O#P;'S$O;'S;=`$g<%lO$OU%vZuSOr%qrs&ist%qtu'Suw%qwx'Sx#O%q#O#P'S#P;'S%q;'S;=`'q<%lO%qU&pUxQuSOt$Ouw$Ox#O$O#P;'S$O;'S;=`$g<%lO$OQ'VTOr'Srs'fs;'S'S;'S;=`'k<%lO'SQ'kOxQQ'nP;=`<%l'SU'tP;=`<%l%q^'|WuSOp$Opq(fqt$Ouw$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!_!`/{!`#O$O#P#T$O#T#X/Q#X#Y@l#Y#o/Q#o;'S$O;'S;=`$g<%lO$OU@s[yQuSOt$Ouw$Ox}$O}!O/Q!O!_$O!_!`/{!`#O$O#P#T$O#T#o/Q#o;'S$O;'S;=`$g<%lO$O^Ap[#XWuSOt$Ouw$Ox}$O}!O/Q!O!_$O!_!`/{!`#O$O#P#T$O#T#o/Q#o;'S$O;'S;=`$g<%lO$O^Bm[#ZWuSOt$Ouw$Ox}$O}!O/Q!O!_$O!_!`/{!`#O$O#P#T$O#T#o/Q#o;'S$O;'S;=`$g<%lO$O^Cj^#YWuSOt$Ouw$Ox}$O}!O/Q!O!_$O!_!`/{!`#O$O#P#T$O#T#f/Q#f#gDf#g#o/Q#o;'S$O;'S;=`$g<%lO$OUDk^uSOt$Ouw$Ox}$O}!O/Q!O!_$O!_!`/{!`#O$O#P#T$O#T#i/Q#i#j?k#j#o/Q#o;'S$O;'S;=`$g<%lO$OUEnU!cQuSOt$Ouw$Ox#O$O#P;'S$O;'S;=`$g<%lO$O~FVO#c~",
tokenizers: [operatorTokenizer, 1, 2, 3, tokenizer, new LocalTokenGroup("[~RP!O!PU~ZO#O~~", 11)],
topRules: {"Program":[0,27]},
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: 1682
skippedNodes: [0,34],
repeatNodeCount: 12,
tokenData: "K]~R!OOX$RXY$pYZ%ZZp$Rpq$pqr$Rrs%tst'ztu)cuw$Rwx)hxy)myz*Wz{$R{|*q|}$R}!O*q!O!P$R!P!Q2x!Q!R+c!R![.Q![!];e!]!^%Z!^!}$R!}#O<O#O#P=t#P#Q=y#Q#R$R#R#S>d#S#T$R#T#Y>}#Y#Z@i#Z#b>}#b#cFV#c#f>}#f#gGY#g#h>}#h#iH]#i#o>}#o#p$R#p#qJm#q;'S$R;'S;=`$j<%l~$R~O$R~~KWS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SOt$Ruw$Rx}$R}!O>}!O!Q$R!Q![>}![!_$R!_!`@O!`#O$R#P#T$R#T#o>}#o;'S$R;'S;=`$j<%lO$R^Hd`#dW!TSOt$Ruw$Rx}$R}!O>}!O!Q$R!Q![>}![!_$R!_!`@O!`#O$R#P#T$R#T#f>}#f#gIf#g#o>}#o;'S$R;'S;=`$j<%lO$RUIk`!TSOt$Ruw$Rx}$R}!O>}!O!Q$R!Q![>}![!_$R!_!`@O!`#O$R#P#T$R#T#i>}#i#jC{#j#o>}#o;'S$R;'S;=`$j<%lO$RUJtUuQ!TSOt$Ruw$Rx#O$R#P;'S$R;'S;=`$j<%lO$R~K]O#m~",
tokenizers: [operatorTokenizer, 1, 2, 3, tokenizer, new LocalTokenGroup("[~RP!O!PU~ZO#]~~", 11)],
topRules: {"Program":[0,35]},
specialized: [{term: 28, get: (value: any, stack: any) => (specializeKeyword(value, stack) << 1), external: specializeKeyword},{term: 28, get: (value: keyof typeof spec_Identifier) => spec_Identifier[value] || -1}],
tokenPrec: 2256
})

View File

@ -368,6 +368,118 @@ describe('Parentheses', () => {
PositionalArg
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`)
})
})
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', () => {
@ -595,6 +707,87 @@ describe('CompoundAssign', () => {
PositionalArg
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', () => {
test('? can still end an identifier', () => {
expect('what?').toMatchTree(`
FunctionCallOrIdentifier
Identifier what?`)
})
test('?? can still end an identifier', () => {
expect('what??').toMatchTree(`
FunctionCallOrIdentifier
Identifier what??`)
})
test('?? can still be in a word', () => {
expect('what??the').toMatchTree(`
FunctionCallOrIdentifier
Identifier what??the`)
})
test('?? can still start a word', () => {
expect('??what??the').toMatchTree(`
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`)
})
})
describe('DotGet whitespace sensitivity', () => {
@ -639,7 +832,7 @@ describe('Comments', () => {
test('are greedy', () => {
expect(`
x = 5 # one banana
y = 2 # two bananas`).toMatchTree(`
y = 2 #two bananas`).toMatchTree(`
Assign
AssignableIdentifier x
Eq =
@ -649,7 +842,7 @@ y = 2 # two bananas`).toMatchTree(`
AssignableIdentifier y
Eq =
Number 2
Comment # two bananas`)
Comment #two bananas`)
expect(`
# some comment
@ -670,11 +863,11 @@ basename = 5 # very astute
})
test('words with # are not considered comments', () => {
expect('find #hashtag-file.txt').toMatchTree(`
expect('find my#hashtag-file.txt').toMatchTree(`
FunctionCall
Identifier find
PositionalArg
Word #hashtag-file.txt`)
Word my#hashtag-file.txt`)
})
test('hastags in strings are not comments', () => {

View File

@ -0,0 +1,72 @@
import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
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

@ -2,6 +2,65 @@ import { expect, describe, test } from 'bun:test'
import '../shrimp.grammar' // Importing this so changes cause it to retest!
describe('number literals', () => {
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('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('positive prefix binary', () => {
expect('+0b110').toMatchTree(`
Number +0b110
`)
})
test('positive prefix hex', () => {
expect('+0xFF').toMatchTree(`
Number +0xFF
`)
})
test('hex and binary in arrays', () => {
expect('[0xFF 0b110 42]').toMatchTree(`
Array
Number 0xFF
Number 0b110
Number 42
`)
})
})
describe('array literals', () => {
test('work with numbers', () => {
expect('[1 2 3]').toMatchTree(`

View File

@ -98,4 +98,81 @@ describe('pipe expressions', () => {
Identifier double
`)
})
test('string literals can be piped', () => {
expect(`'hey there' | echo`).toMatchTree(`
PipeExpr
String
StringFragment hey there
operator |
FunctionCallOrIdentifier
Identifier echo
`)
})
test('number literals can be piped', () => {
expect(`42 | echo`).toMatchTree(`
PipeExpr
Number 42
operator |
FunctionCallOrIdentifier
Identifier echo`)
expect(`4.22 | echo`).toMatchTree(`
PipeExpr
Number 4.22
operator |
FunctionCallOrIdentifier
Identifier echo`)
})
test('null literals can be piped', () => {
expect(`null | echo`).toMatchTree(`
PipeExpr
Null null
operator |
FunctionCallOrIdentifier
Identifier echo`)
})
test('boolean literals can be piped', () => {
expect(`true | echo`).toMatchTree(`
PipeExpr
Boolean true
operator |
FunctionCallOrIdentifier
Identifier echo`)
})
test('array literals can be piped', () => {
expect(`[1 2 3] | echo`).toMatchTree(`
PipeExpr
Array
Number 1
Number 2
Number 3
operator |
FunctionCallOrIdentifier
Identifier echo
`)
})
test('dict literals can be piped', () => {
expect(`[a=1 b=2 c=3] | echo`).toMatchTree(`
PipeExpr
Dict
NamedArg
NamedArgPrefix a=
Number 1
NamedArg
NamedArgPrefix b=
Number 2
NamedArg
NamedArgPrefix c=
Number 3
operator |
FunctionCallOrIdentifier
Identifier echo
`)
})
})

View File

@ -8,9 +8,9 @@ export function specializeKeyword(ident: string) {
// tell the dotGet searcher about builtin globals
export const globals: string[] = []
export const setGlobals = (newGlobals: string[]) => {
export const setGlobals = (newGlobals: string[] | Record<string, any>) => {
globals.length = 0
globals.push(...newGlobals)
globals.push(...(Array.isArray(newGlobals) ? newGlobals : Object.keys(newGlobals)))
}
// The only chars that can't be words are whitespace, apostrophes, closing parens, and EOF.
@ -217,6 +217,15 @@ const chooseIdentifierToken = (input: InputStream, stack: Stack): number => {
const nextCh = getFullCodePoint(input, peekPos)
const nextCh2 = getFullCodePoint(input, peekPos + 1)
const nextCh3 = getFullCodePoint(input, peekPos + 2)
// Check for ??= (three-character compound operator)
if (nextCh === 63 /* ? */ && nextCh2 === 63 /* ? */ && nextCh3 === 61 /* = */) {
const charAfterOp = getFullCodePoint(input, peekPos + 3)
if (isWhiteSpace(charAfterOp) || charAfterOp === -1 /* EOF */) {
return AssignableIdentifier
}
}
// Check for compound assignment operators: +=, -=, *=, /=, %=
if (

View File

@ -6,6 +6,7 @@ import {
} from 'reefvm'
import { dict } from './dict'
import { json } from './json'
import { load } from './load'
import { list } from './list'
import { math } from './math'
@ -13,6 +14,7 @@ import { str } from './str'
export const globals = {
dict,
json,
load,
list,
math,
@ -40,6 +42,11 @@ export const globals = {
'var?': function (this: VM, v: string) {
return typeof v !== 'string' || this.scope.has(v)
},
ref: (fn: Function) => fn,
// env
args: Bun.argv.slice(1),
exit: (num: number) => process.exit(num ?? 0),
// type predicates
'string?': (v: any) => toValue(v).type === 'string',
@ -56,6 +63,7 @@ export const globals = {
// boolean/logic
not: (v: any) => !v,
bnot: (n: number) => ~(n | 0),
// utilities
inc: (n: number) => n + 1,

7
src/prelude/json.ts Normal file
View File

@ -0,0 +1,7 @@
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

@ -14,6 +14,13 @@ export const list = {
}
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) => {
let acc = initial
for (const value of list) acc = await cb(acc, value)
@ -66,6 +73,13 @@ export const list = {
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
reverse: (list: any[]) => list.slice().reverse(),
@ -135,4 +149,5 @@ export const list = {
; (list.push as any).raw = true
; (list.pop as any).raw = true
; (list.shift as any).raw = true
; (list.unshift as any).raw = true
; (list.unshift as any).raw = true
; (list.insert as any).raw = true

View File

@ -1,37 +1,37 @@
// strings
export const str = {
join: (arr: string[], sep: string = ',') => arr.join(sep),
split: (str: string, sep: string = ',') => str.split(sep),
'to-upper': (str: string) => str.toUpperCase(),
'to-lower': (str: string) => str.toLowerCase(),
trim: (str: string) => str.trim(),
split: (str: string, sep: string = ',') => String(str ?? '').split(sep),
'to-upper': (str: string) => String(str ?? '').toUpperCase(),
'to-lower': (str: string) => String(str ?? '').toLowerCase(),
trim: (str: string) => String(str ?? '').trim(),
// predicates
'starts-with?': (str: string, prefix: string) => str.startsWith(prefix),
'ends-with?': (str: string, suffix: string) => str.endsWith(suffix),
'contains?': (str: string, substr: string) => str.includes(substr),
'empty?': (str: string) => str.length === 0,
'starts-with?': (str: string, prefix: string) => String(str ?? '').startsWith(prefix),
'ends-with?': (str: string, suffix: string) => String(str ?? '').endsWith(suffix),
'contains?': (str: string, substr: string) => String(str ?? '').includes(substr),
'empty?': (str: string) => String(str ?? '').length === 0,
// inspection
'index-of': (str: string, search: string) => str.indexOf(search),
'last-index-of': (str: string, search: string) => str.lastIndexOf(search),
'index-of': (str: string, search: string) => String(str ?? '').indexOf(search),
'last-index-of': (str: string, search: string) => String(str ?? '').lastIndexOf(search),
// transformations
replace: (str: string, search: string, replacement: string) => str.replace(search, replacement),
'replace-all': (str: string, search: string, replacement: string) => str.replaceAll(search, replacement),
slice: (str: string, start: number, end?: number | null) => str.slice(start, end ?? undefined),
substring: (str: string, start: number, end?: number | null) => str.substring(start, end ?? undefined),
replace: (str: string, search: string, replacement: string) => String(str ?? '').replace(search, replacement),
'replace-all': (str: string, search: string, replacement: string) => String(str ?? '').replaceAll(search, replacement),
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) => {
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}`)
return str.repeat(count)
return String(str ?? '').repeat(count)
},
'pad-start': (str: string, length: number, pad: string = ' ') => str.padStart(length, pad),
'pad-end': (str: string, length: number, pad: string = ' ') => str.padEnd(length, pad),
lines: (str: string) => str.split('\n'),
chars: (str: string) => str.split(''),
'pad-start': (str: string, length: number, pad: string = ' ') => String(str ?? '').padStart(length, pad),
'pad-end': (str: string, length: number, pad: string = ' ') => String(str ?? '').padEnd(length, pad),
lines: (str: string) => String(str ?? '').split('\n'),
chars: (str: string) => String(str ?? '').split(''),
// regex
match: (str: string, regex: RegExp) => str.match(regex),
'test?': (str: string, regex: RegExp) => regex.test(str),
match: (str: string, regex: RegExp) => String(str ?? '').match(regex),
'test?': (str: string, regex: RegExp) => regex.test(String(str ?? '')),
}

View File

@ -77,3 +77,29 @@ describe('introspection', () => {
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, globals)
})
test('args can be accessed', async () => {
await expect(`type args`).toEvaluateTo('array', globals)
})
test('', async () => {
await expect(`list.first args | 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')
})

View File

@ -0,0 +1,84 @@
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

@ -3,185 +3,186 @@ import { globals } from '#prelude'
describe('string operations', () => {
test('to-upper converts to uppercase', async () => {
await expect(`str.to-upper 'hello'`).toEvaluateTo('HELLO', globals)
await expect(`str.to-upper 'Hello World!'`).toEvaluateTo('HELLO WORLD!', globals)
await expect(`str.to-upper 'hello'`).toEvaluateTo('HELLO')
await expect(`str.to-upper 'Hello World!'`).toEvaluateTo('HELLO WORLD!')
})
test('to-lower converts to lowercase', async () => {
await expect(`str.to-lower 'HELLO'`).toEvaluateTo('hello', globals)
await expect(`str.to-lower 'Hello World!'`).toEvaluateTo('hello world!', globals)
await expect(`str.to-lower 'HELLO'`).toEvaluateTo('hello')
await expect(`str.to-lower 'Hello World!'`).toEvaluateTo('hello world!')
})
test('trim removes whitespace', async () => {
await expect(`str.trim ' hello '`).toEvaluateTo('hello', globals)
await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello', globals)
await expect(`str.trim ' hello '`).toEvaluateTo('hello')
await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello')
})
test('split divides string by separator', async () => {
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globals)
await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'], globals)
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'])
await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o'])
})
test('split with comma separator', async () => {
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'], globals)
await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c'])
})
test('join combines array elements', async () => {
await expect(`str.join ['a' 'b' 'c'] '-'`).toEvaluateTo('a-b-c', globals)
await expect(`str.join ['hello' 'world'] ' '`).toEvaluateTo('hello world', globals)
await expect(`str.join ['a' 'b' 'c'] '-'`).toEvaluateTo('a-b-c')
await expect(`str.join ['hello' 'world'] ' '`).toEvaluateTo('hello world')
})
test('join with comma separator', async () => {
await expect(`str.join ['a' 'b' 'c'] ','`).toEvaluateTo('a,b,c', globals)
await expect(`str.join ['a' 'b' 'c'] ','`).toEvaluateTo('a,b,c')
})
test('starts-with? checks string prefix', async () => {
await expect(`str.starts-with? 'hello' 'hel'`).toEvaluateTo(true, globals)
await expect(`str.starts-with? 'hello' 'bye'`).toEvaluateTo(false, globals)
await expect(`str.starts-with? 'hello' 'hel'`).toEvaluateTo(true)
await expect(`str.starts-with? 'hello' 'bye'`).toEvaluateTo(false)
})
test('ends-with? checks string suffix', async () => {
await expect(`str.ends-with? 'hello' 'lo'`).toEvaluateTo(true, globals)
await expect(`str.ends-with? 'hello' 'he'`).toEvaluateTo(false, globals)
await expect(`str.ends-with? 'hello' 'lo'`).toEvaluateTo(true)
await expect(`str.ends-with? 'hello' 'he'`).toEvaluateTo(false)
})
test('contains? checks for substring', async () => {
await expect(`str.contains? 'hello world' 'o w'`).toEvaluateTo(true, globals)
await expect(`str.contains? 'hello' 'bye'`).toEvaluateTo(false, globals)
await expect(`str.contains? 'hello world' 'o w'`).toEvaluateTo(true)
await expect(`str.contains? 'hello' 'bye'`).toEvaluateTo(false)
})
test('empty? checks if string is empty', async () => {
await expect(`str.empty? ''`).toEvaluateTo(true, globals)
await expect(`str.empty? 'hello'`).toEvaluateTo(false, globals)
await expect(`str.empty? ''`).toEvaluateTo(true)
await expect(`str.empty? 'hello'`).toEvaluateTo(false)
})
test('replace replaces first occurrence', async () => {
await expect(`str.replace 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hello', globals)
await expect(`str.replace 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hello')
})
test('replace-all replaces all occurrences', async () => {
await expect(`str.replace-all 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hi', globals)
await expect(`str.replace-all 'hello hello' 'hello' 'hi'`).toEvaluateTo('hi hi')
})
test('slice extracts substring', async () => {
await expect(`str.slice 'hello' 1 3`).toEvaluateTo('el', globals)
await expect(`str.slice 'hello' 2 null`).toEvaluateTo('llo', globals)
await expect(`str.slice 'hello' 1 3`).toEvaluateTo('el')
await expect(`str.slice 'hello' 2 null`).toEvaluateTo('llo')
await expect(`str.slice 'hello' 2`).toEvaluateTo('llo')
})
test('repeat repeats string', async () => {
await expect(`str.repeat 'ha' 3`).toEvaluateTo('hahaha', globals)
await expect(`str.repeat 'ha' 3`).toEvaluateTo('hahaha')
})
test('pad-start pads beginning', async () => {
await expect(`str.pad-start '5' 3 '0'`).toEvaluateTo('005', globals)
await expect(`str.pad-start '5' 3 '0'`).toEvaluateTo('005')
})
test('pad-end pads end', async () => {
await expect(`str.pad-end '5' 3 '0'`).toEvaluateTo('500', globals)
await expect(`str.pad-end '5' 3 '0'`).toEvaluateTo('500')
})
test('lines splits by newlines', async () => {
await expect(`str.lines 'a\\nb\\nc'`).toEvaluateTo(['a', 'b', 'c'], globals)
await expect(`str.lines 'a\\nb\\nc'`).toEvaluateTo(['a', 'b', 'c'])
})
test('chars splits into characters', async () => {
await expect(`str.chars 'abc'`).toEvaluateTo(['a', 'b', 'c'], globals)
await expect(`str.chars 'abc'`).toEvaluateTo(['a', 'b', 'c'])
})
test('index-of finds substring position', async () => {
await expect(`str.index-of 'hello world' 'world'`).toEvaluateTo(6, globals)
await expect(`str.index-of 'hello' 'bye'`).toEvaluateTo(-1, globals)
await expect(`str.index-of 'hello world' 'world'`).toEvaluateTo(6)
await expect(`str.index-of 'hello' 'bye'`).toEvaluateTo(-1)
})
test('last-index-of finds last occurrence', async () => {
await expect(`str.last-index-of 'hello hello' 'hello'`).toEvaluateTo(6, globals)
await expect(`str.last-index-of 'hello hello' 'hello'`).toEvaluateTo(6)
})
})
describe('boolean logic', () => {
test('not negates value', async () => {
await expect(`not true`).toEvaluateTo(false, globals)
await expect(`not false`).toEvaluateTo(true, globals)
await expect(`not 42`).toEvaluateTo(false, globals)
await expect(`not null`).toEvaluateTo(true, globals)
await expect(`not true`).toEvaluateTo(false)
await expect(`not false`).toEvaluateTo(true)
await expect(`not 42`).toEvaluateTo(false)
await expect(`not null`).toEvaluateTo(true)
})
})
describe('utilities', () => {
test('inc increments by 1', async () => {
await expect(`inc 5`).toEvaluateTo(6, globals)
await expect(`inc -1`).toEvaluateTo(0, globals)
await expect(`inc 5`).toEvaluateTo(6)
await expect(`inc -1`).toEvaluateTo(0)
})
test('dec decrements by 1', async () => {
await expect(`dec 5`).toEvaluateTo(4, globals)
await expect(`dec 0`).toEvaluateTo(-1, globals)
await expect(`dec 5`).toEvaluateTo(4)
await expect(`dec 0`).toEvaluateTo(-1)
})
test('identity returns value as-is', async () => {
await expect(`identity 42`).toEvaluateTo(42, globals)
await expect(`identity 'hello'`).toEvaluateTo('hello', globals)
await expect(`identity 42`).toEvaluateTo(42)
await expect(`identity 'hello'`).toEvaluateTo('hello')
})
})
describe('collections', () => {
test('length', async () => {
await expect(`length 'hello'`).toEvaluateTo(5, globals)
await expect(`length [1 2 3]`).toEvaluateTo(3, globals)
await expect(`length [a=1 b=2]`).toEvaluateTo(2, globals)
await expect(`length 'hello'`).toEvaluateTo(5)
await expect(`length [1 2 3]`).toEvaluateTo(3)
await expect(`length [a=1 b=2]`).toEvaluateTo(2)
})
test('length throws on invalid types', async () => {
await expect(`try: length 42 catch e: 'error' end`).toEvaluateTo('error', globals)
await expect(`try: length true catch e: 'error' end`).toEvaluateTo('error', globals)
await expect(`try: length null catch e: 'error' end`).toEvaluateTo('error', globals)
await expect(`try: length 42 catch e: 'error' end`).toEvaluateTo('error')
await expect(`try: length true catch e: 'error' end`).toEvaluateTo('error')
await expect(`try: length null catch e: 'error' end`).toEvaluateTo('error')
})
test('literal array creates array from arguments', async () => {
await expect(`[ 1 2 3 ]`).toEvaluateTo([1, 2, 3], globals)
await expect(`['a' 'b']`).toEvaluateTo(['a', 'b'], globals)
await expect(`[]`).toEvaluateTo([], globals)
await expect(`[ 1 2 3 ]`).toEvaluateTo([1, 2, 3])
await expect(`['a' 'b']`).toEvaluateTo(['a', 'b'])
await expect(`[]`).toEvaluateTo([])
})
test('literal dict creates object from named arguments', async () => {
await expect(`[ a=1 b=2 ]`).toEvaluateTo({ a: 1, b: 2 }, globals)
await expect(`[=]`).toEvaluateTo({}, globals)
await expect(`[ a=1 b=2 ]`).toEvaluateTo({ a: 1, b: 2 })
await expect(`[=]`).toEvaluateTo({})
})
test('at retrieves element at index', async () => {
await expect(`at [10 20 30] 0`).toEvaluateTo(10, globals)
await expect(`at [10 20 30] 2`).toEvaluateTo(30, globals)
await expect(`at [10 20 30] 0`).toEvaluateTo(10)
await expect(`at [10 20 30] 2`).toEvaluateTo(30)
})
test('at retrieves property from object', async () => {
await expect(`at [name='test'] 'name'`).toEvaluateTo('test', globals)
await expect(`at [name='test'] 'name'`).toEvaluateTo('test')
})
test('slice extracts array subset', async () => {
await expect(`list.slice [1 2 3 4 5] 1 3`).toEvaluateTo([2, 3], globals)
await expect(`list.slice [1 2 3 4 5] 2 5`).toEvaluateTo([3, 4, 5], globals)
await expect(`list.slice [1 2 3 4 5] 1 3`).toEvaluateTo([2, 3])
await expect(`list.slice [1 2 3 4 5] 2 5`).toEvaluateTo([3, 4, 5])
})
test('range creates number sequence', async () => {
await expect(`range 0 5`).toEvaluateTo([0, 1, 2, 3, 4, 5], globals)
await expect(`range 3 6`).toEvaluateTo([3, 4, 5, 6], globals)
await expect(`range 0 5`).toEvaluateTo([0, 1, 2, 3, 4, 5])
await expect(`range 3 6`).toEvaluateTo([3, 4, 5, 6])
})
test('range with single argument starts from 0', async () => {
await expect(`range 3 null`).toEvaluateTo([0, 1, 2, 3], globals)
await expect(`range 0 null`).toEvaluateTo([0], globals)
await expect(`range 3 null`).toEvaluateTo([0, 1, 2, 3])
await expect(`range 0 null`).toEvaluateTo([0])
})
test('empty? checks if list, dict, string is empty', async () => {
await expect(`empty? []`).toEvaluateTo(true, globals)
await expect(`empty? [1]`).toEvaluateTo(false, globals)
await expect(`empty? []`).toEvaluateTo(true)
await expect(`empty? [1]`).toEvaluateTo(false)
await expect(`empty? [=]`).toEvaluateTo(true, globals)
await expect(`empty? [a=true]`).toEvaluateTo(false, globals)
await expect(`empty? [=]`).toEvaluateTo(true)
await expect(`empty? [a=true]`).toEvaluateTo(false)
await expect(`empty? ''`).toEvaluateTo(true, globals)
await expect(`empty? 'cat'`).toEvaluateTo(false, globals)
await expect(`empty? meow`).toEvaluateTo(false, globals)
await expect(`empty? ''`).toEvaluateTo(true)
await expect(`empty? 'cat'`).toEvaluateTo(false)
await expect(`empty? meow`).toEvaluateTo(false)
})
test('list.filter keeps matching elements', async () => {
@ -190,7 +191,16 @@ describe('collections', () => {
x == 3 or x == 4 or x == 5
end
list.filter [1 2 3 4 5] is-positive
`).toEvaluateTo([3, 4, 5], globals)
`).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 () => {
@ -199,7 +209,7 @@ describe('collections', () => {
acc + x
end
list.reduce [1 2 3 4] add 0
`).toEvaluateTo(10, globals)
`).toEvaluateTo(10)
})
test('list.find returns first match', async () => {
@ -208,139 +218,155 @@ describe('collections', () => {
x == 4
end
list.find [1 2 4 5] is-four
`).toEvaluateTo(4, globals)
`).toEvaluateTo(4)
})
test('list.find returns null if no match', async () => {
await expect(`
is-ten = do x: x == 10 end
list.find [1 2 3] is-ten
`).toEvaluateTo(null, globals)
`).toEvaluateTo(null)
})
test('list.empty? checks if list is empty', async () => {
await expect(`list.empty? []`).toEvaluateTo(true, globals)
await expect(`list.empty? [1]`).toEvaluateTo(false, globals)
await expect(`list.empty? []`).toEvaluateTo(true)
await expect(`list.empty? [1]`).toEvaluateTo(false)
})
test('list.contains? checks for element', async () => {
await expect(`list.contains? [1 2 3] 2`).toEvaluateTo(true, globals)
await expect(`list.contains? [1 2 3] 5`).toEvaluateTo(false, globals)
await expect(`list.contains? [1 2 3] 2`).toEvaluateTo(true)
await expect(`list.contains? [1 2 3] 5`).toEvaluateTo(false)
})
test('list.reverse reverses array', async () => {
await expect(`list.reverse [1 2 3]`).toEvaluateTo([3, 2, 1], globals)
await expect(`list.reverse [1 2 3]`).toEvaluateTo([3, 2, 1])
})
test('list.concat combines arrays', async () => {
await expect(`list.concat [1 2] [3 4]`).toEvaluateTo([1, 2, 3, 4], globals)
await expect(`list.concat [1 2] [3 4]`).toEvaluateTo([1, 2, 3, 4])
})
test('list.flatten flattens nested arrays', async () => {
await expect(`list.flatten [[1 2] [3 4]] 1`).toEvaluateTo([1, 2, 3, 4], globals)
await expect(`list.flatten [[1 2] [3 4]] 1`).toEvaluateTo([1, 2, 3, 4])
})
test('list.unique removes duplicates', async () => {
await expect(`list.unique [1 2 2 3 1]`).toEvaluateTo([1, 2, 3], globals)
await expect(`list.unique [1 2 2 3 1]`).toEvaluateTo([1, 2, 3])
})
test('list.zip combines two arrays', async () => {
await expect(`list.zip [1 2] [3 4]`).toEvaluateTo([[1, 3], [2, 4]], globals)
await expect(`list.zip [1 2] [3 4]`).toEvaluateTo([[1, 3], [2, 4]])
})
test('list.first returns first element', async () => {
await expect(`list.first [1 2 3]`).toEvaluateTo(1, globals)
await expect(`list.first []`).toEvaluateTo(null, globals)
await expect(`list.first [1 2 3]`).toEvaluateTo(1)
await expect(`list.first []`).toEvaluateTo(null)
})
test('list.last returns last element', async () => {
await expect(`list.last [1 2 3]`).toEvaluateTo(3, globals)
await expect(`list.last []`).toEvaluateTo(null, globals)
await expect(`list.last [1 2 3]`).toEvaluateTo(3)
await expect(`list.last []`).toEvaluateTo(null)
})
test('list.rest returns all but first', async () => {
await expect(`list.rest [1 2 3]`).toEvaluateTo([2, 3], globals)
await expect(`list.rest [1 2 3]`).toEvaluateTo([2, 3])
})
test('list.take returns first n elements', async () => {
await expect(`list.take [1 2 3 4 5] 3`).toEvaluateTo([1, 2, 3], globals)
await expect(`list.take [1 2 3 4 5] 3`).toEvaluateTo([1, 2, 3])
})
test('list.drop skips first n elements', async () => {
await expect(`list.drop [1 2 3 4 5] 2`).toEvaluateTo([3, 4, 5], globals)
await expect(`list.drop [1 2 3 4 5] 2`).toEvaluateTo([3, 4, 5])
})
test('list.append adds to end', async () => {
await expect(`list.append [1 2] 3`).toEvaluateTo([1, 2, 3], globals)
await expect(`list.append [1 2] 3`).toEvaluateTo([1, 2, 3])
})
test('list.prepend adds to start', async () => {
await expect(`list.prepend [2 3] 1`).toEvaluateTo([1, 2, 3], globals)
await expect(`list.prepend [2 3] 1`).toEvaluateTo([1, 2, 3])
})
test('list.index-of finds element index', async () => {
await expect(`list.index-of [1 2 3] 2`).toEvaluateTo(1, globals)
await expect(`list.index-of [1 2 3] 5`).toEvaluateTo(-1, globals)
await expect(`list.index-of [1 2 3] 2`).toEvaluateTo(1)
await expect(`list.index-of [1 2 3] 5`).toEvaluateTo(-1)
})
test('list.push adds to end and mutates array', async () => {
await expect(`arr = [1 2]; list.push arr 3; arr`).toEvaluateTo([1, 2, 3], globals)
await expect(`arr = [1 2]; list.push arr 3; arr`).toEvaluateTo([1, 2, 3])
})
test('list.push returns the size of the array', async () => {
await expect(`arr = [1 2]; arr | list.push 3`).toEvaluateTo(3, globals)
await expect(`arr = [1 2]; arr | list.push 3`).toEvaluateTo(3)
})
test('list.pop removes from end and mutates array', async () => {
await expect(`arr = [1 2 3]; list.pop arr; arr`).toEvaluateTo([1, 2], globals)
await expect(`arr = [1 2 3]; list.pop arr; arr`).toEvaluateTo([1, 2])
})
test('list.pop returns removed element', async () => {
await expect(`list.pop [1 2 3]`).toEvaluateTo(3, globals)
await expect(`list.pop [1 2 3]`).toEvaluateTo(3)
})
test('list.pop returns null for empty array', async () => {
await expect(`list.pop []`).toEvaluateTo(null, globals)
await expect(`list.pop []`).toEvaluateTo(null)
})
test('list.shift removes from start and mutates array', async () => {
await expect(`arr = [1 2 3]; list.shift arr; arr`).toEvaluateTo([2, 3], globals)
await expect(`arr = [1 2 3]; list.shift arr; arr`).toEvaluateTo([2, 3])
})
test('list.shift returns removed element', async () => {
await expect(`list.shift [1 2 3]`).toEvaluateTo(1, globals)
await expect(`list.shift [1 2 3]`).toEvaluateTo(1)
})
test('list.shift returns null for empty array', async () => {
await expect(`list.shift []`).toEvaluateTo(null, globals)
await expect(`list.shift []`).toEvaluateTo(null)
})
test('list.unshift adds to start and mutates array', async () => {
await expect(`arr = [2 3]; list.unshift arr 1; arr`).toEvaluateTo([1, 2, 3], globals)
await expect(`arr = [2 3]; list.unshift arr 1; arr`).toEvaluateTo([1, 2, 3])
})
test('list.unshift returns the length of the array', async () => {
await expect(`arr = [2 3]; arr | list.unshift 1`).toEvaluateTo(3, globals)
await expect(`arr = [2 3]; arr | list.unshift 1`).toEvaluateTo(3)
})
test('list.splice removes elements and mutates array', async () => {
await expect(`arr = [1 2 3 4 5]; list.splice arr 1 2; arr`).toEvaluateTo([1, 4, 5], globals)
await expect(`arr = [1 2 3 4 5]; list.splice arr 1 2; arr`).toEvaluateTo([1, 4, 5])
})
test('list.splice returns removed elements', async () => {
await expect(`list.splice [1 2 3 4 5] 1 2`).toEvaluateTo([2, 3], globals)
await expect(`list.splice [1 2 3 4 5] 1 2`).toEvaluateTo([2, 3])
})
test('list.splice from start', async () => {
await expect(`list.splice [1 2 3 4 5] 0 2`).toEvaluateTo([1, 2], globals)
await expect(`list.splice [1 2 3 4 5] 0 2`).toEvaluateTo([1, 2])
})
test('list.splice to end', async () => {
await expect(`arr = [1 2 3 4 5]; list.splice arr 3 2; arr`).toEvaluateTo([1, 2, 3], globals)
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 () => {
await expect(`list.sort [3 1 4 1 5] null`).toEvaluateTo([1, 1, 3, 4, 5], globals)
await expect(`list.sort [3 1 4 1 5] null`).toEvaluateTo([1, 1, 3, 4, 5])
})
test('list.sort with callback sorts using comparator', async () => {
@ -349,7 +375,7 @@ describe('collections', () => {
b - a
end
list.sort [3 1 4 1 5] desc
`).toEvaluateTo([5, 4, 3, 1, 1], globals)
`).toEvaluateTo([5, 4, 3, 1, 1])
})
test('list.sort with callback for strings by length', async () => {
@ -358,52 +384,52 @@ describe('collections', () => {
(length a) - (length b)
end
list.sort ['cat' 'a' 'dog' 'elephant'] by-length
`).toEvaluateTo(['a', 'cat', 'dog', 'elephant'], globals)
`).toEvaluateTo(['a', 'cat', 'dog', 'elephant'])
})
test('list.any? checks if any element matches', async () => {
await expect(`
gt-three = do x: x > 3 end
list.any? [1 2 4 5] gt-three
`).toEvaluateTo(true, globals)
`).toEvaluateTo(true)
await expect(`
gt-ten = do x: x > 10 end
list.any? [1 2 3] gt-ten
`).toEvaluateTo(false, globals)
`).toEvaluateTo(false)
})
test('list.all? checks if all elements match', async () => {
await expect(`
positive = do x: x > 0 end
list.all? [1 2 3] positive
`).toEvaluateTo(true, globals)
`).toEvaluateTo(true)
await expect(`
positive = do x: x > 0 end
list.all? [1 -2 3] positive
`).toEvaluateTo(false, globals)
`).toEvaluateTo(false)
})
test('list.sum adds all numbers', async () => {
await expect(`list.sum [1 2 3 4]`).toEvaluateTo(10, globals)
await expect(`list.sum []`).toEvaluateTo(0, globals)
await expect(`list.sum [1 2 3 4]`).toEvaluateTo(10)
await expect(`list.sum []`).toEvaluateTo(0)
})
test('list.count counts matching elements', async () => {
await expect(`
gt-two = do x: x > 2 end
list.count [1 2 3 4 5] gt-two
`).toEvaluateTo(3, globals)
`).toEvaluateTo(3)
})
test('list.partition splits array by predicate', async () => {
await expect(`
gt-two = do x: x > 2 end
list.partition [1 2 3 4 5] gt-two
`).toEvaluateTo([[3, 4, 5], [1, 2]], globals)
`).toEvaluateTo([[3, 4, 5], [1, 2]])
})
test('list.compact removes null values', async () => {
await expect(`list.compact [1 null 2 null 3]`).toEvaluateTo([1, 2, 3], globals)
await expect(`list.compact [1 null 2 null 3]`).toEvaluateTo([1, 2, 3])
})
test('list.group-by groups by key function', async () => {
@ -416,7 +442,7 @@ describe('collections', () => {
end
end
list.group-by ['a' 1 'b' 2] get-type
`).toEvaluateTo({ str: ['a', 'b'], num: [1, 2] }, globals)
`).toEvaluateTo({ str: ['a', 'b'], num: [1, 2] })
})
})
@ -425,14 +451,14 @@ describe('enumerables', () => {
await expect(`
double = do x: x * 2 end
list.map [1 2 3] double
`).toEvaluateTo([2, 4, 6], globals)
`).toEvaluateTo([2, 4, 6])
})
test('map handles empty array', async () => {
await expect(`
double = do x: x * 2 end
list.map [] double
`).toEvaluateTo([], globals)
`).toEvaluateTo([])
})
test('each iterates over array', async () => {
@ -441,165 +467,146 @@ describe('enumerables', () => {
await expect(`
double = do x: x * 2 end
each [1 2 3] double
`).toEvaluateTo([1, 2, 3], globals)
`).toEvaluateTo([1, 2, 3])
})
test('each handles empty array', async () => {
await expect(`
fn = do x: x end
each [] fn
`).toEvaluateTo([], globals)
`).toEvaluateTo([])
})
})
describe('dict operations', () => {
test('dict.keys returns all keys', async () => {
const result = await (async () => {
const { Compiler } = await import('#compiler/compiler')
const { run, fromValue } = await import('reefvm')
const { setGlobals } = await import('#parser/tokenizer')
setGlobals(Object.keys(globals))
const c = new Compiler('dict.keys [a=1 b=2 c=3]')
const r = await run(c.bytecode, globals)
return fromValue(r)
})()
// Check that all expected keys are present (order may vary)
expect(result.sort()).toEqual(['a', 'b', 'c'])
await expect(`dict.keys [a=1 b=2 c=3] | list.sort`).toEvaluateTo(['a', 'b', 'c'].sort())
})
test('dict.values returns all values', async () => {
const result = await (async () => {
const { Compiler } = await import('#compiler/compiler')
const { run, fromValue } = await import('reefvm')
const { setGlobals } = await import('#parser/tokenizer')
setGlobals(Object.keys(globals))
const c = new Compiler('dict.values [a=1 b=2]')
const r = await run(c.bytecode, globals)
return fromValue(r)
})()
// Check that all expected values are present (order may vary)
expect(result.sort()).toEqual([1, 2])
await expect('dict.values [a=1 b=2] | list.sort').toEvaluateTo([1, 2].sort())
})
test('dict.has? checks for key', async () => {
await expect(`dict.has? [a=1 b=2] 'a'`).toEvaluateTo(true, globals)
await expect(`dict.has? [a=1 b=2] 'c'`).toEvaluateTo(false, globals)
await expect(`dict.has? [a=1 b=2] 'a'`).toEvaluateTo(true)
await expect(`dict.has? [a=1 b=2] 'c'`).toEvaluateTo(false)
})
test('dict.get retrieves value with default', async () => {
await expect(`dict.get [a=1] 'a' 0`).toEvaluateTo(1, globals)
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99, globals)
await expect(`dict.get [a=1] 'a' 0`).toEvaluateTo(1)
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99)
await expect(`dict.get [a=1] 'b'`).toEvaluateTo(null)
})
test('dict.set sets value', async () => {
await expect(`map = [a=1]; dict.set map 'b' 99; map.b`).toEvaluateTo(99, globals)
await expect(`map = [a=1]; dict.set map 'a' 100; map.a`).toEvaluateTo(100, globals)
await expect(`map = [a=1]; dict.set map 'b' 99; map.b`).toEvaluateTo(99)
await expect(`map = [a=1]; dict.set map 'a' 100; map.a`).toEvaluateTo(100)
})
test('dict.empty? checks if dict is empty', async () => {
await expect(`dict.empty? [=]`).toEvaluateTo(true, globals)
await expect(`dict.empty? [a=1]`).toEvaluateTo(false, globals)
await expect(`dict.empty? [=]`).toEvaluateTo(true)
await expect(`dict.empty? [a=1]`).toEvaluateTo(false)
})
test('dict.merge combines dicts', async () => {
await expect(`dict.merge [a=1] [b=2]`).toEvaluateTo({ a: 1, b: 2 }, globals)
await expect(`dict.merge [a=1] [b=2]`).toEvaluateTo({ a: 1, b: 2 })
})
test('dict.map transforms values', async () => {
await expect(`
double = do v k: v * 2 end
dict.map [a=1 b=2] double
`).toEvaluateTo({ a: 2, b: 4 }, globals)
`).toEvaluateTo({ a: 2, b: 4 })
})
test('dict.filter keeps matching entries', async () => {
await expect(`
gt-one = do v k: v > 1 end
dict.filter [a=1 b=2 c=3] gt-one
`).toEvaluateTo({ b: 2, c: 3 }, globals)
`).toEvaluateTo({ b: 2, c: 3 })
})
test('dict.from-entries creates dict from array', async () => {
await expect(`dict.from-entries [['a' 1] ['b' 2]]`).toEvaluateTo({ a: 1, b: 2 }, globals)
await expect(`dict.from-entries [['a' 1] ['b' 2]]`).toEvaluateTo({ a: 1, b: 2 })
})
})
describe('math operations', () => {
test('math.abs returns absolute value', async () => {
await expect(`math.abs -5`).toEvaluateTo(5, globals)
await expect(`math.abs 5`).toEvaluateTo(5, globals)
await expect(`math.abs -5`).toEvaluateTo(5)
await expect(`math.abs 5`).toEvaluateTo(5)
})
test('math.floor rounds down', async () => {
await expect(`math.floor 3.7`).toEvaluateTo(3, globals)
await expect(`math.floor 3.7`).toEvaluateTo(3)
})
test('math.ceil rounds up', async () => {
await expect(`math.ceil 3.2`).toEvaluateTo(4, globals)
await expect(`math.ceil 3.2`).toEvaluateTo(4)
})
test('math.round rounds to nearest', async () => {
await expect(`math.round 3.4`).toEvaluateTo(3, globals)
await expect(`math.round 3.6`).toEvaluateTo(4, globals)
await expect(`math.round 3.4`).toEvaluateTo(3)
await expect(`math.round 3.6`).toEvaluateTo(4)
})
test('math.min returns minimum', async () => {
await expect(`math.min 5 2 8 1`).toEvaluateTo(1, globals)
await expect(`math.min 5 2 8 1`).toEvaluateTo(1)
})
test('math.max returns maximum', async () => {
await expect(`math.max 5 2 8 1`).toEvaluateTo(8, globals)
await expect(`math.max 5 2 8 1`).toEvaluateTo(8)
})
test('math.pow computes power', async () => {
await expect(`math.pow 2 3`).toEvaluateTo(8, globals)
await expect(`math.pow 2 3`).toEvaluateTo(8)
})
test('math.sqrt computes square root', async () => {
await expect(`math.sqrt 16`).toEvaluateTo(4, globals)
await expect(`math.sqrt 16`).toEvaluateTo(4)
})
test('math.even? checks if even', async () => {
await expect(`math.even? 4`).toEvaluateTo(true, globals)
await expect(`math.even? 5`).toEvaluateTo(false, globals)
await expect(`math.even? 4`).toEvaluateTo(true)
await expect(`math.even? 5`).toEvaluateTo(false)
})
test('math.odd? checks if odd', async () => {
await expect(`math.odd? 5`).toEvaluateTo(true, globals)
await expect(`math.odd? 4`).toEvaluateTo(false, globals)
await expect(`math.odd? 5`).toEvaluateTo(true)
await expect(`math.odd? 4`).toEvaluateTo(false)
})
test('math.positive? checks if positive', async () => {
await expect(`math.positive? 5`).toEvaluateTo(true, globals)
await expect(`math.positive? -5`).toEvaluateTo(false, globals)
await expect(`math.positive? 0`).toEvaluateTo(false, globals)
await expect(`math.positive? 5`).toEvaluateTo(true)
await expect(`math.positive? -5`).toEvaluateTo(false)
await expect(`math.positive? 0`).toEvaluateTo(false)
})
test('math.negative? checks if negative', async () => {
await expect(`math.negative? -5`).toEvaluateTo(true, globals)
await expect(`math.negative? 5`).toEvaluateTo(false, globals)
await expect(`math.negative? -5`).toEvaluateTo(true)
await expect(`math.negative? 5`).toEvaluateTo(false)
})
test('math.zero? checks if zero', async () => {
await expect(`math.zero? 0`).toEvaluateTo(true, globals)
await expect(`math.zero? 5`).toEvaluateTo(false, globals)
await expect(`math.zero? 0`).toEvaluateTo(true)
await expect(`math.zero? 5`).toEvaluateTo(false)
})
test('math.clamp restricts value to range', async () => {
await expect(`math.clamp 5 0 10`).toEvaluateTo(5, globals)
await expect(`math.clamp -5 0 10`).toEvaluateTo(0, globals)
await expect(`math.clamp 15 0 10`).toEvaluateTo(10, globals)
await expect(`math.clamp 5 0 10`).toEvaluateTo(5)
await expect(`math.clamp -5 0 10`).toEvaluateTo(0)
await expect(`math.clamp 15 0 10`).toEvaluateTo(10)
})
test('math.sign returns sign of number', async () => {
await expect(`math.sign 5`).toEvaluateTo(1, globals)
await expect(`math.sign -5`).toEvaluateTo(-1, globals)
await expect(`math.sign 0`).toEvaluateTo(0, globals)
await expect(`math.sign 5`).toEvaluateTo(1)
await expect(`math.sign -5`).toEvaluateTo(-1)
await expect(`math.sign 0`).toEvaluateTo(0)
})
test('math.trunc truncates decimal', async () => {
await expect(`math.trunc 3.7`).toEvaluateTo(3, globals)
await expect(`math.trunc -3.7`).toEvaluateTo(-3, globals)
await expect(`math.trunc 3.7`).toEvaluateTo(3)
await expect(`math.trunc -3.7`).toEvaluateTo(-3)
})
})

View File

@ -1,6 +1,6 @@
import { describe } from 'bun:test'
import { expect, test } from 'bun:test'
import { Shrimp } from '..'
import { Shrimp, runCode, compileCode, parseCode, bytecodeToString } from '..'
describe('Shrimp', () => {
test('allows running Shrimp code', async () => {
@ -50,4 +50,403 @@ describe('Shrimp', () => {
await shrimp.run('abc = 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)
})
})