From 5350bb8c2b037f195e177f3dc51cdc3820308082 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 9 Nov 2025 21:30:48 -0800 Subject: [PATCH] you're lost --- src/bytecode.ts | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/bytecode.ts b/src/bytecode.ts index 7fc4602..ef760be 100644 --- a/src/bytecode.ts +++ b/src/bytecode.ts @@ -88,30 +88,6 @@ type LabelDefinition = [string] // Just ".label_name:" export type ProgramItem = InstructionTuple | LabelDefinition -// -// Parse bytecode from human-readable string format. -// Operand types are determined by prefix/literal: -// #42 -> immediate number (e.g., JUMP #5, MAKE_ARRAY #3) -// .label -> label reference (e.g., JUMP .loop_start, MAKE_FUNCTION (x y) .body) -// name -> variable/function name (e.g., LOAD x, LOAD_NATIVE add) -// 42 -> number constant (e.g., PUSH 42) -// "str" -> string constant (e.g., PUSH "hello") -// 'str' -> string constant (e.g., PUSH 'hello') -// true -> boolean constant (e.g., PUSH true) -// false -> boolean constant (e.g., PUSH false) -// null -> null constant (e.g., PUSH null) -// -// Labels: -// .label_name: -> label definition (marks current instruction position) -// -// Function definitions: -// MAKE_FUNCTION (x y) #7 -> basic function (numeric offset) -// MAKE_FUNCTION (x y) .body -> basic function (label reference) -// MAKE_FUNCTION (x y=42) #7 -> with defaults -// MAKE_FUNCTION (x ...rest) #7 -> variadic -// MAKE_FUNCTION (x @named) #7 -> named -// - function parseFunctionParams(paramStr: string, constants: Constant[]): { params: string[] defaults: Record @@ -373,6 +349,29 @@ function toBytecodeFromArray(program: ProgramItem[]): Bytecode /* throws */ { } } + +//// +// Parse bytecode from human-readable string format. +// Operand types are determined by prefix/literal: +// #42 -> immediate number (e.g., JUMP #5, MAKE_ARRAY #3) +// .label -> label reference (e.g., JUMP .loop_start, MAKE_FUNCTION (x y) .body) +// name -> variable/function name (e.g., LOAD x, LOAD_NATIVE add) +// 42 -> number constant (e.g., PUSH 42) +// "str" -> string constant (e.g., PUSH "hello") +// 'str' -> string constant (e.g., PUSH 'hello') +// true -> boolean constant (e.g., PUSH true) +// false -> boolean constant (e.g., PUSH false) +// null -> null constant (e.g., PUSH null) +// +// Labels: +// .label_name: -> label definition (marks current instruction position) +// +// Function definitions: +// MAKE_FUNCTION (x y) #7 -> basic function (numeric offset) +// MAKE_FUNCTION (x y) .body -> basic function (label reference) +// MAKE_FUNCTION (x y=42) #7 -> with defaults +// MAKE_FUNCTION (x ...rest) #7 -> variadic +// MAKE_FUNCTION (x @named) #7 -> named function toBytecodeFromString(str: string): Bytecode /* throws */ { const lines = str.trim().split("\n")