-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpc-main.cpp
151 lines (137 loc) · 3.3 KB
/
pc-main.cpp
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
// r4 - A minimal human-readable interpreter
#include "r4.h"
#include <stdlib.h>
#ifdef __PC__
#ifdef __WINDOWS__
CELL_T doMillis() { return (CELL_T)GetTickCount(); }
CELL_T doMicros() { return (CELL_T)doMillis()*1000; }
void doDelay(CELL_T ms) { Sleep((DWORD)ms); }
int qkey() { return _kbhit(); }
int key() { return _getch(); }
#else
// Support for Linux
CELL_T doMillis() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (CELL_T)((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
}
CELL_T doMicros() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (CELL_T)(ts.tv_nsec);
}
void doDelay(CELL_T ms) {
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
#include <unistd.h>
#include <termios.h>
static struct termios normT, rawT;
static int isTtyInit = 0;
void ttyInit() {
tcgetattr( STDIN_FILENO, &normT);
cfmakeraw(&rawT);
isTtyInit = 1;
}
void ttyModeNorm() {
if (!isTtyInit) { ttyInit(); }
tcsetattr( STDIN_FILENO, TCSANOW, &normT);
}
void ttyModeRaw() {
if (!isTtyInit) { ttyInit(); }
tcsetattr( STDIN_FILENO, TCSANOW, &rawT);
}
int qkey() {
struct timeval tv;
fd_set rdfs;
ttyModeRaw();
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rdfs);
FD_SET(STDIN_FILENO, &rdfs);
select(STDIN_FILENO+1, &rdfs, NULL, NULL, &tv);
int x = FD_ISSET(STDIN_FILENO, &rdfs);
ttyModeNorm();
return x;
}
int key() {
ttyModeRaw();
int x = getchar();
ttyModeNorm();
return x;
}
#endif
static char buf[256];
static CELL_T t1, t2;
void printChar(const char c) { printf("%c", c); }
void printString(const char* str) { printf("%s", str); }
CELL_T getSeed() { return doMillis(); }
addr doCustom(byte ir, addr pc) {
switch (ir) {
case 'Q': isBye = 1; break;
case 's': system((char*)pop()); break;
default:
isError = 1;
printString("-notExt-");
}
return pc;
}
void ok() {
printString("\r\nr4:");
dumpStack();
printString(">");
}
void rtrim(char* cp) {
char *x = cp;
while (*x) { ++x; }
--x;
while (*x && (*x < 32) && (cp <= x)) { *(x--) = 0; }
}
void loadCode(const char* src) {
addr in = HERE;
while (*src) {
char c = *(src++);
*(in++) = (c<32) ? 32 : c;
}
*in = 0;
run(HERE);
}
// #define __HISTORY__
void doHistory(char* str) {
#ifdef __HISTORY__
FILE* fp = fopen("history.txt", "at");
if (fp) {
fputs(str, fp);
fclose(fp);
}
#endif
}
void loop() {
if (input_fp) {
int n = fileReadLine(input_fp, buf);
if (n == -1) {
fclose((FILE *)input_fp);
input_fp = fpop();
}
} else {
ok();
fileReadLine((CELL_T)stdin, buf);
doHistory(buf);
rtrim(buf);
}
// doHistory(buf);
loadCode(buf);
}
int main(int argc, char** argv) {
vmInit();
if (1 < argc) { input_fp = (CELL_T)fopen(argv[1], "rt"); }
if (!input_fp) {
loadCode(":CD 0UxIH[IC@#,';=(IPC@':=(N))];");
loadCode("0bL");
}
while (!isBye) { loop(); }
return 0;
}
#endif