aboutsummaryrefslogtreecommitdiff
path: root/compiler.d
blob: 51e45bab3f0391b3e03a7aac0ebe0306ba1cd562 (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
import std.stdio;
import std.string;

import parser;
import chunk;
import dbg;

enum FunctionType {
    FUNCTION,
    SCRIPT,
}

struct Local {
    Symbol sym;
    int depth;
}

class Compiler {

    Function func;
    FunctionType type;
    Parser* parser;
    Local[] locals;
    int scopeDepth;
    Form current;
    Form previous;
    Form outer;
    int outerIdx;

    void advance() {
        previous = current;
        /*
        if (outer.type != FormType.EOF && outerIdx < outer) {
            current = parser.parseForm();
        } else {
            
        }
        */
        current = parser.parseForm();
    }

    void compileNil(Form form) {
        form.compile(func);
        advance();
    }

    void compileAtom(Form form) {
        form.compile(func);
        advance();
    }

    void compileAdd(Form[] args) {
        resolve(args[0]);

        // (+ n) always returns n
        if (args.length == 1) {
            return;
        }

        for (int i = 1; i < args.length; i++) {
            resolve(args[i]);
            func.chunk.writeOp(OpCode.OP_ADD, current.line);
        }
    }

    void compileNegate(Form arg) {
        resolve(arg);
        func.chunk.writeOp(OpCode.OP_NEGATE, current.line);
    }

    void compileSubtract(Form[] args) {
        resolve(args[0]);
        for (int i = 1; i < args.length; i++) {
            resolve(args[i]);
            func.chunk.writeOp(OpCode.OP_SUBTRACT, current.line);
        }
    }

    void compileLess(Form[] args) {
        if (args.length != 2) {
            writeln("'<' requires 2 arguments");
            return;
        }
        resolve(args[0]);
        resolve(args[1]);
        func.chunk.writeOp(OpCode.OP_LESS, current.line);
    }

    void compileCons(Form form) {
        Cons cons = cast(Cons)form;
        Form head = cons.head;
        if (head.type != FormType.SYMBOL) {
            writeln("cons must start with a symbol");
            return;
        }

        Symbol sym = cast(Symbol)head;
        switch (sym.name) {
            case "+":
                compileAdd(cons.tail);
                break;
            case "-":
                if (cons.tail.length == 1) {
                    compileNegate(cons.tail[0]);
                } else {
                    compileSubtract(cons.tail);
                }
                break;
            case "<":
                compileLess(cons.tail);
                break;
            default:
                writefln("unsure how to compile %s", sym.name);
                advance();
                break;
        }

    }

    void resolve(Form form) {
        write("resolving: ");
        printForm(form);
        switch(form.type) {
            case FormType.ATOM:
                this.compileAtom(form);
                break;
            case FormType.CONS:
                this.compileCons(form);
                break;
            case FormType.NIL:
                this.compileNil(form);
                break;
            default:
                write("not sure how to resolve: ");
                printForm(form);
                advance();
                break;
        }
    }

    Function compile() {
        writeln("compiling");
        advance();
        while(current.type != FormType.EOF) {
            resolve(current);
        }
        
        return finish();
    }

    Function finish() {
        //emitReturn();
        writeln("finishing the compiler!");

        this.func.chunk.writeOp(OpCode.OP_RETURN, current.line);
        writeln("wrote a return code");
        return func;
    }

    this(FunctionType type, Parser* parser) {
        this.parser = parser;
        this.func = new Function();
        this.type = type;
        locals ~= Local(new Symbol("", -1), 0);
    }
}