circuitscript 0.1.12 → 0.1.13

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.
@@ -25,7 +25,6 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
25
25
  this.acceptedDirections = [types_js_1.Direction.Up, types_js_1.Direction.Down,
26
26
  types_js_1.Direction.Right, types_js_1.Direction.Left];
27
27
  this.resultData = new Map;
28
- this.paramData = new Map;
29
28
  this.pinTypesList = [
30
29
  PinTypes_js_1.PinTypes.Any,
31
30
  PinTypes_js_1.PinTypes.Input,
@@ -54,17 +53,17 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
54
53
  };
55
54
  this.visitAssignment_expr = (ctx) => {
56
55
  const ctxAtom = ctx.atom_expr();
57
- const ctxFuncCall = ctx.function_call_expr();
56
+ const ctxFuncCallRef = ctx.function_call_expr();
58
57
  let leftSideReference;
59
58
  if (ctxAtom) {
60
59
  leftSideReference = this.getReference(ctx.atom_expr());
61
60
  }
62
- else if (ctxFuncCall) {
63
- leftSideReference = this.visitResult(ctxFuncCall);
61
+ else if (ctxFuncCallRef) {
62
+ this.setResult(ctxFuncCallRef, { keepReference: true });
63
+ leftSideReference = this.visitResult(ctxFuncCallRef);
64
64
  }
65
- const ctxDataExpr = ctx.data_expr();
66
- const dataValue = this.visitResult(ctxDataExpr);
67
- const rhsValue = (0, utils_js_1.prepareValue)(dataValue);
65
+ const rhsCtxResult = this.visitResult(ctx.data_expr());
66
+ const rhsValue = (0, utils_js_1.unwrapValue)(rhsCtxResult);
68
67
  const trailers = leftSideReference.trailers ?? [];
69
68
  const sequenceParts = [];
70
69
  if (trailers.length === 0) {
@@ -107,9 +106,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
107
106
  };
108
107
  this.visitOperator_assignment_expr = (ctx) => {
109
108
  const reference = this.getReference(ctx.atom_expr());
110
- const ctxDataExpr = ctx.data_expr();
111
- this.visit(ctxDataExpr);
112
- const value = this.getResult(ctxDataExpr);
109
+ const value = this.visitResult(ctx.data_expr());
113
110
  if (!reference.found) {
114
111
  this.throwWithContext(ctx, 'Undefined reference: ' + reference.name);
115
112
  }
@@ -201,55 +198,13 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
201
198
  this.setResult(ctx, result);
202
199
  };
203
200
  this.visitFunction_call_expr = (ctx) => {
204
- const executor = this.getExecutor();
205
- const atomId = ctx.ID().getText();
206
- let passedNetNamespace = null;
207
- const netNameSpaceExpr = ctx.net_namespace_expr();
208
- if (netNameSpaceExpr) {
209
- this.visit(netNameSpaceExpr);
210
- passedNetNamespace = this.getResult(netNameSpaceExpr);
211
- }
212
- let currentReference = executor.resolveVariable(this.executionStack, atomId);
213
- if (ctx.trailer_expr().length > 0) {
214
- if (!currentReference.found) {
215
- this.throwWithContext(ctx, "Unknown function name: " + atomId);
216
- }
217
- currentReference.trailers = [];
218
- ctx.trailer_expr().forEach(item => {
219
- const itemValue = item.getText();
220
- if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
221
- let parameters = [];
222
- const ctxParameters = item.parameters();
223
- if (ctxParameters) {
224
- this.visit(ctxParameters);
225
- parameters = this.getResult(ctxParameters);
226
- }
227
- const useNetNamespace = this.getNetNamespace(executor.netNamespace, passedNetNamespace);
228
- try {
229
- const [, functionResult] = executor.callFunction(currentReference.name, parameters, this.executionStack, useNetNamespace);
230
- if ((0, utils_js_1.isReference)(functionResult)) {
231
- currentReference = functionResult;
232
- }
233
- else {
234
- currentReference = new types_js_1.AnyReference({
235
- found: true,
236
- value: functionResult,
237
- trailers: [],
238
- type: (functionResult instanceof ClassComponent_js_1.ClassComponent) ?
239
- globals_js_1.ReferenceTypes.instance : globals_js_1.ReferenceTypes.value,
240
- });
241
- }
242
- }
243
- catch (err) {
244
- this.throwWithContext(ctx, err);
245
- }
246
- }
247
- else {
248
- currentReference.trailers.push(itemValue);
249
- }
250
- });
201
+ const ctxParams = this.getResult(ctx);
202
+ const { keepReference = false } = ctxParams ?? {};
203
+ this.handleFunctionCall(ctx);
204
+ if (!keepReference) {
205
+ const functionResultReference = this.getResult(ctx);
206
+ this.setResult(ctx, functionResultReference.value);
251
207
  }
252
- this.setResult(ctx, currentReference);
253
208
  };
254
209
  this.visitValue_expr = (ctx) => {
255
210
  const sign = ctx.Minus() ? -1 : 1;
@@ -299,12 +254,10 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
299
254
  const ctxValueExpr = ctx.value_expr();
300
255
  const ctxAtomExpr = ctx.atom_expr();
301
256
  if (ctxValueExpr) {
302
- this.visit(ctxValueExpr);
303
- value = this.getResult(ctxValueExpr);
257
+ value = this.visitResult(ctxValueExpr);
304
258
  }
305
259
  else if (ctxAtomExpr) {
306
- this.visit(ctxAtomExpr);
307
- const reference = this.getResult(ctxAtomExpr);
260
+ const reference = this.visitResult(ctxAtomExpr);
308
261
  if (!reference.found) {
309
262
  value = new types_js_1.UndeclaredReference(reference);
310
263
  }
@@ -369,33 +322,34 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
369
322
  executor.returnValue = returnValue;
370
323
  this.setResult(ctx, returnValue);
371
324
  };
372
- this.visitBreak_keyword = (ctx) => {
373
- const breakContext = this.getExecutor().getBreakContext();
374
- const currentResult = this.getResult(breakContext) ?? {};
375
- this.setResult(breakContext, {
376
- ...currentResult,
377
- breakSignal: true
378
- });
379
- };
380
- this.visitContinue_keyword = (ctx) => {
381
- const breakContext = this.getExecutor().getBreakContext();
382
- const currentResult = this.getResult(breakContext) ?? {};
383
- this.setResult(breakContext, {
384
- ...currentResult,
385
- breakSignal: true,
386
- continueSignal: true,
387
- });
325
+ this.visitFlow_expressions = (ctx) => {
326
+ if (ctx.if_expr()) {
327
+ this.visit(ctx.if_expr());
328
+ }
329
+ else if (ctx.while_expr()) {
330
+ this.visit(ctx.while_expr());
331
+ }
332
+ else if (ctx.for_expr()) {
333
+ this.visit(ctx.for_expr());
334
+ }
335
+ else if (ctx.Break() || ctx.Continue()) {
336
+ const breakContext = this.getExecutor().getBreakContext();
337
+ const currentResult = this.getResult(breakContext) ?? {};
338
+ this.setResult(breakContext, {
339
+ ...currentResult,
340
+ breakSignal: true,
341
+ continueSignal: ctx.Continue() ? true : false
342
+ });
343
+ }
388
344
  };
389
345
  this.visitArray_expr = (ctx) => {
390
346
  const array = ctx.data_expr().map(item => {
391
- this.visit(item);
392
- return this.getResult(item);
347
+ return this.visitResult(item);
393
348
  });
394
349
  this.setResult(ctx, array);
395
350
  };
396
351
  this.visitArrayExpr = (ctx) => {
397
- this.visit(ctx.array_expr());
398
- this.setResult(ctx, this.getResult(ctx.array_expr()));
352
+ this.setResult(ctx, this.visitResult(ctx.array_expr()));
399
353
  };
400
354
  this.visitRoundedBracketsExpr = (ctx) => {
401
355
  const ctxDataExpr = ctx.data_expr();
@@ -495,6 +449,57 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
495
449
  }
496
450
  return reference;
497
451
  }
452
+ handleFunctionCall(ctx) {
453
+ const executor = this.getExecutor();
454
+ const atomId = ctx.ID().getText();
455
+ let passedNetNamespace = null;
456
+ const netNameSpaceExpr = ctx.net_namespace_expr();
457
+ if (netNameSpaceExpr) {
458
+ passedNetNamespace = this.visitResult(netNameSpaceExpr);
459
+ }
460
+ let currentReference = executor.resolveVariable(this.executionStack, atomId);
461
+ if (ctx.trailer_expr().length > 0) {
462
+ if (!currentReference.found) {
463
+ this.throwWithContext(ctx, "Unknown function name: " + atomId);
464
+ }
465
+ currentReference.trailers = [];
466
+ ctx.trailer_expr().forEach(item => {
467
+ if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
468
+ let parameters = [];
469
+ const ctxParameters = item.parameters();
470
+ if (ctxParameters) {
471
+ parameters = this.visitResult(ctxParameters);
472
+ }
473
+ const useNetNamespace = this.getNetNamespace(executor.netNamespace, passedNetNamespace);
474
+ try {
475
+ const [, functionResult] = executor.callFunction(currentReference.name, parameters, this.executionStack, useNetNamespace);
476
+ if ((0, utils_js_1.isReference)(functionResult)) {
477
+ currentReference = functionResult;
478
+ }
479
+ else {
480
+ currentReference = new types_js_1.AnyReference({
481
+ found: true,
482
+ value: functionResult,
483
+ trailers: [],
484
+ type: (functionResult instanceof ClassComponent_js_1.ClassComponent) ?
485
+ globals_js_1.ReferenceTypes.instance : globals_js_1.ReferenceTypes.value,
486
+ });
487
+ }
488
+ }
489
+ catch (err) {
490
+ this.throwWithContext(ctx, err);
491
+ }
492
+ }
493
+ else {
494
+ currentReference.trailers.push(item.ID().getText());
495
+ currentReference = this.getExecutor().resolveTrailers(currentReference.type, (currentReference.parentValue !== undefined)
496
+ ? currentReference.parentValue
497
+ : currentReference.value, currentReference.trailers);
498
+ }
499
+ });
500
+ }
501
+ this.setResult(ctx, currentReference);
502
+ }
498
503
  setResult(ctx, value) {
499
504
  this.resultData.set(ctx, value);
500
505
  }
@@ -581,7 +586,7 @@ class BaseVisitor extends CircuitScriptVisitor_js_1.CircuitScriptVisitor {
581
586
  const tmpFuncArg = funcDefinedParameters[i];
582
587
  if (i < passedInParameters.length) {
583
588
  const tmpPassedInArgs = passedInParameters[i];
584
- const argValue = (0, utils_js_1.prepareValue)(tmpPassedInArgs[2]);
589
+ const argValue = (0, utils_js_1.unwrapValue)(tmpPassedInArgs[2]);
585
590
  if (tmpPassedInArgs[0] === 'position') {
586
591
  const variableName = tmpFuncArg[0];
587
592
  executor.log('set variable in scope, var name: ', variableName);