summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/hpml/expr.ts
blob: 18c7c2a14b6da8704b252f6455cdf145c3d1be7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { literalDefs, Type } from '.';

export type ExprBase = {
	id: string;
};

// value

export type EmptyValue = ExprBase & {
	type: null;
	value: null;
};

export type TextValue = ExprBase & {
	type: 'text';
	value: string;
};

export type MultiLineTextValue = ExprBase & {
	type: 'multiLineText';
	value: string;
};

export type TextListValue = ExprBase & {
	type: 'textList';
	value: string;
};

export type NumberValue = ExprBase & {
	type: 'number';
	value: number;
};

export type RefValue = ExprBase & {
	type: 'ref';
	value: string; // value is variable name
};

export type AiScriptRefValue = ExprBase & {
	type: 'aiScriptVar';
	value: string; // value is variable name
};

export type UserFnValue = ExprBase & {
	type: 'fn';
	value: UserFnInnerValue;
};
type UserFnInnerValue = {
	slots: {
		name: string;
		type: Type;
	}[];
	expression: Expr;
};

export type Value =
	EmptyValue | TextValue | MultiLineTextValue | TextListValue | NumberValue | RefValue | AiScriptRefValue | UserFnValue;

export function isLiteralValue(expr: Expr): expr is Value {
	if (expr.type == null) return true;
	if (literalDefs[expr.type]) return true;
	return false;
}

// call function

export type CallFn = ExprBase & { // "fn:hoge" or string
	type: string;
	args: Expr[];
	value: null;
};

// variable
export type Variable = (Value | CallFn) & {
	name: string;
};

// expression
export type Expr = Variable | Value | CallFn;