@katnip-org/compiler 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.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +42 -0
- package/build/cli.d.ts +2 -0
- package/build/cli.js +141 -0
- package/build/cli.js.map +1 -0
- package/build/codegen/SB3Generator.d.ts +1 -0
- package/build/codegen/SB3Generator.js +2 -0
- package/build/codegen/SB3Generator.js.map +1 -0
- package/build/index.d.ts +3 -0
- package/build/index.js +26 -0
- package/build/index.js.map +1 -0
- package/build/ir/IRGenerator.d.ts +1 -0
- package/build/ir/IRGenerator.js +2 -0
- package/build/ir/IRGenerator.js.map +1 -0
- package/build/ir/IRNode.d.ts +1 -0
- package/build/ir/IRNode.js +2 -0
- package/build/ir/IRNode.js.map +1 -0
- package/build/lexer/Lexer.d.ts +69 -0
- package/build/lexer/Lexer.js +441 -0
- package/build/lexer/Lexer.js.map +1 -0
- package/build/lexer/LexerState.d.ts +14 -0
- package/build/lexer/LexerState.js +16 -0
- package/build/lexer/LexerState.js.map +1 -0
- package/build/lexer/Token.d.ts +105 -0
- package/build/lexer/Token.js +114 -0
- package/build/lexer/Token.js.map +1 -0
- package/build/parser/AST-nodes.d.ts +230 -0
- package/build/parser/AST-nodes.js +10 -0
- package/build/parser/AST-nodes.js.map +1 -0
- package/build/parser/BindingPowerTable.d.ts +8 -0
- package/build/parser/BindingPowerTable.js +37 -0
- package/build/parser/BindingPowerTable.js.map +1 -0
- package/build/parser/Parser.d.ts +208 -0
- package/build/parser/Parser.js +1215 -0
- package/build/parser/Parser.js.map +1 -0
- package/build/semantic/InternalTypes.d.ts +43 -0
- package/build/semantic/InternalTypes.js +95 -0
- package/build/semantic/InternalTypes.js.map +1 -0
- package/build/semantic/SemanticAnalyzer.d.ts +109 -0
- package/build/semantic/SemanticAnalyzer.js +998 -0
- package/build/semantic/SemanticAnalyzer.js.map +1 -0
- package/build/semantic/StdlibLoader.d.ts +18 -0
- package/build/semantic/StdlibLoader.js +42 -0
- package/build/semantic/StdlibLoader.js.map +1 -0
- package/build/semantic/SymbolTable.d.ts +64 -0
- package/build/semantic/SymbolTable.js +28 -0
- package/build/semantic/SymbolTable.js.map +1 -0
- package/build/utils/ErrorReporter.d.ts +29 -0
- package/build/utils/ErrorReporter.js +84 -0
- package/build/utils/ErrorReporter.js.map +1 -0
- package/build/utils/Logger.d.ts +32 -0
- package/build/utils/Logger.js +62 -0
- package/build/utils/Logger.js.map +1 -0
- package/build/utils/colors.d.ts +8 -0
- package/build/utils/colors.js +14 -0
- package/build/utils/colors.js.map +1 -0
- package/package.json +58 -0
- package/stdlib/clone.knip +5 -0
- package/stdlib/console.knip +6 -0
- package/stdlib/control.knip +3 -0
- package/stdlib/dict.knip +7 -0
- package/stdlib/events.knip +3 -0
- package/stdlib/list.knip +10 -0
- package/stdlib/looks.knip +4 -0
- package/stdlib/math.knip +7 -0
- package/stdlib/motion.knip +25 -0
- package/stdlib/pen.knip +14 -0
- package/stdlib/prelude.knip +25 -0
- package/stdlib/str.knip +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [2026] [Benjamin Clark]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @katnip-org/compiler
|
|
2
|
+
|
|
3
|
+
Compiler and type-checker for the **Katnip** language (`.knip`), targeting Scratch SB3.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install -g @katnip-org/compiler
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or run without installing:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npx @katnip-org/compiler check path/to/file.knip
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Type-check a file (reports lexer, parser, and semantic errors):
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
katnip check path/to/file.knip
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Other commands:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
katnip parse <source> <output.json> # write the AST as JSON
|
|
29
|
+
katnip tokenize <source> <output.json> # write the token stream as JSON
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Library
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { checkSource } from "@katnip-org/compiler";
|
|
36
|
+
|
|
37
|
+
const errors = await checkSource(sourceText); // readonly KatnipError[]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
Apache-2.0
|
package/build/cli.d.ts
ADDED
package/build/cli.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as fs from "fs/promises";
|
|
3
|
+
import { Lexer } from "./lexer/Lexer.js";
|
|
4
|
+
import { Parser } from "./parser/Parser.js";
|
|
5
|
+
import { SemanticAnalyzer } from "./semantic/SemanticAnalyzer.js";
|
|
6
|
+
import { loadStdlibModules } from "./semantic/StdlibLoader.js";
|
|
7
|
+
import { ErrorReporter } from "./utils/ErrorReporter.js";
|
|
8
|
+
import { Logger } from "./utils/Logger.js";
|
|
9
|
+
import yargs from 'yargs';
|
|
10
|
+
import { hideBin } from 'yargs/helpers';
|
|
11
|
+
yargs(hideBin(process.argv))
|
|
12
|
+
.command('tokenize <source> <output> [logger]', 'Tokenize a file', (yargs) => {
|
|
13
|
+
yargs.positional('source', {
|
|
14
|
+
describe: 'The path to the source file',
|
|
15
|
+
type: 'string',
|
|
16
|
+
demandOption: true,
|
|
17
|
+
}).positional('output', {
|
|
18
|
+
describe: 'The path to the output file for tokens',
|
|
19
|
+
type: 'string',
|
|
20
|
+
demandOption: true,
|
|
21
|
+
}).positional('logger', {
|
|
22
|
+
describe: 'Boolean for whether to keep a log or not',
|
|
23
|
+
type: 'boolean',
|
|
24
|
+
});
|
|
25
|
+
}, (argv) => {
|
|
26
|
+
// Read in the source file
|
|
27
|
+
fs.readFile(argv.source, { encoding: 'utf-8' })
|
|
28
|
+
.then((fileContent) => {
|
|
29
|
+
// Create an error reporter instance
|
|
30
|
+
const reporter = new ErrorReporter(fileContent, true);
|
|
31
|
+
const logger = new Logger(true);
|
|
32
|
+
if (argv.logger === true)
|
|
33
|
+
logger.enable();
|
|
34
|
+
else
|
|
35
|
+
logger.disable();
|
|
36
|
+
const lexer = new Lexer(reporter, logger);
|
|
37
|
+
const tokens = lexer.tokenize(fileContent);
|
|
38
|
+
// Create temp file with tokens for debugging
|
|
39
|
+
const tempFilePath = argv.output; // `${outputPath}.tokens.json`;
|
|
40
|
+
fs.writeFile(tempFilePath, JSON.stringify(tokens, null, 2));
|
|
41
|
+
})
|
|
42
|
+
.catch((err) => {
|
|
43
|
+
console.error('Error reading file:', err);
|
|
44
|
+
});
|
|
45
|
+
})
|
|
46
|
+
.command('parse <source> <output> [logger]', 'Tokenize and parse a file', (yargs) => {
|
|
47
|
+
yargs.positional('source', {
|
|
48
|
+
describe: 'The path to the source file',
|
|
49
|
+
type: 'string',
|
|
50
|
+
demandOption: true,
|
|
51
|
+
}).positional('output', {
|
|
52
|
+
describe: 'The path to the output file for AST',
|
|
53
|
+
type: 'string',
|
|
54
|
+
demandOption: true,
|
|
55
|
+
}).positional('logger', {
|
|
56
|
+
describe: 'Boolean for whether to keep a log or not',
|
|
57
|
+
type: 'boolean',
|
|
58
|
+
});
|
|
59
|
+
}, (argv) => {
|
|
60
|
+
fs.readFile(argv.source, { encoding: 'utf-8' })
|
|
61
|
+
.then((fileContent) => {
|
|
62
|
+
const reporter = new ErrorReporter(fileContent, true);
|
|
63
|
+
const logger = new Logger(true);
|
|
64
|
+
if (argv.logger === true)
|
|
65
|
+
logger.enable();
|
|
66
|
+
else
|
|
67
|
+
logger.disable();
|
|
68
|
+
const lexer = new Lexer(reporter, logger);
|
|
69
|
+
const tokens = lexer.tokenize(fileContent);
|
|
70
|
+
// If lexer produced errors, print and abort
|
|
71
|
+
if (reporter.hasErrors()) {
|
|
72
|
+
reporter.print();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const parser = new Parser(reporter, logger);
|
|
76
|
+
const ast = parser.parse(tokens);
|
|
77
|
+
if (reporter.hasErrors()) {
|
|
78
|
+
reporter.print();
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
fs.writeFile(argv.output, JSON.stringify(ast, null, 2));
|
|
82
|
+
})
|
|
83
|
+
.catch((err) => {
|
|
84
|
+
console.error('Error reading file:', err);
|
|
85
|
+
});
|
|
86
|
+
})
|
|
87
|
+
.command('check <source> [logger]', 'Lex, parse, and run semantic analysis on a file', (yargs) => {
|
|
88
|
+
yargs.positional('source', {
|
|
89
|
+
describe: 'The path to the source file',
|
|
90
|
+
type: 'string',
|
|
91
|
+
demandOption: true,
|
|
92
|
+
}).positional('logger', {
|
|
93
|
+
describe: 'Boolean for whether to keep a log or not',
|
|
94
|
+
type: 'boolean',
|
|
95
|
+
});
|
|
96
|
+
}, (argv) => {
|
|
97
|
+
fs.readFile(argv.source, { encoding: 'utf-8' })
|
|
98
|
+
.then(async (fileContent) => {
|
|
99
|
+
const reporter = new ErrorReporter(fileContent, true);
|
|
100
|
+
const logger = new Logger(true);
|
|
101
|
+
if (argv.logger === true)
|
|
102
|
+
logger.enable();
|
|
103
|
+
else
|
|
104
|
+
logger.disable();
|
|
105
|
+
const lexer = new Lexer(reporter, logger);
|
|
106
|
+
const tokens = lexer.tokenize(fileContent);
|
|
107
|
+
if (reporter.hasErrors()) {
|
|
108
|
+
reporter.print();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const parser = new Parser(reporter, logger);
|
|
112
|
+
const ast = parser.parse(tokens);
|
|
113
|
+
if (!ast) {
|
|
114
|
+
reporter.print();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const analyzer = new SemanticAnalyzer(reporter, logger);
|
|
118
|
+
try {
|
|
119
|
+
analyzer.loadStdlib(await loadStdlibModules(logger));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
reporter.print(); // surface any parse errors we already collected
|
|
123
|
+
return; // the stdlib loader already printed its own errors
|
|
124
|
+
}
|
|
125
|
+
// Analyze even when parsing reported errors: the parser recovers into a
|
|
126
|
+
// usable AST (error nodes are no-ops in the analyzer), so semantic errors
|
|
127
|
+
// surface alongside syntax errors instead of being hidden behind the first.
|
|
128
|
+
analyzer.analyze(ast);
|
|
129
|
+
if (reporter.hasErrors()) {
|
|
130
|
+
reporter.print();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
console.log('No semantic errors found.');
|
|
134
|
+
})
|
|
135
|
+
.catch((err) => {
|
|
136
|
+
console.error('Error reading file:', err);
|
|
137
|
+
});
|
|
138
|
+
})
|
|
139
|
+
.help()
|
|
140
|
+
.argv;
|
|
141
|
+
//# sourceMappingURL=cli.js.map
|
package/build/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM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,CAAC,KAAK,EAAE,CAAC;YACjB,OAAO;QACX,CAAC;QAED,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;IAC7C,CAAC,CAAC;SACD,KAAK,CAAC,CAAC,GAAQ,EAAE,EAAE;QAChB,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,GAAG,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;AACX,CAAC,CAAC;KACD,IAAI,EAAE;KACN,IAAI,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SB3Generator.js","sourceRoot":"","sources":["../../src/codegen/SB3Generator.ts"],"names":[],"mappings":""}
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Lexer } from "./lexer/Lexer.js";
|
|
2
|
+
import { Parser } from "./parser/Parser.js";
|
|
3
|
+
import { SemanticAnalyzer } from "./semantic/SemanticAnalyzer.js";
|
|
4
|
+
import { loadStdlibModules } from "./semantic/StdlibLoader.js";
|
|
5
|
+
import { ErrorReporter } from "./utils/ErrorReporter.js";
|
|
6
|
+
import { Logger } from "./utils/Logger.js";
|
|
7
|
+
let stdlibCache = null;
|
|
8
|
+
export async function checkSource(source) {
|
|
9
|
+
const reporter = new ErrorReporter(source);
|
|
10
|
+
const logger = new Logger(); // disabled by default
|
|
11
|
+
const tokens = new Lexer(reporter, logger).tokenize(source);
|
|
12
|
+
const ast = new Parser(reporter, logger).parse(tokens);
|
|
13
|
+
if (!ast)
|
|
14
|
+
return reporter.getErrors();
|
|
15
|
+
const analyzer = new SemanticAnalyzer(reporter, logger);
|
|
16
|
+
try {
|
|
17
|
+
stdlibCache ??= await loadStdlibModules(logger);
|
|
18
|
+
analyzer.loadStdlib(stdlibCache);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return reporter.getErrors();
|
|
22
|
+
}
|
|
23
|
+
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,CAAC,SAAS,EAAE,CAAC;IAChC,CAAC;IACD,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACtB,OAAO,QAAQ,CAAC,SAAS,EAAE,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IRGenerator.js","sourceRoot":"","sources":["../../src/ir/IRGenerator.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IRNode.js","sourceRoot":"","sources":["../../src/ir/IRNode.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Contains the main lexer class for the Katnip compiler.
|
|
3
|
+
*/
|
|
4
|
+
import type { Token } from "./Token.js";
|
|
5
|
+
import { ErrorReporter } from "../utils/ErrorReporter.js";
|
|
6
|
+
import { Logger } from "../utils/Logger.js";
|
|
7
|
+
export declare class Lexer {
|
|
8
|
+
private reporter;
|
|
9
|
+
private logger;
|
|
10
|
+
private src;
|
|
11
|
+
private position;
|
|
12
|
+
private line;
|
|
13
|
+
private lineStart;
|
|
14
|
+
private col;
|
|
15
|
+
private colStart;
|
|
16
|
+
private currentState;
|
|
17
|
+
private buffer;
|
|
18
|
+
private stringQuote;
|
|
19
|
+
private isInterpolatedString;
|
|
20
|
+
private inInterpolatedExpression;
|
|
21
|
+
private interpolatedBraceDepth;
|
|
22
|
+
private commentType;
|
|
23
|
+
private operatorNode;
|
|
24
|
+
private lastValidOperatorNode;
|
|
25
|
+
private tokens;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new Lexer instance.
|
|
28
|
+
*/
|
|
29
|
+
constructor(reporter: ErrorReporter, logger?: Logger);
|
|
30
|
+
/**
|
|
31
|
+
* Tokenizes the provided source code into an array of tokens.
|
|
32
|
+
*
|
|
33
|
+
* @param src Source code to tokenize.
|
|
34
|
+
* @returns The tokens extracted from the source code.
|
|
35
|
+
*/
|
|
36
|
+
tokenize(src: string): Token[];
|
|
37
|
+
/**
|
|
38
|
+
* Emits a token based on the current buffer and resets the buffer.
|
|
39
|
+
*
|
|
40
|
+
* @param type The type of token to emit.
|
|
41
|
+
*/
|
|
42
|
+
private emit;
|
|
43
|
+
/**
|
|
44
|
+
* Peeks at the next character in the source code without consuming it.
|
|
45
|
+
*
|
|
46
|
+
* @param distance The number of characters to look ahead (default is 0).
|
|
47
|
+
* @returns The character at the specified distance or null if out of bounds.
|
|
48
|
+
*/
|
|
49
|
+
private peek;
|
|
50
|
+
/**
|
|
51
|
+
* Advances the lexer position by one character and updates line/column tracking.
|
|
52
|
+
*
|
|
53
|
+
* @returns The character that was advanced or null if at the end of input.
|
|
54
|
+
*/
|
|
55
|
+
private advance;
|
|
56
|
+
/**
|
|
57
|
+
* Processes a single character from the source code and updates the lexer state.
|
|
58
|
+
*
|
|
59
|
+
* @param char The character to process.
|
|
60
|
+
*/
|
|
61
|
+
private processChar;
|
|
62
|
+
private processStartLike;
|
|
63
|
+
/**
|
|
64
|
+
* Processes the current state of the lexer based on the character and source code.
|
|
65
|
+
*
|
|
66
|
+
* @param char The current character being processed.
|
|
67
|
+
*/
|
|
68
|
+
private processState;
|
|
69
|
+
}
|