Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
typeconv.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2#include <phsint.hpp>
3
4namespace Phasor
5{
6
14
15i64 StdLib::to_int(const std::vector<Value> &args, VM *)
16{
17 checkArgCount(args, 1, "to_int");
18 if (args[0].isInt())
19 return args[0].asInt();
20 if (args[0].isFloat())
21 return static_cast<i64>(args[0].asFloat());
22 if (args[0].isString())
23 {
24 try
25 {
26 return static_cast<i64>(std::stoll(args[0].asString()));
27 }
28 catch (...)
29 {
30 return 0;
31 }
32 }
33 if (args[0].isBool())
34 return args[0].asBool() ? 1 : 0;
35 return 0;
36}
37
38f64 StdLib::to_float(const std::vector<Value> &args, VM *)
39{
40 checkArgCount(args, 1, "to_float");
41 return args[0].asFloat();
42}
43
44PhsString StdLib::to_string(const std::vector<Value> &args, VM *)
45{
46 checkArgCount(args, 1, "to_string");
47 return args[0].toString();
48}
49
50bool StdLib::to_bool(const std::vector<Value> &args, VM *)
51{
52 checkArgCount(args, 1, "to_bool");
53 return args[0].isTruthy();
54}
55
56} // namespace Phasor
static f64 to_float(const std::vector< Value > &args, VM *vm)
Convert to float.
Definition typeconv.cpp:38
static void registerTypeConvFunctions(VM *vm)
Definition typeconv.cpp:7
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static PhsString to_string(const std::vector< Value > &args, VM *vm)
Convert to string.
Definition typeconv.cpp:44
static bool to_bool(const std::vector< Value > &args, VM *vm)
Convert to boolean.
Definition typeconv.cpp:50
static i64 to_int(const std::vector< Value > &args, VM *vm)
Convert to integer.
Definition typeconv.cpp:15
Virtual Machine.
Definition VM.hpp:36
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
The Phasor Programming Language and Runtime.
Definition AST.hpp:13
int64_t i64
Definition phsint.hpp:16
double f64
Definition phsint.hpp:7