lysergic
Version:
Synaptic's neural network compiler
233 lines • 7.29 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const helpers_1 = require("./helpers");
class Node {
constructor() {
this.children = [];
this.hasParenthesis = false;
let originalToString = this.toString;
this.toString = () => {
if (this.hasParenthesis)
return '(' + originalToString.apply(this) + ')';
return originalToString.apply(this);
};
}
addNode(node) {
if (!node)
return;
if (node instanceof Array) {
node.forEach($ => this.addNode($));
return;
}
node.parent = this;
this.children.push(node);
}
toString() {
if (this.originalText) {
return this.originalText;
}
return this.children.map(x => x.toString()).join('\n');
}
get value() {
return null;
}
inspect() {
let childrenInspected = this.children.map((x, i) => {
let name = this.nameMapping && this.nameMapping[i];
if (name)
name = name + ': ';
else
name = '';
return name + (!x ? 'null' : x.inspect());
}).join('\n');
return this.constructor.name +
(this.value ? ' value=' + this.value : '')
+ ' [' + (childrenInspected ? '\n' + helpers_1.indent(childrenInspected) + '\n'
: '') + ']';
}
}
exports.Node = Node;
class ExpressionNode extends Node {
}
exports.ExpressionNode = ExpressionNode;
class DocumentNode extends Node {
toString() {
return this.children.join(';\n');
}
}
exports.DocumentNode = DocumentNode;
class ParameterNode extends ExpressionNode {
constructor(name) {
super();
this.name = name;
}
toString() {
return this.name;
}
}
exports.ParameterNode = ParameterNode;
class ParametersNode extends Node {
toString() {
return `(${this.children.join(', ')})`;
}
}
exports.ParametersNode = ParametersNode;
class BlockNode extends Node {
toString() {
return `{\n` + helpers_1.indent(this.children.map(x => x + ';').join('\n')) + `\n};`;
}
}
exports.BlockNode = BlockNode;
class FunctionNode extends ExpressionNode {
toString() {
return `function ${this.name}() {`
+ '\n'
+ helpers_1.indent(this.body.toString())
+ '\n}';
}
}
__decorate([
helpers_1.childrenRef(0),
__metadata("design:type", ParametersNode)
], FunctionNode.prototype, "parameters", void 0);
__decorate([
helpers_1.childrenRef(1),
__metadata("design:type", BlockNode)
], FunctionNode.prototype, "body", void 0);
exports.FunctionNode = FunctionNode;
class HeapPointer extends ExpressionNode {
toString() {
return `H[${this.position}]`;
}
}
__decorate([
helpers_1.childrenRef(0),
__metadata("design:type", ExpressionNode)
], HeapPointer.prototype, "position", void 0);
exports.HeapPointer = HeapPointer;
class HeapReferenceNode extends ExpressionNode {
constructor(position) {
super();
this.position = position;
}
toString() {
return `H[${this.position}]`;
}
}
exports.HeapReferenceNode = HeapReferenceNode;
class TernaryExpressionNode extends ExpressionNode {
constructor() {
super();
}
toString() {
return this.condition.toString() + ' ? ' + this.truePart.toString() + ' : ' + this.falsePart.toString();
}
}
__decorate([
helpers_1.childrenRef(0),
__metadata("design:type", ExpressionNode)
], TernaryExpressionNode.prototype, "condition", void 0);
__decorate([
helpers_1.childrenRef(1),
__metadata("design:type", ExpressionNode)
], TernaryExpressionNode.prototype, "truePart", void 0);
__decorate([
helpers_1.childrenRef(2),
__metadata("design:type", ExpressionNode)
], TernaryExpressionNode.prototype, "falsePart", void 0);
exports.TernaryExpressionNode = TernaryExpressionNode;
class BinaryExpressionNode extends ExpressionNode {
toString() {
switch (this.operator) {
case "^":
return `Math.pow(${this.lhs}, ${this.rhs})`;
}
return this.lhs.toString() + ' ' + this.operator + ' ' + this.rhs.toString();
}
}
__decorate([
helpers_1.childrenRef(0),
__metadata("design:type", ExpressionNode)
], BinaryExpressionNode.prototype, "lhs", void 0);
__decorate([
helpers_1.childrenRef(1),
__metadata("design:type", ExpressionNode)
], BinaryExpressionNode.prototype, "rhs", void 0);
exports.BinaryExpressionNode = BinaryExpressionNode;
class UnaryExpressionNode extends ExpressionNode {
toString() {
return this.operator + '(' + this.rhs.toString() + ')';
}
}
__decorate([
helpers_1.childrenRef(0),
__metadata("design:type", ExpressionNode)
], UnaryExpressionNode.prototype, "rhs", void 0);
exports.UnaryExpressionNode = UnaryExpressionNode;
class FloatNumberNode extends ExpressionNode {
constructor(numericValue) {
super();
this.numericValue = numericValue;
}
toString() {
return this.numericValue.toFixed(10);
}
}
exports.FloatNumberNode = FloatNumberNode;
class IntNumberNode extends ExpressionNode {
constructor(numericValue) {
super();
this.numericValue = numericValue;
}
toString() {
return this.numericValue.toFixed(0);
}
}
exports.IntNumberNode = IntNumberNode;
class Variable extends HeapReferenceNode {
constructor(id, key, initialValue, tag) {
super(id);
this.id = id;
this.key = key;
this.initialValue = initialValue;
this.tag = tag;
}
}
exports.Variable = Variable;
class VariableDeclaration extends ExpressionNode {
constructor(name, type, initialValue) {
super();
this.name = name;
this.type = type;
this.initialValue = initialValue;
}
}
exports.VariableDeclaration = VariableDeclaration;
class VariableReference extends ExpressionNode {
constructor(variable) {
super();
this.variable = variable;
}
}
exports.VariableReference = VariableReference;
class ForLoopNode extends ExpressionNode {
}
__decorate([
helpers_1.childrenRef(0),
__metadata("design:type", VariableReference)
], ForLoopNode.prototype, "var", void 0);
__decorate([
helpers_1.childrenRef(1),
__metadata("design:type", ExpressionNode)
], ForLoopNode.prototype, "expression", void 0);
exports.ForLoopNode = ForLoopNode;
//# sourceMappingURL=nodes.js.map