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
|
const assert = require('assert');
const { createLexer } = require('../src/core/lexer');
const { createParser } = require('../src/core/parser');
const { createInterpreter } = require('../src/core/interpreter');
describe('Arrow Functions in Table Literals', () => {
function interpret(code) {
const lexer = createLexer(code);
const tokens = lexer.allTokens();
const parser = createParser(tokens);
const ast = parser.parse();
const interpreter = createInterpreter(ast);
return interpreter.interpret();
}
it('should correctly parse and execute single arrow function in table', () => {
const code = `calculator : {
add: x y -> x + y;
};
result : calculator.add 5 3;
result`;
const result = interpret(code);
assert.strictEqual(result.value, 8);
assert.strictEqual(result.isFloat, false);
});
it('should correctly handle arrow function with single parameter', () => {
const code = `calculator : {
double: x -> x * 2;
};
result : calculator.double 5;
result`;
const result = interpret(code);
assert.strictEqual(result.value, 10);
assert.strictEqual(result.isFloat, false);
});
it('should correctly handle arrow function with complex body', () => {
const code = `calculator : {
complex: x y -> (x + y) * (x - y);
};
result : calculator.complex 5 3;
result`;
const result = interpret(code);
assert.strictEqual(result.value, 16);
assert.strictEqual(result.isFloat, false);
});
it('should correctly handle arrow function with parentheses for precedence', () => {
const code = `calculator : {
multiply: x y -> x * (y + 1);
};
result : calculator.multiply 3 2;
result`;
const result = interpret(code);
assert.strictEqual(result.value, 9);
assert.strictEqual(result.isFloat, false);
});
it('should correctly handle multiple arrow functions in table', () => {
const code = `calculator : {
add: x y -> x + y;
subtract: x y -> x - y;
multiply: x y -> x * (y + 1);
complex: x y -> (x + y) * (x - y);
};
result1 : calculator.add 5 3;
result2 : calculator.subtract 10 4;
result3 : calculator.multiply 3 2;
result4 : calculator.complex 5 3;
result1`;
const result = interpret(code);
assert.strictEqual(result.value, 8);
assert.strictEqual(result.isFloat, false);
});
it('should correctly handle arrow functions with different parameter counts', () => {
const code = `calculator : {
add: x y -> x + y;
double: x -> x * 2;
identity: x -> x;
constant: -> 42;
};
result1 : calculator.add 5 3;
result2 : calculator.double 7;
result3 : calculator.identity 99;
result4 : calculator.constant;
result1`;
const result = interpret(code);
assert.strictEqual(result.value, 8);
assert.strictEqual(result.isFloat, false);
});
});
|