git @ Cat's Eye Technologies Jaft / master test / jaftCompiler.test.js
master

Tree @master (Download .tar.gz)

jaftCompiler.test.js @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright (c) 2026 Chris Pressey, Cat's Eye Technologies.
//
// SPDX-License-Identifier: LicenseRef-MIT-X-Jaft

import { describe, it, expect } from "vitest";
import { Scanner, Parser, compile } from "../src/jaftCompiler.js";

describe("Scanner", () => {
  describe("basic tokens", () => {
    it("should tokenize identifiers", () => {
      const t = new Scanner("foo");
      expect(t.token).toBe("foo");
    });

    it("should tokenize parentheses", () => {
      const t = new Scanner("()");
      expect(t.token).toBe("(");
      t.scan();
      expect(t.token).toBe(")");
    });

    it("should tokenize commas", () => {
      const t = new Scanner(",");
      expect(t.token).toBe(",");
    });

    it("should skip whitespace", () => {
      const t = new Scanner("  foo  ");
      expect(t.token).toBe("foo");
    });

    it("should return null at end of input", () => {
      const t = new Scanner("x");
      t.scan();
      expect(t.token).toBe(null);
    });
  });

  describe("numeric literals", () => {
    it("should tokenize integers", () => {
      const t = new Scanner("42");
      expect(t.token).toBe("42");
    });

    it("should tokenize decimals", () => {
      const t = new Scanner("3.14");
      expect(t.token).toBe("3.14");
    });
  });

  describe("string literals", () => {
    it("should tokenize double-quoted strings", () => {
      const t = new Scanner('"hello"');
      expect(t.token).toEqual({ type: "string", value: "hello" });
    });

    it("should tokenize single-quoted strings", () => {
      const t = new Scanner("'world'");
      expect(t.token).toEqual({ type: "string", value: "world" });
    });

    it("should handle escaped characters", () => {
      const t = new Scanner('"hello\\nworld"');
      expect(t.token).toEqual({ type: "string", value: "hello\nworld" });
    });

    it("should handle escaped quotes", () => {
      const t = new Scanner('"say \\"hello\\""');
      expect(t.token).toEqual({ type: "string", value: 'say "hello"' });
    });
  });

  describe("boolean literals", () => {
    it("should tokenize true", () => {
      const t = new Scanner("true");
      expect(t.token).toEqual({ type: "boolean", value: true });
    });

    it("should tokenize false", () => {
      const t = new Scanner("false");
      expect(t.token).toEqual({ type: "boolean", value: false });
    });
  });

  describe("complex expressions", () => {
    it("should tokenize function calls", () => {
      const t = new Scanner("add(1, 2)");
      expect(t.token).toBe("add");
      t.scan();
      expect(t.token).toBe("(");
      t.scan();
      expect(t.token).toBe("1");
      t.scan();
      expect(t.token).toBe(",");
      t.scan();
      expect(t.token).toBe("2");
      t.scan();
      expect(t.token).toBe(")");
    });

    it("should handle multiple identifiers", () => {
      const t = new Scanner("foo bar baz");
      expect(t.token).toBe("foo");
      t.scan();
      expect(t.token).toBe("bar");
      t.scan();
      expect(t.token).toBe("baz");
    });
  });

  describe("isOn and expect", () => {
    it("should check if on a token", () => {
      const t = new Scanner("(");
      expect(t.isOn("(")).toBe(true);
      expect(t.isOn(")")).toBe(false);
    });

    it("should expect and advance for matching token", () => {
      const t = new Scanner("( )");
      t.expect("(");
      expect(t.token).toBe(")");
    });

    it("should throw error for non-matching token", () => {
      const t = new Scanner("(");
      expect(() => t.expect(")")).toThrow("Expected ), found (");
    });
  });
});

