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