crisp 0.1.2

A small, expressive Lisp-inspired programming language.
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
432
use std::collections::HashSet;

use colored::Colorize;
use log::error;
use pest::iterators::Pair;

use crate::parsing::{
    Rule,
    ast::nodes::{SourceInfo, Symbol},
};

pub fn validate_fn(pair: &Pair<Rule>, path: &'static str) -> bool {
    let mut inner = pair.clone().into_inner();
    let fn_op = inner.next().unwrap();
    // function definition has to start with fn:type
    if !matches!(fn_op.as_rule(), Rule::symbol) {
        print_ast_error(
            "Function definition must start with a typed fn symbol",
            &SourceInfo::from_pair(&fn_op, path),
        );
        return false;
    }
    // peak ahead at the second part
    let second = match inner.next() {
        Some(pair) => pair,
        None => {
            print_ast_error(
                "Function definition must be of the shape (fn:type name (param:type...) (body))",
                &SourceInfo::from_pair(pair, path),
            );
            return false;
        }
    };
    // second part might be a name or a parameter list
    let params;
    match second.as_rule() {
        Rule::symbol => {
            params = match inner.next() {
                Some(pair) => pair,
                None => {
                    print_ast_error(
                        "Function definition must be of the shape (fn:type name (param:type...) (body))",
                        &SourceInfo::from_pair(pair, path),
                    );
                    return false;
                }
            };
            match inner.next() {
                Some(pair) => {
                    if !matches!(pair.as_rule(), Rule::list) {
                        print_ast_error("Body must be a list", &SourceInfo::from_pair(&pair, path));
                        return false;
                    }
                }
                None => {
                    print_ast_error(
                        "Function definition must be of the shape (fn:type name (param:type...) (body))",
                        &SourceInfo::from_pair(pair, path),
                    );
                    return false;
                }
            };
        }
        Rule::list => {
            params = second;
            match inner.next() {
                Some(pair) => {
                    if !matches!(pair.as_rule(), Rule::list) {
                        print_ast_error("Body must be a list", &SourceInfo::from_pair(&pair, path));
                        return false;
                    }
                }
                None => {
                    print_ast_error(
                        "Function definition must include a body which is a list",
                        &SourceInfo::from_pair(pair, path),
                    );
                    return false;
                }
            };
        }
        _ => {
            print_ast_error(
                "Unexpected function structure",
                &SourceInfo::from_pair(&second, path),
            );
            return false;
        }
    }
    if inner.next().is_some() {
        print_ast_error(
            "Too many components in function definition",
            &SourceInfo::from_pair(pair, path),
        );
        return false;
    }
    // check if params is actually a list
    if !matches!(params.as_rule(), Rule::list) {
        print_ast_error(
            "Parameters must be a list",
            &SourceInfo::from_pair(&params, path),
        );
        return false;
    }
    // do a quick check to see if param symbols are all typed
    let mut is_params_valid = true;
    for pair in params.into_inner() {
        if !matches!(pair.as_rule(), Rule::symbol) {
            print_ast_error(
                "Parameter must be a symbol",
                &SourceInfo::from_pair(&pair, path),
            );
            is_params_valid &= false;
            continue;
        }
        let param_symbol = Symbol::from_pair(&pair);
        if let Symbol::Untyped { name: _ } = param_symbol {
            print_ast_error(
                "Parameter must be typed",
                &SourceInfo::from_pair(&pair, path),
            );
            is_params_valid &= false;
        }
    }
    if !is_params_valid {
        return false;
    }
    true
}

pub fn validate_if(pair: &Pair<Rule>, path: &'static str) -> bool {
    let pairs: Vec<Pair<Rule>> = pair.clone().into_inner().collect();
    // if must be 3 or 4 elements
    if pairs.len() != 3 && pairs.len() != 4 {
        println!("{}", pairs.len());
        print_ast_error("Invalid if statement", &SourceInfo::from_pair(pair, path));
        return false;
    }
    // predicate must be list, boolean or symbol
    if !matches!(
        pairs[1].as_rule(),
        Rule::list | Rule::boolean | Rule::symbol
    ) {
        print_ast_error(
            "Predicate must be a list",
            &SourceInfo::from_pair(&pairs[1], path),
        );
        return false;
    }
    // then block must be list
    if !matches!(pairs[2].as_rule(), Rule::list) {
        print_ast_error(
            "Then block must be a list",
            &SourceInfo::from_pair(&pairs[2], path),
        );
        return false;
    }
    if pairs.len() == 4 {
        if !matches!(pairs[3].as_rule(), Rule::list) {
            print_ast_error(
                "Invalid else block",
                &SourceInfo::from_pair(&pairs[3], path),
            );
            return false;
        }
    }
    true
}

