-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.cpp
More file actions
45 lines (40 loc) · 1007 Bytes
/
repl.cpp
File metadata and controls
45 lines (40 loc) · 1007 Bytes
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 "ast.h"
#include "lexer.h"
#include "object.h"
#include "parser.h"
#include "token.h"
#include "evaluator.h"
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <fmt/core.h>
using namespace std;
using obj::Environment;
using ast::Programs_Guard;
void print_parser_errors(const vector<string>& errors)
{
for(auto e : errors)
cout << e;
}
void start_repl()
{
auto env = make_unique<Environment>();
Programs_Guard guard;
for (string s = ""; s != "salir()"; getline(cin, s))
{
Lexer lexer(s);
Parser parser(lexer);
auto program = guard.new_program(parser.parse_program());
if(parser.errors().size() > 0)
{
print_parser_errors(parser.errors());
fmt::print("\n>> ");
continue;
}
auto evaluated = evaluate(program, env.get());
if(evaluated != nullptr)
fmt::print("{}", evaluated->inspect());
fmt::print("\n>> ");
}
}