UNPKG

@scalenc/nc-format

Version:

Library for handling TRUMPF NC file format.

202 lines 9.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StatementWriter = void 0; const models_1 = require("../models"); const Constants_1 = require("./Constants"); const ExpressionWriter_1 = require("./ExpressionWriter"); class StatementWriter { stream; expressionWriter; constructor(stream) { this.stream = stream; this.expressionWriter = new ExpressionWriter_1.ExpressionWriter(this.stream); } onAssignment(assignment) { this.stream.write(assignment.variable); if (assignment.variable.length == 1 && !assignment.fieldExpressions && assignment.expression instanceof models_1.Number) { assignment.expression.visit(this.expressionWriter); } else { if (assignment.fieldExpressions) { new models_1.Variable('', assignment.fieldExpressions).visit(this.expressionWriter); } this.stream.write(Constants_1.Constants.ASSIGNMENT_OPERATOR); assignment.expression.visit(this.expressionWriter); this.stream.write(Constants_1.Constants.SPACE); } } onDeclaration(declaration) { this.stream.write(Constants_1.Constants.DEF); this.stream.write(Constants_1.Constants.SPACE); this.stream.write(this.variableTypeToString(declaration.type)); if (declaration.stringLength !== undefined) { this.stream.write(Constants_1.Constants.DEF_FIELD_BEGIN); this.stream.write(declaration.stringLength.toString()); this.stream.write(Constants_1.Constants.DEF_FIELD_END); } this.stream.write(Constants_1.Constants.SPACE); this.writeValueIfDefined(declaration.unit, Constants_1.Constants.DEF_UNIT); this.writeValueIfDefined(declaration.lowerLimit, Constants_1.Constants.DEF_LOWER_LIMIT); this.writeValueIfDefined(declaration.upperLimit, Constants_1.Constants.DEF_UPPER_LIMIT); declaration.variables.forEach((variable, i) => { if (i > 0) { this.stream.write(Constants_1.Constants.DEF_SEPARATOR); this.stream.write(Constants_1.Constants.SPACE); } this.stream.write(variable.name); if (variable.fieldLengths) { this.stream.write(Constants_1.Constants.DEF_FIELD_BEGIN); this.stream.write(variable.fieldLengths.map((f) => f.toString()).join(Constants_1.Constants.DEF_SEPARATOR)); this.stream.write(Constants_1.Constants.DEF_FIELD_END); } if (variable.initExpression) { this.stream.write(Constants_1.Constants.ASSIGNMENT_OPERATOR); variable.initExpression.visit(this.expressionWriter); } this.stream.write(Constants_1.Constants.SPACE); }); } onGCode(gCode) { this.stream.write(Constants_1.Constants.G_CODE); this.stream.write(gCode.id < 10 ? `0${gCode.id}` : gCode.id.toString()); if (gCode.args) { throw new Error(`Arguments to G code are not supported!`); } } onGoto(gotoStatement) { this.stream.write(Constants_1.Constants.GOTO); switch (gotoStatement.direction) { case models_1.GotoDirection.START: this.stream.write(Constants_1.Constants.GOTO_START); break; case models_1.GotoDirection.BACKWARD: this.stream.write(Constants_1.Constants.GOTO_BACKWARD); break; case models_1.GotoDirection.FORWARD: this.stream.write(Constants_1.Constants.GOTO_FORWARD); break; case models_1.GotoDirection.SEARCH: break; case models_1.GotoDirection.SEARCH_WITHOUT_ERROR: this.stream.write(Constants_1.Constants.GOTO_SEARCH_NO_ERRORS); break; default: throw new Error(`Unknown goto direction ${gotoStatement.direction}`); } this.stream.write(Constants_1.Constants.SPACE); if (gotoStatement.target) { gotoStatement.target.visit(this.expressionWriter); this.stream.write(Constants_1.Constants.SPACE); } } onInstruction(instruction) { this.stream.write(instruction.name); if (instruction.expressions) { this.stream.write(Constants_1.Constants.OPENING_BRACE); instruction.expressions.forEach((e, i) => { if (i > 0) { this.stream.write(Constants_1.Constants.ARGUMENT_SEPARATOR); } e?.visit(this.expressionWriter); }); this.stream.write(Constants_1.Constants.CLOSING_BRACE); } else { this.stream.write(Constants_1.Constants.SPACE); } } onFlowControlInstruction(instruction) { let writeOneExpression = false; switch (instruction.type) { case models_1.FlowControlInstructionType.IF: this.stream.write(Constants_1.Constants.IF); writeOneExpression = true; break; case models_1.FlowControlInstructionType.ELSE: this.stream.write(Constants_1.Constants.ELSE); break; case models_1.FlowControlInstructionType.ENDIF: this.stream.write(Constants_1.Constants.ENDIF); break; case models_1.FlowControlInstructionType.WHILE: this.stream.write(Constants_1.Constants.WHILE); writeOneExpression = true; break; case models_1.FlowControlInstructionType.ENDWHILE: this.stream.write(Constants_1.Constants.ENDWHILE); break; case models_1.FlowControlInstructionType.REPEAT: this.stream.write(Constants_1.Constants.REPEAT); break; case models_1.FlowControlInstructionType.UNTIL: this.stream.write(Constants_1.Constants.UNTIL); writeOneExpression = true; break; case models_1.FlowControlInstructionType.LOOP: this.stream.write(Constants_1.Constants.LOOP); break; case models_1.FlowControlInstructionType.ENDLOOP: this.stream.write(Constants_1.Constants.ENDLOOP); break; case models_1.FlowControlInstructionType.FOR: if (!instruction.expressions || instruction.expressions.length < 3) { throw new Error(`Expected three expressions for flow control instruction FOR`); } this.stream.write(Constants_1.Constants.FOR); this.stream.write(Constants_1.Constants.SPACE); instruction.expressions[0].visit(this.expressionWriter); this.stream.write(Constants_1.Constants.ASSIGNMENT_OPERATOR); instruction.expressions[1].visit(this.expressionWriter); this.stream.write(Constants_1.Constants.SPACE + Constants_1.Constants.FOR_TO + Constants_1.Constants.SPACE); instruction.expressions[2].visit(this.expressionWriter); break; case models_1.FlowControlInstructionType.ENDFOR: this.stream.write(Constants_1.Constants.ENDFOR); break; default: throw new Error(`Unknown flow control instruction ${instruction.type}`); } if (writeOneExpression) { if (!instruction.expressions || instruction.expressions.length !== 1) { throw new Error(`Expected one expression for flow control instruction ${instruction.type}`); } this.stream.write(Constants_1.Constants.SPACE); instruction.expressions[0].visit(this.expressionWriter); } this.stream.write(Constants_1.Constants.SPACE); } onMCode(mCode) { this.stream.write(Constants_1.Constants.M_CODE); this.stream.write(mCode.id < 10 ? `0${mCode.id}` : mCode.id.toString()); } writeValueIfDefined(value, token) { if (value !== undefined) { this.stream.write(token); this.stream.write(Constants_1.Constants.SPACE); this.stream.write(value.toString()); this.stream.write(Constants_1.Constants.SPACE); } } variableTypeToString(type) { switch (type) { case models_1.VariableType.INT: return Constants_1.Constants.DEF_INT; case models_1.VariableType.REAL: return Constants_1.Constants.DEF_REAL; case models_1.VariableType.BOOL: return Constants_1.Constants.DEF_BOOL; case models_1.VariableType.CHAR: return Constants_1.Constants.DEF_CHAR; case models_1.VariableType.STRING: return Constants_1.Constants.DEF_STRING; case models_1.VariableType.AXIS: return Constants_1.Constants.DEF_AXIS; case models_1.VariableType.FRAME: return Constants_1.Constants.DEF_FRAME; default: throw new Error(`Unknown tpye '${type}'`); } } } exports.StatementWriter = StatementWriter; //# sourceMappingURL=StatementWriter.js.map