pub fn validate_for(pair: &Pair<Rule>, path: &'static str) -> bool {
    let pairs: Vec<Pair<Rule>> = pair.clone().into_inner().collect();
    // for must be 4 elements
    if pairs.len() != 4 {
        print_ast_error("Invalid for loop", &SourceInfo::from_pair(pair, path));
        return false;
    }
    // dummy must be symbol
    if !matches!(pairs[1].as_rule(), Rule::symbol) {
        print_ast_error(
            "Dummy index is not a symbol",
            &SourceInfo::from_pair(&pairs[1], path),
        );
        return false;
    }
    // iterator must be list
    if !matches!(pairs[2].as_rule(), Rule::list | Rule::symbol) {
        print_ast_error(
            "Iterator is invalid",
            &SourceInfo::from_pair(&pairs[2], path),
        );
        return false;
    }
    // body must be a block
    if !matches!(pairs[3].as_rule(), Rule::list) {
        print_ast_error(
            "Body is not a block",
            &SourceInfo::from_pair(&pairs[3], path),
        );
        return false;
    }
    true
}

pub fn validate_let(pair: &Pair<Rule>, path: &'static str) -> bool {
    let pairs: Vec<Pair<Rule>> = pair.clone().into_inner().collect();
    // let must be 3 elements
    if pairs.len() != 3 {
        print_ast_error("Invalid let statement", &SourceInfo::from_pair(pair, path));
        return false;
    }
    // variable name must be symbol or typed symbol
    if !matches!(pairs[1].as_rule(), Rule::symbol) {
        print_ast_error(
            "Variable name is not a symbol or type annotated symbol",
            &SourceInfo::from_pair(&pairs[1], path),
        );
        return false;
    }
    // value must be literal, symbol or list
    if !matches!(
        pairs[2].as_rule(),
        Rule::number | Rule::string | Rule::boolean | Rule::symbol | Rule::list
    ) {
        print_ast_error(
            "Value is not a literal, untyped symbol or list",
            &SourceInfo::from_pair(&pairs[2], path),
        );
        return false;
    }
    true
}

pub fn validate_given(pair: &Pair<Rule>, path: &'static str) -> bool {
    let pairs: Vec<Pair<Rule>> = pair.clone().into_inner().collect();
    if pairs.len() < 2 {
        print_ast_error(
            "Given statement is missing a predicate",
            &SourceInfo::from_pair(pair, path),
        );
        return false;
    }
    let predicate = &pairs[1];
    match predicate.as_rule() {
        Rule::list | Rule::symbol | Rule::boolean | Rule::number => {}
        _ => {
            print_ast_error(
                "Predicate must be an expression (atom or list)",
                &SourceInfo::from_pair(predicate, path),
            );
            return false;
        }
    }
    for case in &pairs[2..] {
        if !matches!(case.as_rule(), Rule::list) {
            print_ast_error(
                "Each case in a given statement must be a list: (pattern (body))",
                &SourceInfo::from_pair(case, path),
            );
            return false;
        }
        if case.clone().into_inner().count() != 2 {
            print_ast_error(
                "Each case must have exactly a pattern and a body",
                &SourceInfo::from_pair(case, path),
            );
            return false;
        }
    }
    true
}

pub fn validate_ret(pair: &Pair<Rule>, path: &'static str) -> bool {
    let pairs: Vec<Pair<Rule>> = pair.clone().into_inner().collect();
    // ret must be 2 elements
    if pairs.len() != 2 {
        print_ast_error("Invalid return call", &SourceInfo::from_pair(pair, path));
        return false;
    }
    // value must be symbol, literal or list
    if !matches!(
        pairs[1].as_rule(),
        Rule::string | Rule::number | Rule::boolean | Rule::list | Rule::symbol
    ) {
        print_ast_error(
            "Invalid return call value",
            &SourceInfo::from_pair(pair, path),
        );
        return false;
    }
    true
}

