This repository was archived by the owner on Mar 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.cpp
More file actions
203 lines (184 loc) · 6.44 KB
/
Main.cpp
File metadata and controls
203 lines (184 loc) · 6.44 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
<----------------Tinh Linh Language - Linh Interpreter-------------->
// Part of the Tinh Linh language ecosystem:
// - Linh (this project): Interpreter implementation
// - Tinh: AOT compiler with runtime (similar to Go)
// - Lithium: Language standard specification
//
// Tinh Linh is a multi-typed language supporting both static (vas) and dynamic (var) typing, as well as const.
// Tinh Linh does not allow the existence of null. Only 'nothing' is used as the primitive 'no-value'.
//
// The id(x) function returns the id of the value, not a unique id for each variable.
// For primitive types (int, float, bool, string), id(x) is the same if the value is the same.
// For array/map, id(x) is the memory address (reference), so it is the same if they refer to the same object.
//
// IMPORTANT: Linh is NOT Python or JavaScript. Not everything is an object.
// Primitive types (int, uint, float, bool, str, nothing) are NOT objects and do not have methods or properties.
// Only array/map are reference types (objects), but they are not class-based objects.
// There is no class, prototype, or inheritance system. Linh is a statically-typed, value-oriented language.
// <------------------------------------------------------------------>
*/
#include "LinhC/Parsing/Lexer/Lexer.hpp"
#include "LinhC/Parsing/Parser/Parser.hpp"
#include "LinhC/Parsing/AST/ASTPrinter.hpp" // For printing AST
#include "LinhC/Parsing/Semantic/SemanticAnalyzer.hpp"
#include "LinhC/Bytecode/BytecodeEmitter.hpp"
#include "LiVM/LiVM.hpp"
#include "REPL/REPL.hpp" // Thêm dòng này
#include <iostream>
#include <fstream>
#include <sstream>
#include <string> // For std::string
// utf 8
#ifdef _WIN32
#include <windows.h>
#else
#include <locale>
#endif
#include "config/config.hpp" // Thêm dòng này để lấy thông tin version
void force_console_utf8()
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
#else
std::locale::global(std::locale(""));
std::cout.imbue(std::locale());
#endif
}
void runSource(const std::string &source_code,
const std::string &source_file_path = "",
Linh::Semantic::SemanticAnalyzer *sema_ptr = nullptr,
Linh::BytecodeEmitter *emitter_ptr = nullptr,
Linh::LiVM *vm_ptr = nullptr);
void runFile(const std::string &filename)
{
std::ifstream file(filename);
if (!file)
{
#ifdef _DEBUG
std::cerr << "Could not open file: " << filename << std::endl;
#endif
return;
}
std::string line, source;
while (std::getline(file, line))
{
source += line + "\n";
}
runSource(source, filename, nullptr, nullptr, nullptr);
}
// Đặt biến này vào đúng namespace Linh::Semantic để tránh lỗi linker
namespace Linh
{
namespace Semantic
{
Linh::BytecodeEmitter *g_main_emitter = nullptr;
}
}
void runSource(const std::string &source_code,
const std::string &source_file_path,
Linh::Semantic::SemanticAnalyzer *sema_ptr,
Linh::BytecodeEmitter *emitter_ptr,
Linh::LiVM *vm_ptr)
{
#ifdef _DEBUG
std::cout << "--- Source Code Being Parsed ---\n"
<< source_code << "\n--------------------------------\n";
#endif
Linh::Lexer lexer(source_code);
std::vector<Linh::Token> tokens = lexer.scan_tokens();
Linh::Parser parser(tokens);
Linh::AST::StmtList ast = parser.parse();
if (parser.had_error())
{
#ifdef _DEBUG
std::cerr << "Lỗi cú pháp, dừng thực thi." << std::endl;
#endif
return;
}
Linh::BytecodeEmitter emitter;
Linh::Semantic::g_main_emitter = &emitter; // Đặt emitter chính trước khi semantic để import có thể merge
Linh::Semantic::SemanticAnalyzer sema;
// Set the current file path for proper module resolution
if (!source_file_path.empty())
{
sema.set_current_file_path(source_file_path);
}
sema.analyze(ast);
if (!sema.errors.empty())
{
#ifdef _DEBUG
for (const auto &err : sema.errors)
{
std::cerr << "[Line " << err.line << ", Col " << err.column << "] SemanticError : " << err.message << std::endl;
}
std::cerr << "Có lỗi semantic, dừng thực thi." << std::endl;
#endif
return;
}
emitter.emit(ast);
Linh::Semantic::g_main_emitter = nullptr; // Đặt lại sau khi xong
// --- Debug: In ra danh sách function sau khi merge ---
#ifdef _DEBUG
std::cout << "\n--- Function Table After Import Merge ---\n";
for (const auto &kv : emitter.get_functions())
{
std::cout << "Function: " << kv.first << ", param_count: " << kv.second.param_names.size() << "\n";
}
std::cout << "------------------------------------------\n";
#endif
// -----------------------------------------------------
// --- Run VM ---
Linh::LiVM vm;
// Set the current file path for VM module resolution
if (!source_file_path.empty())
{
vm.current_file_path = source_file_path;
}
// --- Chuyển đổi function table ---
std::unordered_map<std::string, Linh::LiVM::Function> vm_functions;
for (const auto &kv : emitter.get_functions())
{
Linh::LiVM::Function fn;
fn.code = kv.second.code;
fn.param_names = kv.second.param_names;
vm_functions[kv.first] = std::move(fn);
}
vm.set_functions(vm_functions);
// --- Kết thúc chuyển đổi ---
vm.run(emitter.get_chunk());
// --- Debug: print VM stack and variables after execution (optional)
#ifdef _DEBUG
// (You can add methods to LiVM to expose stack/vars for debugging if needed)
std::cout << "\nParse succeeded!" << std::endl;
#endif
}
void runSource(const std::string &source_code)
{
runSource(source_code, "", nullptr, nullptr, nullptr);
}
int main(int argc, char **argv)
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
force_console_utf8();
if (argc > 1)
{
std::string arg1 = argv[1];
if (arg1 == "-v" || arg1 == "--version" || arg1 == "-V")
{
std::cout << name << " (" << engine << ") version " << version << " [" << version_number << "]\n";
std::cout << "Copyright (c) 2025 Sao Tin Developer Team\n";
std::cout << "Author: " << author << "\n";
std::cout << "Website: " << web << "\n";
return 0;
}
runFile(argv[1]);
}
else
{
Linh::run_repl(); // Thay thế runREPL()
}
return 0;
}