forked from asutton/waffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
74 lines (61 loc) · 1.74 KB
/
main.cpp
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
#include <iostream>
#include "language.hpp"
#include "lexer.hpp"
#include "parser.hpp"
#include "syntax.hpp"
#include "elab.hpp"
#include "ast.hpp"
#include "eval.hpp"
int main() {
Language lang;
// ------------------------------------------------------------------------ //
// Character input
using Iter = std::istreambuf_iterator<char>;
std::string text(Iter(std::cin), Iter());
// ------------------------------------------------------------------------ //
// Lexical analysis
//
// Lex the given input text.
Lexer lex;
Tokens toks = lex(text);
if (not lex.diags.empty()) {
std::cerr << lex.diags;
return -1;
}
// ------------------------------------------------------------------------ //
// Syntactic analysis
//
// Parse the result.
Parser parse;
Tree* tree = parse(toks);
if (not parse.diags.empty()) {
std::cerr << parse.diags;
return -1;
}
std::cout << "== parsed ==\n" << pretty(tree) << '\n';
// ------------------------------------------------------------------------ //
// Elaboration
//
// Elaborate the parse tree, producing a fully typed abstract
// syntax tree.
Elaborator elab;
Expr* prog = elab(tree);
if (not elab.diags.empty()) {
std::cerr << elab.diags;
return -1;
}
std::cout << "== elaborated ==\n" << pretty(prog) << '\n';
// ------------------------------------------------------------------------ //
// Evaluation
//
// Evaluate the syntax tree, producing a partially evalutaed
// abstract syntax tree.
if (Term* term = as<Term>(prog)) {
Evaluator eval;
std::cout << "== output ==\n";
Expr* result = eval(term);
std::cout << "== result ==\n" << pretty(result) << '\n';
} else {
std::cout << "== no evaluation ==\n";
}
}