nodalis-compiler 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +134 -0
  2. package/package.json +59 -0
  3. package/src/compilers/CPPCompiler.js +272 -0
  4. package/src/compilers/Compiler.js +108 -0
  5. package/src/compilers/JSCompiler.js +293 -0
  6. package/src/compilers/iec-parser/parser.js +4254 -0
  7. package/src/compilers/st-parser/expressionConverter.js +155 -0
  8. package/src/compilers/st-parser/gcctranspiler.js +237 -0
  9. package/src/compilers/st-parser/jstranspiler.js +254 -0
  10. package/src/compilers/st-parser/parser.js +367 -0
  11. package/src/compilers/st-parser/tokenizer.js +78 -0
  12. package/src/compilers/support/generic/json.hpp +25526 -0
  13. package/src/compilers/support/generic/modbus.cpp +378 -0
  14. package/src/compilers/support/generic/modbus.h +124 -0
  15. package/src/compilers/support/generic/nodalis.cpp +421 -0
  16. package/src/compilers/support/generic/nodalis.h +798 -0
  17. package/src/compilers/support/generic/opcua.cpp +267 -0
  18. package/src/compilers/support/generic/opcua.h +50 -0
  19. package/src/compilers/support/generic/open62541.c +151897 -0
  20. package/src/compilers/support/generic/open62541.h +50357 -0
  21. package/src/compilers/support/jint/nodalis/Nodalis.sln +28 -0
  22. package/src/compilers/support/jint/nodalis/NodalisEngine/ModbusClient.cs +200 -0
  23. package/src/compilers/support/jint/nodalis/NodalisEngine/NodalisEngine.cs +817 -0
  24. package/src/compilers/support/jint/nodalis/NodalisEngine/NodalisEngine.csproj +16 -0
  25. package/src/compilers/support/jint/nodalis/NodalisEngine/OPCClient.cs +172 -0
  26. package/src/compilers/support/jint/nodalis/NodalisEngine/OPCServer.cs +275 -0
  27. package/src/compilers/support/jint/nodalis/NodalisPLC/NodalisPLC.csproj +19 -0
  28. package/src/compilers/support/jint/nodalis/NodalisPLC/Program.cs +197 -0
  29. package/src/compilers/support/jint/nodalis/NodalisPLC/bootstrap.bat +5 -0
  30. package/src/compilers/support/jint/nodalis/NodalisPLC/bootstrap.sh +5 -0
  31. package/src/compilers/support/jint/nodalis/build.bat +25 -0
  32. package/src/compilers/support/jint/nodalis/build.sh +31 -0
  33. package/src/compilers/support/nodejs/IOClient.js +110 -0
  34. package/src/compilers/support/nodejs/modbus.js +115 -0
  35. package/src/compilers/support/nodejs/nodalis.js +662 -0
  36. package/src/compilers/support/nodejs/opcua.js +194 -0
  37. package/src/nodalis.js +174 -0
