circuitscript 0.0.24 → 0.0.26

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 (73) hide show
  1. package/LICENSE +1 -1
  2. package/dist/cjs/BaseVisitor.js +485 -0
  3. package/dist/cjs/SemanticTokenVisitor.js +218 -0
  4. package/dist/cjs/SymbolValidatorVisitor.js +233 -0
  5. package/dist/cjs/antlr/CircuitScriptLexer.js +256 -219
  6. package/dist/cjs/antlr/CircuitScriptParser.js +2891 -2151
  7. package/dist/cjs/antlr/CircuitScriptVisitor.js +4 -3
  8. package/dist/cjs/draw_symbols.js +73 -22
  9. package/dist/cjs/execute.js +70 -78
  10. package/dist/cjs/export.js +91 -5
  11. package/dist/cjs/geometry.js +28 -8
  12. package/dist/cjs/globals.js +1 -2
  13. package/dist/cjs/helpers.js +180 -7
  14. package/dist/cjs/index.js +2 -0
  15. package/dist/cjs/layout.js +8 -0
  16. package/dist/cjs/lexer.js +19 -22
  17. package/dist/cjs/main.js +27 -20
  18. package/dist/cjs/objects/ClassComponent.js +4 -0
  19. package/dist/cjs/objects/ExecutionScope.js +1 -0
  20. package/dist/cjs/objects/types.js +7 -1
  21. package/dist/cjs/parser.js +29 -258
  22. package/dist/cjs/render.js +1 -1
  23. package/dist/cjs/validate.js +81 -0
  24. package/dist/cjs/visitor.js +601 -823
  25. package/dist/esm/BaseVisitor.mjs +486 -0
  26. package/dist/esm/SemanticTokenVisitor.mjs +215 -0
  27. package/dist/esm/SymbolValidatorVisitor.mjs +222 -0
  28. package/dist/esm/antlr/CircuitScriptLexer.mjs +231 -218
  29. package/dist/esm/antlr/CircuitScriptParser.mjs +2852 -2144
  30. package/dist/esm/antlr/CircuitScriptVisitor.mjs +13 -4
  31. package/dist/esm/draw_symbols.mjs +74 -23
  32. package/dist/esm/execute.mjs +70 -75
  33. package/dist/esm/export.mjs +89 -6
  34. package/dist/esm/geometry.mjs +28 -8
  35. package/dist/esm/globals.mjs +1 -2
  36. package/dist/esm/helpers.mjs +171 -9
  37. package/dist/esm/index.mjs +2 -0
  38. package/dist/esm/layout.mjs +8 -0
  39. package/dist/esm/lexer.mjs +10 -10
  40. package/dist/esm/main.mjs +28 -21
  41. package/dist/esm/objects/ClassComponent.mjs +4 -0
  42. package/dist/esm/objects/ExecutionScope.mjs +1 -0
  43. package/dist/esm/objects/types.mjs +6 -0
  44. package/dist/esm/parser.mjs +25 -230
  45. package/dist/esm/render.mjs +2 -2
  46. package/dist/esm/validate.mjs +74 -0
  47. package/dist/esm/visitor.mjs +415 -643
  48. package/dist/types/BaseVisitor.d.ts +66 -0
  49. package/dist/types/SemanticTokenVisitor.d.ts +36 -0
  50. package/dist/types/SymbolValidatorVisitor.d.ts +61 -0
  51. package/dist/types/antlr/CircuitScriptLexer.d.ts +37 -29
  52. package/dist/types/antlr/CircuitScriptParser.d.ts +606 -494
  53. package/dist/types/antlr/CircuitScriptVisitor.d.ts +78 -60
  54. package/dist/types/draw_symbols.d.ts +12 -3
  55. package/dist/types/execute.d.ts +5 -10
  56. package/dist/types/export.d.ts +27 -1
  57. package/dist/types/geometry.d.ts +4 -0
  58. package/dist/types/globals.d.ts +2 -3
  59. package/dist/types/helpers.d.ts +32 -1
  60. package/dist/types/index.d.ts +2 -0
  61. package/dist/types/lexer.d.ts +2 -2
  62. package/dist/types/objects/ClassComponent.d.ts +1 -0
  63. package/dist/types/objects/ExecutionScope.d.ts +4 -1
  64. package/dist/types/objects/types.d.ts +5 -0
  65. package/dist/types/parser.d.ts +15 -28
  66. package/dist/types/validate.d.ts +2 -0
  67. package/dist/types/visitor.d.ts +43 -95
  68. package/fonts/Inter-Bold.ttf +0 -0
  69. package/fonts/Inter-Regular.ttf +0 -0
  70. package/fonts/OpenSans-Regular.ttf +0 -0
  71. package/fonts/Roboto-Regular.ttf +0 -0
  72. package/libs/lib.cst +184 -0
  73. package/package.json +11 -6
