Skip to content

Commit 6bd72e7

Browse files
committed
Fixes
1 parent 59eb9d6 commit 6bd72e7

9 files changed

Lines changed: 161 additions & 57 deletions

File tree

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h2>RadonCoding</h2>
3030
*
3131
* - Languages I use often:
3232
* C/C++, C#, Rust, Java,
33-
* JavaScript, Python, Go
33+
* TypeScript, Python, Go
3434
*
3535
*/</pre
3636
>

src/assets/example.rn

Lines changed: 103 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,130 @@ struct Point {
33
long y;
44
}
55

6-
fn distance_squared(Point p): long {
7-
return p.x * p.x + p.y * p.y;
6+
struct Rectangle {
7+
Point top_left;
8+
Point bottom_right;
9+
}
10+
11+
fn distance_squared(Point p1, Point p2): long {
12+
long dx = p1.x - p2.x;
13+
long dy = p1.y - p2.y;
14+
return dx * dx + dy * dy;
815
}
916

1017
fn scale_point(Point p, long factor) {
1118
p.x *= factor;
1219
p.y *= factor;
1320
}
1421

22+
fn factorial(long n): long {
23+
if n <= 1i64 {
24+
return 1i64;
25+
} else {
26+
return n * factorial(n - 1i64);
27+
}
28+
}
29+
1530
fn power(long base, long exp): long {
1631
long result = 1i64;
17-
1832
for (long i = 0i64; i < exp; i++) {
1933
result *= base;
2034
}
2135
return result;
2236
}
2337

24-
fn classify(long d): string {
38+
fn classify_distance(long d): string {
2539
return d == 0i64 ? "origin"
26-
: d < 25i64 ? "near"
27-
: d < 100i64 ? "medium"
28-
: "far";
40+
: d < 25i64 ? "very close"
41+
: d < 100i64 ? "close"
42+
: d < 400i64 ? "medium"
43+
: d < 1000i64 ? "far"
44+
: "very far";
2945
}
3046

31-
fn main() {
32-
Point p;
33-
p.x = 2i64;
34-
p.y = 3i64;
35-
36-
print($"Original point: ({p.x}, {p.y})");
47+
fn compare_numbers(long a, long b): string {
48+
if a == b return "equal";
49+
if a != b && a > b return "a greater";
50+
if a < b return "a smaller";
51+
if a >= b return "a greater or equal";
52+
if a <= b return "a smaller or equal";
53+
return "unknown";
54+
}
3755

38-
scale_point(p, 3i64);
39-
print($"Scaled point: ({p.x}, {p.y})");
56+
fn accumulate_ops(int start): int {
57+
int result = start;
58+
result += 10;
59+
result -= 3;
60+
result *= 2;
61+
result /= 4;
62+
result %= 7;
63+
result **= 2;
64+
return result;
65+
}
4066

41-
long d = distance_squared(p);
42-
print($"Distance squared = {d} -> {classify(d)}");
67+
fn increment_demo(): long {
68+
long counter = 0i64;
69+
counter++;
70+
++counter;
71+
counter--;
72+
--counter;
73+
return counter;
74+
}
4375

44-
long base = 2i64;
45-
long exp = 6i64;
46-
long pow = power(base, exp);
47-
print($"Power: {base} ** {exp} = {pow}");
76+
fn area_calculator(Rectangle rect): long {
77+
Point tl = rect.top_left;
78+
Point br = rect.bottom_right;
79+
long width = br.x - tl.x;
80+
long height = br.y - tl.y;
81+
return width * height;
82+
}
4883

49-
int acc = 5;
50-
acc += 3;
51-
acc *= 2;
52-
acc -= 4;
53-
print($"Accumulator result = {acc}");
84+
fn main() {
85+
Point p1;
86+
p1.x = 3i64;
87+
p1.y = 4i64;
88+
89+
Point p2;
90+
p2.x = 6i64;
91+
p2.y = 8i64;
92+
93+
print($"Point 1: ({p1.x}, {p1.y})");
94+
print($"Point 2: ({p2.x}, {p2.y})");
95+
96+
long dist = distance_squared(p1, p2);
97+
print($"Distance squared: {dist}");
98+
print($"Classification: {classify_distance(dist)}");
99+
100+
scale_point(p1, 2i64);
101+
print($"Scaled point 1: ({p1.x}, {p1.y})");
102+
103+
Rectangle rect;
104+
rect.top_left = p2;
54105

55-
int x = 10;
56-
int y = 15;
57-
print(x > y ? "x is greater" : "y is greater or equal");
106+
Point bottom_right;
107+
bottom_right.x = 10i64;
108+
bottom_right.y = 12i64;
109+
rect.bottom_right = bottom_right;
110+
111+
long area = area_calculator(rect);
112+
print($"Rectangle area: {area}");
113+
114+
long fact5 = factorial(5i64);
115+
print($"5! = {fact5}");
116+
117+
long pow_result = power(2i64, 8i64);
118+
print($"2^8 = {pow_result}");
119+
120+
int acc_result = accumulate_ops(5);
121+
print($"Accumulator result: {acc_result}");
122+
123+
long inc_result = increment_demo();
124+
print($"Increment demo result: {inc_result}");
125+
126+
string comparison = compare_numbers(15i64, 10i64);
127+
print($"15 vs 10: {comparison}");
128+
129+
int x = 42;
130+
int y = 37;
131+
print(x > y ? $"{x} is greater" : $"{y} is greater or equal");
58132
}

src/compiler/ast/Parser.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,12 @@ export class Parser {
349349
if (next.type === TokenTypes.IDENTIFIER) {
350350
if (this.#peek(1)?.type === TokenTypes.LPAREN) {
351351
return this.#parseCallExpression();
352-
} else if (this.#peek(1)?.type === TokenTypes.DOT) {
352+
}
353+
354+
if (this.#peek(1)?.type === TokenTypes.DOT) {
353355
return this.#parseFieldExpression();
354356
}
357+
355358
return this.#parseIdentifier();
356359
}
357360

@@ -573,6 +576,14 @@ export class Parser {
573576
return statements;
574577
}
575578

579+
#parseStatementOrBlock() {
580+
if (this.#peek()?.type === TokenTypes.LBRACE) {
581+
return this.#parseBlock();
582+
} else {
583+
return [this.#parseStatement()];
584+
}
585+
}
586+
576587
#parseFields() {
577588
this.#consume(TokenTypes.LBRACE);
578589

@@ -635,7 +646,7 @@ export class Parser {
635646
#parseIfStatement(): IfStatement {
636647
const token = this.#consume(TokenTypes.IF);
637648
const condition = this.parseExpression();
638-
const consequent = this.#parseBlock();
649+
const consequent = this.#parseStatementOrBlock();
639650

640651
let alternate = null;
641652

@@ -645,7 +656,7 @@ export class Parser {
645656
if (this.#peek()?.type === TokenTypes.IF) {
646657
alternate = this.#parseIfStatement();
647658
} else {
648-
alternate = this.#parseBlock();
659+
alternate = this.#parseStatementOrBlock();
649660
}
650661
}
651662
return new IfStatement(condition, consequent, alternate, token.start);
@@ -675,7 +686,7 @@ export class Parser {
675686
}
676687
this.#consume(TokenTypes.RPAREN);
677688

678-
const body = this.#parseBlock();
689+
const body = this.#parseStatementOrBlock();
679690

680691
return new LoopStatement(declaration, condition, step, body, token.start);
681692
}
@@ -701,17 +712,25 @@ export class Parser {
701712
if (next.type === TokenTypes.IDENTIFIER) {
702713
if (this.#peek(1)?.type === TokenTypes.IDENTIFIER) {
703714
return this.#parseVariableDeclaration();
704-
} else if (
715+
}
716+
717+
if (
705718
this.#peek(1)?.type === TokenTypes.DOT &&
706719
this.#peek(2)?.type === TokenTypes.IDENTIFIER &&
707720
this.#peek(3)?.type === TokenTypes.EQUAL
708721
) {
709722
return this.#parseFieldAssignment();
710-
} else if (this.#peek(1)?.type === TokenTypes.EQUAL) {
723+
}
724+
725+
if (this.#peek(1)?.type === TokenTypes.EQUAL) {
711726
return this.#parseVariableAssignment();
712-
} else if (isCompoundOperator(this.#peek(1)?.type)) {
727+
}
728+
729+
if (isCompoundOperator(this.#peek(1)?.type)) {
713730
return this.#parseCompoundVariableAssignment();
714-
} else if (
731+
}
732+
733+
if (
715734
this.#peek(1)?.type === TokenTypes.DOT &&
716735
this.#peek(2)?.type === TokenTypes.IDENTIFIER &&
717736
isCompoundOperator(this.#peek(3)?.type)

src/compiler/ast/nodes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type { FixExpression } from "./FixExpression";
2323
import type { UnaryExpression } from "./UnaryExpression";
2424

2525
export const NodeKinds = {
26-
STRUCT_DECLARATION: "StructDeclaration",
26+
STRUCT_EXPRESSION: "StructExpression",
2727
FIELD_DECLARATION: "FieldDeclaration",
2828
FIELD_ASSIGNMENT: "FieldAssignment",
2929
FIELD_EXPRESSION: "FieldExpression",

src/compiler/ast/operators.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { TokenTypes } from "../tokens";
22

33
export const BinaryOperators = {
4-
[TokenTypes.EQUALS]: { precedence: 1, associativity: "left" },
5-
[TokenTypes.NOT_EQUALS]: { precedence: 1, associativity: "left" },
6-
[TokenTypes.LTE]: { precedence: 1, associativity: "left" },
7-
[TokenTypes.GTE]: { precedence: 1, associativity: "left" },
8-
[TokenTypes.LT]: { precedence: 1, associativity: "left" },
9-
[TokenTypes.GT]: { precedence: 1, associativity: "left" },
10-
[TokenTypes.PLUS]: { precedence: 2, associativity: "left" },
11-
[TokenTypes.MINUS]: { precedence: 2, associativity: "left" },
12-
[TokenTypes.POW]: { precedence: 4, associativity: "right" },
13-
[TokenTypes.MUL]: { precedence: 3, associativity: "left" },
14-
[TokenTypes.DIV]: { precedence: 3, associativity: "left" },
15-
[TokenTypes.MOD]: { precedence: 3, associativity: "left" },
4+
[TokenTypes.AND]: { precedence: 1, associativity: "left" },
5+
[TokenTypes.EQUALS]: { precedence: 2, associativity: "left" },
6+
[TokenTypes.NOT_EQUALS]: { precedence: 2, associativity: "left" },
7+
[TokenTypes.LT]: { precedence: 3, associativity: "left" },
8+
[TokenTypes.GT]: { precedence: 3, associativity: "left" },
9+
[TokenTypes.LTE]: { precedence: 3, associativity: "left" },
10+
[TokenTypes.GTE]: { precedence: 3, associativity: "left" },
11+
[TokenTypes.PLUS]: { precedence: 4, associativity: "left" },
12+
[TokenTypes.MINUS]: { precedence: 4, associativity: "left" },
13+
[TokenTypes.MUL]: { precedence: 5, associativity: "left" },
14+
[TokenTypes.DIV]: { precedence: 5, associativity: "left" },
15+
[TokenTypes.MOD]: { precedence: 5, associativity: "left" },
16+
[TokenTypes.POW]: { precedence: 6, associativity: "right" },
1617
} as const;
1718

1819
export interface BinaryOperatorInfo {

src/compiler/syntax.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ export function showErrorTooltip(
104104
let node: Node | null;
105105

106106
while ((node = walker.nextNode())) {
107-
if (node.nodeType !== Node.TEXT_NODE) continue;
107+
if (node.nodeType !== Node.TEXT_NODE) {
108+
if (node.nodeName === "BR") {
109+
current++;
110+
}
111+
continue;
112+
}
108113

109114
const length = node.textContent?.length || 0;
110115

src/compiler/tokens.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ export const TokenTypes = {
77
NUMBER_LITERAL: "NUMBER_LITERAL",
88
STRING_LITERAL: "STRING_LITERAL",
99
STRING_INTERPOLATED: "STRING_INTERPOLATED",
10-
NOT: "!",
10+
AND: "&&",
1111
EQUALS: "==",
1212
NOT_EQUALS: "!=",
13+
NOT: "!",
1314
EQUAL: "=",
1415
LTE: "<=",
1516
GTE: ">=",
@@ -167,9 +168,10 @@ export function tokenize(
167168
type: TokenTypes.STRING_INTERPOLATED,
168169
extractor: (match) => match,
169170
},
170-
{ matcher: /!/, type: TokenTypes.NOT },
171+
{ matcher: /&&/, type: TokenTypes.AND },
171172
{ matcher: /==/, type: TokenTypes.EQUALS },
172173
{ matcher: /!=/, type: TokenTypes.NOT_EQUALS },
174+
{ matcher: /!/, type: TokenTypes.NOT },
173175
{ matcher: /=/, type: TokenTypes.EQUAL },
174176
{ matcher: /<=/, type: TokenTypes.LTE },
175177
{ matcher: />=/, type: TokenTypes.GTE },
@@ -181,6 +183,7 @@ export function tokenize(
181183
{ matcher: /--/, type: TokenTypes.DECREMENT },
182184
{ matcher: /-=/, type: TokenTypes.MINUS_EQUAL },
183185
{ matcher: /\-/, type: TokenTypes.MINUS },
186+
{ matcher: /\*\*=/, type: TokenTypes.POW_EQUAL },
184187
{ matcher: /\*\*/, type: TokenTypes.POW },
185188
{ matcher: /\*=/, type: TokenTypes.MUL_EQUAL },
186189
{ matcher: /\*/, type: TokenTypes.MUL },

src/compiler/wasm/Compiler.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ export class Compiler {
187187

188188
const wat = this.#wat.join("\n");
189189

190-
console.log(wat);
191-
192190
const module = await wabt();
193191
const parsed = module.parseWat("module.wat", wat);
194192
const { buffer } = parsed.toBinary({ log: true });
@@ -1175,8 +1173,9 @@ export class Compiler {
11751173
const type = this.#getExpressionResultType(node.left);
11761174

11771175
switch (node.operator) {
1178-
case TokenTypes.NOT_EQUALS:
1176+
case TokenTypes.AND:
11791177
case TokenTypes.EQUALS:
1178+
case TokenTypes.NOT_EQUALS:
11801179
case TokenTypes.LTE:
11811180
case TokenTypes.GTE:
11821181
case TokenTypes.LT:

src/compiler/wasm/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export const WASM_TYPES: Record<ValidTypeKind, string> = {
2020
export const WASM_BINARY_OPERATIONS: Partial<
2121
Record<TypeKind, Partial<Record<BinaryOperator, string>>>
2222
> = {
23+
[TypeKinds.BOOL]: {
24+
[TokenTypes.AND]: "i32.and",
25+
},
2326
[TypeKinds.INT]: {
2427
[TokenTypes.EQUALS]: "i32.eq",
2528
[TokenTypes.NOT_EQUALS]: "i32.ne",

0 commit comments

Comments
 (0)