Skip to content

Commit a30c150

Browse files
authored
refactor: Add AST nodes of infix operations (#741)
* Add AST nodes of infix operations * get infix operations from the scope * revert
1 parent 90fd542 commit a30c150

File tree

5 files changed

+363
-19
lines changed

5 files changed

+363
-19
lines changed

etc/aiscript.api.md

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
// Warning: (ae-forgotten-export) The symbol "NodeBase" needs to be exported by the entry point index.d.ts
88
//
9+
// @public (undocumented)
10+
type Add = NodeBase & {
11+
type: 'add';
12+
left: Expression;
13+
right: Expression;
14+
operatorLoc: Loc;
15+
};
16+
917
// @public (undocumented)
1018
type AddAssign = NodeBase & {
1119
type: 'addAssign';
@@ -138,6 +146,18 @@ declare namespace Ast {
138146
Assign,
139147
Expression,
140148
Not,
149+
Pow,
150+
Mul,
151+
Div,
152+
Rem,
153+
Add,
154+
Sub,
155+
Lt,
156+
Lteq,
157+
Gt,
158+
Gteq,
159+
Eq,
160+
Neq,
141161
And,
142162
Or,
143163
If,
@@ -226,6 +246,14 @@ type Definition = NodeBase & {
226246
attr: Attribute[];
227247
};
228248

249+
// @public (undocumented)
250+
type Div = NodeBase & {
251+
type: 'div';
252+
left: Expression;
253+
right: Expression;
254+
operatorLoc: Loc;
255+
};
256+
229257
// @public (undocumented)
230258
type Each = NodeBase & {
231259
type: 'each';
@@ -234,6 +262,14 @@ type Each = NodeBase & {
234262
for: Statement | Expression;
235263
};
236264

265+
// @public (undocumented)
266+
type Eq = NodeBase & {
267+
type: 'eq';
268+
left: Expression;
269+
right: Expression;
270+
operatorLoc: Loc;
271+
};
272+
237273
// @public (undocumented)
238274
function eq(a: Value, b: Value): boolean;
239275

@@ -264,7 +300,7 @@ type Exists = NodeBase & {
264300
function expectAny(val: Value | null | undefined): asserts val is Value;
265301

266302
// @public (undocumented)
267-
type Expression = If | Fn | Match | Block | Exists | Tmpl | Str | Num | Bool | Null | Obj | Arr | Not | And | Or | Identifier | Call | Index | Prop;
303+
type Expression = If | Fn | Match | Block | Exists | Tmpl | Str | Num | Bool | Null | Obj | Arr | Not | Pow | Mul | Div | Rem | Add | Sub | Lt | Lteq | Gt | Gteq | Eq | Neq | And | Or | Identifier | Call | Index | Prop;
268304

269305
// @public (undocumented)
270306
const FALSE: {
@@ -311,6 +347,22 @@ type For = NodeBase & {
311347
// @public (undocumented)
312348
function getLangVersion(input: string): string | null;
313349

350+
// @public (undocumented)
351+
type Gt = NodeBase & {
352+
type: 'gt';
353+
left: Expression;
354+
right: Expression;
355+
operatorLoc: Loc;
356+
};
357+
358+
// @public (undocumented)
359+
type Gteq = NodeBase & {
360+
type: 'gteq';
361+
left: Expression;
362+
right: Expression;
363+
operatorLoc: Loc;
364+
};
365+
314366
// @public (undocumented)
315367
type Identifier = NodeBase & {
316368
type: 'identifier';
@@ -403,6 +455,22 @@ type Loop = NodeBase & {
403455
statements: (Statement | Expression)[];
404456
};
405457

458+
// @public (undocumented)
459+
type Lt = NodeBase & {
460+
type: 'lt';
461+
left: Expression;
462+
right: Expression;
463+
operatorLoc: Loc;
464+
};
465+
466+
// @public (undocumented)
467+
type Lteq = NodeBase & {
468+
type: 'lteq';
469+
left: Expression;
470+
right: Expression;
471+
operatorLoc: Loc;
472+
};
473+
406474
// @public (undocumented)
407475
type Match = NodeBase & {
408476
type: 'match';
@@ -421,6 +489,14 @@ type Meta = NodeBase & {
421489
value: Expression;
422490
};
423491

492+
// @public (undocumented)
493+
type Mul = NodeBase & {
494+
type: 'mul';
495+
left: Expression;
496+
right: Expression;
497+
operatorLoc: Loc;
498+
};
499+
424500
// @public (undocumented)
425501
type NamedTypeSource = NodeBase & {
426502
type: 'namedTypeSource';
@@ -435,6 +511,14 @@ type Namespace = NodeBase & {
435511
members: (Definition | Namespace)[];
436512
};
437513

514+
// @public (undocumented)
515+
type Neq = NodeBase & {
516+
type: 'neq';
517+
left: Expression;
518+
right: Expression;
519+
operatorLoc: Loc;
520+
};
521+
438522
// @public (undocumented)
439523
type Node_2 = Namespace | Meta | Statement | Expression | TypeSource | Attribute;
440524

@@ -510,13 +594,29 @@ type Pos = {
510594
column: number;
511595
};
512596

597+
// @public (undocumented)
598+
type Pow = NodeBase & {
599+
type: 'pow';
600+
left: Expression;
601+
right: Expression;
602+
operatorLoc: Loc;
603+
};
604+
513605
// @public (undocumented)
514606
type Prop = NodeBase & {
515607
type: 'prop';
516608
target: Expression;
517609
name: string;
518610
};
519611

612+
// @public (undocumented)
613+
type Rem = NodeBase & {
614+
type: 'rem';
615+
left: Expression;
616+
right: Expression;
617+
operatorLoc: Loc;
618+
};
619+
520620
// @public (undocumented)
521621
function reprValue(value: Value, literalLike?: boolean, processedObjects?: Set<object>): string;
522622

@@ -566,6 +666,14 @@ type Str = NodeBase & {
566666
value: string;
567667
};
568668

669+
// @public (undocumented)
670+
type Sub = NodeBase & {
671+
type: 'sub';
672+
left: Expression;
673+
right: Expression;
674+
operatorLoc: Loc;
675+
};
676+
569677
// @public (undocumented)
570678
type SubAssign = NodeBase & {
571679
type: 'subAssign';

src/interpreter/index.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,102 @@ export class Interpreter {
546546
return NULL; // nop
547547
}
548548

549+
case 'pow': {
550+
const callee = scope.get('Core:pow');
551+
assertFunction(callee);
552+
const left = await this._eval(node.left, scope);
553+
const right = await this._eval(node.right, scope);
554+
return this._fn(callee, [left, right]);
555+
}
556+
557+
case 'mul': {
558+
const callee = scope.get('Core:mul');
559+
assertFunction(callee);
560+
const left = await this._eval(node.left, scope);
561+
const right = await this._eval(node.right, scope);
562+
return this._fn(callee, [left, right]);
563+
}
564+
565+
case 'div': {
566+
const callee = scope.get('Core:div');
567+
assertFunction(callee);
568+
const left = await this._eval(node.left, scope);
569+
const right = await this._eval(node.right, scope);
570+
return this._fn(callee, [left, right]);
571+
}
572+
573+
case 'rem': {
574+
const callee = scope.get('Core:mod');
575+
assertFunction(callee);
576+
const left = await this._eval(node.left, scope);
577+
const right = await this._eval(node.right, scope);
578+
return this._fn(callee, [left, right]);
579+
}
580+
581+
case 'add': {
582+
const callee = scope.get('Core:add');
583+
assertFunction(callee);
584+
const left = await this._eval(node.left, scope);
585+
const right = await this._eval(node.right, scope);
586+
return this._fn(callee, [left, right]);
587+
}
588+
589+
case 'sub': {
590+
const callee = scope.get('Core:sub');
591+
assertFunction(callee);
592+
const left = await this._eval(node.left, scope);
593+
const right = await this._eval(node.right, scope);
594+
return this._fn(callee, [left, right]);
595+
}
596+
597+
case 'lt': {
598+
const callee = scope.get('Core:lt');
599+
assertFunction(callee);
600+
const left = await this._eval(node.left, scope);
601+
const right = await this._eval(node.right, scope);
602+
return this._fn(callee, [left, right]);
603+
}
604+
605+
case 'lteq': {
606+
const callee = scope.get('Core:lteq');
607+
assertFunction(callee);
608+
const left = await this._eval(node.left, scope);
609+
const right = await this._eval(node.right, scope);
610+
return this._fn(callee, [left, right]);
611+
}
612+
613+
case 'gt': {
614+
const callee = scope.get('Core:gt');
615+
assertFunction(callee);
616+
const left = await this._eval(node.left, scope);
617+
const right = await this._eval(node.right, scope);
618+
return this._fn(callee, [left, right]);
619+
}
620+
621+
case 'gteq': {
622+
const callee = scope.get('Core:gteq');
623+
assertFunction(callee);
624+
const left = await this._eval(node.left, scope);
625+
const right = await this._eval(node.right, scope);
626+
return this._fn(callee, [left, right]);
627+
}
628+
629+
case 'eq': {
630+
const callee = scope.get('Core:eq');
631+
assertFunction(callee);
632+
const left = await this._eval(node.left, scope);
633+
const right = await this._eval(node.right, scope);
634+
return this._fn(callee, [left, right]);
635+
}
636+
637+
case 'neq': {
638+
const callee = scope.get('Core:neq');
639+
assertFunction(callee);
640+
const left = await this._eval(node.left, scope);
641+
const right = await this._eval(node.right, scope);
642+
return this._fn(callee, [left, right]);
643+
}
644+
549645
case 'and': {
550646
const leftValue = await this._eval(node.left, scope);
551647
assertBoolean(leftValue);

0 commit comments

Comments
 (0)