Skip to content

Commit 2c90e8a

Browse files
committed
1337
1 parent a273599 commit 2c90e8a

32 files changed

Lines changed: 96 additions & 965 deletions

index.html

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@ <h2>RadonCoding</h2>
4848
</a>
4949
</div>
5050
</div>
51-
<div class="system">
52-
<div class="window">
53-
<header id="terminal-title"></header>
54-
<div id="terminal-io" class="io"></div>
55-
</div>
56-
<div class="window">
51+
<div class="container">
52+
<div class="compiler">
5753
<header>
5854
Compiler
5955
<a id="compile-button"><i class="fa-solid fa-gears"></i></a>

src/compiler/ast/Parser.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class Parser {
7777
if (!token || token.type !== expected) {
7878
throw new InvalidSyntaxError(
7979
`Expected ${expected} but got ${token ? token.type : "EOF"}`,
80-
token?.offset || 0
80+
token?.start || 0
8181
);
8282
}
8383
this.#buffer.shift();
@@ -99,17 +99,17 @@ export class Parser {
9999
} else if (type in this.#types) {
100100
return this.#types[type];
101101
}
102-
throw new InvalidSyntaxError(`Invalid type "${type}"`, token.offset);
102+
throw new InvalidSyntaxError(`Invalid type "${type}"`, token.start);
103103
}
104104

105105
#parseIdentifier() {
106106
const name = this.#consume(TokenTypes.IDENTIFIER);
107-
return new Identifier(name.value!, name.offset);
107+
return new Identifier(name.value!, name.start);
108108
}
109109

110110
#parseBoolLiteral() {
111111
const literal = this.#consume(TokenTypes.BOOL_LITERAL);
112-
return new BoolLiteral(literal.value === "true", literal.offset);
112+
return new BoolLiteral(literal.value === "true", literal.start);
113113
}
114114

115115
#parseNumberLiteral() {
@@ -124,27 +124,27 @@ export class Parser {
124124
if (!isNumericSuffix(token.value)) {
125125
throw new InvalidSyntaxError(
126126
`Invalid suffix "${token.value}"`,
127-
token.offset
127+
token.start
128128
);
129129
}
130130

131131
suffix = token.value;
132132
}
133133
}
134-
return new NumberLiteral(BigInt(literal.value!), suffix, literal.offset);
134+
return new NumberLiteral(BigInt(literal.value!), suffix, literal.start);
135135
}
136136

137137
#parseStringLiteral() {
138138
const literal = this.#consume(TokenTypes.STRING_LITERAL);
139-
return new StringLiteral(literal.value!, literal.offset);
139+
return new StringLiteral(literal.value!, literal.start);
140140
}
141141

142142
#parseStringInterpolated(): InterpolatedString {
143143
const token = this.#consume(TokenTypes.STRING_INTERPOLATED);
144144
const text = token.value!.slice(2, -1);
145145
const parts = [];
146146

147-
const start = token.offset + 2;
147+
const start = token.start + 2;
148148

149149
let index = 0;
150150

@@ -237,7 +237,7 @@ export class Parser {
237237
parts.push(
238238
new StringLiteral(
239239
"}".repeat(count / 2),
240-
token.offset + braceStart + 1
240+
token.start + braceStart + 1
241241
)
242242
);
243243
continue;
@@ -246,7 +246,7 @@ export class Parser {
246246
// Odd number of closing braces with no matching opening
247247
throw new InvalidSyntaxError(
248248
"Unmatched closing brace",
249-
token.offset + braceStart + 1
249+
token.start + braceStart + 1
250250
);
251251
}
252252

@@ -267,13 +267,13 @@ export class Parser {
267267
);
268268
}
269269
}
270-
return new InterpolatedString(parts, token.offset);
270+
return new InterpolatedString(parts, token.start);
271271
}
272272

273273
#parseParameter() {
274274
const type = this.#type();
275275
const name = this.#consume(TokenTypes.IDENTIFIER);
276-
return new Parameter(type, name.value!, name.offset);
276+
return new Parameter(type, name.value!, name.start);
277277
}
278278

