Phasor 3.1.1
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
37Value StdLib::io_c_format(const std::vector<Value> &args, VM *)
38{
39 if (args.empty())
40 {
41 return Value(""); // 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 Value(out);
86}
87
88Value 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 Value("");
93}
94
95Value 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 Value("");
101}
102
103Value 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 Value("");
109}
110
111Value 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).toString();
116 vm->regRun(OpCode::PRINT_R, input + "\n");
117 return Value("");
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
128Value 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 Value("");
134}
135
136Value 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).toString();
141 vm->regRun(OpCode::PRINTERROR_R, input + "\n");
142 return Value("");
143}
144} // namespace Phasor
#define REGISTER1
Definition VM.hpp:171
static Value io_clear(const std::vector< Value > &args, VM *vm)
Clear the console.
Definition io.cpp:29
static Value io_printf(const std::vector< Value > &args, VM *vm)
Print formatted string.
Definition io.cpp:95
static Value io_puts_error(const std::vector< Value > &args, VM *vm)
Print string with newline to error output.
Definition io.cpp:128
static void registerIOFunctions(VM *vm)
Definition io.cpp:13
static Value io_puts(const std::vector< Value > &args, VM *vm)
Print string with newline.
Definition io.cpp:103
static Value io_putf(const std::vector< Value > &args, VM *vm)
Print formatted string with newline.
Definition io.cpp:111
static Value io_prints(const std::vector< Value > &args, VM *vm)
Print string without newline.
Definition io.cpp:88
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static Value io_c_format(const std::vector< Value > &args, VM *vm)
Format string.
Definition io.cpp:37
static Value io_putf_error(const std::vector< Value > &args, VM *vm)
Print formatted string with newline to error output.
Definition io.cpp:136
static Value io_gets(const std::vector< Value > &args, VM *vm)
Get string.
Definition io.cpp:121
Virtual Machine.
Definition VM.hpp:30
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:227
A value in the Phasor VM.
Definition Value.hpp:67
std::string toString() const
Convert to string for printing.
Definition Value.hpp:415
double asFloat() const
Get the value as a double.
Definition Value.hpp:214
std::string asString() const
Get the value as a string.
Definition Value.hpp:223
int64_t asInt() const
Get the value as an integer.
Definition Value.hpp:205
static std::string toHex(int value)
Definition io.cpp:3
The Phasor Programming Language and Runtime.
Definition AST.hpp:11
@ 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