Skip to content

Commit

Permalink
ZSArray & ZSMap's Interpreter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Jan 27, 2019
1 parent 3efb3eb commit dce4976
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
10 changes: 10 additions & 0 deletions server/parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ export interface ASTNodePackage extends ASTNode {
type: string = 'package';
item: string[];
}

export interface ASTNodeArray extends ASTNode {
type: string = 'array';
array: any[];
}

export interface ASTNodeMap extends ASTNode {
type: string = 'map';
map: Map;
}
42 changes: 34 additions & 8 deletions server/parser/zsInterpreter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { ZSParser } from './zsParser';
import { CstNode, IToken } from 'chevrotain';
import {
NodeContext,
ASTNode,
ASTNodeArray,
ASTNodeDeclare,
ASTNodePackage,
ASTNodeProgram,
ASTNodeDeclare,
NodeContext,
ASTNodeMap,
} from '.';
import { CstNode, IToken } from 'chevrotain';
import { ZSParser } from './zsParser';

class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() {
constructor() {
Expand Down Expand Up @@ -261,15 +263,39 @@ class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() {
};
}

protected ZSArray(ctx: NodeContext): ASTNode {
protected ZSArray(ctx: NodeContext): ASTNodeArray {
const arr: any[] = [];
if (ctx.AssignExpression) {
ctx.AssignExpression.forEach((item: any) => {
arr.push(this.visit(item));
});
}

return {
type: 'array',
};
}
array: arr,
};
}

protected ZSMap(ctx: NodeContext): ASTNodeMap {
const map = new Map();
if (ctx.KEY) {
const keys = ctx.KEY.map((key: any) => this.visit(key));
const values = ctx.VALUE.map((value: any) => this.visit(value));
for (const i in keys) {
if (keys.hasOwnProperty(i) && values.hasOwnProperty(i)) {
if (map.has(keys[i])) {
// TODO: throw error here.
} else {
map.set(keys[i], values[i]);
}
}
}
}

protected ZSMap(ctx: NodeContext): ASTNode {
return {
type: 'map',
map: map,
};
}

Expand Down

0 comments on commit dce4976

Please sign in to comment.