-
Notifications
You must be signed in to change notification settings - Fork 0
/
native.c
204 lines (179 loc) · 5.64 KB
/
native.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mas.h"
#define NATIVE_LIB_NAME "mas.lang.file"
static MAS_NativePointerInfo st_native_lib_info = {
NATIVE_LIB_NAME
};
static void add_ref_in_env(LocalEnvironment* env, MAS_Object* object) {
// fprintf(stderr, "add_ref_in_env called(%p)\n", object);
RefInNativeFunc* ref = (RefInNativeFunc*)MEM_malloc(sizeof(RefInNativeFunc));
ref->next = env->ref_in_native;
env->ref_in_native = ref;
ref->object = object;
}
MAS_Value mas_nv_fgets(MAS_Interpreter* interp, LocalEnvironment* env,
int arg_count, MAS_Value* args) {
MAS_Value v;
FILE *fp;
char buf[LINE_BUF_SIZE];
char* ret_buf = NULL;
int ret_len = 0;
if (arg_count < 1) {
mas_runtime_error(0, ARGUMENT_TOO_FEW_ERR,
MESSAGE_ARGUMENT_END);
}
if (arg_count > 1) {
mas_runtime_error(0, ARGUMENT_TOO_MANY_ERR,
MESSAGE_ARGUMENT_END);
}
if (args[0].type != MAS_NATIVE_POINTER_VALUE) {
mas_runtime_error(0, FGETS_ARGUMENT_TYPE_ERR,
MESSAGE_ARGUMENT_END);
}
fp = args[0].u.native_pointer.pointer;
while(fgets(buf, LINE_BUF_SIZE, fp)) {
int new_len;
new_len = ret_len + strlen(buf);
ret_buf = MEM_realloc(ret_buf, new_len + 1);
if (ret_len == 0) {
strcpy(ret_buf, buf);
} else {
strcat(ret_buf, buf);
}
ret_len = new_len;
if(ret_buf[ret_len - 1] == '\n') {
break;
}
}
if (ret_len > 0) {
v.type = MAS_STRING_VALUE;
MAS_Object* new_obj = mas_create_mas_ostring(interp, ret_buf);
add_ref_in_env(env, new_obj);
v.u.object_value = new_obj;
} else {
v.type = MAS_NULL_VALUE;
}
return v;
}
MAS_Value mas_nv_fputs(MAS_Interpreter* interp, LocalEnvironment* env,
int arg_count, MAS_Value* args) {
MAS_Value v;
FILE* fp;
if (arg_count < 2) {
mas_runtime_error(0, ARGUMENT_TOO_FEW_ERR,
MESSAGE_ARGUMENT_END);
}
if (arg_count > 2) {
mas_runtime_error(0, ARGUMENT_TOO_MANY_ERR,
MESSAGE_ARGUMENT_END);
}
if (args[0].type != MAS_STRING_VALUE || args[1].type != MAS_NATIVE_POINTER_VALUE) {
mas_runtime_error(0, FPUTS_ARGUMENT_TYPE_ERR,
MESSAGE_ARGUMENT_END);
}
// char* str = args[0].u.string_value->string;
char* str = args[0].u.object_value->u.string.string;
fp = args[1].u.native_pointer.pointer;
fputs(str, fp);
v.type = MAS_NULL_VALUE;
return v;
}
MAS_Value mas_nv_open(MAS_Interpreter* interp, LocalEnvironment* env,
int arg_count, MAS_Value* args) {
MAS_Value v;
if (arg_count < 2) {
mas_runtime_error(0, ARGUMENT_TOO_FEW_ERR,
MESSAGE_ARGUMENT_END);
}
if (arg_count > 2) {
mas_runtime_error(0, ARGUMENT_TOO_MANY_ERR,
MESSAGE_ARGUMENT_END);
}
if (args[0].type != MAS_STRING_VALUE || args[1].type != MAS_STRING_VALUE) {
mas_runtime_error(0, FOPEN_ARGUMENT_TYPE_ERR,
MESSAGE_ARGUMENT_END);
}
FILE* fp = fopen(args[0].u.object_value->u.string.string,
args[1].u.object_value->u.string.string);
if (fp == NULL) {
exit(1);
v.type = MAS_NULL_VALUE;
v.u.native_pointer.pointer = NULL;
} else {
v.type = MAS_NATIVE_POINTER_VALUE;
v.u.native_pointer.info = &st_native_lib_info;
v.u.native_pointer.pointer = fp;
}
return v;
}
MAS_Value mas_nv_close(MAS_Interpreter* interp, LocalEnvironment* env,
int arg_count, MAS_Value* args) {
MAS_Value v;
if (arg_count < 1) {
mas_runtime_error(0, ARGUMENT_TOO_FEW_ERR,
MESSAGE_ARGUMENT_END);
}
if (arg_count > 1) {
mas_runtime_error(0, ARGUMENT_TOO_MANY_ERR,
MESSAGE_ARGUMENT_END);
}
if (args[0].type != MAS_NATIVE_POINTER_VALUE) {
mas_runtime_error(0, FCLOSE_ARGUMENT_TYPE_ERR,
MESSAGE_ARGUMENT_END);
}
fclose(args[0].u.native_pointer.pointer);
v.type = MAS_NULL_VALUE;
return v;
}
MAS_Value mas_nv_print(MAS_Interpreter* interp, LocalEnvironment* env,
int arg_count, MAS_Value* args) {
MAS_Value v;
v.type = MAS_NULL_VALUE;
if (arg_count < 1) {
mas_runtime_error(0, ARGUMENT_TOO_FEW_ERR,
MESSAGE_ARGUMENT_END);
}
if (arg_count > 1) {
mas_runtime_error(0, ARGUMENT_TOO_MANY_ERR,
MESSAGE_ARGUMENT_END);
}
switch (args[0].type) {
case MAS_STRING_VALUE: {
printf("%s", args[0].u.object_value->u.string.string);
break;
}
case MAS_INT_VALUE: {
printf("%d", args[0].u.int_value);
break;
}
case MAS_BOOLEAN_VALUE: {
if (args[0].u.boolean_value == MAS_TRUE) {
printf("true");
} else {
printf("false");
}
break;
}
case MAS_DOUBLE_VALUE: {
printf("%lf", args[0].u.double_value);
break;
}
case MAS_NULL_VALUE: {
printf("null");
break;
}
case MAS_NATIVE_POINTER_VALUE: {
printf("(%s:%p)", args[0].u.native_pointer.info->name,
args[0].u.native_pointer.pointer);
break;
}
default: {
fprintf(stderr, "no mas type(%d) in mas_nv_print\n", args[0].type);
exit(1);
break;
}
}
return v;
}