Sharkey/packages/frontend/src/scripts/hpml/index.ts

104 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-05-01 12:33:11 +03:00
/**
2020-04-20 15:35:27 +03:00
* Hpml
2019-05-01 12:33:11 +03:00
*/
2021-01-02 16:03:15 +02:00
import autobind from 'autobind-decorator';
import { Hpml } from './evaluator';
import { funcDefs } from './lib';
2019-05-01 12:33:11 +03:00
2021-01-02 16:03:15 +02:00
export type Fn = {
slots: string[];
exec: (args: Record<string, any>) => ReturnType<Hpml['evaluate']>;
};
2019-05-01 13:31:34 +03:00
export type Type = 'string' | 'number' | 'boolean' | 'stringArray' | null;
2019-05-01 12:33:11 +03:00
2019-05-01 13:31:34 +03:00
export const literalDefs: Record<string, { out: any; category: string; icon: any; }> = {
text: { out: 'string', category: 'value', icon: 'ti ti-quote' },
2023-02-14 06:17:00 +02:00
multiLineText: { out: 'string', category: 'value', icon: 'ti ti-align-left' },
textList: { out: 'stringArray', category: 'value', icon: 'ti ti-list' },
number: { out: 'number', category: 'value', icon: 'ti ti-list-numbers' },
ref: { out: null, category: 'value', icon: 'ti ti-wand' },
aiScriptVar: { out: null, category: 'value', icon: 'ti ti-wand' },
fn: { out: 'function', category: 'value', icon: 'ti ti-math-function' },
2019-05-01 12:33:11 +03:00
};
export const blockDefs = [
...Object.entries(literalDefs).map(([k, v]) => ({
type: k, out: v.out, category: v.category, icon: v.icon,
2019-05-01 12:33:11 +03:00
})),
...Object.entries(funcDefs).map(([k, v]) => ({
type: k, out: v.out, category: v.category, icon: v.icon,
})),
2019-05-01 12:33:11 +03:00
];
export type PageVar = { name: string; value: any; type: Type; };
2019-05-01 13:31:34 +03:00
export const envVarsDef: Record<string, Type> = {
2019-05-01 12:33:11 +03:00
AI: 'string',
URL: 'string',
VERSION: 'string',
LOGIN: 'boolean',
NAME: 'string',
USERNAME: 'string',
USERID: 'string',
NOTES_COUNT: 'number',
FOLLOWERS_COUNT: 'number',
FOLLOWING_COUNT: 'number',
IS_CAT: 'boolean',
SEED: null,
YMD: 'string',
2020-04-15 18:39:21 +03:00
AISCRIPT_DISABLED: 'boolean',
2019-05-10 08:18:18 +03:00
NULL: null,
2019-05-01 12:33:11 +03:00
};
2021-01-02 16:03:15 +02:00
export class HpmlScope {
private layerdStates: Record<string, any>[];
public name: string;
constructor(layerdStates: HpmlScope['layerdStates'], name?: HpmlScope['name']) {
this.layerdStates = layerdStates;
this.name = name || 'anonymous';
}
@autobind
public createChildScope(states: Record<string, any>, name?: HpmlScope['name']): HpmlScope {
const layer = [states, ...this.layerdStates];
return new HpmlScope(layer, name);
}
/**
*
* @param name
*/
@autobind
public getState(name: string): any {
for (const later of this.layerdStates) {
const state = later[name];
if (state !== undefined) {
return state;
}
}
throw new HpmlError(
`No such variable '${name}' in scope '${this.name}'`, {
scope: this.layerdStates,
2021-01-02 16:03:15 +02:00
});
}
}
export class HpmlError extends Error {
public info?: any;
constructor(message: string, info?: any) {
super(message);
this.info = info;
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, HpmlError);
}
}
}