-
Notifications
You must be signed in to change notification settings - Fork 12
/
cat-test.js
116 lines (116 loc) · 3.87 KB
/
cat-test.js
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
"use strict";
// Test module for the Cat language
Object.defineProperty(exports, "__esModule", { value: true });
var cat_1 = require("./cat");
var type_inference_1 = require("./node_modules/type-inference/type_inference");
//===============================================================
// Test functions
function printEnvironment(ce) {
for (var k in ce.instructions) {
var i = ce.instructions[k];
var t = cat_1.CatLanguage.typeToString(i.type);
console.log(k + "\t : " + t);
}
}
function testEvaluator() {
console.log("=================");
console.log("Testing evaluator");
console.log("=================");
var ce = new cat_1.CatLanguage.CatEvaluator();
ce.trace = true;
ce.eval("6 7 dup mul sub");
console.log("expected 43 on stack");
}
function testComposition() {
var ce = new cat_1.CatLanguage.CatEnvironment();
for (var _i = 0, _a = ce.getInstructions(); _i < _a.length; _i++) {
var i1 = _a[_i];
for (var _b = 0, _c = ce.getInstructions(); _b < _c.length; _b++) {
var i2 = _c[_b];
testComposeInstruction(i1, i2);
}
}
}
function testComposeInstruction(i1, i2) {
try {
var q = new cat_1.CatLanguage.CatAbstraction([i1, i2]);
var t = type_inference_1.TypeInference.composeFunctions(i1.type, i2.type);
console.log(q + " : " + cat_1.CatLanguage.typeToString(t));
}
catch (e) {
console.log(i1.name + " " + i2.name + " : " + e);
}
}
function testCompose(a, b) {
testComposeInstruction(getInstruction(a), getInstruction(b));
}
var globalEnv = new cat_1.CatLanguage.CatEnvironment();
function getInstruction(s) {
return globalEnv.instructions[s];
}
function testEnvironment() {
var ce = new cat_1.CatLanguage.CatEnvironment();
printEnvironment(ce);
}
function outputInstruction(i) {
console.log(i.toDebugString());
}
function outputDefinitions() {
var ce = new cat_1.CatLanguage.CatEnvironment();
console.log('=====================');
console.log("Core Stack Operations");
console.log('=====================');
for (var k in ce.primOps)
console.log(k + " : " + ce.primOps[k]);
console.log('===================');
console.log("Primitive Functions");
console.log('===================');
for (var k in ce.primFuncs)
outputInstruction(ce.getInstruction(k));
console.log('================');
console.log("Standard Library");
console.log('================');
for (var k in ce.stdOps)
outputInstruction(ce.getInstruction(k));
}
function outputGrammar() {
console.log('===========');
console.log('Cat Grammar');
console.log('===========');
console.log(cat_1.CatLanguage.grammarString());
console.log('==============');
console.log('Cat AST Schema');
console.log('==============');
console.log(cat_1.CatLanguage.astSchemaString());
}
function outputFormalTypes() {
var ce = new cat_1.CatLanguage.CatEnvironment();
console.log('=======================');
console.log("Core Stack Formal Types");
console.log('=======================');
for (var k in ce.primOps) {
var i = ce.getInstruction(k);
console.log(k + " : " + i.type.toString());
}
console.log('=============================');
console.log("Standard Library Formal Types");
console.log('=============================');
for (var k in ce.stdOps) {
var i = ce.getInstruction(k);
var t = type_inference_1.TypeInference.alphabetizeVarNames(i.type);
console.log(k + " : " + t);
}
}
function testTypes() {
var ce = new cat_1.CatLanguage.CatEvaluator();
var i = ce.env.addDefinition("test", "quote dup apply");
console.log("Type of " + i.name + " : " + i.type);
ce.eval("10 test");
}
outputGrammar();
outputDefinitions();
outputFormalTypes();
testEvaluator();
testTypes();
process.exit();
//# sourceMappingURL=cat-test.js.map