-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaf.compiler.d
110 lines (92 loc) · 1.9 KB
/
leaf.compiler.d
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
module leaf.compiler;
import leaf.syscall;
import kernel.memorystream;
import kernel.common;
extern(C):
/**********************
* QUICK HACKS *
**********************/
MemoryStream newMemoryStreamUser(void **heap, void *p, uint len) {
//MemoryStream model = scoped!MemoryStream;
auto size = __traits(classInstanceSize, MemoryStream);
MemoryStream cls = cast(MemoryStream) malloc(heap, size);
//cls.__ctor();
//panic();
//memcpy(cast(void *) cls, cast(void *) model, size);
memset(cast(void *) cls, 0, size);
ubyte *tmp = cast(ubyte *) p;
//cls.data = tmp[0..len];
cls.data = tmp[0..len];
//cls.position = 0;
cls.position = 0;
//panic();
return cls;
}
enum HEAP_START = 0xA0000000;
static __gshared void *compiler_heap;
void *malloc(void **heap, uint size) @system
{
void *p = *heap;
*heap += size;
return p;
}
void free(void *p) @system
{
// Do nothing...
}
enum BUFFER_SIZE = 1024;
struct FILE {
ubyte[] buffer;
MemoryStream stream;
};
int getchar(FILE *file) @trusted
{
return 0;
}
FILE *open(void **heap) @trusted
{
FILE *file = cast(FILE *) malloc(heap, FILE.sizeof);
ubyte *temp = cast(ubyte *) malloc(heap, BUFFER_SIZE);
file.buffer = temp[0..BUFFER_SIZE];
memset(file.buffer.ptr, 0, file.buffer.length);
file.stream = newMemoryStreamUser(heap, file.buffer.ptr, file.buffer.length);
return file;
}
int close(FILE *file) @trusted
{
free(file);
return 0;
}
/************************
* THE COMPILER
************************/
enum Token {
TAG_OPEN,
TAG_CLOSE,
IDENTIFIER,
EOF
}
Token lex(FILE *file) @safe
{
getchar(file);
return Token.EOF;
}
int parse(FILE *file) @safe
{
lex(file);
return 0;
}
int compile() @trusted
{
void *compiler_heap = cast(void *) HEAP_START;
sys_mmap(cast(void *) HEAP_START, 4096);
FILE *file = open(&compiler_heap);
user_print("a\n");
if (file == null)
return 1;
if (parse(file) != 0)
return 2;
if (close(file) != 0)
return 3;
return 0;
}