forked from lkesteloot/turbopascal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.js
115 lines (107 loc) · 5.64 KB
/
builtin.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
// Builtin symbols, such as "Sin()" and "Pi".
'use strict';
define(["Node", "Token", "inst"], function (Node, Token, inst) {
// Special handling of Random() because its return type depends on whether
// it has an argument.
var builtinRandom = function (ctl, t) {
if (t === undefined) {
return Math.random();
} else {
return Math.round(Math.random()*t);
}
};
return {
// Import all the symbols for the builtins.
importSymbols: function (symbolTable) {
// Built-in types.
symbolTable.addNativeType("String", Node.stringType);
symbolTable.addNativeType("Integer", Node.integerType);
symbolTable.addNativeType("ShortInt", Node.integerType);
symbolTable.addNativeType("LongInt", Node.integerType);
symbolTable.addNativeType("Char", Node.charType);
symbolTable.addNativeType("Boolean", Node.booleanType);
symbolTable.addNativeType("Real", Node.realType);
symbolTable.addNativeType("Double", Node.realType);
symbolTable.addNativeType("Pointer", Node.pointerType);
// Constants and functions.
symbolTable.addNativeConstant("Nil", null,
new Node(Node.SIMPLE_TYPE, new Token("Nil", Token.IDENTIFIER), {
typeCode: inst.A,
typeName: null, // Important -- this is what makes this nil.
type: null
}));
symbolTable.addNativeConstant("True", true, Node.booleanType);
symbolTable.addNativeConstant("False", false, Node.booleanType);
symbolTable.addNativeConstant("Pi", Math.PI, Node.realType);
symbolTable.addNativeFunction("Sin", Node.realType, [Node.realType],
function (ctl, t) { return Math.sin(t); });
symbolTable.addNativeFunction("Cos", Node.realType, [Node.realType],
function (ctl, t) { return Math.cos(t); });
symbolTable.addNativeFunction("Round", Node.integerType, [Node.realType],
function (ctl, t) { return Math.round(t); });
symbolTable.addNativeFunction("Trunc", Node.integerType, [Node.realType],
function (ctl, t) { return (t < 0) ? Math.ceil(t) : Math.floor(t); });
symbolTable.addNativeFunction("Odd", Node.booleanType, [Node.integerType],
function (ctl, t) { return Math.round(t) % 2 !== 0; });
symbolTable.addNativeFunction("Abs", Node.realType, [Node.realType],
function (ctl, t) { return Math.abs(t); });
symbolTable.addNativeFunction("Sqrt", Node.realType, [Node.realType],
function (ctl, t) { return Math.sqrt(t); });
symbolTable.addNativeFunction("Ln", Node.realType, [Node.realType],
function (ctl, t) { return Math.log(t); });
symbolTable.addNativeFunction("Sqr", Node.realType, [Node.realType],
function (ctl, t) { return t*t; });
symbolTable.addNativeFunction("Random", Node.realType, [], builtinRandom);
symbolTable.addNativeFunction("Randomize", Node.voidType, [],
function (ctl) { /* Nothing. */ });
var symbol = symbolTable.addNativeFunction("Inc", Node.voidType,
[Node.integerType, Node.integerType], function (ctl, v, dv) {
if (dv === undefined) {
dv = 1;
}
ctl.writeDstore(v, ctl.readDstore(v) + dv);
});
symbol.type.parameters[0].byReference = true;
symbolTable.addNativeFunction("WriteLn", Node.voidType, [], function (ctl) {
// Skip ctl parameter.
var elements = [];
for (var i = 1; i < arguments.length; i++) {
// Convert to string.
elements.push("" + arguments[i]);
}
ctl.writeln(elements.join(" "));
});
symbolTable.addNativeFunction("Halt", Node.voidType, [], function (ctl) {
// Halt VM.
ctl.stop();
});
symbolTable.addNativeFunction("Delay", Node.voidType, [Node.integerType],
function (ctl, ms) {
// Tell VM to delay by ms asynchronously.
ctl.delay(ms);
});
symbol = symbolTable.addNativeFunction("New", Node.voidType,
[Node.pointerType, Node.integerType],
function (ctl, p, size) {
// Allocate and store address in p.
ctl.writeDstore(p, ctl.malloc(size));
});
symbol.type.parameters[0].byReference = true;
symbol = symbolTable.addNativeFunction("GetMem", Node.voidType,
[Node.pointerType, Node.integerType],
function (ctl, p, size) {
// Allocate and store address in p.
ctl.writeDstore(p, ctl.malloc(size));
});
symbol.type.parameters[0].byReference = true;
symbol = symbolTable.addNativeFunction("Dispose", Node.voidType,
[Node.pointerType],
function (ctl, p) {
// Free p and store 0 (nil) into it.
ctl.free(ctl.readDstore(p));
ctl.writeDstore(p, 0);
});
symbol.type.parameters[0].byReference = true;
}
};
});