-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.c
More file actions
215 lines (193 loc) · 4.06 KB
/
main.c
File metadata and controls
215 lines (193 loc) · 4.06 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
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
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#ifdef READLINE
#include <readline/readline.h>
#include <readline/history.h>
#elif defined(LINENOISE)
#include "linenoise.h"
#endif
#include "tinyscript.h"
#include "tinyscript_lib.h"
#ifdef __propeller__
#include <propeller.h>
#define ARENA_SIZE 2048
#else
#define ARENA_SIZE 8192
#define MAX_SCRIPT_SIZE 100000
#endif
int inchar() {
return getchar();
}
void outchar(int c) {
putchar(c);
}
void * ts_malloc(Val size) {
return malloc(size);
}
void ts_free(void * pointer) {
free(pointer);
}
void _ts_printf(ts_list *format, ...) {
va_list args;
va_start(args, format);
char *format_string = ts_list_to_string(format);
vprintf(format_string, args);
ts_free(format_string);
}
void ts_printf(ts_list *format, Val a) {
_ts_printf(format, a);
}
void ts_printf_(ts_list *format, Val a, Val b) {
_ts_printf(format, a, b);
}
void ts_printf__(ts_list *format, Val a, Val b, Val c) {
_ts_printf(format, a, b, c);
}
#ifdef MAX_SCRIPT_SIZE
char script[MAX_SCRIPT_SIZE];
void
runscript(const char *filename)
{
FILE *f = fopen(filename, "r");
int r;
if (!f) {
perror(filename);
return;
}
r=fread(script, 1, MAX_SCRIPT_SIZE, f);
fclose(f);
if (r <= 0) {
fprintf(stderr, "File read error on %s\n", filename);
return;
}
script[r] = 0;
r = TinyScript_Run(script, 0, 1);
if (r != 0) {
printf("script error %d\n", r);
}
exit(r);
}
#endif
#ifdef __propeller__
static Val getcnt_fn()
{
#ifdef CNT
return CNT;
#else
return _cnt();
#endif
}
static Val waitcnt_fn(Val when)
{
waitcnt(when);
return when;
}
static Val pinout_fn(Val pin, Val onoff)
{
unsigned mask = 1<<pin;
DIRA |= mask;
if (onoff) {
OUTA |= mask;
} else {
OUTA &= ~mask;
}
return OUTA;
}
static Val pinin_fn(Val pin)
{
unsigned mask=1<<pin;
DIRA &= ~mask;
return (INA & mask) ? 1 : 0;
}
#else
// compute a function of two variables
// used for testing scripts
static Val testfunc(Val x, Val y)
{
return x*x + y*y;
}
#endif
struct def {
const char *name;
intptr_t val;
int nargs;
} funcdefs[] = {
#ifdef __propeller__
{ "getcnt", (intptr_t)getcnt_fn, 0 },
{ "waitcnt", (intptr_t)waitcnt_fn, 1 },
{ "pinout", (intptr_t)pinout_fn, 2 },
{ "pinin", (intptr_t)pinin_fn, 1 },
#else
{ "dsqr", (intptr_t)testfunc, 2 },
{"printf", (intptr_t)ts_printf, 2},
{"printf_", (intptr_t)ts_printf_, 3},
{"printf__", (intptr_t)ts_printf__, 4},
#endif
{ NULL, 0 }
};
void
REPL()
{
int r;
char *buf;
#if defined(READLINE)
read_history("tinyscript_history");
#elif defined(LINENOISE)
linenoiseHistoryLoad("tinyscript_history");
#endif
for(;;) {
#if defined(READLINE)
buf = readline("ts> ");
if (!buf) break;
add_history(buf);
write_history("tinyscript_history");
#elif defined(LINENOISE)
buf = linenoise("ts> ");
if (!buf) break;
linenoiseHistoryAdd(buf);
linenoiseHistorySave("tinyscript_history");
#else
static char sbuf[128];
printf("ts> "); fflush(stdout);
buf = fgets(sbuf, sizeof(sbuf), stdin);
if (!buf) break;
#endif
r = TinyScript_Run(buf, 1, 1);
if (r != 0) {
printf("error %d\n", r);
}
#if defined(READLINE) || defined(LINENOISE)
free(buf);
#endif
}
}
char memarena[ARENA_SIZE];
int
main(int argc, char **argv)
{
int err;
int i;
err = TinyScript_Init(memarena, sizeof(memarena));
for (i = 0; funcdefs[i].name; i++) {
err |= TinyScript_Define(funcdefs[i].name, CFUNC(funcdefs[i].nargs), funcdefs[i].val);
}
err |= ts_define_funcs();
if (err != 0) {
printf("Initialization of interpreter failed!\n");
return 1;
}
#ifdef __propeller__
REPL();
#else
if (argc > 2) {
printf("Usage: tinyscript [file]\n");
}
if (argv[1]) {
runscript(argv[1]);
} else {
REPL();
}
#endif
return 0;
}