-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.cpp
159 lines (131 loc) · 3.44 KB
/
reader.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
152
153
154
155
156
157
158
#include "vm.h"
#include "assert.h"
#include "reader.h"
#include "value_helpers.h"
#include "err.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//
// 1 the definition of lispobj * and utility functions like car, cdr, cons -- at the C level, not the VM level)
// 2 the VM - stack manipulation, function calls ,etc
// 3 a reader - something that takes a string/stream and returns a lispobj* from it
// 4 compiler - given a lispobj * generate bytecode for it
//
//
// eval(const char *s) {
// lispobj *p = read(s);
// lispobj *fn = compile(p);
// lispobj *result = execute(fn);
// return results;
// }
stream_t *stream_create(char const *code)
{
stream_t * stream = (stream_t *)malloc(sizeof(stream_t));
stream->code = code;
stream->index = 0;
stream->length = strlen(code);
return stream;
}
void stream_destroy(stream_t *p_stream)
{
free(p_stream);
}
value_t *read_atom(vm_t *p_vm, stream_t *p_stream)
{
char atom[1024];
int index = 0;
bool non_numeric = false;
bool is_string = false;
atom[0] = 0;
// Check if this is a string
if(p_stream->pop() == '\"') {
is_string = true;
} else {
p_stream->restore();
}
while(p_stream->index < p_stream->length) {
char val = p_stream->pop(is_string);
if (((is_string == false) && ((val == ' ') || (val == '\n') || (val == '\r') || (val == '(') || (val == ')'))) || ((is_string == true) && (val == '\"'))) {
atom[index] = 0;
value_t *ret = 0;
if (is_string == true) {
ret = value_create_string(p_vm, atom);
} else if (non_numeric == false && (index != 1 || atom[0] != '-')) {
int i = atoi(atom);
ret = make_fixnum(i);
} else {
ret = value_create_symbol(p_vm, atom);
}
if ((is_string == false) && ((val == '(') || (val == ')'))) {
p_stream->restore();
}
return ret;
}
if (val > '9' || val < '0') {
if ((index != 0) || (val != '-')) {
non_numeric = true;
}
}
atom[index++] = val;
verify(index < 1024, "Atom is greater than 1024 characters");
}
atom[index] = 0;
value_t *ret = 0;
if (is_string == true) {
ret = value_create_string(p_vm, atom);
} else if (non_numeric == false && (index != 1 || atom[0] != '-')) {
int i = atoi(atom);
ret = make_fixnum(i);
} else {
ret = value_create_symbol(p_vm, atom);
}
return ret;
}
void process_next(vm_t *p_vm, stream_t *p_stream)
{
char val = p_stream->pop();
if (val == '(') {
// Need to read in rest
int args = reader(p_vm, p_stream, true);
// Need to change this to vm_list since there could be more than two
// args in the list, this will also let me handle the list of size
// 0, otherwise known as nil
if (args == 0) {
vm_push(p_vm, p_vm->nil);
} else {
vm_list(p_vm, args);
}
} else {
// This is an atom (symbol or fixnum)
p_stream->restore();
value_t *ret = read_atom(p_vm, p_stream);
vm_push(p_vm, ret);
}
return;
}
int reader(vm_t *p_vm, stream_t *p_stream, bool p_in_list)
{
int list_size = 0;
while(p_stream->index < p_stream->length) {
char val = p_stream->pop();
if ((val == ' ') || (val == '\n') || (val == '\r') || (val == '\t')) {
// Clear out white space
continue;
} else if (val == '\'') {
value_t *qt = value_create_symbol(p_vm, "QUOTE");
vm_push(p_vm, qt);
process_next(p_vm, p_stream);
vm_list(p_vm, 2);
list_size++;
} else if (val == ')') {
assert(p_in_list);
return list_size;
} else {
p_stream->restore();
process_next(p_vm, p_stream);
list_size++;
}
}
return list_size;
}