forked from pocketpy/xtensor-numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (151 loc) · 4.72 KB
/
main.cpp
File metadata and controls
170 lines (151 loc) · 4.72 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
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "pocketpy.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
extern "C" bool py_module_initialize();
static char* readfile(const char* path, int* data_size) {
FILE* f = fopen(path, "rb");
if(f == NULL) return NULL;
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
char* buffer = (char*)PK_MALLOC(size + 1);
size = fread(buffer, 1, size, f);
buffer[size] = 0;
fclose(f);
if(data_size) *data_size = (int)size;
return buffer;
}
static char buf[2048];
int main(int argc, char** argv) {
#if _WIN32
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
#endif
bool profile = false;
bool debug = false;
bool compile = false;
const char* arg1 = NULL;
const char* arg2 = NULL;
for(int i = 1; i < argc; i++) {
if(strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
printf("Usage: pocketpy [--profile] [--debug] [--compile] filename\n");
printf(" pocketpy --help\n");
return 1;
}
if(strcmp(argv[i], "--profile") == 0) {
profile = true;
continue;
}
if(strcmp(argv[i], "--debug") == 0) {
debug = true;
continue;
}
if(strcmp(argv[i], "--compile") == 0) {
compile = true;
continue;
}
if(arg1 == NULL) {
arg1 = argv[i];
continue;
}
if(arg2 == NULL) {
arg2 = argv[i];
continue;
}
printf("Usage: pocketpy [--profile] [--debug] [--compile] filename\n");
}
if(debug && profile) {
printf("Error: --debug and --profile cannot be used together.\n");
return 1;
}
if(compile && (debug || profile)) {
printf("Error: --compile cannot be used with --debug or --profile.\n");
return 1;
}
py_initialize();
py_module_initialize(); // Register the numpy module
py_sys_setargv(argc, argv);
if(compile) {
bool ok = py_compilefile(arg1, arg2);
if(!ok) py_printexc();
py_finalize();
return ok ? 0 : 1;
}
const char* filename = arg1;
if(filename == NULL) {
if(profile) printf("Warning: --profile is ignored in REPL mode.\n");
if(debug) printf("Warning: --debug is ignored in REPL mode.\n");
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
#ifndef NDEBUG
printf(" (DEBUG)");
#endif
printf("\n");
printf("https://github.com/pocketpy/pocketpy\n");
printf("Type \"exit()\" to exit.\n");
while(true) {
int size = py_replinput(buf, sizeof(buf));
if(size == -1) { // Ctrl-D (i.e. EOF)
printf("\n");
break;
}
assert(size < (int)sizeof(buf));
if(size >= 0) {
py_StackRef p0 = py_peek(0);
if(!py_exec(buf, "<stdin>", SINGLE_MODE, NULL)) {
py_printexc();
py_clearexc(p0);
}
}
}
} else {
if(profile) py_profiler_begin();
if(debug) py_debugger_waitforattach("127.0.0.1", 6110);
int data_size;
char* data = readfile(filename, &data_size);
// check filename endswith .pyc
bool is_pyc = false;
int filename_len = (int)strlen(filename);
if(filename_len >= 4) {
if(filename[filename_len - 4] == '.' &&
filename[filename_len - 3] == 'p' &&
filename[filename_len - 2] == 'y' &&
filename[filename_len - 1] == 'c') {
is_pyc = true;
}
}
if(data) {
bool ok;
if(is_pyc) {
ok = py_execo(data, data_size, filename, NULL);
} else {
ok = py_exec(data, filename, EXEC_MODE, NULL);
}
if(!ok) py_printexc();
if(profile) {
char* json_report = py_profiler_report();
FILE* report_file = fopen("profiler_report.json", "w");
if(report_file) {
fprintf(report_file, "%s", json_report);
fclose(report_file);
}
PK_FREE(json_report);
}
PK_FREE(data);
} else {
printf("Error: cannot open file '%s'\n", filename);
py_finalize();
return 1;
}
}
int code = py_checkexc() ? 1 : 0;
py_finalize();
if(debug) py_debugger_exit(code);
return code;
}