circuitscript 0.1.8 → 0.1.9

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.
@@ -47,7 +47,7 @@ export class ParserVisitor extends BaseVisitor {
47
47
  });
48
48
  }
49
49
  catch (err) {
50
- throw new RuntimeExecutionError(err.message, ctx.start, ctx.stop);
50
+ throw new RuntimeExecutionError(err.message, ctx);
51
51
  }
52
52
  });
53
53
  return this.getExecutor().getCurrentPoint();
@@ -115,6 +115,24 @@ export class ParserVisitor extends BaseVisitor {
115
115
  };
116
116
  visitCreate_component_expr = (ctx) => {
117
117
  const scope = this.getScope();
118
+ const definedPinIds = [];
119
+ const arrangedPinIds = [];
120
+ const checkPinExistsAndNotDuplicated = (pinId, ctx) => {
121
+ if (definedPinIds.indexOf(pinId) === -1) {
122
+ this.warnings.push({
123
+ message: `Invalid pin ${pinId}`, ctx
124
+ });
125
+ }
126
+ if (arrangedPinIds.indexOf(pinId) !== -1) {
127
+ this.warnings.push({
128
+ message: `Pin ${pinId} specified more than once`,
129
+ ctx,
130
+ });
131
+ }
132
+ arrangedPinIds.push(pinId);
133
+ };
134
+ let didDefineArrangeProp = false;
135
+ let didDefineDisplayProp = false;
118
136
  scope.setOnPropertyHandler((path, value, ctx) => {
119
137
  if (path.length === 1) {
120
138
  const [, keyName] = path[0];
@@ -127,9 +145,25 @@ export class ParserVisitor extends BaseVisitor {
127
145
  case 'height':
128
146
  this.validateNumeric(value, ctx);
129
147
  break;
148
+ case 'display':
149
+ if (didDefineArrangeProp) {
150
+ throw new RuntimeExecutionError("arrange property has already been defined", ctx);
151
+ }
152
+ didDefineDisplayProp = true;
153
+ break;
154
+ case 'arrange':
155
+ if (didDefineDisplayProp) {
156
+ throw new RuntimeExecutionError("display property already defined", ctx);
157
+ }
158
+ didDefineArrangeProp = true;
159
+ break;
130
160
  case 'pins':
131
161
  if (!(value instanceof Map)) {
132
162
  this.validateNumeric(value, ctx);
163
+ const numPins = value.toNumber();
164
+ for (let i = 0; i < numPins; i++) {
165
+ definedPinIds.push(i + 1);
166
+ }
133
167
  }
134
168
  break;
135
169
  case 'copy':
@@ -140,7 +174,7 @@ export class ParserVisitor extends BaseVisitor {
140
174
  this.validateBoolean(value, ctx);
141
175
  }
142
176
  else {
143
- throw new RuntimeExecutionError("Invalid value for 'copy' property", ctx.start, ctx.end);
177
+ throw new RuntimeExecutionError("Invalid value for 'copy' property", ctx);
144
178
  }
145
179
  break;
146
180
  }
@@ -150,20 +184,26 @@ export class ParserVisitor extends BaseVisitor {
150
184
  if (keyName === 'arrange') {
151
185
  const [sideKeyCtx, sideKeyName] = path[1];
152
186
  if (ValidPinSides.indexOf(sideKeyName) === -1) {
153
- throw new RuntimeExecutionError(`Invalid side ${sideKeyName} in arrange`, sideKeyCtx.start, sideKeyCtx.stop);
187
+ throw new RuntimeExecutionError(`Invalid side ${sideKeyName} in arrange`, sideKeyCtx);
154
188
  }
155
189
  else {
156
- if (path.length > 2 && path[2][0] === 'index') {
190
+ if (path.length === 2 && value instanceof NumericValue) {
191
+ checkPinExistsAndNotDuplicated(value.toNumber(), ctx);
192
+ }
193
+ else if (path.length > 2 && path[2][0] === 'index') {
157
194
  if (Array.isArray(value)) {
158
195
  const goodBlank = value.length === 1 &&
159
196
  value[0] instanceof NumericValue;
160
197
  if (!goodBlank) {
161
- throw new RuntimeExecutionError(`Invalid blank specifier`, ctx.start, ctx.stop);
198
+ throw new RuntimeExecutionError(`Invalid blank specifier`, ctx);
162
199
  }
163
200
  }
164
201
  else {
165
202
  if (!(value instanceof NumericValue)) {
166
- throw new RuntimeExecutionError(`Invalid numeric value for arrange.${sideKeyName}`, ctx.start, ctx.stop);
203
+ throw new RuntimeExecutionError(`Invalid numeric value for arrange.${sideKeyName}`, ctx);
204
+ }
205
+ else {
206
+ checkPinExistsAndNotDuplicated(value.toNumber(), ctx);
167
207
  }
168
208
  }
169
209
  }
@@ -184,10 +224,12 @@ export class ParserVisitor extends BaseVisitor {
184
224
  }
185
225
  else if (keyName === 'pins') {
186
226
  if (path.length === 2) {
227
+ const idName = path[1][1];
228
+ definedPinIds.push(idName);
187
229
  if (value.length === 2) {
188
230
  const [pinType,] = value;
189
231
  if (pinType instanceof UndeclaredReference) {
190
- throw new RuntimeExecutionError(`Invalid pin type: ${pinType.reference.name}`, ctx.start, ctx.end);
232
+ throw new RuntimeExecutionError(`Invalid pin type: ${pinType.reference.name}`, ctx);
191
233
  }
192
234
  }
193
235
  }
@@ -416,7 +458,7 @@ export class ParserVisitor extends BaseVisitor {
416
458
  this.getScope().enterContext(ctxValue);
417
459
  const keyName = this.visitResult(ctxKey);
418
460
  const value = this.visitResult(ctxValue);
419
- scope.triggerPropertyHandler(value, ctxValue);
461
+ scope.triggerPropertyHandler(this, value, ctxValue);
420
462
  this.getScope().exitContext();
421
463
  this.getScope().exitContext();
422
464
  if (value instanceof UndeclaredReference && (value.reference.parentValue === undefined
@@ -437,7 +479,7 @@ export class ParserVisitor extends BaseVisitor {
437
479
  value = ctx.data_expr().map((item, index) => {
438
480
  this.getScope().enterContext(index);
439
481
  const result = this.visitResult(item);
440
- this.getScope().triggerPropertyHandler(result, item);
482
+ this.getScope().triggerPropertyHandler(this, result, item);
441
483
  this.getScope().exitContext();
442
484
  return result;
443
485
  });
@@ -1063,45 +1105,46 @@ export class ParserVisitor extends BaseVisitor {
1063
1105
  parseCreateComponentPins(pinData) {
1064
1106
  const pins = [];
1065
1107
  if (pinData instanceof NumericValue) {
1108
+ const tmpMap = new Map();
1066
1109
  const lastPin = pinData.toNumber();
1067
1110
  for (let i = 0; i < lastPin; i++) {
1068
1111
  const pinId = i + 1;
1069
- pins.push(new PinDefinition(pinId, PinIdType.Int, pinId.toString()));
1112
+ tmpMap.set(pinId, numeric(pinId));
1070
1113
  }
1071
- }
1072
- else if (pinData instanceof Map) {
1073
- for (const [pinId, pinDef] of pinData) {
1074
- let pinIdType = PinIdType.Int;
1075
- let pinType = PinTypes.Any;
1076
- let pinName = null;
1077
- let altPinNames = [];
1078
- if (typeof pinId === 'string') {
1079
- pinIdType = PinIdType.Str;
1080
- }
1081
- if (Array.isArray(pinDef)) {
1082
- const firstValue = pinDef[0];
1083
- if (firstValue.type
1084
- && firstValue.type === ReferenceTypes.pinType
1085
- && this.pinTypes.indexOf(firstValue.value) !== -1) {
1086
- pinType = firstValue.value;
1087
- pinName = pinDef[1];
1088
- if (pinDef.length > 2) {
1089
- altPinNames = pinDef.slice(2);
1090
- }
1091
- }
1092
- else {
1093
- pinName = pinDef[0];
1094
- if (pinDef.length > 1) {
1095
- altPinNames = pinDef.slice(1);
1096
- }
1114
+ pinData = tmpMap;
1115
+ }
1116
+ pinData = pinData ?? [];
1117
+ for (const [pinId, pinDef] of pinData) {
1118
+ let pinIdType = PinIdType.Int;
1119
+ let pinType = PinTypes.Any;
1120
+ let pinName = null;
1121
+ let altPinNames = [];
1122
+ if (typeof pinId === 'string') {
1123
+ pinIdType = PinIdType.Str;
1124
+ }
1125
+ if (Array.isArray(pinDef)) {
1126
+ const firstValue = pinDef[0];
1127
+ if (firstValue.type
1128
+ && firstValue.type === ReferenceTypes.pinType
1129
+ && this.pinTypes.indexOf(firstValue.value) !== -1) {
1130
+ pinType = firstValue.value;
1131
+ pinName = pinDef[1];
1132
+ if (pinDef.length > 2) {
1133
+ altPinNames = pinDef.slice(2);
1097
1134
  }
1098
1135
  }
1099
1136
  else {
1100
- pinName = pinDef;
1137
+ pinName = pinDef[0];
1138
+ if (pinDef.length > 1) {
1139
+ altPinNames = pinDef.slice(1);
1140
+ }
1101
1141
  }
1102
- this.log('pins', pinId, pinIdType, pinName, pinType, altPinNames);
1103
- pins.push(new PinDefinition(pinId, pinIdType, pinName, pinType, altPinNames));
1104
1142
  }
1143
+ else {
1144
+ pinName = pinDef;
1145
+ }
1146
+ this.log('pins', pinId, pinIdType, pinName, pinType, altPinNames);
1147
+ pins.push(new PinDefinition(pinId, pinIdType, pinName, pinType, altPinNames));
1105
1148
  }
1106
1149
  return pins;
1107
1150
  }
@@ -1310,6 +1353,9 @@ export class ParserVisitor extends BaseVisitor {
1310
1353
  });
1311
1354
  return properties;
1312
1355
  }
1356
+ getWarnings() {
1357
+ return this.warnings;
1358
+ }
1313
1359
  }
1314
1360
  const ComponentRefDesPrefixes = {
1315
1361
  res: 'R',
@@ -6,6 +6,7 @@ import { ClassComponent } from "./objects/ClassComponent.js";
6
6
  import { Net } from "./objects/Net.js";
7
7
  import { CallableParameter, CFunctionOptions, ComplexType, Direction, FunctionDefinedParameter, ReferenceType } from "./objects/types.js";
8
8
  import { ParserRuleContext } from 'antlr4ng';
9
+ import { ExecutionWarning } from "./utils.js";
9
10
  import { BaseError } from './utils.js';
10
11
  import { ExecutionScope } from './objects/ExecutionScope.js';
11
12
  import { NodeScriptEnvironment } from "./environment.js";
@@ -24,6 +25,7 @@ export declare class BaseVisitor extends CircuitScriptVisitor<ComplexType | Refe
24
25
  onErrorHandler: OnErrorHandler | null;
25
26
  environment: NodeScriptEnvironment;
26
27
  protected importedFiles: ImportFile[];
28
+ protected warnings: ExecutionWarning[];
27
29
  onImportFile: (visitor: BaseVisitor, filePath: string, fileData: string, onErrorHandler: OnErrorHandler) => Promise<{
28
30
  hasError: boolean;
29
31
  hasParseError: boolean;
@@ -60,7 +62,7 @@ export declare class BaseVisitor extends CircuitScriptVisitor<ComplexType | Refe
60
62
  visitArrayExpr: (ctx: ArrayExprContext) => void;
61
63
  protected setResult(ctx: ParserRuleContext, value: any): void;
62
64
  protected getResult(ctx: ParserRuleContext): any;
63
- protected visitResult(ctx: ParserRuleContext): any;
65
+ visitResult(ctx: ParserRuleContext): any;
64
66
  protected handleImportFile(name: string, throwErrors?: boolean, ctx?: ParserRuleContext | null): Promise<ImportFile>;
65
67
  visitRoundedBracketsExpr: (ctx: RoundedBracketsExprContext) => void;
66
68
  protected setupDefinedParameters(funcDefinedParameters: FunctionDefinedParameter[], passedInParameters: CallableParameter[], executor: ExecutionContext): void;
@@ -1,4 +1,5 @@
1
1
  import { BlockTypes, FrameType } from './globals.js';
2
+ import { ExecutionWarning } from "./utils.js";
2
3
  import { ClassComponent } from './objects/ClassComponent.js';
3
4
  import { ExecutionScope } from './objects/ExecutionScope.js';
4
5
  import { Net } from './objects/Net.js';
@@ -28,7 +29,9 @@ export declare class ExecutionContext {
28
29
  __functionCache: {};
29
30
  parentContext: ExecutionContext;
30
31
  componentAngleFollowsWire: boolean;
31
- constructor(name: string, namespace: string, netNamespace: string, executionLevel: number | undefined, indentLevel: number | undefined, silent: boolean | undefined, logger: Logger, parent: ExecutionContext);
32
+ warnings: ExecutionWarning[];
33
+ constructor(name: string, namespace: string, netNamespace: string, executionLevel: number | undefined, indentLevel: number | undefined, silent: boolean | undefined, logger: Logger, warnings: ExecutionWarning[], parent: ExecutionContext);
34
+ logWarning(message: string, context?: ParserRuleContext, fileName?: string): void;
32
35
  log(...params: any[]): void;
33
36
  private setupRoot;
34
37
  getUniqueInstanceName(): string;
@@ -46,6 +49,8 @@ export declare class ExecutionContext {
46
49
  angle?: NumericValue;
47
50
  followWireOrientation: boolean;
48
51
  }, isModule?: boolean): ClassComponent;
52
+ private removeArrangePropDuplicates;
53
+ private getArrangePropPins;
49
54
  printPoint(extra?: string): void;
50
55
  addComponentExisting(component: ClassComponent, pin: number): ComponentPin;
51
56
  toComponent(component: ClassComponent, pinId: number | null, options?: {
@@ -18,6 +18,7 @@ export declare class ClassComponent {
18
18
  _copyID?: number;
19
19
  _copyFrom?: ClassComponent;
20
20
  _pointLinkComponent?: ClassComponent;
21
+ _unplacedPins: number[];
21
22
  arrangeProps: Map<string, NumericValue[]> | null;
22
23
  displayProp: SymbolDrawingCommands | null;
23
24
  widthProp: number | null;
@@ -5,6 +5,7 @@ import { LayoutDirection } from '../globals.js';
5
5
  import { Wire, WireSegment } from './Wire.js';
6
6
  import { Frame } from './Frame.js';
7
7
  import { ParserRuleContext } from 'antlr4ng';
8
+ import { BaseVisitor } from 'src/BaseVisitor.js';
8
9
  type OnPropertyHandler = (path: PropertyTreeKey[], value: any, valueContext: ParserRuleContext) => void;
9
10
  export type PropertyTreeKey = [ctx: ParserRuleContext, value: any] | ['index', number];
10
11
  export declare class ExecutionScope {
@@ -50,10 +51,10 @@ export declare class ExecutionScope {
50
51
  setCurrent(component: ClassComponent | null, pin?: number | null): void;
51
52
  enterContext(context: ParserRuleContext): void;
52
53
  exitContext(): ParserRuleContext;
53
- findPropertyKeyTree(): PropertyTreeKey[];
54
+ private findPropertyKeyTree;
54
55
  setOnPropertyHandler(handler: OnPropertyHandler): void;
55
56
  popOnPropertyHandler(): OnPropertyHandler;
56
- triggerPropertyHandler(value: any, valueCtx: ParserRuleContext): void;
57
+ triggerPropertyHandler(visitor: BaseVisitor, value: any, valueCtx: ParserRuleContext): void;
57
58
  }
58
59
  export declare enum SequenceAction {
59
60
  To = "to",
@@ -4,6 +4,7 @@ import { ClassComponent } from "./objects/ClassComponent.js";
4
4
  import { NumericValue } from "./objects/ParamDefinition.js";
5
5
  import { SequenceAction, SequenceItem } from './objects/ExecutionScope.js';
6
6
  import { BlockTypes } from './globals.js';
7
+ import { ExecutionWarning } from './utils.js';
7
8
  export declare class SimpleStopwatch {
8
9
  startTime: Date;
9
10
  constructor();
@@ -49,9 +50,10 @@ export declare class BaseError extends Error {
49
50
  startToken?: Token;
50
51
  endToken?: Token;
51
52
  filePath?: string;
52
- constructor(message: string, startToken?: Token, endToken?: Token, filePath?: string);
53
+ constructor(message: string, startTokenOrCtx?: Token | ParserRuleContext, endToken?: Token, filePath?: string);
53
54
  toString(): string;
54
55
  }
56
+ export declare function getLinePositionAsString(ctx: ParserRuleContext): string | null;
55
57
  export declare class ParseSyntaxError extends BaseError {
56
58
  name: string;
57
59
  }
@@ -65,3 +67,9 @@ export declare class RenderError extends Error {
65
67
  stage?: string;
66
68
  constructor(message: string, stage?: string);
67
69
  }
70
+ export type ExecutionWarning = {
71
+ message: string;
72
+ fileName?: string;
73
+ ctx?: ParserRuleContext;
74
+ };
75
+ export declare function printWarnings(warnings: ExecutionWarning[]): void;
@@ -2,6 +2,7 @@ import { Add_component_exprContext, AdditionExprContext, At_blockContext, At_blo
2
2
  import { ClassComponent } from './objects/ClassComponent.js';
3
3
  import { PinTypes } from './objects/PinTypes.js';
4
4
  import { ComponentPin, ComponentPinNet, ComponentPinNetPair } from './objects/types.js';
5
+ import { ExecutionWarning } from "./utils.js";
5
6
  import { Net } from './objects/Net.js';
6
7
  import { BaseVisitor } from './BaseVisitor.js';
7
8
  import { ParserRuleContext } from 'antlr4ng';
@@ -83,6 +84,7 @@ export declare class ParserVisitor extends BaseVisitor {
83
84
  };
84
85
  private resolveNets;
85
86
  private getPropertyExprList;
87
+ getWarnings(): ExecutionWarning[];
86
88
  }
87
89
  export type NetListItem = {
88
90
  instanceName: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "circuitscript",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Interpreter for the circuitscript language",
5
5
  "homepage": "https://circuitscript.net",
6
6
  "engines": {