pub fn validate_call(pair: &Pair<Rule>, path: &'static str) -> bool {
    let pairs: Vec<Pair<Rule>> = pair.clone().into_inner().collect();
    // call must be 1 or more elements
    if pairs.len() < 1 {
        print_ast_error("Invalid call", &SourceInfo::from_pair(pair, path));
        return false;
    }
    // first element should be a symbol
    if !matches!(pairs[0].as_rule(), Rule::symbol) {
        print_ast_error(
            "Invalid call identifier",
            &SourceInfo::from_pair(&pairs[0], path),
        );
        return false;
    }
    // every other element should be a symbol, list or literal
    for pair in pairs[1..].iter() {
        if !matches!(
            pair.as_rule(),
            Rule::symbol | Rule::list | Rule::boolean | Rule::string | Rule::number
        ) {
            print_ast_error("Invalid call argument", &SourceInfo::from_pair(pair, path));
            return false;
        }
    }
    true
}

pub fn validate_block(pair: &Pair<Rule>, path: &'static str) -> bool {
    let inner = pair.clone().into_inner();
    if inner.len() == 0 {
        print_ast_error(
            "Empty blocks are not allowed",
            &SourceInfo::from_pair(pair, path),
        );
        return false;
    }
    for pair in inner {
        match pair.as_rule() {
            Rule::list | Rule::symbol | Rule::number | Rule::string => continue,
            _ => {
                print_ast_error(
                    "Invalid expression inside block",
                    &SourceInfo::from_pair(&pair, path),
                );
                return false;
            }
        }
    }
    true
}

pub fn validate_params(pair: &Pair<Rule>, path: &'static str) -> bool {
    let inner = pair.clone().into_inner();
    let mut names = HashSet::new();
    for param in inner {
        let name = match param.as_rule() {
            Rule::symbol => param.as_str(),
            _ => {
                print_ast_error(
                    "Parameter must be a symbol",
                    &SourceInfo::from_pair(&param, path),
                );
                return false;
            }
        };
        if !names.insert(name) {
            print_ast_error(
                &format!("Duplicate parameter name: {}", name),
                &SourceInfo::from_pair(&param, path),
            );
            return false;
        }
    }
    true
}

pub fn validate_list(pair: &Pair<Rule>, path: &'static str) -> bool {
    let span = pair.as_span();
    let content = span.as_str().trim();
    if !content.starts_with('(') || !content.ends_with(')') {
        print_ast_error(
            "Missing surrounding parentheses",
            &SourceInfo::from_pair(pair, path),
        );
        return false;
    }
    for inner_pair in pair.clone().into_inner() {
        match inner_pair.as_rule() {
            Rule::symbol => {
                let _ = Symbol::from_pair(&inner_pair);
            }
            Rule::list => {
                if !validate_list(&inner_pair, path) {
                    return false;
                }
            }
            Rule::EOI => {}
            _ => {
                print_ast_error(
                    "Unexpected token in list",
                    &SourceInfo::from_pair(&inner_pair, path),
                );
                return false;
            }
        }
    }

    true
}

pub fn print_ast_error(msg: &str, info: &SourceInfo) {
    let span = info.span;
    let input = span.get_input();
    let start = span.start();

    let line_start = input[..start].rfind('\n').map(|i| i + 1).unwrap_or(0);
    let line_end = input[start..]
        .find('\n')
        .map(|i| start + i)
        .unwrap_or(input.len());
    let line_text = &input[line_start..line_end];

    let indent = " ".repeat(info.col - 1);
    let span_len = (span.end() - span.start()).max(1);
    let pointer = "~".repeat(span_len).red();

    error!(
        "{}\n--> {}[{}|{}]\n{:>4} |\n{:>4} | {}\n     | {}{}",
        msg.bold(),
        info.path.blue(),
        info.line.to_string().red(),
        info.col.to_string().red(),
        "|",
        info.line.to_string().red(),
        line_text,
        indent,
        pointer
    );
}