stone-lang 0.1.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 (68) hide show
  1. package/README.md +52 -0
  2. package/StoneEngine.js +879 -0
  3. package/StoneEngineService.js +1727 -0
  4. package/adapters/FileSystemAdapter.js +230 -0
  5. package/adapters/OutputAdapter.js +208 -0
  6. package/adapters/index.js +6 -0
  7. package/cli/CLIOutputAdapter.js +196 -0
  8. package/cli/DaemonClient.js +349 -0
  9. package/cli/JSONOutputAdapter.js +135 -0
  10. package/cli/ReplSession.js +567 -0
  11. package/cli/ViewerServer.js +590 -0
  12. package/cli/commands/check.js +84 -0
  13. package/cli/commands/daemon.js +189 -0
  14. package/cli/commands/kill.js +66 -0
  15. package/cli/commands/package.js +713 -0
  16. package/cli/commands/ps.js +65 -0
  17. package/cli/commands/run.js +537 -0
  18. package/cli/entry.js +169 -0
  19. package/cli/index.js +14 -0
  20. package/cli/stonec.js +358 -0
  21. package/cli/test-compiler.js +181 -0
  22. package/cli/viewer/index.html +495 -0
  23. package/daemon/IPCServer.js +455 -0
  24. package/daemon/ProcessManager.js +327 -0
  25. package/daemon/ProcessRunner.js +307 -0
  26. package/daemon/daemon.js +398 -0
  27. package/daemon/index.js +16 -0
  28. package/frontend/analysis/index.js +5 -0
  29. package/frontend/analysis/livenessAnalyzer.js +568 -0
  30. package/frontend/analysis/treeShaker.js +265 -0
  31. package/frontend/index.js +20 -0
  32. package/frontend/parsing/astBuilder.js +2196 -0
  33. package/frontend/parsing/index.js +7 -0
  34. package/frontend/parsing/sonParser.js +592 -0
  35. package/frontend/parsing/stoneAstTypes.js +703 -0
  36. package/frontend/parsing/terminal-registry.js +435 -0
  37. package/frontend/parsing/tokenizer.js +692 -0
  38. package/frontend/type-checker/OverloadedFunctionType.js +43 -0
  39. package/frontend/type-checker/TypeEnvironment.js +165 -0
  40. package/frontend/type-checker/bidirectionalInference.js +149 -0
  41. package/frontend/type-checker/index.js +10 -0
  42. package/frontend/type-checker/moduleAnalysis.js +248 -0
  43. package/frontend/type-checker/operatorMappings.js +35 -0
  44. package/frontend/type-checker/overloadResolution.js +605 -0
  45. package/frontend/type-checker/typeChecker.js +452 -0
  46. package/frontend/type-checker/typeCompatibility.js +389 -0
  47. package/frontend/type-checker/visitors/controlFlow.js +483 -0
  48. package/frontend/type-checker/visitors/functions.js +604 -0
  49. package/frontend/type-checker/visitors/index.js +38 -0
  50. package/frontend/type-checker/visitors/literals.js +341 -0
  51. package/frontend/type-checker/visitors/modules.js +159 -0
  52. package/frontend/type-checker/visitors/operators.js +109 -0
  53. package/frontend/type-checker/visitors/statements.js +768 -0
  54. package/frontend/types/index.js +5 -0
  55. package/frontend/types/operatorMap.js +134 -0
  56. package/frontend/types/types.js +2046 -0
  57. package/frontend/utils/errorCollector.js +244 -0
  58. package/frontend/utils/index.js +5 -0
  59. package/frontend/utils/moduleResolver.js +479 -0
  60. package/package.json +50 -0
  61. package/packages/browserCache.js +359 -0
  62. package/packages/fetcher.js +236 -0
  63. package/packages/index.js +130 -0
  64. package/packages/lockfile.js +271 -0
  65. package/packages/manifest.js +291 -0
  66. package/packages/packageResolver.js +356 -0
  67. package/packages/resolver.js +310 -0
  68. package/packages/semver.js +635 -0
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Stone Engine - Unified Error Collector
3
+ *
4
+ * Provides consistent error handling across all compiler phases:
5
+ * - Parse (lexer, parser)
6
+ * - Type check
7
+ * - Compile (bytecode generation)
8
+ * - Runtime (VM execution)
9
+ *
10
+ * Features:
11
+ * - Consistent error/warning format with severity, message, location, and phase
12
+ * - Error codes for programmatic error handling
13
+ * - Merge support for combining errors from multiple phases
14
+ * - Formatted output for display
15
+ */
16
+
17
+ /**
18
+ * Represents a collected error or warning
19
+ * @typedef {Object} CollectedError
20
+ * @property {'error' | 'warning'} severity - Error severity
21
+ * @property {string} message - Human-readable error message
22
+ * @property {Object} [location] - Source location
23
+ * @property {number} [location.line] - Line number (1-indexed)
24
+ * @property {number} [location.column] - Column number (1-indexed)
25
+ * @property {string} phase - Compiler phase that generated the error
26
+ * @property {string} [code] - Optional error code (e.g., 'E001')
27
+ */
28
+
29
+ /**
30
+ * Unified error collector for Stone compiler phases
31
+ */
32
+ export class ErrorCollector {
33
+ /**
34
+ * Create an error collector
35
+ * @param {Object} options - Configuration options
36
+ * @param {string} [options.phase='unknown'] - Compiler phase name
37
+ */
38
+ constructor(options = {}) {
39
+ this.phase = options.phase || 'unknown';
40
+ this.errors = [];
41
+ this.warnings = [];
42
+ }
43
+
44
+ /**
45
+ * Add an error
46
+ * @param {string} message - Error message
47
+ * @param {Object} [location] - Source location
48
+ * @param {string} [code] - Optional error code
49
+ */
50
+ addError(message, location = null, code = null) {
51
+ this.errors.push({
52
+ severity: 'error',
53
+ message,
54
+ location,
55
+ phase: this.phase,
56
+ code,
57
+ });
58
+ }
59
+
60
+ /**
61
+ * Add a warning
62
+ * @param {string} message - Warning message
63
+ * @param {Object} [location] - Source location
64
+ * @param {string} [code] - Optional warning code
65
+ */
66
+ addWarning(message, location = null, code = null) {
67
+ this.warnings.push({
68
+ severity: 'warning',
69
+ message,
70
+ location,
71
+ phase: this.phase,
72
+ code,
73
+ });
74
+ }
75
+
76
+ /**
77
+ * Check if there are any errors
78
+ * @returns {boolean} True if there are errors
79
+ */
80
+ hasErrors() {
81
+ return this.errors.length > 0;
82
+ }
83
+
84
+ /**
85
+ * Check if there are any warnings
86
+ * @returns {boolean} True if there are warnings
87
+ */
88
+ hasWarnings() {
89
+ return this.warnings.length > 0;
90
+ }
91
+
92
+ /**
93
+ * Get all errors and warnings combined
94
+ * @returns {CollectedError[]} All collected errors and warnings
95
+ */
96
+ getAll() {
97
+ return [...this.errors, ...this.warnings];
98
+ }
99
+
100
+ /**
101
+ * Merge errors and warnings from another collector
102
+ * @param {ErrorCollector} other - Another error collector
103
+ */
104
+ merge(other) {
105
+ if (other) {
106
+ this.errors.push(...other.errors);
107
+ this.warnings.push(...other.warnings);
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Clear all errors and warnings
113
+ */
114
+ clear() {
115
+ this.errors = [];
116
+ this.warnings = [];
117
+ }
118
+
119
+ /**
120
+ * Format a single error for display
121
+ * @param {CollectedError} err - Error to format
122
+ * @returns {string} Formatted error string
123
+ */
124
+ formatError(err) {
125
+ const loc = err.location
126
+ ? `[${err.location.line}:${err.location.column}] `
127
+ : '';
128
+ const code = err.code ? `${err.code}: ` : '';
129
+ return `${err.phase}: ${loc}${code}${err.message}`;
130
+ }
131
+
132
+ /**
133
+ * Format all errors for display
134
+ * @param {Object} options - Format options
135
+ * @param {boolean} [options.includeWarnings=true] - Include warnings in output
136
+ * @returns {string} Formatted error messages
137
+ */
138
+ format(options = {}) {
139
+ const { includeWarnings = true } = options;
140
+
141
+ const items = includeWarnings ? this.getAll() : this.errors;
142
+ return items.map(err => this.formatError(err)).join('\n');
143
+ }
144
+
145
+ /**
146
+ * Format errors as structured objects (for JSON output)
147
+ * @returns {Object} Structured error output
148
+ */
149
+ toJSON() {
150
+ return {
151
+ hasErrors: this.hasErrors(),
152
+ hasWarnings: this.hasWarnings(),
153
+ errors: this.errors,
154
+ warnings: this.warnings,
155
+ };
156
+ }
157
+
158
+ /**
159
+ * Create a child collector for a sub-phase
160
+ * @param {string} subPhase - Sub-phase name (will be appended to current phase)
161
+ * @returns {ErrorCollector} New error collector
162
+ */
163
+ createChild(subPhase) {
164
+ return new ErrorCollector({
165
+ phase: `${this.phase}/${subPhase}`,
166
+ });
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Standard error codes for Stone compiler errors
172
+ */
173
+ export const ErrorCodes = {
174
+ // Parse errors (P001-P099)
175
+ PARSE_UNEXPECTED_TOKEN: 'P001',
176
+ PARSE_UNTERMINATED_STRING: 'P002',
177
+ PARSE_INVALID_NUMBER: 'P003',
178
+
179
+ // Type errors (T001-T099)
180
+ TYPE_MISMATCH: 'T001',
181
+ TYPE_UNDEFINED_VARIABLE: 'T002',
182
+ TYPE_UNDEFINED_FUNCTION: 'T003',
183
+ TYPE_WRONG_ARITY: 'T004',
184
+ TYPE_NO_MATCHING_OVERLOAD: 'T005',
185
+
186
+ // Compile errors (C001-C099)
187
+ COMPILE_UNDEFINED_VARIABLE: 'C001',
188
+ COMPILE_UNKNOWN_OPERATOR: 'C002',
189
+ COMPILE_INVALID_ASSIGNMENT: 'C003',
190
+
191
+ // Runtime errors (R001-R099)
192
+ RUNTIME_DIVISION_BY_ZERO: 'R001',
193
+ RUNTIME_INDEX_OUT_OF_BOUNDS: 'R002',
194
+ RUNTIME_STACK_OVERFLOW: 'R003',
195
+ RUNTIME_ITERATION_LIMIT: 'R004',
196
+ RUNTIME_TIMEOUT: 'R005',
197
+ RUNTIME_UNDEFINED_PROPERTY: 'R006',
198
+ RUNTIME_UNDEFINED_METHOD: 'R007',
199
+ RUNTIME_NULL_REFERENCE: 'R008',
200
+ RUNTIME_NOT_CALLABLE: 'R009',
201
+ };
202
+
203
+ /**
204
+ * Helper to get a human-readable type name for Stone values
205
+ * @param {any} value - The value to get the type of
206
+ * @returns {string} Human-readable type name
207
+ */
208
+ export function getStoneTypeName(value) {
209
+ if (value === null) return 'null';
210
+ if (value === undefined) return 'undefined';
211
+
212
+ // Check for Stone-specific types first
213
+ if (value._type === 'StoneArray') {
214
+ const elemType = value.elementType || 'num';
215
+ const rank = value.rank || 1;
216
+ return `array<${elemType}, ${rank}>`;
217
+ }
218
+ if (value._type === 'Complex' || (value.real !== undefined && value.imag !== undefined && typeof value.real === 'number')) {
219
+ return 'complex';
220
+ }
221
+
222
+ // Standard JavaScript types
223
+ if (Array.isArray(value)) {
224
+ if (value.length === 0) return 'array (empty)';
225
+ const firstType = typeof value[0];
226
+ return `array<${firstType}>`;
227
+ }
228
+ if (typeof value === 'object') {
229
+ // Check if it's a plain object (record)
230
+ const keys = Object.keys(value);
231
+ if (keys.length === 0) return 'object (empty)';
232
+ if (keys.length <= 3) {
233
+ return `{ ${keys.join(', ')} }`;
234
+ }
235
+ return `{ ${keys.slice(0, 3).join(', ')}, ... }`;
236
+ }
237
+ if (typeof value === 'function') {
238
+ return value.name ? `function ${value.name}` : 'function';
239
+ }
240
+
241
+ return typeof value;
242
+ }
243
+
244
+ export default ErrorCollector;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Frontend Utils - Barrel exports
3
+ */
4
+ export { ModuleResolver, createModuleResolver } from './moduleResolver.js';
5
+ export { ErrorCollector, ErrorCodes } from './errorCollector.js';