forked from defunkt/ReefVM
named
This commit is contained in:
parent
c0ef5f55eb
commit
f18e014d3e
|
|
@ -31,7 +31,7 @@ export type Constant =
|
||||||
// MAKE_FUNCTION (x y) #7 -> basic function
|
// MAKE_FUNCTION (x y) #7 -> basic function
|
||||||
// MAKE_FUNCTION (x y=42) #7 -> with defaults
|
// MAKE_FUNCTION (x y=42) #7 -> with defaults
|
||||||
// MAKE_FUNCTION (x ...rest) #7 -> variadic
|
// MAKE_FUNCTION (x ...rest) #7 -> variadic
|
||||||
// MAKE_FUNCTION (x @named) #7 -> kwargs
|
// MAKE_FUNCTION (x @named) #7 -> named
|
||||||
//
|
//
|
||||||
|
|
||||||
function parseFunctionParams(paramStr: string, constants: Constant[]): {
|
function parseFunctionParams(paramStr: string, constants: Constant[]): {
|
||||||
|
|
@ -43,12 +43,12 @@ function parseFunctionParams(paramStr: string, constants: Constant[]): {
|
||||||
const params: string[] = []
|
const params: string[] = []
|
||||||
const defaults: Record<string, number> = {}
|
const defaults: Record<string, number> = {}
|
||||||
let variadic = false
|
let variadic = false
|
||||||
let kwargs = false
|
let named = false
|
||||||
|
|
||||||
// Remove parens and split by whitespace
|
// Remove parens and split by whitespace
|
||||||
const paramList = paramStr.slice(1, -1).trim()
|
const paramList = paramStr.slice(1, -1).trim()
|
||||||
if (!paramList) {
|
if (!paramList) {
|
||||||
return { params, defaults, variadic, named: kwargs }
|
return { params, defaults, variadic, named: named }
|
||||||
}
|
}
|
||||||
|
|
||||||
const parts = paramList.split(/\s+/)
|
const parts = paramList.split(/\s+/)
|
||||||
|
|
@ -56,7 +56,7 @@ function parseFunctionParams(paramStr: string, constants: Constant[]): {
|
||||||
for (const part of parts) {
|
for (const part of parts) {
|
||||||
// Check for named args (@name)
|
// Check for named args (@name)
|
||||||
if (part.startsWith('@')) {
|
if (part.startsWith('@')) {
|
||||||
kwargs = true
|
named = true
|
||||||
params.push(part.slice(1))
|
params.push(part.slice(1))
|
||||||
|
|
||||||
} else if (part.startsWith('...')) {
|
} 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 */ {
|
export function toBytecode(str: string): Bytecode /* throws */ {
|
||||||
|
|
@ -130,7 +130,7 @@ export function toBytecode(str: string): Bytecode /* throws */ {
|
||||||
const bodyStr = match[2]!
|
const bodyStr = match[2]!
|
||||||
const body = parseInt(bodyStr.slice(1))
|
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
|
// Add function definition to constants
|
||||||
bytecode.constants.push({
|
bytecode.constants.push({
|
||||||
|
|
@ -139,7 +139,7 @@ export function toBytecode(str: string): Bytecode /* throws */ {
|
||||||
defaults,
|
defaults,
|
||||||
body,
|
body,
|
||||||
variadic,
|
variadic,
|
||||||
kwargs
|
named
|
||||||
})
|
})
|
||||||
|
|
||||||
operandValue = bytecode.constants.length - 1
|
operandValue = bytecode.constants.length - 1
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ export type Value =
|
||||||
body: number,
|
body: number,
|
||||||
parentScope: Scope,
|
parentScope: Scope,
|
||||||
variadic: boolean,
|
variadic: boolean,
|
||||||
kwargs: boolean
|
named: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Dict = Map<string, Value>
|
export type Dict = Map<string, Value>
|
||||||
|
|
@ -25,7 +25,7 @@ export type FunctionDef = {
|
||||||
defaults: Record<string, number>
|
defaults: Record<string, number>
|
||||||
body: number
|
body: number
|
||||||
variadic: boolean
|
variadic: boolean
|
||||||
kwargs: boolean
|
named: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toValue(v: any): Value /* throws */ {
|
export function toValue(v: any): Value /* throws */ {
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,7 @@ test("THROW - exception unwinds call stack", async () => {
|
||||||
defaults: {},
|
defaults: {},
|
||||||
body: 7,
|
body: 7,
|
||||||
variadic: false,
|
variadic: false,
|
||||||
kwargs: false
|
named: false
|
||||||
},
|
},
|
||||||
toValue('function error'),
|
toValue('function error'),
|
||||||
toValue(999)
|
toValue(999)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user