-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_native.c
More file actions
171 lines (139 loc) · 4.98 KB
/
convert_native.c
File metadata and controls
171 lines (139 loc) · 4.98 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// --------------------------------------------------
// Project: ProX Programming Language (ProXPL)
// Author: ProgrammerKR
// Created: 2025-12-16
// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved.
/*
* ProXPL Standard Library - Type Conversion Module
* Native C implementation of type conversion functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../include/common.h"
#include "../include/vm.h"
#include "../include/value.h"
#include "../include/object.h"
// to_int(value) - Convert to integer
static Value native_to_int(int argCount, Value* args) {
if (argCount < 1) return NUMBER_VAL(0);
if (IS_NUMBER(args[0])) {
// Cast to int then back to double to truncate decimals
return NUMBER_VAL((double)(int)AS_NUMBER(args[0]));
} else if (IS_STRING(args[0])) {
const char* str = AS_CSTRING(args[0]);
char* endptr;
long value = strtol(str, &endptr, 10);
return NUMBER_VAL((double)value);
} else if (IS_BOOL(args[0])) {
return NUMBER_VAL(AS_BOOL(args[0]) ? 1.0 : 0.0);
}
return NUMBER_VAL(0);
}
// to_float(value) - Convert to float
static Value native_to_float(int argCount, Value* args) {
if (argCount < 1) return NUMBER_VAL(0.0);
if (IS_NUMBER(args[0])) {
return args[0];
} else if (IS_STRING(args[0])) {
const char* str = AS_CSTRING(args[0]);
double value = strtod(str, NULL);
return NUMBER_VAL(value);
} else if (IS_BOOL(args[0])) {
return NUMBER_VAL(AS_BOOL(args[0]) ? 1.0 : 0.0);
}
return NUMBER_VAL(0.0);
}
// to_string(value) - Convert to string
static Value native_to_string(int argCount, Value* args) {
if (argCount < 1) return OBJ_VAL(copyString("", 0));
char buffer[256];
if (IS_NUMBER(args[0])) {
snprintf(buffer, sizeof(buffer), "%.15g", AS_NUMBER(args[0]));
} else if (IS_BOOL(args[0])) {
snprintf(buffer, sizeof(buffer), "%s", AS_BOOL(args[0]) ? "true" : "false");
} else if (IS_NIL(args[0])) {
snprintf(buffer, sizeof(buffer), "null");
} else if (IS_STRING(args[0])) {
return args[0];
} else {
snprintf(buffer, sizeof(buffer), "<object>");
}
return OBJ_VAL(copyString(buffer, (int)strlen(buffer)));
}
// to_bool(value) - Convert to boolean
static Value native_to_bool(int argCount, Value* args) {
if (argCount < 1) return BOOL_VAL(false);
if (IS_BOOL(args[0])) {
return args[0];
} else if (IS_NIL(args[0])) {
return BOOL_VAL(false);
} else if (IS_NUMBER(args[0])) {
return BOOL_VAL(AS_NUMBER(args[0]) != 0);
} else if (IS_STRING(args[0])) {
return BOOL_VAL(AS_STRING(args[0])->length > 0);
}
return BOOL_VAL(true);
}
// to_hex(value) - Convert integer to hexadecimal string
static Value native_to_hex(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) {
return OBJ_VAL(copyString("0x0", 3));
}
int value = (int)AS_NUMBER(args[0]);
char buffer[32];
snprintf(buffer, sizeof(buffer), "0x%x", value);
return OBJ_VAL(copyString(buffer, (int)strlen(buffer)));
}
// to_bin(value) - Convert integer to binary string
static Value native_to_bin(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) {
return OBJ_VAL(copyString("0b0", 3));
}
int value = (int)AS_NUMBER(args[0]);
char buffer[66] = "0b";
int pos = 2;
if (value == 0) {
buffer[pos++] = '0';
} else {
int bits[32];
int count = 0;
unsigned int uval = (unsigned int)value;
while (uval > 0 && count < 32) {
bits[count++] = uval % 2;
uval /= 2;
}
for (int i = count - 1; i >= 0; i--) {
buffer[pos++] = '0' + bits[i];
}
}
buffer[pos] = '\0';
return OBJ_VAL(copyString(buffer, pos));
}
// char_at(str, index) - Get character at index
static Value native_char_at(int argCount, Value* args) {
if (argCount < 2 || !IS_STRING(args[0]) || !IS_NUMBER(args[1])) {
return NULL_VAL;
}
ObjString* str = AS_STRING(args[0]);
int index = (int)AS_NUMBER(args[1]);
if (index < 0 || index >= (int)str->length) {
return NULL_VAL;
}
char ch[2] = {str->chars[index], '\0'};
return OBJ_VAL(copyString(ch, 1));
}
// len(value) - Get length of string or collection
// len(value) moved to stdlib_core.c
// Register all conversion functions with the VM
void register_convert_natives(VM* pVM) {
defineNative(pVM, "to_int", native_to_int);
defineNative(pVM, "to_float", native_to_float);
defineNative(pVM, "to_string", native_to_string);
defineNative(pVM, "to_bool", native_to_bool);
defineNative(pVM, "to_hex", native_to_hex);
defineNative(pVM, "to_bin", native_to_bin);
defineNative(pVM, "char_at", native_char_at);
// defineNative(pVM, "len", native_len); // Moved to stdlib_core.c
}