Primitives
Padd:
PROC [self: Root] ~ {
num2: Num ~ PopNum[self.ostack];
num1: Num ~ PopNum[self.ostack];
PushNum[self.ostack, Add[num1, num2]];
};
Psub:
PROC [self: Root] ~ {
num2: Num ~ PopNum[self.ostack];
num1: Num ~ PopNum[self.ostack];
PushNum[self.ostack, Sub[num1, num2]];
};
Pmul:
PROC [self: Root] ~ {
num2: Num ~ PopNum[self.ostack];
num1: Num ~ PopNum[self.ostack];
PushNum[self.ostack, Mul[num1, num2]];
};
Pdiv:
PROC [self: Root] ~ {
n2: REAL ~ PopReal[self.ostack];
n1: REAL ~ PopReal[self.ostack];
PushReal[self.ostack, n1/n2];
};
Pidiv:
PROC [self: Root] ~ {
int2: INT ~ PopInt[self.ostack];
int1: INT ~ PopInt[self.ostack];
PushInt[self.ostack, int1/int2];
};
Pmod:
PROC [self: Root] ~ {
int2: INT ~ PopInt[self.ostack];
int1: INT ~ PopInt[self.ostack];
PushInt[self.ostack, int1 MOD int2];
};
Pabs:
PROC [self: Root] ~ {
num: Num ~ PopNum[self.ostack];
PushNum[self.ostack, Abs[num]];
};
Pneg:
PROC [self: Root] ~ {
num: Num ~ PopNum[self.ostack];
PushNum[self.ostack, Neg[num]];
};
Pceiling:
PROC [self: Root] ~ {
x: Any ~ Pop[self.ostack];
SELECT Type[x]
FROM
integer => Push[self.ostack, x];
real => PushReal[self.ostack, Ceiling[RealFromAny[x]]];
ENDCASE => ERROR Error[typecheck];
};
Pfloor:
PROC [self: Root] ~ {
x: Any ~ Pop[self.ostack];
SELECT Type[x]
FROM
integer => Push[self.ostack, x];
real => PushReal[self.ostack, Floor[RealFromAny[x]]];
ENDCASE => ERROR Error[typecheck];
};
Pround:
PROC [self: Root] ~ {
x: Any ~ Pop[self.ostack];
SELECT Type[x]
FROM
integer => Push[self.ostack, x];
real => PushReal[self.ostack, Round[RealFromAny[x]]];
ENDCASE => ERROR Error[typecheck];
};
Ptruncate:
PROC [self: Root] ~ {
x: Any ~ Pop[self.ostack];
SELECT Type[x]
FROM
integer => Push[self.ostack, x];
real => PushReal[self.ostack, Truncate[RealFromAny[x]]];
ENDCASE => ERROR Error[typecheck];
};
Psqrt:
PROC [self: Root] ~ {
num: REAL ~ PopReal[self.ostack];
PushReal[self.ostack, RealFns.SqRt[num]];
};
Patan:
PROC [self: Root] ~ {
den: REAL ~ PopReal[self.ostack];
num: REAL ~ PopReal[self.ostack];
PushReal[self.ostack, RealFns.ArcTanDeg[num, den]];
};
Pcos:
PROC [self: Root] ~ {
angle: REAL ~ PopReal[self.ostack];
PushReal[self.ostack, RealFns.CosDeg[angle]];
};
Psin:
PROC [self: Root] ~ {
angle: REAL ~ PopReal[self.ostack];
PushReal[self.ostack, RealFns.SinDeg[angle]];
};
Pexp:
PROC [self: Root] ~ {
exponent: REAL ~ PopReal[self.ostack];
base: REAL ~ PopReal[self.ostack];
PushReal[self.ostack, RealFns.Power[base, exponent]];
};
Pln:
PROC [self: Root] ~ {
num: REAL ~ PopReal[self.ostack];
PushReal[self.ostack, RealFns.Ln[num]];
};
Plog:
PROC [self: Root] ~ {
num: REAL ~ PopReal[self.ostack];
PushReal[self.ostack, RealFns.Log[10, num]];
};
Prand:
PROC [self: Root] ~ {
int: INT ~ Random.NextInt[self.random];
PushInt[self.ostack, int];
};
Psrand:
PROC [self: Root] ~ {
int: INT ~ PopInt[self.ostack];
self.random ← Random.Create[seed: int];
};
Prrand:
PROC [self: Root] ~ {
ERROR Error[unimplemented];
};
Register0:
PROC [self: Root] ~ {
Register[self, "add", Padd];
Register[self, "div", Pdiv];
Register[self, "idiv", Pidiv];
Register[self, "mod", Pmod];
Register[self, "mul", Pmul];
Register[self, "sub", Psub];
Register[self, "abs", Pabs];
Register[self, "neg", Pneg];
Register[self, "ceiling", Pceiling];
Register[self, "floor", Pfloor];
Register[self, "round", Pround];
Register[self, "truncate", Ptruncate];
Register[self, "sqrt", Psqrt];
Register[self, "atan", Patan];
Register[self, "cos", Pcos];
Register[self, "sin", Psin];
Register[self, "exp", Pexp];
Register[self, "ln", Pln];
Register[self, "log", Plog];
Register[self, "rand", Prand];
Register[self, "srand", Psrand];
Register[self, "rrand", Prrand];
};
RegisterPrimitives[Register0];