misskey/src/client/scripts/hpml/index.ts

198 lines
8.0 KiB
TypeScript
Raw Normal View History

2019-05-01 09:33:11 +00:00
/**
2020-04-20 12:35:27 +00:00
* Hpml
2019-05-01 09:33:11 +00:00
*/
2021-01-02 14:03:15 +00:00
import autobind from 'autobind-decorator';
2019-05-01 09:33:11 +00:00
import {
2019-05-01 10:45:05 +00:00
faMagic,
faSquareRootAlt,
2019-05-01 09:33:11 +00:00
faAlignLeft,
faShareAlt,
faPlus,
faMinus,
faTimes,
faDivide,
faList,
faQuoteRight,
faEquals,
faGreaterThan,
faLessThan,
faGreaterThanEqual,
faLessThanEqual,
faNotEqual,
faDice,
faSortNumericUp,
faExchangeAlt,
faRecycle,
faIndent,
2019-12-19 17:09:51 +00:00
faCalculator,
2019-05-01 09:33:11 +00:00
} from '@fortawesome/free-solid-svg-icons';
import { faFlag } from '@fortawesome/free-regular-svg-icons';
2021-01-02 14:03:15 +00:00
import { Hpml } from './evaluator';
2019-05-01 09:33:11 +00:00
export type Block<V = any> = {
id: string;
type: string;
args: Block[];
value: V;
};
export type FnBlock = Block<{
slots: {
name: string;
type: Type;
}[];
expression: Block;
}>;
export type Variable = Block & {
name: string;
};
2021-01-02 14:03:15 +00:00
export type Fn = {
slots: string[];
exec: (args: Record<string, any>) => ReturnType<Hpml['evaluate']>;
};
2019-05-01 10:31:34 +00:00
export type Type = 'string' | 'number' | 'boolean' | 'stringArray' | null;
2019-05-01 09:33:11 +00:00
2019-05-01 10:31:34 +00:00
export const funcDefs: Record<string, { in: any[]; out: any; category: string; icon: any; }> = {
2019-05-01 09:33:11 +00:00
if: { in: ['boolean', 0, 0], out: 0, category: 'flow', icon: faShareAlt, },
for: { in: ['number', 'function'], out: null, category: 'flow', icon: faRecycle, },
not: { in: ['boolean'], out: 'boolean', category: 'logical', icon: faFlag, },
or: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, },
and: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, },
add: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faPlus, },
subtract: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faMinus, },
multiply: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faTimes, },
divide: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, },
2019-06-15 08:06:03 +00:00
mod: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, },
2019-12-19 17:09:51 +00:00
round: { in: ['number'], out: 'number', category: 'operation', icon: faCalculator, },
2019-05-01 09:33:11 +00:00
eq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faEquals, },
notEq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faNotEqual, },
gt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThan, },
lt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThan, },
gtEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThanEqual, },
ltEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThanEqual, },
strLen: { in: ['string'], out: 'number', category: 'text', icon: faQuoteRight, },
strPick: { in: ['string', 'number'], out: 'string', category: 'text', icon: faQuoteRight, },
strReplace: { in: ['string', 'string', 'string'], out: 'string', category: 'text', icon: faQuoteRight, },
strReverse: { in: ['string'], out: 'string', category: 'text', icon: faQuoteRight, },
join: { in: ['stringArray', 'string'], out: 'string', category: 'text', icon: faQuoteRight, },
stringToNumber: { in: ['string'], out: 'number', category: 'convert', icon: faExchangeAlt, },
numberToString: { in: ['number'], out: 'string', category: 'convert', icon: faExchangeAlt, },
splitStrByLine: { in: ['string'], out: 'stringArray', category: 'convert', icon: faExchangeAlt, },
2019-05-05 11:36:09 +00:00
pick: { in: [null, 'number'], out: null, category: 'list', icon: faIndent, },
2019-06-22 15:06:39 +00:00
listLen: { in: [null], out: 'number', category: 'list', icon: faIndent, },
2019-05-01 09:33:11 +00:00
rannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, },
dailyRannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, },
seedRannum: { in: [null, 'number', 'number'], out: 'number', category: 'random', icon: faDice, },
random: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, },
dailyRandom: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, },
seedRandom: { in: [null, 'number'], out: 'boolean', category: 'random', icon: faDice, },
randomPick: { in: [0], out: 0, category: 'random', icon: faDice, },
dailyRandomPick: { in: [0], out: 0, category: 'random', icon: faDice, },
seedRandomPick: { in: [null, 0], out: 0, category: 'random', icon: faDice, },
2019-05-05 11:16:05 +00:00
DRPWPM: { in: ['stringArray'], out: 'string', category: 'random', icon: faDice, }, // dailyRandomPickWithProbabilityMapping
2019-05-01 09:33:11 +00:00
};
2019-05-01 10:31:34 +00:00
export const literalDefs: Record<string, { out: any; category: string; icon: any; }> = {
2019-05-01 09:33:11 +00: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 10:45:05 +00:00
ref: { out: null, category: 'value', icon: faMagic, },
2020-04-12 18:23:23 +00:00
aiScriptVar: { out: null, category: 'value', icon: faMagic, },
2019-05-01 10:45:05 +00:00
fn: { out: 'function', category: 'value', icon: faSquareRootAlt, },
2019-05-01 09:33:11 +00: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 function isFnBlock(block: Block): block is FnBlock {
return block.type === 'fn';
}
export type PageVar = { name: string; value: any; type: Type; };
2019-05-01 10:31:34 +00:00
export const envVarsDef: Record<string, Type> = {
2019-05-01 09:33:11 +00: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 15:39:21 +00:00
AISCRIPT_DISABLED: 'boolean',
2019-05-10 05:18:18 +00:00
NULL: null,
2019-05-01 09:33:11 +00:00
};
export function isLiteralBlock(v: Block) {
if (v.type === null) return true;
if (literalDefs[v.type]) return true;
return false;
}
2021-01-02 14:03:15 +00: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);
}
}
}