Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
math.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2
3namespace Phasor
4{
5
22
23Value StdLib::math_sqrt(const std::vector<Value> &args, VM *vm)
24{
25 checkArgCount(args, 1, "math_sqrt");
26 return Value(asm_sqrt(args[0].asFloat()));
27}
28
29Value StdLib::math_pow(const std::vector<Value> &args, VM *vm)
30{
31 checkArgCount(args, 2, "math_pow");
32 double base = args[0].asFloat();
33 double expv = args[1].asFloat();
34 return Value(asm_pow(base, expv));
35}
36
37Value StdLib::math_abs(const std::vector<Value> &args, VM *vm)
38{
39 // @TODO: Implement abs natively
40 checkArgCount(args, 1, "math_abs");
41 if (args[0].isInt())
42 return std::abs(args[0].asInt());
43 return std::abs(args[0].asFloat());
44}
45
46Value StdLib::math_floor(const std::vector<Value> &args, VM *vm)
47{
48 // @TODO: Implement floor natively
49 checkArgCount(args, 1, "math_floor");
50 return static_cast<int64_t>(std::floor(args[0].asFloat()));
51}
52
53Value StdLib::math_ceil(const std::vector<Value> &args, VM *vm)
54{
55 // @TODO: Implement ceil natively
56 checkArgCount(args, 1, "math_ceil");
57 return static_cast<int64_t>(std::ceil(args[0].asFloat()));
58}
59
60Value StdLib::math_round(const std::vector<Value> &args, VM *vm)
61{
62 // @TODO: Implement round natively
63 checkArgCount(args, 1, "math_round");
64 return static_cast<int64_t>(std::round(args[0].asFloat()));
65}
66
67Value StdLib::math_min(const std::vector<Value> &args, VM *vm)
68{
69 checkArgCount(args, 2, "math_min");
70 const Value &a = args[0];
71 const Value &b = args[1];
72 if (a.isInt() && b.isInt())
73 {
74 int64_t ai = a.asInt();
75 int64_t bi = b.asInt();
76 return Value(asm_iless_than(ai, bi) ? ai : bi);
77 }
78 if (a.isNumber() && b.isNumber())
79 {
80 double af = a.asFloat();
81 double bf = b.asFloat();
82 return Value(asm_flless_than(af, bf) ? af : bf);
83 }
84 return a < b ? a : b;
85}
86
87Value StdLib::math_max(const std::vector<Value> &args, VM *vm)
88{
89 checkArgCount(args, 2, "math_max");
90 const Value &a = args[0];
91 const Value &b = args[1];
92 if (a.isInt() && b.isInt())
93 {
94 int64_t ai = a.asInt();
95 int64_t bi = b.asInt();
96 return Value(ai > bi ? ai : bi);
97 }
98 if (a.isNumber() && b.isNumber())
99 {
100 double af = a.asFloat();
101 double bf = b.asFloat();
102 return Value(af > bf ? af : bf);
103 }
104 return a > b ? a : b;
105}
106
107Value StdLib::math_log(const std::vector<Value> &args, VM *vm)
108{
109 checkArgCount(args, 1, "math_log");
110 return Value(asm_log(args[0].asFloat()));
111}
112
113Value StdLib::math_exp(const std::vector<Value> &args, VM *vm)
114{
115 checkArgCount(args, 1, "math_exp");
116 return Value(asm_exp(args[0].asFloat()));
117}
118
119Value StdLib::math_sin(const std::vector<Value> &args, VM *vm)
120{
121 checkArgCount(args, 1, "math_sin");
122 return Value(asm_sin(args[0].asFloat()));
123}
124
125Value StdLib::math_cos(const std::vector<Value> &args, VM *vm)
126{
127 checkArgCount(args, 1, "math_cos");
128 return Value(asm_cos(args[0].asFloat()));
129}
130
131Value StdLib::math_tan(const std::vector<Value> &args, VM *vm)
132{
133 checkArgCount(args, 1, "math_tan");
134 return Value(asm_tan(args[0].asFloat()));
135}
136} // namespace Phasor
double asm_tan(double a)
Native tangent.
Definition crt.c:89
double asm_log(double a)
Native natural logarithm.
Definition crt.c:69
double asm_sqrt(double a)
Native square root.
Definition crt.c:59
double asm_sin(double a)
Native sine.
Definition crt.c:79
double asm_exp(double a)
Native exponential.
Definition crt.c:74
double asm_cos(double a)
Native cosine.
Definition crt.c:84
double asm_pow(double a, double b)
Native power.
Definition crt.c:64
static Value math_sin(const std::vector< Value > &args, VM *vm)
Sine.
Definition math.cpp:119
static Value math_max(const std::vector< Value > &args, VM *vm)
Maximum.
Definition math.cpp:87
static Value math_floor(const std::vector< Value > &args, VM *vm)
Floor.
Definition math.cpp:46
static Value math_ceil(const std::vector< Value > &args, VM *vm)
Ceiling.
Definition math.cpp:53
static Value math_round(const std::vector< Value > &args, VM *vm)
Round.
Definition math.cpp:60
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static void registerMathFunctions(VM *vm)
Definition math.cpp:6
static Value math_pow(const std::vector< Value > &args, VM *vm)
Power.
Definition math.cpp:29
static Value math_tan(const std::vector< Value > &args, VM *vm)
Tangent.
Definition math.cpp:131
static Value math_log(const std::vector< Value > &args, VM *vm)
Natural logarithm.
Definition math.cpp:107
static Value math_exp(const std::vector< Value > &args, VM *vm)
Exponential.
Definition math.cpp:113
static Value math_cos(const std::vector< Value > &args, VM *vm)
Cosine.
Definition math.cpp:125
static Value math_abs(const std::vector< Value > &args, VM *vm)
Absolute value.
Definition math.cpp:37
static Value math_sqrt(const std::vector< Value > &args, VM *vm)
Square root.
Definition math.cpp:23
static Value math_min(const std::vector< Value > &args, VM *vm)
Minimum.
Definition math.cpp:67
Virtual Machine.
Definition VM.hpp:30
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
A value in the Phasor VM.
Definition Value.hpp:67
double asFloat() const
Get the value as a double.
Definition Value.hpp:214
bool isNumber() const
Check if the value is a number.
Definition Value.hpp:189
int64_t asInt() const
Get the value as an integer.
Definition Value.hpp:205
bool isInt() const
Check if the value is an integer.
Definition Value.hpp:174
int64_t asm_iless_than(int64_t a, int64_t b)
Native Less than comparison.
Definition crt.c:35
int64_t asm_flless_than(double a, double b)
Definition crt.c:87
The Phasor Programming Language and Runtime.
Definition AST.hpp:11