This commit is contained in:
Chris Wanstrath 2025-10-05 20:43:17 -07:00
parent c0ef5f55eb
commit f18e014d3e
3 changed files with 10 additions and 10 deletions

View File

@ -31,7 +31,7 @@ export type Constant =
// MAKE_FUNCTION (x y) #7 -> basic function
// MAKE_FUNCTION (x y=42) #7 -> with defaults
// MAKE_FUNCTION (x ...rest) #7 -> variadic
// MAKE_FUNCTION (x @named) #7 -> kwargs
// MAKE_FUNCTION (x @named) #7 -> named
//
function parseFunctionParams(paramStr: string, constants: Constant[]): {
@ -43,12 +43,12 @@ function parseFunctionParams(paramStr: string, constants: Constant[]): {
const params: string[] = []
const defaults: Record<string, number> = {}
let variadic = false
let kwargs = false
let named = false
// Remove parens and split by whitespace
const paramList = paramStr.slice(1, -1).trim()
if (!paramList) {
return { params, defaults, variadic, named: kwargs }
return { params, defaults, variadic, named: named }
}
const parts = paramList.split(/\s+/)
@ -56,7 +56,7 @@ function parseFunctionParams(paramStr: string, constants: Constant[]): {
for (const part of parts) {
// Check for named args (@name)
if (part.startsWith('@')) {
kwargs = true
named = true
params.push(part.slice(1))
} else if (part.startsWith('...')) {
@ -91,7 +91,7 @@ function parseFunctionParams(paramStr: string, constants: Constant[]): {
}
}
return { params, defaults, variadic, named: kwargs }
return { params, defaults, variadic, named: named }
}
export function toBytecode(str: string): Bytecode /* throws */ {
@ -130,7 +130,7 @@ export function toBytecode(str: string): Bytecode /* throws */ {
const bodyStr = match[2]!
const body = parseInt(bodyStr.slice(1))
const { params, defaults, variadic, named: kwargs } = parseFunctionParams(paramStr, bytecode.constants)
const { params, defaults, variadic, named } = parseFunctionParams(paramStr, bytecode.constants)
// Add function definition to constants
bytecode.constants.push({
@ -139,7 +139,7 @@ export function toBytecode(str: string): Bytecode /* throws */ {
defaults,
body,
variadic,
kwargs
named
})
operandValue = bytecode.constants.length - 1

View File

@ -14,7 +14,7 @@ export type Value =
body: number,
parentScope: Scope,
variadic: boolean,
kwargs: boolean
named: boolean
}
export type Dict = Map<string, Value>
@ -25,7 +25,7 @@ export type FunctionDef = {
defaults: Record<string, number>
body: number
variadic: boolean
kwargs: boolean
named: boolean
}
export function toValue(v: any): Value /* throws */ {

View File

@ -105,7 +105,7 @@ test("THROW - exception unwinds call stack", async () => {
defaults: {},
body: 7,
variadic: false,
kwargs: false
named: false
},
toValue('function error'),
toValue(999)