279279
#parseParameters() {
@@ -356,8 +356,8 @@ export class Parser {
356356
}
357357

358358
throw new InvalidSyntaxError(
359-
`Unexpected token ${next.type} at offset ${next.offset}`,
360-
next.offset
359+
`Unexpected token ${next.type} at offset ${next.start}`,
360+
next.start
361361
);
362362
}
363363

@@ -370,7 +370,7 @@ export class Parser {
370370
return new FixExpression(
371371
FIX_TO_BINARY[next.type],
372372
expression,
373-
next.offset,
373+
next.start,
374374
true
375375
);
376376
}
@@ -387,7 +387,7 @@ export class Parser {
387387
expression = new FixExpression(
388388
FIX_TO_BINARY[next.type],
389389
expression,
390-
next.offset,
390+
next.start,
391391
false
392392
);
393393
}
@@ -400,7 +400,7 @@ export class Parser {
400400
if (next && isUnaryOperator(next.type)) {
401401
this.#consume(next.type);
402402
const operand = this.#parseUnaryExpression();
403-
return new UnaryExpression(next.type, operand, next.offset);
403+
return new UnaryExpression(next.type, operand, next.start);
404404
}
405405
return this.#parsePrefixExpression();
406406
}
@@ -420,7 +420,7 @@ export class Parser {
420420
const right = this.#parseBinaryExpression(
421421
info.associativity === "left" ? info.precedence + 1 : info.precedence
422422
);
423-
left = new BinaryExpression(next.type, left, right, next.offset);
423+
left = new BinaryExpression(next.type, left, right, next.start);
424424
next = this.#peek();
425425
}
426426
return left;
@@ -440,7 +440,7 @@ export class Parser {
440440
condition,
441441
consequent,
442442
alternate,
443-
token.offset
443+
token.start
444444
);
445445
}
446446
return condition;
@@ -454,7 +454,7 @@ export class Parser {
454454
const type = this.#type();
455455
const name = this.#consume(TokenTypes.IDENTIFIER);
456456
this.#consume(TokenTypes.SEMICOLON);
457-
return new FieldDeclaration(type, name.value!, name.offset);
457+
return new FieldDeclaration(type, name.value!, name.start);
458458
}
459459

460460
#parseFieldAssignment() {
@@ -468,7 +468,7 @@ export class Parser {
468468
parent.value!,
469469
member.value!,
470470
expression,
471-
member.offset
471+
member.start
472472
);
473473
}
474474

@@ -485,15 +485,15 @@ export class Parser {
485485

486486
this.#consume(TokenTypes.SEMICOLON);
487487

488-
return new VariableDeclaration(type, name.value!, expression, name.offset);
488+
return new VariableDeclaration(type, name.value!, expression, name.start);
489489
}
490490

491491
#parseVariableAssignment() {
492492
const name = this.#consume(TokenTypes.IDENTIFIER);
493493
this.#consume(TokenTypes.EQUAL);
494494
const expression = this.parseExpression();
495495
this.#consume(TokenTypes.SEMICOLON);
496-
return new VariableAssignment(name.value!, expression, name.offset);
496+
return new VariableAssignment(name.value!, expression, name.start);
497497
}
498498

