summaryrefslogtreecommitdiff
path: root/tnslc/compiler.tnsl
blob: b61ff0591bb8697e61fa530ee4c2f092438ed1ba (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Actual compilation of the vector of tokens, ported from the "dirty tnsl"
# that was originally written for the interpreter

# CompData represents three vectors:
# hsec - the heading of the output assembly
# dsec - the data tied to the assembly
# csec - the .text section that is the code of the assembly
struct CompData {
    Vector
        hsec,
        dsec,
        csec
}

/; method CompData
    /; start
        self.hsec.start(1)
        self.dsec.start(1)
        self.csec.start(1)
    ;/

    /; add (CompData c)
        self.hsec.add(c.hsec)
        self.dsec.add(c.dsec)
        self.csec.add(c.csec)
    ;/

    /; _del
        self.hsec._del()
        self.dsec._del()
        self.csec._del()
    ;/
;/

# Path represents the actual path of a file
# that we are trying to tokenize
# Assumes that the last item in the path array is a file name
struct Path {
    int 
        path_count,
    ~~uint8
        split_path
}

/; method Path
    /; start (~uint8 path)
        self.split_path = _alloc(8)
        self.path_count = 0
        self.relative_file(path)
    ;/

    /; copy [Path]
        Path out
        ~uint8 f_pth = self.full_path()
        out.start(f_pth)
        _delete(f_pth)
        return out
    ;/
    
    /; relative_file(~uint8 rel_path)
        # Assume the last string is the file name
        /; if (self.path_count > 0)
            int idx = self.path_count - 1
            _delete(self.split_path{idx})
            self.path_count--
        ;/

        ~uint8 n_ptr = _alloc(1)
        n_ptr{0} = 0
        int idx = self.path_count

        /; loop (int i = 0; i < cstr_len(rel_path)) [i++]
            /; if (rel_path{i} == '\\' || rel_path{i} == '/')
                /; if (cstr_len(n_ptr) > 0)
                    self.path_count++
                    idx = self.path_count
                    self.split_path = _realloc(self.split_path, idx * 8)
                    self.split_path{idx - 1} = n_ptr

                    n_ptr = _alloc(1)
                    n_ptr{0} = 0
                ;/
            ;; else
                idx = cstr_len(n_ptr)
                n_ptr = _realloc(n_ptr, idx + 2)
                n_ptr{idx} = rel_path{i}
                n_ptr{idx + 1} = 0
            ;/
        ;/

        /; if (cstr_len(n_ptr) > 0)
            self.path_count++
            idx = self.path_count
            self.split_path = _realloc(self.split_path, idx * 8)
            self.split_path{idx - 1} = n_ptr
        ;/
    ;/

    /; full_path [~uint8]
        ~uint8 pth = _alloc(1)
        pth{0} = 0

        ~uint8 w_ptr = self.split_path{0}

        /; loop (int i = 0; i < self.path_count) [i++]
            w_ptr = self.split_path{i}
            int old_len = cstr_len(pth)
            int new_len = old_len + cstr_len(w_ptr)
            
            pth = _realloc(pth, new_len + 1)
            pth{new_len} = 0
            

            /; loop (int j = 0; j < cstr_len(w_ptr)) [j++]
                pth{old_len + j} = w_ptr{j}
            ;/

            /; if (i < self.path_count - 1)
                pth = _realloc(pth, new_len + 2)
                pth{new_len} = '/'
                pth{new_len + 1} = 0
            ;/
        ;/

        return pth
    ;/

    /; open_read [~void]
        ~uint8 path = self.full_path()
        ~void out = _open_file(path)
        _delete(path)
        return out
    ;/

    /; open_write [~void]
        ~uint8 path = self.full_path()
        ~void out = _create_file(path)
        _delete(path)
        return out
    ;/

    /; print_all
        /; loop (int i = 0; i < self.path_count) [i++]
            _printf(self.split_path{i})
        ;/
    ;/

    /; _del
        /; loop (int i = 0; i < self.path_count) [i++]
            _delete(self.split_path{i})
        ;/

        _delete(self.split_path)
    ;/
;/

########################################
# Compiler functions - here be dragons #
########################################

/; compile_file (Path in) [CompData]
    CompData out
    out.start()



    return out
;/

/; compile (Path in, out)
    ~void fin = in.open_read()
    Vector v = tokenize_file(fin)
    _close_file(fin)

    ~void fout = out.open_write()
    /; loop (int i = 0; i < v.num_el) [i++]
        ~Token t_ptr = v.get(i)
        print_token(t_ptr`, fout)
        t_ptr`._del()
    ;/
    v._del()
    _close_file(fout)
;/