/** * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2020-02-20 19:46:19 * @modify date 2020-08-06 08:03:09 * @desc [description] */ import * as handlebars from 'handlebars'; import { IParsedLogic } from '../types/interface'; // declare const handlebars; import { IParser } from './default.parser'; export class SiemensParser implements IParser { mainLoop(context: IParsedLogic): string { const templates = { ifHeaderTemplate: `{{#if isFirst}}IF{{/if}}{{#unless isFirst}}ELSIF{{/unless}}{{> testPrePlaces}}{{> testPostPlaces}}{{> testGuard}} THEN`, testPrePlaces: `{{#each inputs}} ("{{placeTokenVar}}"{{#if minTokens}} - "{{minTokens}}"{{/if}}) >= "{{tokensToRemove}}" AND{{/each}}`, testPostPlaces: `{{#each testOutputs}} ("{{placeTokenVar}}" + "{{tokensToAdd}}") <= {{maxTokens}}" AND {{/each}}`, testGuard: ` {{guard.external}}`, removeTokens: `{{#each inputs}}"{{placeTokenVar}}" := "{{placeTokenVar}}" - "{{tokensToRemove}}";{{/each}}`, addTokens: `{{#each outputs}}"{{placeTokenVar}}" := "{{placeTokenVar}}" + "{{tokensToAdd}}";{{/each}}` } for (const partial in templates) { handlebars.registerPartial(partial, templates[partial]); } const functions: { [index: string]: (context: any) => string } = { 'plc.set': (context) => { console.log(context); return '' } } // handlebars.registerHelper(require('handlebars-helpers/lib/array.js')) handlebars.registerHelper('getFunctionCode', (context, name, paramers, dynParams) => { console.log('FUNC', context, name, paramers, dynParams) }) const mainTemplate = `` + ` ORGANIZATION_BLOCK "Main" TITLE = "Main Program Sweep (Cycle)" { S7_Optimized_Access := 'TRUE' } VERSION : 0.1 BEGIN {{#each transitions}} // Test Transtion "{{{label}}}" {{> ifHeaderTemplate}} {{#if inputs}} // Removing Tokens from Pre-Places. {{> removeTokens}} {{/if}} {{#if outputs}} // Adding Tokens to the Post-Places. {{> addTokens}} {{/if}} {{#if funcName}} // Execute the desired Function {{#getFunctionCode funcName params dynamicParameters}}{{/getFunctionCode}} {{/if}} {{#if isLast}} END_IF; {{/if}} {{/each}} END_ORGANIZATION_BLOCK ` // handlebars.registerHelper("joinList", helpers_1.joinListHelper); const render = handlebars.compile(mainTemplate); return ''; } variableDefinition(context: IParsedLogic): string { const template = `` + ` DATA_BLOCK "VARS_DB" { S7_Optimized_Access := 'FALSE' } VERSION : 0.1 NON_RETAIN VAR {{#each variables}} {{name}} : {{type}}; {{/each}} END_VAR BEGIN {{#some variables "v=>(v.hasValue)"}} {{#joinList this "\n" "v=>(v.hasValue)"}} {{name}} := {{value}}; {{/joinList}} {{/some}} END_DATA_BLOCK ` const render = handlebars.compile(template); return render(context); } instanceCreation(context: IParsedLogic): string { throw new Error("Method not implemented."); } }