-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnaryOp.java
52 lines (44 loc) · 1.88 KB
/
UnaryOp.java
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
public class UnaryOp extends Exp
{
public Exp Right;
public String Type;
public UnaryOp(String type, Exp right)
{
Right = right;
Type = type;
}
public @Override String toString()
{
return ColoredToken("(")+Type+" "+Right.toString()+ColoredToken(")");
}
public @Override void Eval(Cpu c)
{
c.Execute(Right);
switch(Type)
{
case "-": c.Push(-c.Pop()); break;
case "!": c.Push(1-c.Pop()); break; //boolean negation
case "malloc": c.Push(c.Malloc((int)c.Pop())); break;
case "length": c.Push(c.Size(c.Pop())); break;
case "abs": c.Push(Math.abs(c.Pop())); break;
case "cos": c.Push(Math.cos(c.Pop())); break;
case "sin": c.Push(Math.sin(c.Pop())); break;
case "tan": c.Push(Math.tan(c.Pop())); break;
case "cosh": c.Push(Math.cosh(c.Pop())); break;
case "sinh": c.Push(Math.sinh(c.Pop())); break;
case "tanh": c.Push(Math.tanh(c.Pop())); break;
case "acos": c.Push(Math.acos(c.Pop())); break;
case "asin": c.Push(Math.asin(c.Pop())); break;
case "atan": c.Push(Math.atan(c.Pop())); break;
case "floor": c.Push(Math.floor(c.Pop())); break;
case "ceil" : c.Push(Math.ceil (c.Pop())); break;
case "round": c.Push(Math.round(c.Pop())); break;
case "random": c.Push(Math.random()*c.Pop()); break;
case "log" : c.Push(Math.log(c.Pop())); break;
case "log10" : c.Push(Math.log10(c.Pop())); break;
case "exp" : c.Push(Math.exp(c.Pop())); break;
case "sqrt" : c.Push(Math.sqrt(c.Pop())); break;
default: Useful.crash("Unknow unary operator : '"+Type+"'"); break;
}
}
}