~jb55/protoverse

ref: 954621d2d19e878daa9c0d5db47cae947d31a48e protoverse/src/protoverse.c -rw-r--r-- 3.0 KiB
954621d2William Casarin perf fixes 2 months ago
                                                                                
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
#include "io.h"
#include "parse.h"
#include "describe.h"
#include "serve.h"
#include "client.h"
#include "wasm.h"
#include "resource.h"
#include "entity.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

#define MAX_ENTITIES 1048576
#define streq(a, b) strcmp(a,b) == 0

/*
static void print_all_cells(struct parser *parser)
{
	struct cell *cell;
	int i, j;
	int ncells;

	ncells = cursor_count(parser->cells, sizeof(struct cell));
	printf("ncells %d\n", ncells);
	for (i = 0; i < ncells; i++) {
		cell = get_cell(parser->cells, i);
		print_cell(parser->attributes, cell);
		printf("\n");

		for (j = 0; j < cell->n_children; j++) {
			cell = get_cell(parser->cells, cell->children[j]);
			assert(cell);
			printf("  ");
			print_cell(parser->attributes, cell);
			printf("\n");
		}
	}
}
*/

static int print_cell_tree(struct parser *parser, u16 root, int depth)
{
	int i;


	struct cell *cell = get_cell(&parser->cells, root);
	if (!cell) {
		printf("no root cell...\n");
		return 0;
	}

	/*  sanity TODO: configurable max depth */
	if (depth > 255)
		return 0;

	for (i = 0; i < depth; i++) {
		printf("  ");
	}

	print_cell(&parser->attributes, cell);
	printf("\n");

	for (i = 0; i < cell->n_children; i++) {
		print_cell_tree(parser, cell->children[i], depth+1);
	}


	return 1;
}

static void init_protoverse_server(struct protoverse_server *server)
{
	(void)server;
	/*
	init_resource_manager(&server->env.entities, sizeof(struct entity),
			1024, MAX_ENTITIES, "entity");
			*/
}

static void free_protoverse_server(struct protoverse_server *server)
{
	(void)server;
	//destroy_resource_manager(&server->env.entities);
}

static int usage(void)
{
	printf("usage: protoverse <command> [args]\n\n");
	printf("   COMMANDS\n\n");
	printf("       parse file.space\n");
	printf("       serve file.space\n");
	printf("       client\n");
	printf("       run code.wasm\n");

	return 1;
}



int main(int argc, const char *argv[])
{
	const char *space, *code_file;
	const char *cmd;
	unsigned char *wasm_data;
	struct parser parser;
	struct protoverse_server server;
	u16 root;
	int ok;
	size_t len;

	if (argc < 2)
		return usage();

	cmd = argv[1];

	if (streq(cmd, "parse")) {
		if (argc != 3)
			return usage();
		ok = init_parser(&parser);
		if (!ok) return 1;
		space = argv[2];
		ok = parse_file(&parser, space, &root);
		if (!ok) return 1;

		print_cell_tree(&parser, root, 0);

		describe(&parser, root);
		free_parser(&parser);
	} else if (streq(cmd, "serve")) {
		if (argc != 3)
			return usage();
		space = argv[2];
		printf("serving protoverse on port 1988...\n");

		server.port = 1988;
		server.bind = "127.0.0.1";

		init_protoverse_server(&server);
		protoverse_serve(&server);
		free_protoverse_server(&server);
	} else if (streq(cmd, "client")) {
		protoverse_connect("127.0.0.1", 1988);
	} else if (streq(cmd, "run")) {
		if (argc != 3)
			return usage();
		code_file = argv[2];
		if (!map_file(code_file, &wasm_data, &len)) {
			perror("mmap");
			return 1;
		}
		if (!run_wasm(wasm_data, len)) {
			return 2;
		}
		munmap(wasm_data, len);
	}

	return 0;
}