499499
#parseCompoundVariableAssignment(): AnyAstNode {
@@ -503,7 +503,7 @@ export class Parser {
503503
if (!token || !isCompoundOperator(token.type)) {
504504
throw new InvalidSyntaxError(
505505
"Expected compound assignment operator",
506-
token?.offset || left.offset
506+
token?.start || left.offset
507507
);
508508
}
509509

@@ -515,7 +515,7 @@ export class Parser {
515515
COMPOUND_TO_BINARY[token.type],
516516
left,
517517
right,
518-
token.offset
518+
token.start
519519
);
520520
return new VariableAssignment(left.name, expression, left.offset);
521521
}
@@ -527,7 +527,7 @@ export class Parser {
527527
if (!token || !isCompoundOperator(token.type)) {
528528
throw new InvalidSyntaxError(
529529
"Expected compound assignment operator",
530-
token?.offset || left.offset
530+
token?.start || left.offset
531531
);
532532
}
533533

@@ -539,7 +539,7 @@ export class Parser {
539539
COMPOUND_TO_BINARY[token.type],
540540
left,
541541
right,
542-
token.offset
542+
token.start
543543
);
544544
return new FieldAssignment(
545545
left.object,
@@ -591,7 +591,7 @@ export class Parser {
591591
const name = this.#value(TokenTypes.IDENTIFIER);
592592
const fields = this.#parseFields();
593593
this.#types[name] = { kind: TypeKinds.STRUCT, name: name };
594-
return new StructDeclaration(name, fields, struct.offset);
594+
return new StructDeclaration(name, fields, struct.start);
595595
}
596596

597597
#parseFunction() {
@@ -614,7 +614,7 @@ export class Parser {
614614
name,
615615
params,
616616
body,
617-
func.offset
617+
func.start
618618
);
619619
}
620620

@@ -629,7 +629,7 @@ export class Parser {
629629

630630
this.#consume(TokenTypes.SEMICOLON);
631631

632-
return new ReturnStatement(value, token.offset);
632+
return new ReturnStatement(value, token.start);
633633
}
634634

635635
#parseIfStatement(): IfStatement {
@@ -648,7 +648,7 @@ export class Parser {
648648
alternate = this.#parseBlock();
649649
}
650650
}
651-
return new IfStatement(condition, consequent, alternate, token.offset);
651+
return new IfStatement(condition, consequent, alternate, token.start);
652652
}
653653

654654
#parseForLoop() {
@@ -677,7 +677,7 @@ export class Parser {
677677

678678
const body = this.#parseBlock();
679679

680-
return new LoopStatement(declaration, condition, step, body, token.offset);
680+
return new LoopStatement(declaration, condition, step, body, token.start);
681681
}
682682

683683
#parseStatement(): AnyAstNode {

src/compiler/ast/nodes/IfStatement.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ export class IfStatement extends AstNode {
4242
} else {
4343
branches.push([this.alternate]);
4444
}
45-
} else {
46-
branches.push([]);
4745
}
4846
return branches;
4947
}

src/compiler/errors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export class LanguageError extends Error {
2-
index: number;
2+
offset: number;
33

4-
constructor(message: string, index: number) {
4+
constructor(message: string, offset: number) {
55
super(message);
66

7-
this.index = index;
7+
this.offset = offset;
88
}
99
}
1010
export class InvalidSyntaxError extends LanguageError {}

src/compiler/index.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async function compile() {
7575
}
7676

7777
if (err instanceof LanguageError) {
78-
showErrorTooltip(inputElement, err.message, err.index);
78+
showErrorTooltip(inputElement, err.message, err.offset);
7979
}
8080
}
8181
}
@@ -89,35 +89,6 @@ document.addEventListener("DOMContentLoaded", () => {
8989
highlight(inputElement);
9090

9191
inputElement.addEventListener("input", () => highlight(inputElement));
92-
inputElement.addEventListener("keydown", (e) => {
93-
if (e.key === "Enter") {
94-
e.preventDefault();
95-
const sel = window.getSelection();
96-
97-
if (sel) {
98-
const range = sel.getRangeAt(0);
99-
const newLine = document.createTextNode("\n");
100-
range.insertNode(newLine);
101-
range.setStartAfter(newLine);
102-
range.collapse(true);
103-
sel.removeAllRanges();
104-
sel.addRange(range);
105-
}
106-
} else if (e.key === "Tab") {
107-
e.preventDefault();
108-
const sel = window.getSelection();
109-
110-
if (sel) {
111-
const range = sel.getRangeAt(0);
112-
const tab = document.createTextNode(" ".repeat(4));
113-
range.insertNode(tab);
114-
range.setStartAfter(tab);
115-
range.collapse(true);
116-
sel.removeAllRanges();
117-
sel.addRange(range);
118-
}
119-
}
120-
});
12192

12293
let isCollapsed = false;
12394
const toggle = document.getElementById("toggle-output")!;

0 commit comments

Comments
 (0)