Skip to content

Commit

Permalink
refactor: Add AST nodes of infix operations (#741)
Browse files Browse the repository at this point in the history
* Add AST nodes of infix operations

* get infix operations from the scope

* revert
  • Loading branch information
marihachi authored Aug 3, 2024
1 parent 90fd542 commit a30c150
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 19 deletions.
110 changes: 109 additions & 1 deletion etc/aiscript.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

// Warning: (ae-forgotten-export) The symbol "NodeBase" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
type Add = NodeBase & {
type: 'add';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type AddAssign = NodeBase & {
type: 'addAssign';
Expand Down Expand Up @@ -138,6 +146,18 @@ declare namespace Ast {
Assign,
Expression,
Not,
Pow,
Mul,
Div,
Rem,
Add,
Sub,
Lt,
Lteq,
Gt,
Gteq,
Eq,
Neq,
And,
Or,
If,
Expand Down Expand Up @@ -226,6 +246,14 @@ type Definition = NodeBase & {
attr: Attribute[];
};

// @public (undocumented)
type Div = NodeBase & {
type: 'div';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Each = NodeBase & {
type: 'each';
Expand All @@ -234,6 +262,14 @@ type Each = NodeBase & {
for: Statement | Expression;
};

// @public (undocumented)
type Eq = NodeBase & {
type: 'eq';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
function eq(a: Value, b: Value): boolean;

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

// @public (undocumented)
type Expression = If | Fn | Match | Block | Exists | Tmpl | Str | Num | Bool | Null | Obj | Arr | Not | And | Or | Identifier | Call | Index | Prop;
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;

// @public (undocumented)
const FALSE: {
Expand Down Expand Up @@ -311,6 +347,22 @@ type For = NodeBase & {
// @public (undocumented)
function getLangVersion(input: string): string | null;

// @public (undocumented)
type Gt = NodeBase & {
type: 'gt';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Gteq = NodeBase & {
type: 'gteq';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Identifier = NodeBase & {
type: 'identifier';
Expand Down Expand Up @@ -403,6 +455,22 @@ type Loop = NodeBase & {
statements: (Statement | Expression)[];
};

// @public (undocumented)
type Lt = NodeBase & {
type: 'lt';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Lteq = NodeBase & {
type: 'lteq';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Match = NodeBase & {
type: 'match';
Expand All @@ -421,6 +489,14 @@ type Meta = NodeBase & {
value: Expression;
};

// @public (undocumented)
type Mul = NodeBase & {
type: 'mul';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type NamedTypeSource = NodeBase & {
type: 'namedTypeSource';
Expand All @@ -435,6 +511,14 @@ type Namespace = NodeBase & {
members: (Definition | Namespace)[];
};

// @public (undocumented)
type Neq = NodeBase & {
type: 'neq';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Node_2 = Namespace | Meta | Statement | Expression | TypeSource | Attribute;

Expand Down Expand Up @@ -510,13 +594,29 @@ type Pos = {
column: number;
};

// @public (undocumented)
type Pow = NodeBase & {
type: 'pow';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type Prop = NodeBase & {
type: 'prop';
target: Expression;
name: string;
};

// @public (undocumented)
type Rem = NodeBase & {
type: 'rem';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
function reprValue(value: Value, literalLike?: boolean, processedObjects?: Set<object>): string;

Expand Down Expand Up @@ -566,6 +666,14 @@ type Str = NodeBase & {
value: string;
};

// @public (undocumented)
type Sub = NodeBase & {
type: 'sub';
left: Expression;
right: Expression;
operatorLoc: Loc;
};

// @public (undocumented)
type SubAssign = NodeBase & {
type: 'subAssign';
Expand Down
96 changes: 96 additions & 0 deletions src/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,102 @@ export class Interpreter {
return NULL; // nop
}

case 'pow': {
const callee = scope.get('Core:pow');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'mul': {
const callee = scope.get('Core:mul');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'div': {
const callee = scope.get('Core:div');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'rem': {
const callee = scope.get('Core:mod');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'add': {
const callee = scope.get('Core:add');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'sub': {
const callee = scope.get('Core:sub');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'lt': {
const callee = scope.get('Core:lt');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'lteq': {
const callee = scope.get('Core:lteq');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'gt': {
const callee = scope.get('Core:gt');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'gteq': {
const callee = scope.get('Core:gteq');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'eq': {
const callee = scope.get('Core:eq');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'neq': {
const callee = scope.get('Core:neq');
assertFunction(callee);
const left = await this._eval(node.left, scope);
const right = await this._eval(node.right, scope);
return this._fn(callee, [left, right]);
}

case 'and': {
const leftValue = await this._eval(node.left, scope);
assertBoolean(leftValue);
Expand Down
Loading

0 comments on commit a30c150

Please sign in to comment.