circuitscript 0.1.14 → 0.1.16
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.
- package/dist/cjs/BaseVisitor.js +96 -34
- package/dist/cjs/antlr/CircuitScriptLexer.js +3 -3
- package/dist/cjs/antlr/CircuitScriptParser.js +868 -757
- package/dist/cjs/builtinMethods.js +11 -1
- package/dist/cjs/execute.js +18 -11
- package/dist/cjs/geometry.js +19 -0
- package/dist/cjs/globals.js +6 -2
- package/dist/cjs/graph.js +298 -0
- package/dist/cjs/helpers.js +6 -2
- package/dist/cjs/layout.js +27 -261
- package/dist/cjs/objects/types.js +27 -6
- package/dist/cjs/render.js +20 -14
- package/dist/cjs/visitor.js +32 -30
- package/dist/esm/BaseVisitor.js +96 -34
- package/dist/esm/antlr/CircuitScriptLexer.js +3 -3
- package/dist/esm/antlr/CircuitScriptParser.js +864 -755
- package/dist/esm/antlr/CircuitScriptVisitor.js +2 -0
- package/dist/esm/builtinMethods.js +11 -1
- package/dist/esm/execute.js +19 -12
- package/dist/esm/geometry.js +19 -0
- package/dist/esm/globals.js +5 -1
- package/dist/esm/graph.js +293 -0
- package/dist/esm/helpers.js +6 -2
- package/dist/esm/layout.js +23 -237
- package/dist/esm/objects/types.js +27 -6
- package/dist/esm/render.js +20 -14
- package/dist/esm/visitor.js +33 -31
- package/dist/types/BaseVisitor.d.ts +3 -1
- package/dist/types/antlr/CircuitScriptParser.d.ts +42 -26
- package/dist/types/antlr/CircuitScriptVisitor.d.ts +4 -0
- package/dist/types/draw_symbols.d.ts +1 -1
- package/dist/types/execute.d.ts +5 -5
- package/dist/types/geometry.d.ts +3 -1
- package/dist/types/globals.d.ts +7 -3
- package/dist/types/graph.d.ts +28 -0
- package/dist/types/layout.d.ts +6 -10
- package/dist/types/objects/ExecutionScope.d.ts +3 -3
- package/dist/types/objects/types.d.ts +16 -6
- package/package.json +1 -1
package/dist/esm/BaseVisitor.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Big } from 'big.js';
|
|
2
|
-
import { ExpressionContext } from "./antlr/CircuitScriptParser.js";
|
|
3
2
|
import { CircuitScriptVisitor } from "./antlr/CircuitScriptVisitor.js";
|
|
4
3
|
import { ExecutionContext } from "./execute.js";
|
|
5
4
|
import { Logger } from "./logger.js";
|
|
@@ -7,7 +6,7 @@ import { ClassComponent } from "./objects/ClassComponent.js";
|
|
|
7
6
|
import { NumberOperator, NumericValue, PercentageValue } from "./objects/ParamDefinition.js";
|
|
8
7
|
import { PinTypes } from "./objects/PinTypes.js";
|
|
9
8
|
import { Direction, AnyReference, UndeclaredReference } from "./objects/types.js";
|
|
10
|
-
import { ComponentTypes, DoubleDelimiter1, GlobalDocumentName, ReferenceTypes } from './globals.js';
|
|
9
|
+
import { ComponentTypes, DoubleDelimiter1, GlobalDocumentName, ReferenceTypes, TrailerArrayIndex } from './globals.js';
|
|
11
10
|
import { isReference, unwrapValue as unwrapValue } from "./utils.js";
|
|
12
11
|
import { linkBuiltInMethods } from './builtinMethods.js';
|
|
13
12
|
import { resolveToNumericValue, RuntimeExecutionError, throwWithContext } from './utils.js';
|
|
@@ -134,28 +133,37 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
134
133
|
const ctxAtom = ctx.atom_expr();
|
|
135
134
|
const ctxFuncCallRef = ctx.function_call_expr();
|
|
136
135
|
let leftSideReference;
|
|
136
|
+
let lhsCtx;
|
|
137
137
|
if (ctxAtom) {
|
|
138
138
|
leftSideReference = this.getReference(ctx.atom_expr());
|
|
139
|
+
lhsCtx = ctxAtom;
|
|
139
140
|
}
|
|
140
141
|
else if (ctxFuncCallRef) {
|
|
141
142
|
this.setResult(ctxFuncCallRef, { keepReference: true });
|
|
142
143
|
leftSideReference = this.visitResult(ctxFuncCallRef);
|
|
144
|
+
lhsCtx = ctxFuncCallRef;
|
|
145
|
+
}
|
|
146
|
+
const rhsCtx = ctx.data_expr();
|
|
147
|
+
const rhsCtxResult = this.visitResult(rhsCtx);
|
|
148
|
+
if (isReference(rhsCtxResult) && !rhsCtxResult.found) {
|
|
149
|
+
this.throwWithContext(rhsCtx, rhsCtx.getText() + ' is not defined');
|
|
143
150
|
}
|
|
144
|
-
const rhsCtxResult = this.visitResult(ctx.data_expr());
|
|
145
151
|
const rhsValue = unwrapValue(rhsCtxResult);
|
|
146
152
|
const trailers = leftSideReference.trailers ?? [];
|
|
147
153
|
const sequenceParts = [];
|
|
148
154
|
if (trailers.length === 0) {
|
|
155
|
+
this.getScope().setVariable(leftSideReference.name, rhsValue);
|
|
156
|
+
let itemType = '';
|
|
149
157
|
if (rhsValue instanceof ClassComponent) {
|
|
150
|
-
|
|
151
|
-
sequenceParts.push(...['instance', leftSideReference.name, rhsValue]);
|
|
158
|
+
itemType = ReferenceTypes.instance;
|
|
152
159
|
this.log2(`assigned '${leftSideReference.name}' to ClassComponent`);
|
|
153
160
|
}
|
|
154
161
|
else {
|
|
162
|
+
itemType = ReferenceTypes.variable;
|
|
155
163
|
this.getScope().setVariable(leftSideReference.name, rhsValue);
|
|
156
164
|
this.log2(`assigned variable ${leftSideReference.name} to ${rhsValue}`);
|
|
157
|
-
sequenceParts.push(...['variable', leftSideReference.name, rhsValue]);
|
|
158
165
|
}
|
|
166
|
+
sequenceParts.push(...[itemType, leftSideReference.name, rhsValue]);
|
|
159
167
|
}
|
|
160
168
|
else {
|
|
161
169
|
if (leftSideReference.parentValue instanceof ClassComponent) {
|
|
@@ -171,8 +179,20 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
171
179
|
}
|
|
172
180
|
}
|
|
173
181
|
else if (leftSideReference.parentValue instanceof Object) {
|
|
174
|
-
|
|
175
|
-
|
|
182
|
+
if (Array.isArray(trailers[0]) && trailers[0][0] === TrailerArrayIndex) {
|
|
183
|
+
if (Array.isArray(leftSideReference.parentValue)) {
|
|
184
|
+
const arrayIndexValue = trailers[0][1];
|
|
185
|
+
leftSideReference.parentValue[arrayIndexValue] = rhsValue;
|
|
186
|
+
this.log2(`assigned array index ${leftSideReference.parentValue} index: ${arrayIndexValue} value: ${rhsValue}`);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
this.throwWithContext(lhsCtx, "Invalid array");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
leftSideReference.parentValue[trailers.join('.')] = rhsValue;
|
|
194
|
+
this.log2(`assigned object ${leftSideReference.parentValue} trailers: ${trailers} value: ${rhsValue}`);
|
|
195
|
+
}
|
|
176
196
|
sequenceParts.push(...['variable', [leftSideReference.parentValue, trailers], rhsValue]);
|
|
177
197
|
}
|
|
178
198
|
}
|
|
@@ -252,19 +272,39 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
252
272
|
}
|
|
253
273
|
return reference;
|
|
254
274
|
}
|
|
275
|
+
visitTrailer_expr2 = (ctx) => {
|
|
276
|
+
const reference = this.getResult(ctx);
|
|
277
|
+
const ctxID = ctx.ID();
|
|
278
|
+
const ctxDataExpr = ctx.data_expr();
|
|
279
|
+
const useValue = reference.value;
|
|
280
|
+
let nextReference;
|
|
281
|
+
if (ctxID) {
|
|
282
|
+
reference.trailers.push(ctxID.getText());
|
|
283
|
+
nextReference = this.getExecutor().resolveTrailers(reference.type, useValue, reference.trailers);
|
|
284
|
+
}
|
|
285
|
+
else if (ctxDataExpr) {
|
|
286
|
+
const arrayIndex = this.visitResult(ctxDataExpr);
|
|
287
|
+
if (arrayIndex instanceof NumericValue) {
|
|
288
|
+
const arrayIndexValue = arrayIndex.toNumber();
|
|
289
|
+
const foundValue = useValue[arrayIndexValue];
|
|
290
|
+
const refType = foundValue instanceof ClassComponent
|
|
291
|
+
? ReferenceTypes.instance : ReferenceTypes.variable;
|
|
292
|
+
nextReference = new AnyReference({
|
|
293
|
+
found: true,
|
|
294
|
+
type: refType,
|
|
295
|
+
value: foundValue,
|
|
296
|
+
trailers: [[TrailerArrayIndex, arrayIndexValue]],
|
|
297
|
+
parentValue: useValue
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
this.setResult(ctx, nextReference);
|
|
302
|
+
};
|
|
255
303
|
visitAtom_expr = (ctx) => {
|
|
256
304
|
const executor = this.getExecutor();
|
|
257
305
|
const firstId = ctx.ID(0);
|
|
258
306
|
const atomId = firstId.getText();
|
|
259
307
|
let currentReference;
|
|
260
|
-
const idTrailers = [];
|
|
261
|
-
if (ctx.ID().length > 1) {
|
|
262
|
-
const idLength = ctx.ID().length;
|
|
263
|
-
for (let i = 1; i < idLength; i++) {
|
|
264
|
-
const tmpCtx = ctx.ID(i);
|
|
265
|
-
idTrailers.push(tmpCtx.getText());
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
308
|
if (this.pinTypesList.indexOf(atomId) !== -1) {
|
|
269
309
|
currentReference = new AnyReference({
|
|
270
310
|
found: true,
|
|
@@ -273,18 +313,18 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
273
313
|
});
|
|
274
314
|
}
|
|
275
315
|
else {
|
|
276
|
-
currentReference = executor.resolveVariable(this.executionStack, atomId
|
|
316
|
+
currentReference = executor.resolveVariable(this.executionStack, atomId);
|
|
277
317
|
}
|
|
278
|
-
if (currentReference
|
|
279
|
-
const
|
|
280
|
-
for (
|
|
281
|
-
|
|
318
|
+
if (currentReference !== undefined && currentReference.found) {
|
|
319
|
+
const trailersLength = ctx.trailer_expr2().length;
|
|
320
|
+
for (let i = 0; i < trailersLength; i++) {
|
|
321
|
+
const trailerCtx = ctx.trailer_expr2(i);
|
|
322
|
+
this.setResult(trailerCtx, currentReference);
|
|
323
|
+
currentReference = this.visitResult(trailerCtx);
|
|
282
324
|
}
|
|
283
325
|
}
|
|
284
|
-
if (ctx.parent instanceof ExpressionContext && !currentReference.found) {
|
|
285
|
-
this.throwWithContext(ctx, "Unknown symbol: " + atomId);
|
|
286
|
-
}
|
|
287
326
|
this.setResult(ctx, currentReference);
|
|
327
|
+
this.log2('atom resolved: ' + ctx.getText() + ' -> ' + currentReference);
|
|
288
328
|
};
|
|
289
329
|
visitFunctionCallExpr = (ctx) => {
|
|
290
330
|
const result = this.visitResult(ctx.function_call_expr());
|
|
@@ -315,6 +355,11 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
315
355
|
currentReference.trailers = [];
|
|
316
356
|
ctx.trailer_expr().forEach(item => {
|
|
317
357
|
if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
|
|
358
|
+
if (currentReference.type === ReferenceTypes.variable) {
|
|
359
|
+
if (currentReference.value instanceof AnyReference && currentReference.value.type === ReferenceTypes.function) {
|
|
360
|
+
currentReference = currentReference.value;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
318
363
|
let parameters = [];
|
|
319
364
|
const ctxParameters = item.parameters();
|
|
320
365
|
if (ctxParameters) {
|
|
@@ -341,10 +386,9 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
341
386
|
}
|
|
342
387
|
}
|
|
343
388
|
else {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
: currentReference.value, currentReference.trailers);
|
|
389
|
+
const ctxTrailer = item.trailer_expr2();
|
|
390
|
+
this.setResult(ctxTrailer, currentReference);
|
|
391
|
+
currentReference = this.visitResult(ctxTrailer);
|
|
348
392
|
}
|
|
349
393
|
});
|
|
350
394
|
}
|
|
@@ -410,7 +454,8 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
410
454
|
value = reference;
|
|
411
455
|
}
|
|
412
456
|
else {
|
|
413
|
-
if (reference.trailers && reference.trailers.length > 0)
|
|
457
|
+
if ((reference.trailers && reference.trailers.length > 0)
|
|
458
|
+
|| reference.type === ReferenceTypes.function) {
|
|
414
459
|
value = reference;
|
|
415
460
|
}
|
|
416
461
|
else {
|
|
@@ -445,11 +490,11 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
445
490
|
const returnList = [];
|
|
446
491
|
dataExpressions.forEach((item, index) => {
|
|
447
492
|
const value = this.visitResult(item);
|
|
448
|
-
returnList.push(['position', index, value]);
|
|
493
|
+
returnList.push(['position', index, unwrapValue(value)]);
|
|
449
494
|
});
|
|
450
495
|
keywordAssignmentExpressions.forEach((item) => {
|
|
451
496
|
const [key, value] = this.visitResult(item);
|
|
452
|
-
returnList.push(['keyword', key, value]);
|
|
497
|
+
returnList.push(['keyword', key, unwrapValue(value)]);
|
|
453
498
|
});
|
|
454
499
|
this.setResult(ctx, returnList);
|
|
455
500
|
};
|
|
@@ -459,9 +504,7 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
459
504
|
visitFunction_return_expr = (ctx) => {
|
|
460
505
|
const executor = this.getExecutor();
|
|
461
506
|
executor.log('return from function');
|
|
462
|
-
const
|
|
463
|
-
this.visit(ctxDataExpr);
|
|
464
|
-
const returnValue = this.getResult(ctxDataExpr);
|
|
507
|
+
const returnValue = this.visitResult(ctx.data_expr());
|
|
465
508
|
executor.stopFurtherExpressions = true;
|
|
466
509
|
executor.returnValue = returnValue;
|
|
467
510
|
this.setResult(ctx, returnValue);
|
|
@@ -495,6 +538,25 @@ export class BaseVisitor extends CircuitScriptVisitor {
|
|
|
495
538
|
visitArrayExpr = (ctx) => {
|
|
496
539
|
this.setResult(ctx, this.visitResult(ctx.array_expr()));
|
|
497
540
|
};
|
|
541
|
+
visitArrayIndexExpr = (ctx) => {
|
|
542
|
+
const ctxArray = ctx.data_expr(0);
|
|
543
|
+
const ctxArrayIndex = ctx.data_expr(1);
|
|
544
|
+
const arrayItem = this.visitResult(ctxArray);
|
|
545
|
+
const indexValue = this.visitResult(ctxArrayIndex);
|
|
546
|
+
if (!Array.isArray(arrayItem)) {
|
|
547
|
+
throw new RuntimeExecutionError("Invalid array", ctxArray);
|
|
548
|
+
}
|
|
549
|
+
if (!(indexValue instanceof NumericValue)) {
|
|
550
|
+
throw new RuntimeExecutionError("Invalid index value", ctxArrayIndex);
|
|
551
|
+
}
|
|
552
|
+
const indexValueNumber = indexValue.toNumber();
|
|
553
|
+
if (isNaN(indexValueNumber)) {
|
|
554
|
+
throw new RuntimeExecutionError("Invalid index value", ctxArrayIndex);
|
|
555
|
+
}
|
|
556
|
+
if (Array.isArray(arrayItem)) {
|
|
557
|
+
this.setResult(ctx, arrayItem[indexValueNumber]);
|
|
558
|
+
}
|
|
559
|
+
};
|
|
498
560
|
setResult(ctx, value) {
|
|
499
561
|
this.resultData.set(ctx, value);
|
|
500
562
|
}
|
|
@@ -69,7 +69,7 @@ export class CircuitScriptLexer extends antlr.Lexer {
|
|
|
69
69
|
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
|
|
70
70
|
];
|
|
71
71
|
static literalNames = [
|
|
72
|
-
null, "':'", "','", "'='", "'..'", "'
|
|
72
|
+
null, "':'", "','", "'='", "'..'", "'['", "']'", "'.'", "'break'",
|
|
73
73
|
"'branch'", "'create'", "'component'", "'graphic'", "'module'",
|
|
74
74
|
"'wire'", "'pin'", "'add'", "'at'", "'to'", "'point'", "'join'",
|
|
75
75
|
"'parallel'", "'return'", "'def'", "'import'", "'for'", "'in'",
|
|
@@ -235,8 +235,8 @@ export class CircuitScriptLexer extends antlr.Lexer {
|
|
|
235
235
|
0, 131, 473, 1, 0, 0, 0, 133, 477, 1, 0, 0, 0, 135, 486, 1, 0, 0, 0, 137, 138, 5, 58, 0,
|
|
236
236
|
0, 138, 2, 1, 0, 0, 0, 139, 140, 5, 44, 0, 0, 140, 4, 1, 0, 0, 0, 141, 142, 5, 61, 0, 0,
|
|
237
237
|
142, 6, 1, 0, 0, 0, 143, 144, 5, 46, 0, 0, 144, 145, 5, 46, 0, 0, 145, 8, 1, 0, 0, 0, 146,
|
|
238
|
-
147, 5,
|
|
239
|
-
151, 5,
|
|
238
|
+
147, 5, 91, 0, 0, 147, 10, 1, 0, 0, 0, 148, 149, 5, 93, 0, 0, 149, 12, 1, 0, 0, 0, 150,
|
|
239
|
+
151, 5, 46, 0, 0, 151, 14, 1, 0, 0, 0, 152, 153, 5, 98, 0, 0, 153, 154, 5, 114, 0, 0, 154,
|
|
240
240
|
155, 5, 101, 0, 0, 155, 156, 5, 97, 0, 0, 156, 157, 5, 107, 0, 0, 157, 16, 1, 0, 0, 0,
|
|
241
241
|
158, 159, 5, 98, 0, 0, 159, 160, 5, 114, 0, 0, 160, 161, 5, 97, 0, 0, 161, 162, 5, 110,
|
|
242
242
|
0, 0, 162, 163, 5, 99, 0, 0, 163, 164, 5, 104, 0, 0, 164, 18, 1, 0, 0, 0, 165, 166, 5,
|