Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
io.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2
3static std::string toHex(int value)
4{
5 std::stringstream ss;
6 ss << std::showbase << std::hex << value;
7 return ss.str();
8}
9
10namespace Phasor
11{
12
27
28#ifndef SANDBOXED
29Value StdLib::io_clear(const std::vector<Value> &args, VM *vm)
30{
31 checkArgCount(args, 0, "clear");
32 vm->regRun(OpCode::PRINT_R, "\033[2J\033[H");
33 return Value();
34}
35#endif
36
37std::string StdLib::io_c_format(const std::vector<Value> &args, VM *)
38{
39 if (args.empty())
40 {
41 return ""; // Return empty string if no arguments
42 }
43
44 const std::string &fmt = args[0].asString();
45 std::string out;
46 size_t argIndex = 1; // Start from 1 because args[0] is the format string
47
48 for (size_t i = 0; i < fmt.size(); ++i)
49 {
50 if (fmt[i] == '%' && i + 1 < fmt.size())
51 {
52 char spec = fmt[++i]; // Move to the format specifier
53 if (argIndex < args.size())
54 {
55 const Value &v = args[argIndex++];
56 switch (spec)
57 {
58 case 's':
59 case 'c':
60 out += v.asString();
61 break;
62 case 'd':
63 out += std::to_string(v.asInt());
64 break;
65 case 'f':
66 out += std::to_string(v.asFloat());
67 break;
68 case '%':
69 out += '%';
70 break;
71 case 'x':
72 out += "0x" + toHex((int)v.asInt());
73 break;
74 default:
75 // Unknown specifier, include it literally
76 out += '%';
77 out += spec;
78 break;
79 }
80 continue;
81 }
82 }
83 out += fmt[i];
84 }
85 return out;
86}
87
88std::string StdLib::io_prints(const std::vector<Value> &args, VM *vm)
89{
90 checkArgCount(args, 1, "prints");
91 vm->regRun(OpCode::PRINT_R, args[0]);
92 return "";
93}
94
95std::string StdLib::io_printf(const std::vector<Value> &args, VM *vm)
96{
97 checkArgCount(args, 1, "printf", true);
98 std::vector<Value> formatArgs(args.begin(), args.end());
99 vm->regRun(OpCode::PRINT_R, io_c_format(formatArgs, vm));
100 return "";
101}
102
103std::string StdLib::io_puts(const std::vector<Value> &args, VM *vm)
104{
105 checkArgCount(args, 1, "puts", true);
106 std::string input = args[0].toString();
107 vm->regRun(OpCode::PRINT_R, input + "\n");
108 return "";
109}
110
111std::string StdLib::io_putf(const std::vector<Value> &args, VM *vm)
112{
113 checkArgCount(args, 1, "putf", true);
114 std::vector<Value> formatArgs(args.begin(), args.end());
115 std::string input = io_c_format(formatArgs, vm);
116 vm->regRun(OpCode::PRINT_R, input + "\n");
117 return "";
118}
119
120#ifndef SANDBOXED
121Value StdLib::io_gets(const std::vector<Value> &args, VM *vm)
122{
123 checkArgCount(args, 0, "gets");
125}
126#endif
127
128std::string StdLib::io_puts_error(const std::vector<Value> &args, VM *vm)
129{
130 checkArgCount(args, 1, "puts_error", true);
131 std::string input = args[0].toString();
132 vm->regRun(OpCode::PRINTERROR_R, input + "\n");
133 return "";
134}
135
136std::string StdLib::io_putf_error(const std::vector<Value> &args, VM *vm)
137{
138 checkArgCount(args, 1, "putf_error", true);
139 std::vector<Value> formatArgs(args.begin(), args.end());
140 std::string input = io_c_format(formatArgs, vm);
141 vm->regRun(OpCode::PRINTERROR_R, input + "\n");
142 return "";
143}
144} // namespace Phasor
#define REGISTER1
Definition VM.hpp:151
static Value io_clear(const std::vector< Value > &args, VM *vm)
Clear the console.
Definition io.cpp:29
static void registerIOFunctions(VM *vm)
Definition io.cpp:13
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static std::string io_putf(const std::vector< Value > &args, VM *vm)
Print formatted string with newline.
Definition io.cpp:111
static std::string io_c_format(const std::vector< Value > &args, VM *vm)
Format string.
Definition io.cpp:37
static std::string io_puts(const std::vector< Value > &args, VM *vm)
Print string with newline.
Definition io.cpp:103
static std::string io_puts_error(const std::vector< Value > &args, VM *vm)
Print string with newline to error output.
Definition io.cpp:128
static std::string io_prints(const std::vector< Value > &args, VM *vm)
Print string without newline.
Definition io.cpp:88
static std::string io_printf(const std::vector< Value > &args, VM *vm)
Print formatted string.
Definition io.cpp:95
static Value io_gets(const std::vector< Value > &args, VM *vm)
Get string.
Definition io.cpp:121
static std::string io_putf_error(const std::vector< Value > &args, VM *vm)
Print formatted string with newline to error output.
Definition io.cpp:136
Virtual Machine.
Definition VM.hpp:33
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
Value regRun(OpCode opcode, Args &&...args)
Run an opcode with arguments pre-loaded into registers.
Definition VM.hpp:208
A value in the Phasor VM.
Definition Value.hpp:58
int64_t asInt() const noexcept
Get the value as an integer.
Definition Value.hpp:159
double asFloat() const noexcept
Get the value as a double.
Definition Value.hpp:172
std::string asString() const noexcept
Get the value as a string.
Definition Value.hpp:185
static std::string toHex(int value)
Definition io.cpp:3
The Phasor Programming Language and Runtime.
Definition AST.hpp:12
@ PRINTERROR_R
Print register to stderr: printerror(R[rA]).
Definition ISA.hpp:155
@ READLINE_R
Read line into register: readline(R[rA]).
Definition ISA.hpp:156
@ PRINT_R
Print register: print(R[rA]).
Definition ISA.hpp:154