nodespeak 0.2.1

A JIT-ish compiler for number-crunching applications.
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::high_level::problem::*;
use crate::vague::structure as i;
use ProblemType::Error;
use ProblemType::Hint;

pub fn wrong_number_of_inputs(
    macro_call_pos: FilePosition,
    header_pos: FilePosition,
    provided: usize,
    expected: usize,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![
        ProblemDescriptor::new(
            macro_call_pos,
            Error,
            &format!(
                concat!(
                    "Wrong Number Of Inputs\nThis macro call has {} input ",
                    "arguments but the macro it is calling has {} input ",
                    "parameters.",
                ),
                provided, expected,
            ),
        ),
        ProblemDescriptor::new(
            header_pos,
            Hint,
            concat!("The header of the macro being called is as follows:"),
        ),
    ])
}

pub fn wrong_number_of_outputs(
    macro_call_pos: FilePosition,
    header_pos: FilePosition,
    provided: usize,
    expected: usize,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![
        ProblemDescriptor::new(
            macro_call_pos,
            Error,
            &format!(
                concat!(
                    "Wrong Number Of Outputs\nThis macro call has {} output ",
                    "arguments but the macro it is calling has {} output ",
                    "parameters.",
                ),
                provided, expected,
            ),
        ),
        ProblemDescriptor::new(
            header_pos,
            Hint,
            concat!("The header of the macro being called is as follows:"),
        ),
    ])
}

pub fn not_macro(expr_pos: FilePosition, typ: &i::DataType) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        expr_pos,
        Error,
        &format!(
            concat!(
                "Incorrect Type\nThe highlighted expression should resolve to a macro because it ",
                "is being used in a macro call. However, it resolves to a {:?} instead.",
            ),
            typ
        ),
    )])
}

pub fn not_data_type(expr_pos: FilePosition, typ: &i::DataType) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        expr_pos,
        Error,
        &format!(
            concat!(
                "Incorrect Type\nThe highlighted expression should resolve to a data type because ",
                "it is being used to declare a variable. However, it resolves to a {:?} instead.",
            ),
            typ
        ),
    )])
}

pub fn guaranteed_assert(assert_pos: FilePosition) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        assert_pos,
        Error,
        "Assert Guranteed To Fail",
    )])
}

pub fn array_index_not_int(
    index: FilePosition,
    index_type: &i::DataType,
    expression: FilePosition,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![
        ProblemDescriptor::new(
            index,
            Error,
            &format!(
                "Array Index Not Int\nExpected an integer, got a {:?}:",
                index_type
            ),
        ),
        ProblemDescriptor::new(
            expression,
            Hint,
            "Encountered while resolving this expression:",
        ),
    ])
}

pub fn array_index_less_than_zero(
    index: FilePosition,
    value: i64,
    expression: FilePosition,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![
        ProblemDescriptor::new(
            index,
            Error,
            &format!(
                concat!(
                    "Array Index Less Than Zero\nThe value of the highlighted expression was ",
                    "computed to be {}:",
                ),
                value
            ),
        ),
        ProblemDescriptor::new(
            expression,
            Hint,
            "Encountered while resolving this expression:",
        ),
    ])
}

pub fn array_index_too_big(
    index: FilePosition,
    value: usize,
    arr_size: usize,
    expression: FilePosition,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![
        ProblemDescriptor::new(
            index,
            Error,
            &format!(
                concat!(
                    "Array Index Too Big\nThe value of the highlighted expression was ",
                    "computed to be {}, which is too big when indexing an array of size {}:",
                ),
                value, arr_size,
            ),
        ),
        ProblemDescriptor::new(
            expression,
            Hint,
            "Encountered while resolving this expression:",
        ),
    ])
}

pub fn array_base_not_data_type(base: FilePosition, typ: &i::DataType) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        base,
        Error,
        &format!(
            concat!(
                "Array Base Not Data Type\nExpected a data type (as a base for an array type), ",
                "got a {:?} instead."
            ),
            typ,
        ),
    )])
}

pub fn array_size_less_than_one(size: FilePosition, value: i64) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        size,
        Error,
        &format!(
            concat!("Array Size Less Than One\nThe highlighted expression resolves to {}. ",),
            value
        ),
    )])
}

pub fn array_size_not_int(size: FilePosition, size_type: &i::DataType) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        size,
        Error,
        &format!(
            "Array Size Not Int\nExpected an integer, got a {:?}:",
            size_type
        ),
    )])
}

pub fn array_size_not_resolved(size: FilePosition) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        size,
        Error,
        concat!(
            "Dynamic Array Size\nArray sizes must be specified at compile time. The following ",
            "expression can only be evaluated at runtime:"
        ),
    )])
}

