summaryrefslogtreecommitdiff
path: root/sheep/vm.c
blob: 97e9d2b2f027ac04ae947a7ad0c7bd5f547c95b0 (plain)
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
/*
 * sheep/vm.c
 *
 * Copyright (c) 2009 Johannes Weiner <hannes@cmpxchg.org>
 */
#include <sheep/function.h>
#include <sheep/sequence.h>
#include <sheep/number.h>
#include <sheep/object.h>
#include <sheep/string.h>
#include <sheep/alien.h>
#include <sheep/bool.h>
#include <sheep/core.h>
#include <sheep/eval.h>
#include <sheep/list.h>
#include <sheep/type.h>
#include <sheep/util.h>
#include <sheep/gc.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>

#include <sheep/vm.h>

#define DEFAULT_ERRBUF		128

void sheep_error(struct sheep_vm *vm, const char *fmt, ...)
{
	va_list ap;
	size_t len;

	sheep_bug_on(vm->error);
	vm->error = sheep_malloc(DEFAULT_ERRBUF);

	va_start(ap, fmt);
	len = vsnprintf(vm->error, DEFAULT_ERRBUF, fmt, ap);
	va_end(ap);

	if (len >= DEFAULT_ERRBUF) {
		vm->error = sheep_realloc(vm->error, len + 1);
		va_start(ap, fmt);
		vsprintf(vm->error, fmt, ap);
		va_end(ap);
	}
}

void sheep_report_error(struct sheep_vm *vm, sheep_t sheep)
{
	sheep_bug_on(!vm->error);

	if (sheep) {
		char *context;

		context = sheep_format(sheep);
		fprintf(stderr, "%s: ", context);
		sheep_free(context);
	}
	fprintf(stderr, "%s\n", vm->error);

	sheep_free(vm->error);
	vm->error = NULL;
}

unsigned int sheep_vm_key(struct sheep_vm *vm, const char *key)
{
	unsigned int slot = 0;

	if (vm->keys) {
		while (vm->keys[slot]) {
			if (!strcmp(vm->keys[slot], key))
				return slot;
			slot++;
		}
	}

	vm->keys = sheep_realloc(vm->keys, sizeof(char *) * (slot + 2));
	vm->keys[slot] = sheep_strdup(key);
	vm->keys[slot + 1] = NULL;
	return slot;
}

static void drain_keys(struct sheep_vm *vm)
{
	unsigned int slot;

	if (!vm->keys)
		return;

	for (slot = 0; vm->keys[slot]; slot++)
		sheep_free(vm->keys[slot]);

	sheep_free(vm->keys);
}

void sheep_vm_mark(struct sheep_vm *vm)
{
	unsigned int i;

	for (i = 0; i < vm->globals.nr_items; i++)
		if (vm->globals.items[i])
			sheep_mark(vm->globals.items[i]);

	for (i = 0; i < vm->stack.nr_items; i++)
		if (vm->stack.items[i])
			sheep_mark(vm->stack.items[i]);

	for (i = 2; i < vm->calls.nr_items; i += 3)
		sheep_mark(vm->calls.items[i]);
}

unsigned int sheep_vm_variable(struct sheep_vm *vm,
			const char *name, sheep_t value)
{
	unsigned int slot;

	slot = sheep_vm_constant(vm, value);
	sheep_map_set(&vm->builtins, name, (void *)(unsigned long)slot);
	return slot;
}

void sheep_vm_function(struct sheep_vm *vm, const char *name, sheep_alien_t f)
{
	sheep_vm_variable(vm, name, sheep_make_alien(vm, f, name));
}

static void setup_argv(struct sheep_vm *vm, int ac, char **av)
{
	struct sheep_list *p;
	sheep_t list;

	list = sheep_make_cons(vm, NULL, NULL);
	sheep_protect(vm, list);

	p = sheep_list(list);
	while (ac--) {
		p->head = sheep_make_string(vm, *av);
		p->tail = sheep_make_cons(vm, NULL, NULL);
		p = sheep_list(p->tail);
		av++;
	}

	sheep_vm_variable(vm, "argv", list);
	sheep_unprotect(vm, list);
}

void sheep_vm_init(struct sheep_vm *vm, int ac, char **av)
{
	memset(vm, 0, sizeof(*vm));
	sheep_core_init(vm);
	sheep_object_builtins(vm);
	sheep_bool_builtins(vm);
	sheep_number_builtins(vm);
	sheep_string_builtins(vm);
	sheep_list_builtins(vm);
	sheep_sequence_builtins(vm);
	sheep_function_builtins(vm);
	sheep_module_builtins(vm);
	setup_argv(vm, ac, av);
}

void sheep_vm_exit(struct sheep_vm *vm)
{
	sheep_map_drain(&vm->builtins);
	sheep_core_exit(vm);
	sheep_evaluator_exit(vm);
	sheep_free(vm->globals.items);
	drain_keys(vm);
	sheep_gc_exit(vm);
}