@@ -0,0 +1,367 @@
1
+ /* eslint-disable curly */
2
+ /* eslint-disable eqeqeq */
3
+ // Copyright [2025] Nathan Skipper
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 (the "License");
6
+ // you may not use this file except in compliance with the License.
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+
17
+
18
+ /**
19
+ * @description Structured Text Parser
20
+ * @author Nathan Skipper, MTI
21
+ * @version 1.0.2
22
+ * @copyright Apache 2.0
23
+ */
24
+
25
+ import { tokenize } from './tokenizer.js';
26
+ import {mapType} from "./gcctranspiler.js";
27
+
28
+ /**
29
+ * Parses a block of structured text code and divides it into statement objects that can then be transpiled.
30
+ * @param {string} code A block of Structured Text Code
31
+ * @returns {[]} An array of tokenized and parsed statements.
32
+ */
33
+ export function parseStructuredText(code) {
34
+ const tokens = tokenize(code);
35
+ let position = 0;
36
+
37
+ function peek(offset = 0) {
38
+ return tokens[position + offset];
39
+ }
40
+
41
+ function consume() {
42
+ return tokens[position++];
43
+ }
44
+
45
+ function expect(value) {
46
+ const token = consume();
47
+ if (!token || token.value.toUpperCase() !== value.toUpperCase()) {
48
+ throw new Error(`Expected '${value}', but got '${token?.value}'`);
49
+ }
50
+ return token;
51
+ }
52
+
53
+ function parseBlock() {
54
+ const token = peek();
55
+ if (!token) return null;
56
+
57
+ switch (token.value.toUpperCase()) {
58
+ case 'PROGRAM':
59
+ return parseProgram();
60
+ case 'FUNCTION':
61
+ return parseFunction();
62
+ case 'FUNCTION_BLOCK':
63
+ return parseFunctionBlock();
64
+ case 'VAR_GLOBAL':
65
+ return parseGlobalVarSection();
66
+ default:
67
+ consume();
68
+ return null;
69
+ }
70
+ }
71
+
72
+ function parseGlobalVarSection() {
73
+ expect('VAR_GLOBAL');
74
+ const variables = [];
75
+
76
+ while (peek() && peek().value.toUpperCase() !== 'END_VAR') {
77
+ const name = consume().value;
78
+ let address = null;
79
+ let token = peek();
80
+ if (peek()?.value.toUpperCase?.() === 'AT') {
81
+ consume(); // skip 'AT'
82
+ const addrToken = consume();
83
+ if (addrToken?.type === 'ADDRESS' || addrToken?.type === 'IDENTIFIER') {
84
+ address = addrToken.value;
85
+ } else {
86
+ throw new Error(`Expected address after AT, got '${addrToken?.value}'`);
87
+ }
88
+ }
89
+ expect(':');
90
+ const type = consume().value;
91
+ let initialValue = null;
92
+
93
+ if (peek()?.value === ':=') {
94
+ consume(); // consume ':='
95
+ initialValue = consume().value;
96
+ }
97
+ variables.push({ name, type, address, initialValue, sectionType: 'VAR_GLOBAL' });
98
+ if (peek()?.value === ';') consume();
99
+ }
100
+
101
+ expect('END_VAR');
102
+ return { type: 'GlobalVars', variables };
103
+ }
104
+
105
+
106
+ function parseVarSection() {
107
+ const variables = [];
108
+ const sectionType = consume().value.toUpperCase();
109
+
110
+ while (peek() && peek().value.toUpperCase() !== 'END_VAR') {
111
+ const name = consume().value;
112
+ expect(':');
113
+ const type = consume().value;
114
+ let initialValue = null;
115
+
116
+ if (peek()?.value === ':=') {
117
+ consume(); // consume ':='
118
+ initialValue = consume().value;
119
+ }
120
+ variables.push({ name, type, initialValue, sectionType });
121
+ if (peek()?.value === ';') consume();
122
+ }
123
+ expect('END_VAR');
124
+ return variables;
125
+ }
126
+
127
+ function parseStatements(until) {
128
+ const statements = [];
129
+ while (peek() && peek().value.toUpperCase() !== until) {
130
+ const stmt = parseStatement();
131
+ if (stmt) statements.push(stmt);
132
+ }
133
+ return statements;
134
+ }
135
+
136
+ function parseStatement() {
137
+ const token = peek();
138
+ if (!token) return null;
139
+
140
+ if (token.value.toUpperCase() === 'IF') return parseIf();
141
+ if (token.value.toUpperCase() === 'WHILE') return parseWhile();
142
+ if (token.value.toUpperCase() === 'FOR') return parseFor();
143
+ if (token.value.toUpperCase() === 'REPEAT') return parseRepeat();
144
+ if (token.value.toUpperCase() === 'CASE') return parseCase();
145
+
146
+ // Assignment: x := y;
147
+ const lhsTokens = [];
148
+ let i = 0;
149
+ while (peek(i) && peek(i).value !== ':=' && peek(i).value !== ';') {
150
+ lhsTokens.push(peek(i));
151
+ i++;
152
+ }
153
+ if (peek(i)?.value === ':=') {
154
+ const lhs = lhsTokens.map(t => t.value).join('');
155
+ for (let j = 0; j < i + 1; j++) consume(); // consume LHS and :=
156
+
157
+ const right = [];
158
+ while (peek() && peek().value !== ';') {
159
+ right.push(consume().value);
160
+ }
161
+ if (peek()?.value === ';') consume();
162
+ return { type: 'ASSIGN', left: lhs, right };
163
+ }
164
+
165
+ // Function block call like: T1();
166
+ if (token.value && peek(1)?.value === '(' && peek(2)?.value === ')') {
167
+ const name = consume().value;
168
+ consume(); // (
169
+ consume(); // )
170
+ if (peek()?.value === ';') consume();
171
+ return { type: 'CALL', name };
172
+ }
173
+
174
+ consume(); // Skip unknown
175
+ return null;
176
+ }
177
+
178
+
179
+ function parseIf() {
180
+ consume(); // IF
181
+
182
+ // Collect condition tokens until THEN
183
+ const conditionTokens = [];
184
+ while (peek() && peek().value.toUpperCase() !== 'THEN') {
185
+ conditionTokens.push(consume().value);
186
+ }
187
+ consume(); // THEN
188
+
189
+ const thenBlock = parseStatementsUntil(['ELSIF', 'ELSE', 'END_IF']);
190
+ const elseIfBlocks = [];
191
+ let elseBlock = null;
192
+
193
+ while (peek()?.value.toUpperCase() === 'ELSIF') {
194
+ consume(); // ELSIF
195
+ const elifCondTokens = [];
196
+ while (peek() && peek().value.toUpperCase() !== 'THEN') {
197
+ elifCondTokens.push(consume().value);
198
+ }
199
+ consume(); // THEN
200
+ const elifBlock = parseStatementsUntil(['ELSIF', 'ELSE', 'END_IF']);
201
+ elseIfBlocks.push({ condition: elifCondTokens, block: elifBlock });
202
+ }
203
+
204
+ if (peek()?.value.toUpperCase() === 'ELSE') {
205
+ consume(); // ELSE
206
+ elseBlock = parseStatementsUntil(['END_IF']);
207
+ }
208
+
209
+ if (peek()?.value.toUpperCase() === 'END_IF') {
210
+ consume(); // END_IF
211
+ }
212
+
213
+ return {
214
+ type: 'IF',
215
+ condition: conditionTokens,
216
+ thenBlock,
217
+ elseIfBlocks,
218
+ elseBlock
219
+ };
220
+ }
221
+
222
+
223
+ function parseStatementsUntil(endTokens) {
224
+ const statements = [];
225
+ while (peek() && !endTokens.includes(peek().value.toUpperCase())) {
226
+ const stmt = parseStatement();
227
+ if (stmt) {
228
+ statements.push(stmt);
229
+ } else {
230
+ console.warn('⚠️ Unrecognized statement at token:', peek());
231
+ consume(); // prevent infinite loop
232
+ }
233
+ }
234
+ return statements;
235
+ }
236
+
237
+
238
+ function parseWhile() {
239
+ consume(); // WHILE
240
+ const condition = [];
241
+ while (peek() && peek().value.toUpperCase() !== 'DO') {
242
+ condition.push(consume().value);
243
+ }
244
+ expect('DO');
245
+ const body = parseStatements('END_WHILE');
246
+ expect('END_WHILE');
247
+ return { type: 'WHILE', condition, body };
248
+ }
249
+
250
+ function parseFor() {
251
+ consume(); // FOR
252
+ const variable = consume().value;
253
+ expect(':=');
254
+ const from = consume().value;
255
+ expect('TO');
256
+ const to = consume().value;
257
+ let step = '1';
258
+ if (peek()?.value.toUpperCase() === 'BY') {
259
+ consume();
260
+ step = consume().value;
261
+ }
262
+ expect('DO');
263
+ const body = parseStatements('END_FOR');
264
+ expect('END_FOR');
265
+ return { type: 'FOR', variable, from, to, step, body };
266
+ }
267
+
268
+ function parseRepeat() {
269
+ consume(); // REPEAT
270
+ const body = parseStatements('UNTIL');
271
+ expect('UNTIL');
272
+ const condition = [];
273
+ while (peek() && peek().value !== ';') {
274
+ condition.push(consume().value);
275
+ }
276
+ if (peek()?.value === ';') consume();
277
+ return { type: 'REPEAT', condition, body };
278
+ }
279
+
280
+ function parseCase() {
281
+ consume(); // CASE
282
+ const expression = [];
283
+ while (peek() && peek().value.toUpperCase() !== 'OF') {
284
+ expression.push(consume().value);
285
+ }
286
+ expect('OF');
287
+ const branches = [];
288
+ while (peek() && peek().value.toUpperCase() !== 'END_CASE') {
289
+ const label = consume().value;
290
+ expect(':');
291
+ const body = parseStatements('ELSE');
292
+ branches.push({ label, body });
293
+ }
294
+ expect('END_CASE');
295
+ return { type: 'CASE', expression, branches };
296
+ }
297
+
298
+ function parseProgram() {
299
+ expect('PROGRAM');
300
+ const name = consume().value;
301
+ const vars = [];
302
+ const stmts = [];
303
+
304
+ while (peek() && peek().value.toUpperCase().startsWith('VAR')) {
305
+ vars.push(...parseVarSection());
306
+ }
307
+ vars.forEach((v) => {
308
+ if(mapType(v.type) === "auto"){
309
+ stmts.push({type: "CALL", name: v.name});
310
+ }
311
+ });
312
+ stmts.push(...parseStatements('END_PROGRAM'));
313
+ expect('END_PROGRAM');
314
+
315
+ return { type: 'ProgramDeclaration', name, varSections: vars, statements: stmts };
316
+ }
317
+
318
+ function parseFunction() {
319
+ expect('FUNCTION');
320
+ const name = consume().value;
321
+ expect(':');
322
+ const returnType = consume().value;
323
+ const vars = [];
324
+ const stmts = [];
325
+
326
+ while (peek() && peek().value.toUpperCase().startsWith('VAR')) {
327
+ vars.push(...parseVarSection());
328
+ }
329
+ vars.forEach((v) => {
330
+ if(mapType(v.type) === "auto"){
331
+ stmts.push({type: "CALL", name: v.name});
332
+ }
333
+ });
334
+ stmts.push(...parseStatements('END_FUNCTION'));
335
+ expect('END_FUNCTION');
336
+
337
+ return { type: 'FunctionDeclaration', name, returnType, varSections: vars, statements: stmts };
338
+ }
339
+
340
+ function parseFunctionBlock() {
341
+ expect('FUNCTION_BLOCK');
342
+ const name = consume().value;
343
+ const vars = [];
344
+ const stmts = [];
345
+
346
+ while (peek() && peek().value.toUpperCase().startsWith('VAR')) {
347
+ vars.push(...parseVarSection());
348
+ }
349
+ vars.forEach((v) => {
350
+ if(mapType(v.type) === "auto"){
351
+ stmts.push({type: "CALL", name: v.name});
352
+ }
353
+ });
354
+ stmts.push(...parseStatements('END_FUNCTION_BLOCK'));
355
+ expect('END_FUNCTION_BLOCK');
356
+
357
+ return { type: 'FunctionBlockDeclaration', name, varSections: vars, statements: stmts };
358
+ }
359
+
360
+ const body = [];
361
+ while (position < tokens.length) {
362
+ const block = parseBlock();
363
+ if (block) body.push(block);
364
+ }
365
+
366
+ return { type: 'Program', body };
367
+ }
@@ -0,0 +1,78 @@
1
+ /* eslint-disable curly */
2
+ /* eslint-disable eqeqeq */
3
+ // Copyright [2025] Nathan Skipper
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 (the "License");
6
+ // you may not use this file except in compliance with the License.
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+
17
+ /**
18
+ * @description Structured Text Tokenizer
19
+ * @author Nathan Skipper, MTI
20
+ * @version 1.0.2
21
+ * @copyright Apache 2.0
22
+ */
23
+
24
+ /**
25
+ * Tokenizes a block of structured text into their types and values.
26
+ * @param {string} code A block of structured text code.
27
+ * @returns {{type: string, value: string}[]} An array of tokens.
28
+ */
29
+ export function tokenize(code) {
30
+ const tokens = [];
31
+ let match;
32
+ // Remove single-line comments (//...)
33
+ code = code.replace(/\/\/.*$/gm, '');
34
+
35
+ // Remove multi-line comments ((*...*))
36
+ code = code.replace(/\(\*[\s\S]*?\*\)/g, '');
37
+
38
+ //const regex = /(%[IQM][A-Z]?[0-9]+(?:\.[0-9]+)?)|(:=)|([A-Za-z_]\w*\.\d+)|([A-Za-z_]\w*\.\w+)|([A-Za-z_]\w*)|(\d+)|([:;()<>+\-*/=])/g;
39
+ const regex = /(%[IQM][A-Z]*\d+(?:\.\d+)?)|(:=|>=|<=|<>|!=)|([A-Za-z_]\w*\.\d+)|([A-Za-z_]\w*\.\w+)|([A-Za-z_]\w*)|(\d+)|([<>+\-*/=;():])/g;
40
+
41
+ while ((match = regex.exec(code)) !== null) {
42
+ const [_, address, compoundSymbol, bitIdentifier, propIdentifier, identifier, number, symbol] = match;
43
+
44
+ if (address)
45
+ tokens.push({ type: 'ADDRESS', value: address });
46
+ else if (compoundSymbol)
47
+ tokens.push({ type: 'SYMBOL', value: compoundSymbol });
48
+ else if (bitIdentifier)
49
+ tokens.push({ type: 'IDENTIFIER', value: bitIdentifier });
50
+ else if (propIdentifier)
51
+ tokens.push({ type: 'IDENTIFIER', value: propIdentifier });
52
+ else if (identifier)
53
+ tokens.push({ type: 'IDENTIFIER', value: identifier });
54
+ else if (number)
55
+ tokens.push({ type: 'NUMBER', value: number });
56
+ else if (symbol)
57
+ tokens.push({ type: 'SYMBOL', value: symbol });
58
+
59
+ }
60
+ return tokens;
61
+ }
62
+
63
+ function getTokenType(value) {
64
+ const keywords = new Set([
65
+ 'PROGRAM', 'FUNCTION_BLOCK', 'FUNCTION', 'VAR_INPUT', 'VAR_OUTPUT', 'VAR', 'END_VAR',
66
+ 'END_FUNCTION_BLOCK', 'END_FUNCTION', 'END_PROGRAM'
67
+ ]);
68
+
69
+ const symbols = new Set([':=', ';', ':', '(', ')', '+', '-', '*', '/', '>', '<', '=']);
70
+
71
+ if (keywords.has(value.toUpperCase())) return 'KEYWORD';
72
+ if (symbols.has(value)) return 'SYMBOL';
73
+ if (/^\d+$/.test(value)) return 'NUMBER';
74
+ if (/^[A-Za-z_]\w*$|(%[IQM][\d.]+)/.test(value)) return 'IDENTIFIER';
75
+
76
+ return 'UNKNOWN';
77
+ }
78
+