-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
113 lines (103 loc) · 3 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
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
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include "parser.hpp"
#include "interpreter.hpp"
#include "environment.hpp"
#include "values.hpp"
std::string nodeType(ast::NodeType type) {
switch (type) {
case ast::NodeType::Program:
return "ast::NodeType::Program";
break;
case ast::NodeType::VarDeclaration:
return "ast::NodeType::VarDeclaration";
break;
case ast::NodeType::AssignmentExpr:
return "ast::NodeType::AssignmentExpr" ;
break;
case ast::NodeType::MemberExpr:
return "ast::NodeType::MemberExpr" ;
break;
case ast::NodeType::CallExpr:
return "ast::NodeType::CallExpr" ;
break;
case ast::NodeType::Property:
return "ast::NodeType::Property";
break;
case ast::NodeType::ObjectLiteral:
return "ast::NodeType::ObjectLiteral";
break;
case ast::NodeType::NumericLiteral:
return "ast::NodeType::NumericLiteral";
break;
case ast::NodeType::NullLiteral:
return "ast::NodeType::NullLiteral";
break;
case ast::NodeType::Identifier:
return "ast::NodeType::Identifier" ;
break;
case ast::NodeType::BinaryExpr:
return "ast::NodeType::BinaryExpr" ;
break;
default:
return "Unknown NodeType";
break;
}
}
std::string ValueType(ValueType type) {
switch (type) {
case ValueType::Boolean:
return "Boolean";
break;
case ValueType::Null:
return "Null";
break;
case ValueType::Number:
return "Number";
break;
default:
return "Unknown ValueType";
break;
}
}
int main() {
/*std::string test = "let r = (45 + 45) ";
std::vector<Token> token = Tokenize(test);
for (auto it : token) {
std::cout << it.m_value << " = " << it.m_type << std::endl;
}*/
Environment env;
// Create Default Global Environment
//env.declareVar("x", MK_NUMBER("100"), false);
env.declareVar("true", MK_BOOL(true), false);
env.declareVar("false", MK_BOOL(false), false);
env.declareVar("null", MK_NULL(), false);
Parser parser;
std::cout << "\nTakt v0.1" << std::endl;
std::shared_ptr<ast::Program> program;
// Continue Repl Until User Stops Or Types `exit`
while (true) {
std::string input;
std::cout << "> ";
std::getline(std::cin, input);
// Check for no user input or exit keyword.
if (input.empty() || input.find("exit") != std::string::npos) {
std::exit(0);
}
// Produce AST From sourc-code
program = parser.produceAST(input);
for(auto i : program->body) {
std::cout << nodeType(i->kind)
<< " val: " << i->value
<< std::endl;
}
std::cout << "AST Produced:" << std::endl;
auto result = evaluate(program, env);
std::cout << ValueType(result.type) << std::endl;
std::cout << result.value << std::endl;
}
return 0;
}