-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
125 lines (105 loc) · 3.71 KB
/
main.cpp
File metadata and controls
125 lines (105 loc) · 3.71 KB
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
#include "token.h"
#include "lexer.h"
#include "parser.h"
#include "interpreter.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace AR437;
void runFile(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file '" << filename << "'" << std::endl;
return;
}
std::ostringstream buffer;
buffer << file.rdbuf();
std::string source = buffer.str();
file.close();
try {
// Lexical analysis
Lexer lexer(source);
std::vector<Token> tokens = lexer.tokenize();
// Syntax analysis
Parser parser(tokens);
auto program = parser.parse();
// Interpretation
Interpreter interpreter;
interpreter.interpret(*program);
} catch (const ParseError& error) {
std::cerr << "Parse Error: " << error.what() << std::endl;
} catch (const RuntimeError& error) {
std::cerr << "Runtime Error: " << error.what() << std::endl;
} catch (const std::exception& error) {
std::cerr << "Error: " << error.what() << std::endl;
}
}
void runInteractive() {
std::cout << "AR Programming Language Interpreter v1.0" << std::endl;
std::cout << "Enter commands (type 'exit' to quit):" << std::endl;
Interpreter interpreter;
std::string line;
while (true) {
std::cout << "AR> ";
if (!std::getline(std::cin, line)) {
break;
}
if (line == "exit" || line == "quit") {
break;
}
if (line.empty()) {
continue;
}
try {
// Lexical analysis
Lexer lexer(line);
std::vector<Token> tokens = lexer.tokenize();
// Syntax analysis
Parser parser(tokens);
auto program = parser.parse();
// Interpretation
interpreter.interpret(*program);
} catch (const ParseError& error) {
std::cerr << "Parse Error: " << error.what() << std::endl;
} catch (const RuntimeError& error) {
std::cerr << "Runtime Error: " << error.what() << std::endl;
} catch (const std::exception& error) {
std::cerr << "Error: " << error.what() << std::endl;
}
}
std::cout << "Goodbye!" << std::endl;
}
void showHelp() {
std::cout << "AR Programming Language" << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << " ar [script.ar] - Run a script file" << std::endl;
std::cout << " ar - Start interactive mode" << std::endl;
std::cout << " ar --help - Show this help message" << std::endl;
std::cout << std::endl;
std::cout << "Language Features:" << std::endl;
std::cout << " - Variables: let x = 10;" << std::endl;
std::cout << " - Functions: func add(a, b) { return a + b; }" << std::endl;
std::cout << " - Control flow: if, while, for" << std::endl;
std::cout << " - Data types: numbers, strings, booleans, null" << std::endl;
std::cout << " - Operators: +, -, *, /, %, ==, !=, <, >, <=, >=, &&, ||, !" << std::endl;
std::cout << " - Built-ins: print" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc == 1) {
// Interactive mode
runInteractive();
} else if (argc == 2) {
std::string arg = argv[1];
if (arg == "--help" || arg == "-h") {
showHelp();
} else {
// Run file
runFile(arg);
}
} else {
std::cerr << "Usage: " << argv[0] << " [script.ar]" << std::endl;
return 1;
}
return 0;
}