2019-05-01 11:33:11 +02:00
|
|
|
/**
|
2020-04-20 14:35:27 +02:00
|
|
|
* Hpml
|
2019-05-01 11:33:11 +02:00
|
|
|
*/
|
|
|
|
|
2021-01-02 15:03:15 +01:00
|
|
|
import autobind from 'autobind-decorator';
|
2019-05-01 11:33:11 +02:00
|
|
|
import {
|
2019-05-01 12:45:05 +02:00
|
|
|
faMagic,
|
|
|
|
faSquareRootAlt,
|
2019-05-01 11:33:11 +02:00
|
|
|
faAlignLeft,
|
|
|
|
faList,
|
|
|
|
faQuoteRight,
|
|
|
|
faSortNumericUp,
|
|
|
|
} from '@fortawesome/free-solid-svg-icons';
|
2021-01-02 15:03:15 +01:00
|
|
|
import { Hpml } from './evaluator';
|
2021-01-30 02:59:05 +01:00
|
|
|
import { funcDefs } from './lib';
|
2019-05-01 11:33:11 +02:00
|
|
|
|
2021-01-02 15:03:15 +01:00
|
|
|
export type Fn = {
|
|
|
|
slots: string[];
|
|
|
|
exec: (args: Record<string, any>) => ReturnType<Hpml['evaluate']>;
|
|
|
|
};
|
|
|
|
|
2019-05-01 12:31:34 +02:00
|
|
|
export type Type = 'string' | 'number' | 'boolean' | 'stringArray' | null;
|
2019-05-01 11:33:11 +02:00
|
|
|
|
2019-05-01 12:31:34 +02:00
|
|
|
export const literalDefs: Record<string, { out: any; category: string; icon: any; }> = {
|
2019-05-01 11:33:11 +02:00
|
|
|
text: { out: 'string', category: 'value', icon: faQuoteRight, },
|
|
|
|
multiLineText: { out: 'string', category: 'value', icon: faAlignLeft, },
|
|
|
|
textList: { out: 'stringArray', category: 'value', icon: faList, },
|
|
|
|
number: { out: 'number', category: 'value', icon: faSortNumericUp, },
|
2019-05-01 12:45:05 +02:00
|
|
|
ref: { out: null, category: 'value', icon: faMagic, },
|
2020-04-12 20:23:23 +02:00
|
|
|
aiScriptVar: { out: null, category: 'value', icon: faMagic, },
|
2019-05-01 12:45:05 +02:00
|
|
|
fn: { out: 'function', category: 'value', icon: faSquareRootAlt, },
|
2019-05-01 11:33:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const blockDefs = [
|
|
|
|
...Object.entries(literalDefs).map(([k, v]) => ({
|
|
|
|
type: k, out: v.out, category: v.category, icon: v.icon
|
|
|
|
})),
|
|
|
|
...Object.entries(funcDefs).map(([k, v]) => ({
|
|
|
|
type: k, out: v.out, category: v.category, icon: v.icon
|
|
|
|
}))
|
|
|
|
];
|
|
|
|
|
|
|
|
export type PageVar = { name: string; value: any; type: Type; };
|
|
|
|
|
2019-05-01 12:31:34 +02:00
|
|
|
export const envVarsDef: Record<string, Type> = {
|
2019-05-01 11:33:11 +02: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 17:39:21 +02:00
|
|
|
AISCRIPT_DISABLED: 'boolean',
|
2019-05-10 07:18:18 +02:00
|
|
|
NULL: null,
|
2019-05-01 11:33:11 +02:00
|
|
|
};
|
|
|
|
|
2021-01-02 15:03:15 +01: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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|