better echo
This commit is contained in:
parent
787d2f2611
commit
d7bcc590fe
|
|
@ -1,6 +1,7 @@
|
||||||
// The prelude creates all the builtin Shrimp functions.
|
// The prelude creates all the builtin Shrimp functions.
|
||||||
|
|
||||||
import { toValue, type Value, extractParamInfo, isWrapped, getOriginalFunction } from 'reefvm'
|
import { readFileSync } from 'fs'
|
||||||
|
import { VM, isValue, toValue, type Value, extractParamInfo, isWrapped, getOriginalFunction } from 'reefvm'
|
||||||
|
|
||||||
export const colors = {
|
export const colors = {
|
||||||
reset: '\x1b[0m',
|
reset: '\x1b[0m',
|
||||||
|
|
@ -15,40 +16,28 @@ export const colors = {
|
||||||
pink: '\x1b[38;2;255;105;180m'
|
pink: '\x1b[38;2;255;105;180m'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const valueFunctions = {
|
export const nativeFunctions = {
|
||||||
echo: (...args: Value[]) => {
|
// hello
|
||||||
console.log(...args.map(a => formatValue(a)))
|
echo: (...args: any[]) => {
|
||||||
|
console.log(...args.map(a => {
|
||||||
|
const v = toValue(a)
|
||||||
|
return ['array', 'dict'].includes(v.type) ? formatValue(v, true) : v.value
|
||||||
|
}))
|
||||||
return toValue(null)
|
return toValue(null)
|
||||||
},
|
},
|
||||||
|
|
||||||
length: (value: Value) => {
|
// info
|
||||||
|
type: (v: any) => toValue(v).type,
|
||||||
|
inspect: (v: any) => formatValue(toValue(v)),
|
||||||
|
length: (v: any) => {
|
||||||
|
const value = toValue(v)
|
||||||
switch (value.type) {
|
switch (value.type) {
|
||||||
case 'string': case 'array':
|
case 'string': case 'array': return value.value.length
|
||||||
return toValue(value.value.length)
|
case 'dict': return value.value.size
|
||||||
case 'dict':
|
default: return 0
|
||||||
return toValue(Object.keys(value.value).length)
|
|
||||||
default:
|
|
||||||
return toValue(0)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
type: (value: Value) => toValue(value.type),
|
|
||||||
inspect: (value: Value) => toValue(formatValue(value))
|
|
||||||
}
|
|
||||||
|
|
||||||
export const nativeFunctions = {
|
|
||||||
range: (start: number, end: number | null) => {
|
|
||||||
if (end === null) {
|
|
||||||
end = start
|
|
||||||
start = 0
|
|
||||||
}
|
|
||||||
const result: number[] = []
|
|
||||||
for (let i = start; i <= end; i++) {
|
|
||||||
result.push(i)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
},
|
|
||||||
|
|
||||||
// strings
|
// strings
|
||||||
join: (arr: any[], sep: string = ',') => arr.join(sep),
|
join: (arr: any[], sep: string = ',') => arr.join(sep),
|
||||||
split: (str: string, sep: string = ',') => str.split(sep),
|
split: (str: string, sep: string = ',') => str.split(sep),
|
||||||
|
|
@ -61,6 +50,17 @@ export const nativeFunctions = {
|
||||||
list: (...args: any[]) => args,
|
list: (...args: any[]) => args,
|
||||||
dict: (atNamed = {}) => atNamed,
|
dict: (atNamed = {}) => atNamed,
|
||||||
slice: (list: any[], start: number, end?: number) => list.slice(start, end),
|
slice: (list: any[], start: number, end?: number) => list.slice(start, end),
|
||||||
|
range: (start: number, end: number | null) => {
|
||||||
|
if (end === null) {
|
||||||
|
end = start
|
||||||
|
start = 0
|
||||||
|
}
|
||||||
|
const result: number[] = []
|
||||||
|
for (let i = start; i <= end; i++) {
|
||||||
|
result.push(i)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
},
|
||||||
|
|
||||||
// enumerables
|
// enumerables
|
||||||
map: async (list: any[], cb: Function) => {
|
map: async (list: any[], cb: Function) => {
|
||||||
|
|
@ -71,9 +71,16 @@ export const nativeFunctions = {
|
||||||
each: async (list: any[], cb: Function) => {
|
each: async (list: any[], cb: Function) => {
|
||||||
for (const value of list) await cb(value)
|
for (const value of list) await cb(value)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// modules
|
||||||
|
use: function (this: VM, path: string) {
|
||||||
|
const file = readFileSync(path + '.sh')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatValue(value: Value, inner = false): string {
|
export function formatValue(value: Value | any, inner = false): string {
|
||||||
|
if (!isValue(value)) value = toValue(value)
|
||||||
|
|
||||||
switch (value.type) {
|
switch (value.type) {
|
||||||
case 'string':
|
case 'string':
|
||||||
return `${colors.green}'${value.value.replaceAll("'", "\\'")}${colors.green}'${colors.reset}`
|
return `${colors.green}'${value.value.replaceAll("'", "\\'")}${colors.green}'${colors.reset}`
|
||||||
Loading…
Reference in New Issue
Block a user