describe("Parser", () => {
  describe("parseExpr", () => {
    it("should parse variables", () => {
      const parser = new Parser(new Scanner("x"));
      const ast = parser.parseExpr();
      expect(ast).toEqual({ type: "variable", name: "x" });
    });

    it("should parse numeric literals", () => {
      const parser = new Parser(new Scanner("42"));
      const ast = parser.parseExpr();
      expect(ast).toEqual({ type: "literal", valueType: "number", value: 42 });
    });

    it("should parse string literals", () => {
      const p = new Parser(new Scanner('"hello"'));
      const ast = p.parseExpr();
      expect(ast).toEqual({
        type: "literal",
        valueType: "string",
        value: "hello",
      });
    });

    it("should parse boolean literals", () => {
      const p = new Parser(new Scanner("true"));
      const ast = p.parseExpr();
      expect(ast).toEqual({
        type: "literal",
        valueType: "boolean",
        value: true,
      });
    });

    it("should parse function calls with no arguments", () => {
      const s = new Scanner("random()");
      const p = new Parser(s);
      const ast = p.parseExpr();
      expect(ast).toEqual({
        type: "call",
        name: "random",
        args: [],
      });
    });

    it("should parse function calls with one argument", () => {
      const s = new Scanner("sqrt(16)");
      const p = new Parser(s);
      const ast = p.parseExpr();
      expect(ast).toEqual({
        type: "call",
        name: "sqrt",
        args: [{ type: "literal", valueType: "number", value: 16 }],
      });
    });

    it("should parse function calls with multiple arguments", () => {
      const t = new Scanner("add(1, 2)");
      const p = new Parser(t);
      const ast = p.parseExpr();
      expect(ast).toEqual({
        type: "call",
        name: "add",
        args: [
          { type: "literal", valueType: "number", value: 1 },
          { type: "literal", valueType: "number", value: 2 },
        ],
      });
    });

    it("should parse nested function calls", () => {
      const t = new Scanner("mul(add(1, 2), 3)");
      const p = new Parser(t);
      const ast = p.parseExpr();
      expect(ast).toEqual({
        type: "call",
        name: "mul",
        args: [
          {
            type: "call",
            name: "add",
            args: [
              { type: "literal", valueType: "number", value: 1 },
              { type: "literal", valueType: "number", value: 2 },
            ],
          },
          { type: "literal", valueType: "number", value: 3 },
        ],
      });
    });

    it("should parse function calls with variables", () => {
      const t = new Scanner("mul(a, b)");
      const p = new Parser(t);
      const ast = p.parseExpr();
      expect(ast).toEqual({
        type: "call",
        name: "mul",
        args: [
          { type: "variable", name: "a" },
          { type: "variable", name: "b" },
        ],
      });
    });

    it("should throw an error for mismatched parentheses", () => {
      const p = new Parser(new Scanner('(a) => a === "FOO") ? 1 : 2'));
      expect(() => p.parseFunDefn()).toThrow("Unexpected )");
    });

    it("should throw an error for unmatched parentheses", () => {
      const p = new Parser(new Scanner('(a) => a === "FOO" ? 1 : (2'));
      expect(() => p.parseFunDefn()).toThrow("Expected )");
    });
  });

  describe("parseExpr with binops", () => {
    it("should parse arithmetic operators with correct precedence", () => {
      const t = new Scanner("a + b * c");
      const p = new Parser(t);
      const ast = p.parseExpr();
      expect(ast).toEqual({
        type: "binop",
        op: "+",
        left: { type: "variable", name: "a" },
        right: {
          type: "binop",
          op: "*",
          left: { type: "variable", name: "b" },
          right: { type: "variable", name: "c" },
        },
      });
    });

    it("should parse comparison with arithmetic", () => {
      const t = new Scanner("x + 1 >= y * 2");
      const ast = new Parser(t).parseExpr();
      expect(ast.type).toBe("binop");
      expect(ast.op).toBe(">=");
      expect(ast.left.type).toBe("binop");
      expect(ast.left.op).toBe("+");
      expect(ast.right.type).toBe("binop");
      expect(ast.right.op).toBe("*");
    });

    it("should parse comparison with logical operators", () => {
      const t = new Scanner("x >= 1 && y <= 2");
      const ast = new Parser(t).parseExpr();
      expect(ast.type).toBe("binop");
      expect(ast.op).toBe("&&");
      expect(ast.left.type).toBe("binop");
      expect(ast.left.op).toBe(">=");
      expect(ast.right.type).toBe("binop");
      expect(ast.right.op).toBe("<=");
    });

    it("should parse member access with highest precedence", () => {
      const t = new Scanner("obj.prop + 5");
      const ast = new Parser(t).parseExpr();
      expect(ast).toEqual({
        type: "binop",
        op: "+",
        left: {
          type: "binop",
          op: ".",
          left: { type: "variable", name: "obj" },
          right: { type: "literal", valueType: "string", value: "prop" },
        },
        right: { type: "literal", valueType: "number", value: 5 },
      });
    });
  });

  describe("parseExpr with let", () => {
    it("should parse simple let expression", () => {
      const t = new Scanner("let x = 5; x");
      const ast = new Parser(t).parseExpr();
      expect(ast).toEqual({
        type: "let",
        allowShadowing: true,
        bindings: [
          {
            name: "x",
            value: { type: "literal", valueType: "number", value: 5 },
          },
        ],
        body: { type: "variable", name: "x" },
      });
    });

    it("should parse let with multiple bindings", () => {
      const t = new Scanner("let x = 1, y = 2; add(x, y)");
      const ast = new Parser(t).parseExpr();
      expect(ast).toEqual({
        type: "let",
        allowShadowing: true,
        bindings: [
          {
            name: "x",
            value: { type: "literal", valueType: "number", value: 1 },
          },
          {
            name: "y",
            value: { type: "literal", valueType: "number", value: 2 },
          },
        ],
        body: {
          type: "call",
          name: "add",
          args: [
            { type: "variable", name: "x" },
            { type: "variable", name: "y" },
          ],
        },
      });
    });

    it("should parse let with function call bindings", () => {
      const t = new Scanner("let x = add(1, 2); mul(x, x)");
      const ast = new Parser(t).parseExpr();
      expect(ast.type).toBe("let");
      expect(ast.bindings[0].value.type).toBe("call");
      expect(ast.body.type).toBe("call");
    });

    it("should parse let with ternary bindings and expression", () => {
      const t = new Scanner("let x = y > 2 ? 0 : 1; x > 2 ? 0 : 1");
      const ast = new Parser(t).parseExpr();
      expect(ast.type).toBe("let");
      expect(ast.bindings[0].value.type).toBe("ternary");
      expect(ast.body.type).toBe("ternary");
    });
  });

  describe("parseExpr with ternary", () => {
    it("should parse simple ternary with literals", () => {
      const t = new Scanner("true ? 1 : 2");
      const ast = new Parser(t).parseExpr();
      expect(ast).toEqual({
        type: "ternary",
        condition: { type: "literal", valueType: "boolean", value: true },
        consequent: { type: "literal", valueType: "number", value: 1 },
        alternate: { type: "literal", valueType: "number", value: 2 },
      });
    });

    it("should parse ternary with variables", () => {
      const t = new Scanner("x ? y : z");
      const ast = new Parser(t).parseExpr();
      expect(ast).toEqual({
        type: "ternary",
        condition: { type: "variable", name: "x" },
        consequent: { type: "variable", name: "y" },
        alternate: { type: "variable", name: "z" },
      });
    });

    it("should parse ternary with function calls", () => {
      const t = new Scanner("isPositive(x) ? add(x, 1) : sub(x, 1)");
      const ast = new Parser(t).parseExpr();
      expect(ast).toEqual({
        type: "ternary",
        condition: {
          type: "call",
          name: "isPositive",
          args: [{ type: "variable", name: "x" }],
        },
        consequent: {
          type: "call",
          name: "add",
          args: [
            { type: "variable", name: "x" },
            { type: "literal", valueType: "number", value: 1 },
          ],
        },
        alternate: {
          type: "call",
          name: "sub",
          args: [
            { type: "variable", name: "x" },
            { type: "literal", valueType: "number", value: 1 },
          ],
        },
      });
    });

    it("should parse nested ternary expressions", () => {
      const t = new Scanner("x ? (y ? 1 : 2) : 3");
      const ast = new Parser(t).parseExpr();
      expect(ast.type).toBe("ternary");
      expect(ast.consequent.type).toBe("ternary");
    });
  });
});

