-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathio_native.c
More file actions
156 lines (139 loc) · 4.59 KB
/
io_native.c
File metadata and controls
156 lines (139 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// --------------------------------------------------
// Project: ProX Programming Language (ProXPL)
// Author: ProgrammerKR
// Created: 2025-12-16
// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved.
// --------------------------------------------------
/*
* ProXPL Standard Library - I/O Module (Native)
* Native C implementation of low-level I/O functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "vm.h"
#include "value.h"
#include "object.h"
#include "memory.h"
extern VM vm;
// Helper
static void defineModuleFn(ObjModule* module, const char* name, NativeFn function) {
ObjString* nameObj = copyString(name, (int)strlen(name));
push(&vm, OBJ_VAL(nameObj));
push(&vm, OBJ_VAL(newNative(function)));
tableSet(&module->exports, nameObj, peek(&vm, 0));
pop(&vm);
pop(&vm);
}
// print_raw(str) - Print string to stdout without newline
static Value native_print_raw(int argCount, Value* args) {
if (argCount > 0) {
if (IS_STRING(args[0])) {
printf("%s", AS_CSTRING(args[0]));
} else {
printValue(args[0]);
}
}
return NIL_VAL;
}
// eprint_raw(str) - Print string to stderr
static Value native_eprint_raw(int argCount, Value* args) {
if (argCount > 0) {
if (IS_STRING(args[0])) {
fprintf(stderr, "%s", AS_CSTRING(args[0]));
} else {
// printValue prints to stdout usually, so manual handling might be needed
// For now, just print what we can or rely on printValue if it was flexible
// But let's assume we pass strings primarily
fprintf(stderr, "<value>");
}
}
return NIL_VAL;
}
// println(val) - Print value with newline
static Value native_println(int argCount, Value* args) {
if (argCount > 0) {
if (IS_STRING(args[0])) {
printf("%s\n", AS_CSTRING(args[0]));
} else {
printValue(args[0]);
printf("\n");
}
} else {
printf("\n");
}
return NIL_VAL;
}
// input_raw() - Read line from stdin
static Value native_input_raw(int argCount, Value* args) {
char buffer[1024];
if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
// Remove newline(s)
size_t len = strlen(buffer);
while (len > 0 && (buffer[len-1] == '\n' || buffer[len-1] == '\r')) {
buffer[len-1] = '\0';
len--;
}
return OBJ_VAL(copyString(buffer, (int)len));
}
return NIL_VAL;
}
// input(prompt) - Display prompt and read line from stdin
static Value native_input(int argCount, Value* args) {
// Print optional prompt
if (argCount > 0 && IS_STRING(args[0])) {
printf("%s", AS_CSTRING(args[0]));
fflush(stdout);
}
// Read input
char buffer[1024];
if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
// Remove newline(s)
size_t len = strlen(buffer);
while (len > 0 && (buffer[len-1] == '\n' || buffer[len-1] == '\r')) {
buffer[len-1] = '\0';
len--;
}
return OBJ_VAL(copyString(buffer, (int)len));
}
return NIL_VAL;
}
// flush_raw() - Flush stdout
static Value native_flush_raw(int argCount, Value* args) {
fflush(stdout);
return NIL_VAL;
}
// set_color_raw(code) - Set terminal color using ANSI
static Value native_set_color_raw(int argCount, Value* args) {
if (argCount > 0 && IS_NUMBER(args[0])) {
int code = (int)AS_NUMBER(args[0]);
printf("\033[%dm", code);
}
return NIL_VAL;
}
// Register all I/O functions with the VM
ObjModule* create_std_io_module() {
ObjString* name = copyString("std.native.io", 13);
push(&vm, OBJ_VAL(name));
ObjModule* module = newModule(name);
push(&vm, OBJ_VAL(module));
defineModuleFn(module, "print_raw", native_print_raw);
defineModuleFn(module, "print", native_print_raw);
defineModuleFn(module, "write", native_print_raw);
defineModuleFn(module, "println", native_println);
defineModuleFn(module, "eprint_raw", native_eprint_raw);
defineModuleFn(module, "input_raw", native_input_raw);
defineModuleFn(module, "input", native_input);
defineModuleFn(module, "flush_raw", native_flush_raw);
defineModuleFn(module, "set_color_raw", native_set_color_raw);
pop(&vm); // module
pop(&vm); // name
return module;
}
// Register I/O functions as globals (for convenience)
void register_io_globals(VM* pVM) {
defineNative(pVM, "print", native_print_raw);
defineNative(pVM, "println", native_println);
defineNative(pVM, "input", native_input);
}