Skip to content

Commit ed362ec

Browse files
enhance: null値のアサーション関数を追加
1 parent ba96b89 commit ed362ec

1 file changed

Lines changed: 42 additions & 29 deletions

File tree

src/interpreter/util.ts

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
11
import { AiScriptRuntimeError } from '../error.js';
22
import { STR, NUM, ARR, OBJ, NULL, BOOL } from './value.js';
3-
import type { Value, VStr, VNum, VBool, VFn, VObj, VArr } from './value.js';
3+
import type { Value, VStr, VNum, VBool, VFn, VObj, VArr, VNull } from './value.js';
44

55
export function expectAny(val: Value | null | undefined): asserts val is Value {
66
if (val == null) {
77
throw new AiScriptRuntimeError('Expect anything, but got nothing.');
88
}
99
}
1010

11+
export function isBoolean(val: Value): val is VBool {
12+
return val.type === 'bool';
13+
}
14+
15+
export function isFunction(val: Value): val is VFn {
16+
return val.type === 'fn';
17+
}
18+
19+
export function isString(val: Value): val is VStr {
20+
return val.type === 'str';
21+
}
22+
23+
export function isNumber(val: Value): val is VNum {
24+
return val.type === 'num';
25+
}
26+
27+
export function isObject(val: Value): val is VObj {
28+
return val.type === 'obj';
29+
}
30+
31+
export function isArray(val: Value): val is VArr {
32+
return val.type === 'arr';
33+
}
34+
35+
export function isNull(val: Value): val is VNull {
36+
return val.type === 'null';
37+
}
38+
1139
export function assertBoolean(val: Value | null | undefined): asserts val is VBool {
1240
if (val == null) {
1341
throw new AiScriptRuntimeError('Expect boolean, but got nothing.');
1442
}
15-
if (val.type !== 'bool') {
43+
if (!isBoolean(val)) {
1644
throw new AiScriptRuntimeError(`Expect boolean, but got ${val.type}.`);
1745
}
1846
}
@@ -21,7 +49,7 @@ export function assertFunction(val: Value | null | undefined): asserts val is VF
2149
if (val == null) {
2250
throw new AiScriptRuntimeError('Expect function, but got nothing.');
2351
}
24-
if (val.type !== 'fn') {
52+
if (!isFunction(val)) {
2553
throw new AiScriptRuntimeError(`Expect function, but got ${val.type}.`);
2654
}
2755
}
@@ -30,7 +58,7 @@ export function assertString(val: Value | null | undefined): asserts val is VStr
3058
if (val == null) {
3159
throw new AiScriptRuntimeError('Expect string, but got nothing.');
3260
}
33-
if (val.type !== 'str') {
61+
if (!isString(val)) {
3462
throw new AiScriptRuntimeError(`Expect string, but got ${val.type}.`);
3563
}
3664
}
@@ -39,7 +67,7 @@ export function assertNumber(val: Value | null | undefined): asserts val is VNum
3967
if (val == null) {
4068
throw new AiScriptRuntimeError('Expect number, but got nothing.');
4169
}
42-
if (val.type !== 'num') {
70+
if (!isNumber(val)) {
4371
throw new AiScriptRuntimeError(`Expect number, but got ${val.type}.`);
4472
}
4573
}
@@ -48,7 +76,7 @@ export function assertObject(val: Value | null | undefined): asserts val is VObj
4876
if (val == null) {
4977
throw new AiScriptRuntimeError('Expect object, but got nothing.');
5078
}
51-
if (val.type !== 'obj') {
79+
if (!isObject(val)) {
5280
throw new AiScriptRuntimeError(`Expect object, but got ${val.type}.`);
5381
}
5482
}
@@ -57,33 +85,18 @@ export function assertArray(val: Value | null | undefined): asserts val is VArr
5785
if (val == null) {
5886
throw new AiScriptRuntimeError('Expect array, but got nothing.');
5987
}
60-
if (val.type !== 'arr') {
88+
if (!isArray(val)) {
6189
throw new AiScriptRuntimeError(`Expect array, but got ${val.type}.`);
6290
}
6391
}
6492

65-
export function isBoolean(val: Value): val is VBool {
66-
return val.type === 'bool';
67-
}
68-
69-
export function isFunction(val: Value): val is VFn {
70-
return val.type === 'fn';
71-
}
72-
73-
export function isString(val: Value): val is VStr {
74-
return val.type === 'str';
75-
}
76-
77-
export function isNumber(val: Value): val is VNum {
78-
return val.type === 'num';
79-
}
80-
81-
export function isObject(val: Value): val is VObj {
82-
return val.type === 'obj';
83-
}
84-
85-
export function isArray(val: Value): val is VArr {
86-
return val.type === 'arr';
93+
export function assertNull(val: Value | null | undefined): asserts val is VNull {
94+
if (val == null) {
95+
throw new AiScriptRuntimeError('Expect null, but got nothing.');
96+
}
97+
if (!isNull(val)) {
98+
throw new AiScriptRuntimeError(`Expect null, but got ${val.type}.`);
99+
}
87100
}
88101

89102
export function eq(a: Value, b: Value): boolean {

0 commit comments

Comments
 (0)