forked from shadymeowy/tinyscheme-embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyScheme_64.c
96 lines (77 loc) · 1.92 KB
/
TinyScheme_64.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
// gcc scheme.c example-repl-simple.c -o example-repl-simple.out
#include "scheme.h"
#include "scheme-private.h"
#include <libdragon.h>
#include "usb.h"
#define MAX_BUFFER_SIZE 1024
int incoming_size = 0;
char incoming_type = 0;
void read_line( char *buf){
while (usb_poll() != 0){
uint32_t header = usb_poll();
// Store the size from the header
incoming_size = USBHEADER_GETSIZE(header);
incoming_type = USBHEADER_GETTYPE(header);
// If the amount of data is larger than our echo buffer
if (incoming_size > 1024)
{
// Purge the USB data
usb_purge();
// Write an error message to buffer instead
sprintf(buf, "Incoming data larger than echo buffer!\n");
incoming_size = strlen(buf)+1;
incoming_type = DATATYPE_TEXT;
// Restart the while loop to check if there's more USB data
continue;
}
// Read the data from the USB into our echo buffer
usb_read(buf, incoming_size);
}
int i = 0;
while (i < incoming_size - 1) {
char c = buf[i];
if (c == '\n') {
break;
}
buf[i++] = c;
}
buf[i] = '\0';
}
int main( int argc, char **argv ){
scheme *sc;
pointer write;
pointer args;
if (!(sc = scheme_init_new())) {
return 2;
}
console_init();
console_set_render_mode(RENDER_MANUAL);
console_clear();
usb_initialize();
printf("\n\nTinyScheme 64\n");
usb_write(DATATYPE_TEXT, "\n\nTinyScheme 64\n ", 17);
scheme_set_input_port_file(sc, stdin);
scheme_set_output_port_file(sc, stdout);
write = scheme_eval(sc, mk_symbol(sc, "write"));
console_render();
while (1) {
char buf[MAX_BUFFER_SIZE];
read_line( buf);
if (incoming_size > 0){
printf("> ");
printf(buf);
printf("\n");
scheme_load_string(sc, buf);
args = cons(sc, sc->value, sc->NIL);
scheme_call(sc, write, args);
printf("\n\n");
console_render();
// Clear everything
incoming_type = 0;
incoming_size = 0;
memset(buf, 0, 1024);
}
}
scheme_deinit(sc);
return 0;
}