-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
157 lines (121 loc) · 3.38 KB
/
main.cpp
File metadata and controls
157 lines (121 loc) · 3.38 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
#include "value.h"
#include "value_helpers.h"
#include "reader.h"
#include "compile.h"
#include "env.h"
#include "gc.h"
#include "err.h"
#include "lib.h"
#include "unit-test.h"
#include "vm.h"
//#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
void load_string(vm_t *p_vm, char const *p_code);
void load_string(vm_t *p_vm, char const *p_code)
{
unsigned long vm_bp = p_vm->m_bp;
unsigned long vm_sp = p_vm->m_sp;
unsigned long vm_ev = p_vm->m_ev;
int vm_ip = p_vm->m_ip;
value_t *vm_current_env = p_vm->m_current_env[p_vm->m_ev - 1];
//printf("Load string: %s\n", p_code);
stream_t *strm = stream_create(p_code);
int i = setjmp(*push_handler_stack());
if (i == 0) {
int args = reader(p_vm, strm, false);
//printf("reader found %d forms\n", args);
vm_push(p_vm, p_vm->nil);
int count_down = args;
while(count_down > 0) {
//printf("SAVE CSP: %lu\n", p_vm->m_csp);
// get value off stack
value_t *rd = p_vm->m_stack[p_vm->m_sp - count_down - 1];
//printf("read form: "); value_print(p_vm, rd); printf("\n");
//printf("cd: %d sp: %lu\n", count_down, p_vm->m_sp);
//vm_print_stack(p_vm);
// Evaluate it
eval(p_vm, rd);
// Print a result
printf("res: "); value_print(p_vm, p_vm->m_stack[p_vm->m_sp - count_down]); printf("\n");
//printf("RESTORE CSP: %lu sp: %ld ev: %lu\n", p_vm->m_csp, p_vm->m_sp, p_vm->m_ev);
count_down--;
}
p_vm->m_sp -= args + 1;
} else {
vm_print_stack(p_vm);
p_vm->m_bp = vm_bp;
p_vm->m_sp = vm_sp;
p_vm->m_ev = vm_ev;
p_vm->m_ip = vm_ip;
p_vm->m_current_env[p_vm->m_ev - 1] = vm_current_env;
printf("\t%s\n", g_err);
}
verify(p_vm->m_sp == vm_sp && p_vm->m_bp == vm_bp, "internal error");
// printf("env: %p\n", p_vm->m_current_env[p_vm->m_ev - 1]);
pop_handler_stack();
stream_destroy(strm);
//vm_print_env(p_vm);
//vm_print_stack(p_vm);
//printf("sp: %lu ev: %lu\n", p_vm->m_sp, p_vm->m_ev);
gc(p_vm, 1);
//printf("---------------- gc ---------------\n");
//vm_print_env(p_vm);
// printf("Memory: Free: %lu Allocated: %lu\n", mem_free(p_vm), mem_allocated(p_vm, true));
}
int main(int argc, char *arg[])
{
vm_t *vm = vm_create(1000000, NULL);
lib_init(vm);
value_t *vm_val = value_create(vm, VT_PROCESS, sizeof(vm_t *), false);
*(vm_t **)vm_val->m_data = vm;
g_kernel_proc = vm_val;
if (argc > 1) {
unit_test();
} else {
#if 0
load_string(vm, "(load \"qsort\") (pivot (seq 1000))");
#elif 0
value_t *vm1_val = value_create_process(vm, vm_val);
vm_t *vm1 = *(vm_t **)vm1_val->m_data;
int flip = 0;
char input[256];
memset(input, 0, 256);
printf("\nawesome-lang 0.0.1, copyright (c) 2011 by jeffrey thompson\n");
printf("%d> ", flip);
while(gets(input) != NULL && strcmp(input, "quit")) {
if (!flip) {
load_string(vm, input);
flip = 1;
} else {
load_string(vm1, input);
flip = 0;
}
input[0] = 0;
printf("%d> ", flip);
}
#elif 1
int flip = 0;
char input[256];
memset(input, 0, 256);
printf("\nawesome-lang 0.0.1, copyright (c) 2011 by jeffrey thompson\n");
printf("%d> ", flip);
while(gets(input) != NULL && strcmp(input, "quit")) {
load_string(vm, input);
input[0] = 0;
printf("%d> ", flip);
}
#else
load_string(vm, "(loop (print (eval (read))))");
#endif
}
//printf("---------- END OF DAYS---------\n");
vm->m_sp = 0;
vm->m_ev = 0;
gc(vm, 1);
//printf("---------- END OF END OF DAYS---------\n");
vm_destroy(vm);
}