pub fn bad_array_literal(
    bad_item_pos: FilePosition,
    bad_item_type: &i::DataType,
    first_item_pos: FilePosition,
    first_item_type: &i::DataType,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![
        ProblemDescriptor::new(
            bad_item_pos,
            Error,
            &format!(
                "Bad Array Literal\nThe highlighted item has an unexpected data type of {:?}.",
                bad_item_type,
            ),
        ),
        ProblemDescriptor::new(
            first_item_pos,
            Hint,
            &format!(
                "The first item in the array literal is of data type {:?}:",
                first_item_type
            ),
        ),
    ])
}

pub fn no_bct(
    expression: FilePosition,
    op1: FilePosition,
    op1_type: &i::DataType,
    op2: FilePosition,
    op2_type: &i::DataType,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![
        ProblemDescriptor::new(
            expression,
            Error,
            concat!(
                "No Biggest Common Type\nCannot determine what data type the result of the ",
                "highlighted expression will have:"
            ),
        ),
        ProblemDescriptor::new(
            op1,
            Hint,
            &format!("The first operand has data type {:?}:", op1_type),
        ),
        ProblemDescriptor::new(
            op2,
            Hint,
            &format!("But the second operand has data type {:?}:", op2_type),
        ),
    ])
}

pub fn mismatched_assign(
    expression: FilePosition,
    lhs: FilePosition,
    lhs_type: &i::DataType,
    rhs: FilePosition,
    rhs_type: &i::DataType,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![
        ProblemDescriptor::new(
            expression,
            Error,
            concat!(
                "Mismatched Datatype In Assignment\nCannot figure out how to assign the ",
                "right hand side of this statement to the left hand side:"
            ),
        ),
        ProblemDescriptor::new(
            rhs,
            Hint,
            &format!("The right hand side has data type {:?}:", rhs_type),
        ),
        ProblemDescriptor::new(
            lhs,
            Hint,
            &format!("But the left hand side has data type {:?}:", lhs_type),
        ),
    ])
}

pub fn value_not_run_time_compatible(
    value_pos: FilePosition,
    dtype: &i::DataType,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        value_pos,
        Error,
        &format!(
            concat!(
                "Value Not Run Time Compatible\nThe value of the highlighted expression was ",
                "calculated at compile time, but the way it is used requires it to be available ",
                "at run time. This is not possible as it yields a value of type {:?}."
            ),
            dtype
        ),
    )])
}

pub fn rt_indexes_on_ct_variable(expr_pos: FilePosition, dtype: &i::DataType) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        expr_pos,
        Error,
        &format!(
            concat!(
                "Runtime Index On Compiletime Variable\nThe highlighted expression has indexes ",
                "which will only be known at run time. However, it refers to a value of type ",
                "{:?}, which can only be used at compile time."
            ),
            dtype
        ),
    )])
}

pub fn too_many_indexes(
    expr_pos: FilePosition,
    num_indexes: usize,
    max_indexes: usize,
    base_pos: FilePosition,
    base_type: &i::DataType,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![
        ProblemDescriptor::new(
            expr_pos,
            Error,
            &format!(
                concat!(
                    "Too Many Indexes\nThe highlighted expression is indexing a value {} times, ",
                    "but the value it is indexing can only be indexed at most {} times."
                ),
                num_indexes, max_indexes
            ),
        ),
        ProblemDescriptor::new(
            base_pos,
            Hint,
            &format!(
                concat!("The base of the expression has the data type {:?}"),
                base_type
            ),
        ),
    ])
}

pub fn vpe_wrong_type(
    vpe_pos: FilePosition,
    expected: &i::DataType,
    found: &i::DataType,
) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        vpe_pos,
        Error,
        &format!(
            "Wrong Data Type\nExpected a {:?}, found a {:?}.",
            expected, found
        ),
    )])
}

pub fn unresolved_auto_var(var_pos: FilePosition) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        var_pos,
        Error,
        concat!(
            "Unresolved Auto Var\nThe highlighted variable was declared with an automatic data ",
            "type. It has not been assigned any value before this point so its data type cannot ",
            "be determined. Assigning the variable a value somewhere earlier in the program will ",
            "fix this error."
        ),
    )])
}

pub fn dangling_value(bad_expr_pos: FilePosition, typ: &i::DataType) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        bad_expr_pos,
        Error,
        &format!(
            concat!(
                "Dangling Value\nThe highlighted expression yields a value of type {:?}, but it ",
                "is not stored in any variable."
            ),
            typ
        ),
    )])
}

pub fn compile_time_input(input_decl_pos: FilePosition, typ: &i::DataType) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        input_decl_pos,
        Error,
        &format!(
            concat!(
                "Compile Time Input\nThe highlighted input was given the data type {:?}, which ",
                "can only be used at compile time."
            ),
            typ
        ),
    )])
}

pub fn compile_time_output(output_decl_pos: FilePosition, typ: &i::DataType) -> CompileProblem {
    CompileProblem::from_descriptors(vec![ProblemDescriptor::new(
        output_decl_pos,
        Error,
        &format!(
            concat!(
                "Compile Time Output\nThe highlighted output was given the data type {:?}, which ",
                "can only be used at compile time."
            ),
            typ
        ),
    )])
}