describe("Compiler", () => {
  describe("compile", () => {
    it("should compile variables", () => {
      const ctx = new Map();
      const ast = { type: "variable", name: "x" };
      const code = compile(ctx, ast);
      expect(code).toBe("x");
    });

    it("should compile numeric literals", () => {
      const ctx = new Map();
      const ast = { type: "literal", valueType: "number", value: 42 };
      const code = compile(ctx, ast);
      expect(code).toBe("42");
    });

    it("should compile string literals", () => {
      const ctx = new Map();
      const ast = { type: "literal", valueType: "string", value: "hello" };
      const code = compile(ctx, ast);
      expect(code).toBe('"hello"');
    });

    it("should compile boolean literals", () => {
      const ctx = new Map();
      const ast = { type: "literal", valueType: "boolean", value: true };
      const code = compile(ctx, ast);
      expect(code).toBe("true");
    });

    it("should compile function calls", () => {
      const add = (a, b) => a + b;
      const ctx = new Map([["add", add]]);
      const ast = {
        type: "call",
        name: "add",
        args: [
          { type: "literal", valueType: "number", value: 1 },
          { type: "literal", valueType: "number", value: 2 },
        ],
      };
      const code = compile(ctx, ast);
      expect(code).toBe("add(1, 2)");
    });

    it("should throw error for unknown function", () => {
      const ctx = new Map();
      const ast = { type: "call", name: "unknown", args: [] };
      expect(() => compile(ctx, ast)).toThrow("Unknown function: unknown");
    });
  });

  describe("compile with binops", () => {
    it("should compile arithmetic with parentheses", () => {
      const ctx = new Map();
      const ast = {
        type: "binop",
        op: "+",
        left: { type: "variable", name: "a" },
        right: {
          type: "binop",
          op: "*",
          left: { type: "variable", name: "b" },
          right: { type: "literal", valueType: "number", value: 2 },
        },
      };
      const code = compile(ctx, ast);
      expect(code).toBe("(a + (b * 2))");
    });

    it("should compile member access", () => {
      const ctx = new Map();
      const ast = {
        type: "binop",
        op: ".",
        left: { type: "variable", name: "obj" },
        right: { type: "literal", valueType: "string", value: "length" },
      };
      const code = compile(ctx, ast);
      expect(code).toBe("(obj.length)");
    });
  });

  describe("compile with let", () => {
    it("should compile let expressions", () => {
      const ctx = new Map();
      const ast = {
        type: "let",
        bindings: [
          {
            name: "x",
            value: { type: "literal", valueType: "number", value: 5 },
          },
        ],
        body: { type: "variable", name: "x" },
      };
      const code = compile(ctx, ast);
      expect(code).toBe("((x) => x)(5)");
    });

    it("should compile let with multiple bindings", () => {
      const add = (a, b) => a + b;
      const ctx = new Map([["add", add]]);
      const ast = {
        type: "let",
        bindings: [
          {
            name: "x",
            value: { type: "literal", valueType: "number", value: 1 },
          },
          {
            name: "y",
            value: { type: "literal", valueType: "number", value: 2 },
          },
        ],
        body: {
          type: "call",
          name: "add",
          args: [
            { type: "variable", name: "x" },
            { type: "variable", name: "y" },
          ],
        },
      };
      const code = compile(ctx, ast);
      expect(code).toContain("(x, y) =>");
    });
  });

  describe("compile with ternary", () => {
    it("should compile ternary expressions", () => {
      const ctx = new Map();
      const ast = {
        type: "ternary",
        condition: { type: "literal", valueType: "boolean", value: true },
        consequent: { type: "literal", valueType: "number", value: 1 },
        alternate: { type: "literal", valueType: "number", value: 2 },
      };
      const code = compile(ctx, ast);
      expect(code).toBe("(true ? 1 : 2)");
    });
  });
});