@@ -0,0 +1,222 @@
1
+ import { BaseVisitor } from "./BaseVisitor.mjs";
2
+ import { ParseSymbolType } from "./objects/types.mjs";
3
+ export class SymbolValidatorVisitor extends BaseVisitor {
4
+ symbolTable = new SymbolTable();
5
+ addSymbolVariable(name, value, executor = null) {
6
+ const useExecutor = executor === null ? this.getExecutor() : executor;
7
+ this.symbolTable.addVariable(useExecutor, name, value);
8
+ this.log2('add symbol variable: ' + name);
9
+ }
10
+ addSymbolFunction(functionName, funcDefinedParameters) {
11
+ if (!this.symbolTable.exists(this.getExecutor(), functionName)) {
12
+ this.symbolTable.addFunction(this.getExecutor(), functionName, funcDefinedParameters);
13
+ this.log2('add symbol function: ' + functionName);
14
+ }
15
+ }
16
+ handleAtomSymbol(atom) {
17
+ const atomId = atom.getText();
18
+ const executor = this.getExecutor();
19
+ let tmpSymbol;
20
+ if (this.symbolTable.exists(executor, atomId)) {
21
+ tmpSymbol = this.symbolTable.get(executor, atomId);
22
+ }
23
+ else {
24
+ const foundContext = this.symbolTable.searchParentContext(executor, atomId);
25
+ if (foundContext === null) {
26
+ tmpSymbol = this.symbolTable.addUndefined(executor, atomId, atom);
27
+ this.log2('symbol not found: ' + atomId);
28
+ }
29
+ else {
30
+ tmpSymbol = this.symbolTable.get(foundContext, atomId);
31
+ }
32
+ }
33
+ return tmpSymbol;
34
+ }
35
+ setSymbols(symbolTable) {
36
+ this.symbolTable = symbolTable;
37
+ }
38
+ visitImport_expr = (ctx) => {
39
+ const ID = ctx.ID().toString();
40
+ const { pathExists } = this.handleImportFile(ID, false);
41
+ if (!pathExists) {
42
+ this.symbolTable.addUndefined(this.getExecutor(), ID, ctx.ID());
43
+ }
44
+ };
45
+ visitAssignment_expr = (ctx) => {
46
+ const atomStr = ctx.atom_expr().getText();
47
+ const ctxDataExpr = ctx.data_expr();
48
+ this.visit(ctxDataExpr);
49
+ const value = this.getResult(ctxDataExpr);
50
+ this.addSymbolVariable(atomStr, value);
51
+ return null;
52
+ };
53
+ visitAtom_expr = (ctx) => {
54
+ const tmpSymbol = this.handleAtomSymbol(ctx.ID(0));
55
+ this.setResult(ctx, tmpSymbol);
56
+ };
57
+ visitFunction_call_expr = (ctx) => {
58
+ this.handleAtomSymbol(ctx.ID());
59
+ if (ctx.trailer_expr().length > 0) {
60
+ ctx.trailer_expr().forEach(item => {
61
+ if (item.OPEN_PAREN() && item.CLOSE_PAREN()) {
62
+ const params = item.parameters();
63
+ if (params) {
64
+ this.visit(params);
65
+ }
66
+ }
67
+ });
68
+ }
69
+ };
70
+ visitValueAtomExpr = (ctx) => {
71
+ let value = null;
72
+ const ctxValueExpr = ctx.value_expr();
73
+ const cxtAtomExpr = ctx.atom_expr();
74
+ if (ctxValueExpr) {
75
+ this.visit(ctxValueExpr);
76
+ value = this.getResult(ctxValueExpr);
77
+ }
78
+ else if (cxtAtomExpr) {
79
+ this.visit(cxtAtomExpr);
80
+ value = this.getResult(cxtAtomExpr);
81
+ }
82
+ this.setResult(ctx, value);
83
+ };
84
+ visitUnaryOperatorExpr = (ctx) => {
85
+ this.visit(ctx.data_expr());
86
+ };
87
+ visitMultiplyExpr = (ctx) => {
88
+ this.visit(ctx.data_expr(0));
89
+ this.visit(ctx.data_expr(1));
90
+ };
91
+ visitAdditionExpr = (ctx) => {
92
+ this.visit(ctx.data_expr(0));
93
+ this.visit(ctx.data_expr(1));
94
+ };
95
+ visitBinaryOperatorExpr = (ctx) => {
96
+ this.visit(ctx.data_expr(0));
97
+ this.visit(ctx.data_expr(1));
98
+ };
99
+ visitDataExpr = (ctx) => {
100
+ return;
101
+ };
102
+ visitFunction_def_expr = (ctx) => {
103
+ const functionName = ctx.ID().getText();
104
+ let funcDefinedParameters = [];
105
+ const ctxFunctionArgsExpr = ctx.function_args_expr();
106
+ if (ctxFunctionArgsExpr) {
107
+ this.visit(ctxFunctionArgsExpr);
108
+ funcDefinedParameters = this.getResult(ctxFunctionArgsExpr);
109
+ }
110
+ this.addSymbolFunction(functionName, funcDefinedParameters);
111
+ const executionContextName = functionName + '_validate';
112
+ const passedInParamsNull = funcDefinedParameters.map((param, index) => {
113
+ return ['position', index, null];
114
+ });
115
+ const newExecutor = this.enterNewChildContext(this.executionStack, this.getExecutor(), executionContextName, { netNamespace: "" }, funcDefinedParameters, passedInParamsNull);
116
+ funcDefinedParameters.forEach(param => {
117
+ this.addSymbolVariable(param[0], null, newExecutor);
118
+ });
119
+ this.runExpressions(newExecutor, ctx.function_expr());
120
+ this.executionStack.pop();
121
+ };
122
+ getSymbols() {
123
+ return this.symbolTable;
124
+ }
125
+ dumpSymbols() {
126
+ this.symbolTable.dumpSymbols();
127
+ }
128
+ }
129
+ export class SymbolValidatorResolveVisitor extends SymbolValidatorVisitor {
130
+ addSymbolVariable(name, value) {
131
+ }
132
+ addSymbolFunction(functionName, funcDefinedParameters) {
133
+ if (this.symbolTable.exists(this.getExecutor(), functionName)) {
134
+ this.symbolTable.addFunction(this.getExecutor(), functionName, funcDefinedParameters);
135
+ }
136
+ }
137
+ }
138
+ export class SymbolTable {
139
+ symbols = new Map();
140
+ executonContextsNamespaces = [];
141
+ getSymbols() {
142
+ return this.symbols;
143
+ }
144
+ addFunction(executionContext, id, funcDefinedParameters) {
145
+ return this.add(executionContext, id, ParseSymbolType.Function, {
146
+ funcDefinedParameters
147
+ });
148
+ }
149
+ addVariable(executionContext, id, variableValue) {
150
+ return this.add(executionContext, id, ParseSymbolType.Variable, {
151
+ variableValue
152
+ });
153
+ }
154
+ addUndefined(executionContext, id, node) {
155
+ return this.add(executionContext, id, ParseSymbolType.Undefined, {
156
+ node
157
+ });
158
+ }
159
+ add(executionContext, id, type, extra) {
160
+ if (this.executonContextsNamespaces.indexOf(executionContext.namespace) === -1) {
161
+ this.executonContextsNamespaces.push(executionContext.namespace);
162
+ }
163
+ const item = {
164
+ id,
165
+ type,
166
+ context: executionContext,
167
+ extra
168
+ };
169
+ this.symbols.set(this.idName(executionContext, id), item);
170
+ return item;
171
+ }
172
+ idName(executionContext, id) {
173
+ return executionContext.namespace + id;
174
+ }
175
+ dumpSymbols() {
176
+ for (const [key, value] of this.symbols) {
177
+ console.log(value.type.padEnd(10, " "), key);
178
+ }
179
+ }
180
+ exists(executionContext, id) {
181
+ const name = this.idName(executionContext, id);
182
+ return this.symbols.has(name);
183
+ }
184
+ existsAny(executionContext, id) {
185
+ if (this.exists(executionContext, id)) {
186
+ return true;
187
+ }
188
+ else {
189
+ return this.searchParentContext(executionContext, id) !== null;
190
+ }
191
+ }
192
+ get(executionContext, id) {
193
+ const name = this.idName(executionContext, id);
194
+ return this.symbols.get(name);
195
+ }
196
+ getParentContexts(executionContext, contextsNamespace) {
197
+ if (executionContext.parentContext !== null) {
198
+ contextsNamespace.push(executionContext.parentContext.namespace);
199
+ this.getParentContexts(executionContext.parentContext, contextsNamespace);
200
+ }
201
+ return contextsNamespace;
202
+ }
203
+ searchParentContext(executionContext, id) {
204
+ const contextNames = this.getParentContexts(executionContext, []);
205
+ for (const [key,] of this.symbols) {
206
+ if (key.endsWith(`.${id}`)) {
207
+ const { context } = this.symbols.get(key);
208
+ if (contextNames.indexOf(context.namespace) !== -1) {
209
+ return context;
210
+ }
211
+ }
212
+ }
213
+ return null;
214
+ }
215
+ clearUndefined() {
216
+ for (const [key, value] of this.symbols) {
217
+ if (value.type === ParseSymbolType.Undefined) {
218
+ this.symbols.delete(key);
219
+ }
220
+ }
221
+ }
222
+ }