-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbuiltinlib.ts
161 lines (151 loc) · 4.44 KB
/
builtinlib.ts
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
import { Type } from './ast';
import { RunTimeError } from './error_reporting';
import { BOOL, NONE, NUM } from './utils';
import { splitString, stackTrace } from './runtime_error'
type BuiltinFunc = {
name: string
body: Function
typeSig: [Type[], Type]
}
// here to register builtinFunctions
export const BuiltinLib:BuiltinFunc[] = [
{
name: "factorial",
body: factorial,
typeSig: [[NUM], NUM]
},
{
name: "randint",
body: randint,
typeSig: [[NUM,NUM,NUM,NUM], NUM]
},
{
name: "gcd",
body: gcd,
typeSig: [[NUM,NUM,NUM,NUM], NUM]
},
{
name: "lcm",
body: lcm,
typeSig: [[NUM,NUM,NUM,NUM], NUM]
},
{
name: "comb",
body: comb,
typeSig: [[NUM,NUM,NUM,NUM], NUM]
},
{
name: "perm",
body: perm,
typeSig: [[NUM,NUM,NUM,NUM], NUM]
},
{
name: "randrange",
body: randrange,
typeSig: [[NUM,NUM, NUM,NUM,NUM], NUM]
},
{
name: "time",
body: ()=>Date.now()%1000000000,
typeSig: [[], NUM]
},
{
name: "sleep",
body: sleep,
typeSig: [[NUM], NONE]
},
{
name: "int",
body: (x:any)=>x,
typeSig: [[BOOL], NUM]
},
{
name: "bool",
body: (x:number)=>x!=0,
typeSig: [[NUM], BOOL]
},
{
name: "abs",
body: Math.abs,
typeSig: [[NUM], NUM]
},
{
name: "min",
body: Math.min,
typeSig: [[NUM, NUM], NUM]
},
{
name: "max",
body: Math.max,
typeSig: [[NUM, NUM], NUM]
},
{
name: "pow",
body: Math.pow,
typeSig: [[NUM, NUM], NUM]
}
]
function factorial(x:number):number{
return x>0 ? x*factorial(x-1): 1
}
function randint(x:number, y:number, line: number, col: number):number{
if(y<x) {
var message = stackTrace() + `\nRUNTIME ERROR: randint range error, upperBound less than lowerBound in line ` + line.toString() + " at column " + col.toString() + "\n" + splitString()[line-1].trim();
// throw new RunTimeError("randint range error, upperBound less than lowerBound");
throw new RunTimeError(message);
}
return Math.floor(Math.random()*(y-x+1) + x);
}
function gcd(a:number,b:number, line: number, col:number):number{
if (a<0 || b<0 || a==0 && b==0) {
var message = stackTrace() + `\nRUNTIME ERROR: gcd param error, eq or less than 0 in line ` + line.toString() + " at column " + col.toString() + "\n" + splitString()[line-1].trim();
// throw new RunTimeError("gcd param error, eq or less than 0");
throw new RunTimeError(message);
}
return b==0 ? a : gcd(b,a % b, line, col);
}
function lcm(x:number, y:number, line: number, col:number):number{
if (x<=0 || y<=0 || x==0 && y==0) {
var message = stackTrace() + `\nRUNTIME ERROR: lcm param negative error, eq or less than 0 in line ` + line.toString() + " at column " + col.toString() + "\n" + splitString()[line-1].trim();
// throw new RunTimeError("lcm param negative error, eq or less than 0");
throw new RunTimeError(message);
}
return Math.floor(x*y/gcd(x,y, line, col))
}
function comb(x:number, y:number, line:number, col:number):number{
if (x < y || x < 0 || y < 0) {
var message = stackTrace() + `\nRUNTIME ERROR: comb param error in line ` + line.toString() + " at column " + col.toString() + "\n" + splitString()[line-1].trim();
// throw new RunTimeError("comb param error");
throw new RunTimeError(message);
}
return perm(x, y, line, col) / perm(y, y, line, col)
}
function perm(x:number, y:number, line:number, col:number):number{
if (x < y || x < 0 || y < 0){
var message = stackTrace() + `\nRUNTIME ERROR: perm param error in line ` + line.toString() + " at column " + col.toString() + "\n" + splitString()[line-1].trim();
// throw new RunTimeError("perm param error");
throw new RunTimeError(message);
}
let result = 1
for (var i = 0; i < y; i++) {
result *= (x - i)
}
return result
}
function randrange(x:number, y:number, step:number,line:number, col:number){
if(y<x){
var message = stackTrace() + `\nRUNTIME ERROR: randrange range error, upperBound less than lowerBound in line ` + line.toString() + " at column " + col.toString() + "\n" + splitString()[line-1].trim();
// throw new RunTimeError("randrange range error, upperBound less than lowerBound");
throw new RunTimeError(message);
}
let result = randint(x, y, line, col)
while ((result - x) % step !== 0) {
result = randint(x, y, line, col)
}
return result
}
function sleep(ms:number):number{
const start = Date.now();
while (Date.now()-start<ms);
return 0;
}