Compare commits
No commits in common. "92ce43b5089dfb145abe0a9021ee39c9aaf74b0f" and "1a3f1c6c439a8c1ed6827628ee3eedeac480b3fa" have entirely different histories.
92ce43b508
...
1a3f1c6c43
8
ha.sh
Normal file
8
ha.sh
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
bob = [ name= Bob age= 44 ]
|
||||||
|
mike = [
|
||||||
|
name= Mike
|
||||||
|
age= 46
|
||||||
|
]
|
||||||
|
|
||||||
|
echo bob
|
||||||
|
echo (mike | at name)
|
||||||
|
|
@ -1,16 +1,9 @@
|
||||||
import { type Value, toString, toValue } from 'reefvm'
|
|
||||||
|
|
||||||
export const dict = {
|
export const dict = {
|
||||||
keys: (dict: Record<string, any>) => Object.keys(dict),
|
keys: (dict: Record<string, any>) => Object.keys(dict),
|
||||||
values: (dict: Record<string, any>) => Object.values(dict),
|
values: (dict: Record<string, any>) => Object.values(dict),
|
||||||
entries: (dict: Record<string, any>) => Object.entries(dict).map(([k, v]) => ({ key: k, value: v })),
|
entries: (dict: Record<string, any>) => Object.entries(dict).map(([k, v]) => ({ key: k, value: v })),
|
||||||
'has?': (dict: Record<string, any>, key: string) => key in dict,
|
'has?': (dict: Record<string, any>, key: string) => key in dict,
|
||||||
get: (dict: Record<string, any>, key: string, defaultValue: any = null) => dict[key] ?? defaultValue,
|
get: (dict: Record<string, any>, key: string, defaultValue: any = null) => dict[key] ?? defaultValue,
|
||||||
set: (dict: Value, key: Value, value: Value) => {
|
|
||||||
const map = dict.value as Map<string, Value>
|
|
||||||
map.set(toString(key), value)
|
|
||||||
return dict
|
|
||||||
},
|
|
||||||
merge: (...dicts: Record<string, any>[]) => Object.assign({}, ...dicts),
|
merge: (...dicts: Record<string, any>[]) => Object.assign({}, ...dicts),
|
||||||
'empty?': (dict: Record<string, any>) => Object.keys(dict).length === 0,
|
'empty?': (dict: Record<string, any>) => Object.keys(dict).length === 0,
|
||||||
map: async (dict: Record<string, any>, cb: Function) => {
|
map: async (dict: Record<string, any>, cb: Function) => {
|
||||||
|
|
@ -29,7 +22,3 @@ export const dict = {
|
||||||
},
|
},
|
||||||
'from-entries': (entries: [string, any][]) => Object.fromEntries(entries),
|
'from-entries': (entries: [string, any][]) => Object.fromEntries(entries),
|
||||||
}
|
}
|
||||||
|
|
||||||
// raw functions deal directly in Value types, meaning we can modify collection
|
|
||||||
// careful - the MUST return a Value!
|
|
||||||
; (dict.set as any).raw = true
|
|
||||||
|
|
|
||||||
|
|
@ -471,12 +471,6 @@ describe('dict operations', () => {
|
||||||
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99, globals)
|
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99, globals)
|
||||||
})
|
})
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
test('dict.empty? checks if dict is empty', async () => {
|
test('dict.empty? checks if dict is empty', async () => {
|
||||||
await expect(`dict.empty? [=]`).toEvaluateTo(true, globals)
|
await expect(`dict.empty? [=]`).toEvaluateTo(true, globals)
|
||||||
await expect(`dict.empty? [a=1]`).toEvaluateTo(false, globals)
|
await expect(`dict.empty? [a=1]`).toEvaluateTo(false, globals)
|
||||||
|
|
|
||||||
|
|
@ -108,10 +108,16 @@ expect.extend({
|
||||||
if (expected instanceof RegExp) expected = String(expected)
|
if (expected instanceof RegExp) expected = String(expected)
|
||||||
if (value instanceof RegExp) value = String(value)
|
if (value instanceof RegExp) value = String(value)
|
||||||
|
|
||||||
expect(value).toEqual(expected)
|
if (isEqual(value, expected)) {
|
||||||
|
return { pass: true }
|
||||||
|
} else {
|
||||||
return {
|
return {
|
||||||
message: () => `Expected evaluation to be ${expected}, but got ${value}`,
|
message: () =>
|
||||||
pass: true,
|
`Expected evaluation to be ${JSON.stringify(expected)}, but got ${JSON.stringify(
|
||||||
|
value
|
||||||
|
)}`,
|
||||||
|
pass: false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
|
|
@ -161,3 +167,29 @@ const trimWhitespace = (str: string): string => {
|
||||||
})
|
})
|
||||||
.join('\n')
|
.join('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isEqual(a: any, b: any): boolean {
|
||||||
|
if (a === null && b === null) return true
|
||||||
|
|
||||||
|
switch (typeof a) {
|
||||||
|
case 'string':
|
||||||
|
case 'number':
|
||||||
|
case 'boolean':
|
||||||
|
case 'undefined':
|
||||||
|
return a === b
|
||||||
|
default:
|
||||||
|
return JSON.stringify(sortKeys(a)) === JSON.stringify(sortKeys(b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortKeys(o: any): any {
|
||||||
|
if (Array.isArray(o)) return o.map(sortKeys)
|
||||||
|
if (o && typeof o === 'object' && o.constructor === Object)
|
||||||
|
return Object.keys(o)
|
||||||
|
.sort()
|
||||||
|
.reduce((r, k) => {
|
||||||
|
r[k] = sortKeys(o[k])
|
||||||
|
return r
|
||||||
|
}, {} as any)
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { Tree, TreeCursor } from '@lezer/common'
|
import { Tree, TreeCursor } from '@lezer/common'
|
||||||
|
import { assertNever } from '#utils/utils'
|
||||||
import { type Value, fromValue } from 'reefvm'
|
import { type Value, fromValue } from 'reefvm'
|
||||||
|
|
||||||
export const treeToString = (tree: Tree, input: string): string => {
|
export const treeToString = (tree: Tree, input: string): string => {
|
||||||
|
|
@ -34,5 +35,6 @@ export const treeToString = (tree: Tree, input: string): string => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const VMResultToValue = (result: Value): unknown => {
|
export const VMResultToValue = (result: Value): unknown => {
|
||||||
return result.type === 'function' ? Function : fromValue(result)
|
if (result.type === 'function') return Function
|
||||||
|
else return fromValue(result)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user