summaryrefslogtreecommitdiff
path: root/src/main.c
blob: a45da388906dc7f8d00426346d2afdd529272ff5 (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
#include <stdio.h>

#include "utils.h"
#include "lexer.h"
#include "parser.h"
#include "runtime.h"

pit_value test_print(pit_runtime *rt, pit_value args) {
    pit_value x = pit_car(rt, args);
    pit_trace(rt, x);
    return x;
}

pit_value test_add(pit_runtime *rt, pit_value args) {
    i64 x = pit_as_integer(rt, pit_car(rt, args));
    i64 y = pit_as_integer(rt, pit_car(rt, pit_cdr(rt, args)));
    return pit_integer_new(rt, x + y);
}

pit_value test_sub(pit_runtime *rt, pit_value args) {
    i64 x = pit_as_integer(rt, pit_car(rt, args));
    i64 y = pit_as_integer(rt, pit_car(rt, pit_cdr(rt, args)));
    return pit_integer_new(rt, x - y);
}

int main(int argc, char **argv) {
    if (argc < 2) pit_panic("usage: %s FILE", argv[0]);

    pit_runtime *rt = pit_runtime_new();
    pit_check_error_maybe_panic(rt);
    pit_fset(rt, pit_intern_cstr(rt, "print"), pit_nativefunc_new(rt, test_print));
    pit_fset(rt, pit_intern_cstr(rt, "+"), pit_nativefunc_new(rt, test_add));
    pit_fset(rt, pit_intern_cstr(rt, "-"), pit_nativefunc_new(rt, test_sub));

    pit_lexer *lex = pit_lex_file(argv[1]);

    pit_parser *parse = pit_parser_from_lexer(lex);
    pit_value program = pit_parse(rt, parse);
    pit_check_error_maybe_panic(rt);
    pit_trace(rt, program);

    pit_value ret = pit_eval(rt, program);
    pit_check_error_maybe_panic(rt);
    pit_trace(rt, ret);
}