-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharbitrary.js
39 lines (32 loc) · 1.08 KB
/
arbitrary.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
function arbExpr(types = ["num", "unOp", "binOp"]){
var type = randEl(types);
switch(type){
case "num":
return arbNum();
case "unOp":
return arbUnOp(types);
case "binOp":
return arbBinOp(types);
}
}
function arbNum(){
var numStr = '';
do{
numStr += Math.floor(Math.random() * 10);
}while(Math.random() > 0.4);
if(Math.random() > 0.5){
numStr = '-' + numStr;
}
return newNum(numStr);
}
function arbUnOp(types = ["num", "unOp", "binOp"]){
var op = randEl(["asin", "acos", "atan", "sin", "cos", "tan", "ln", "lg", "abs", "√", "∛", "∜", "¬", "sgn", "⌊", "⌈", "round", "!", "‼"]);
return {type: "unOp", op: op, expr: arbExpr(types)};
}
function arbBinOp(types = ["num", "unOp", "binOp"]){
var op = randEl(["⋎", "⊻", "⋏", "=", "≈", "<", ">", "≤", "≥", "+", "-", "×", "/", "^"]);
return {type: "binOp", op: op, expr0: arbExpr(types), expr1: arbExpr(types)};
}
function randEl(list){
return list[Math.floor(Math.random() * list.length)];
}