Skip to content

Commit 898f169

Browse files
authored
Merge pull request #19 from Shad7ows/main
تم تعديل الملفات لقبول اللغة العربية
2 parents ae0a4aa + fb5cfba commit 898f169

File tree

3 files changed

+133
-12
lines changed

3 files changed

+133
-12
lines changed

source/Alif5.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77

88
#include<iostream>
99
#include<fstream>
10-
#include<Windows.h>
1110
#include<string>
1211
#include<vector>
1312
#include<map>
1413
#include<math.h>
1514
#include<algorithm> // لعمل تتالي على المصفوفات
15+
16+
#ifndef _WIN64
17+
#include<codecvt>
18+
#include<locale>
19+
#else
20+
#include<Windows.h>
1621
#include<fcntl.h> //لقبول ادخال الاحرف العربية من الكونسل
1722
#include<io.h> //لقبول ادخال الاحرف العربية من الكونسل
23+
#endif
1824

1925
#include "Constants.h"
2026
#include "Position.h"
@@ -24,7 +30,28 @@
2430
#include "Parser.h"
2531
#include "AlifRun.h"
2632

27-
int wmain(int argc, wchar_t* argv[])
33+
#ifndef _WIN64
34+
int main(int argc, char** argv)
35+
{
36+
setlocale(LC_ALL, "en_US.UTF-8");
37+
if (argc > 1)
38+
{
39+
if (argc > 2)
40+
{
41+
prnt("يجب ان يتم تمرير اسم الملف فقط");
42+
exit(-1);
43+
}
44+
45+
file_run(argv[1]);
46+
}
47+
else
48+
{
49+
terminal_run();
50+
}
51+
}
52+
#else
53+
54+
int wmain(int argc, wchar_t** argv)
2855
{
2956
if (argc > 1)
3057
{
@@ -40,4 +67,6 @@ int wmain(int argc, wchar_t* argv[])
4067
{
4168
terminal_run();
4269
}
43-
}
70+
}
71+
72+
#endif // !_WIN64

source/AlifRun.h

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,99 @@
1+
#ifndef _WIN64
2+
3+
void file_run(char* _fileName) {
4+
5+
STR input_;
6+
std::string u8input;
7+
std::string line;
8+
9+
std::ifstream fileContent(_fileName);
10+
if (!fileContent.is_open()) {
11+
prnt(L"لا يمكن فتح الملف او انه غير موجود - تاكد من اسم الملف -");
12+
exit(-1);
13+
}
14+
15+
while (std::getline(fileContent, line))
16+
{
17+
if (line != "" and line != "\r")
18+
{
19+
u8input += line;
20+
u8input += "\n";
21+
}
22+
}
23+
fileContent.close();
24+
25+
int fnLength = sizeof(_fileName) / sizeof(char) + 6;
26+
STR fileName(&_fileName[0], &_fileName[fnLength]); // تحويل من char الى wchar_t
27+
28+
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert;
29+
/*
30+
تحويل utf-8
31+
الى utf-16
32+
لكي يقوم بحساب الحرف الواحد على انه 2 بايت وليس 1 بايت
33+
حيث نظام utf-8 يأخذ اول بايت من الحرف فقط ويترك البايت الثاني
34+
** يجب مراجعة جدول بايتات نظام utf-8
35+
*/
36+
input_ = convert.from_bytes(u8input);
37+
38+
// المعرب اللغوي
39+
/////////////////////////////////////////////////////////////////
40+
41+
Lexer lexer(fileName, input_);
42+
lexer.make_token();
43+
44+
// المحلل اللغوي
45+
/////////////////////////////////////////////////////////////////
46+
47+
Parser parser = Parser(&lexer.tokens_, fileName, input_);
48+
parser.parse_file();
49+
}
50+
51+
void terminal_run() {
52+
53+
STR fileName = L"<طرفية>";
54+
const STR about_ = L"ألف نـ5.0.0";
55+
STR input_;
56+
prnt(about_);
57+
58+
while (true) {
59+
60+
std::wcout << L"ألف -> ";
61+
std::getline(std::wcin, input_);
62+
63+
if (input_ == L"خروج")
64+
{
65+
exit(0);
66+
}
67+
68+
// المعرب اللغوي
69+
/////////////////////////////////////////////////////////////////
70+
71+
Lexer lexer(fileName, input_);
72+
lexer.make_token();
73+
74+
// المحلل اللغوي
75+
/////////////////////////////////////////////////////////////////
76+
77+
Parser parser = Parser(&lexer.tokens_, fileName, input_);
78+
parser.parse_terminal();
79+
80+
// std::wcin.ignore(); // لمنع ارسال قيمة فارغة في المتغير input_
81+
82+
83+
}
84+
}
85+
86+
#else
87+
188
std::wstring utf8_decode(const std::string& str)
289
{
90+
/*
91+
تحويل utf-8
92+
الى utf-16
93+
لكي يقوم بحساب الحرف الواحد على انه 2 بايت وليس 1 بايت
94+
حيث نظام utf-8 يأخذ اول بايت من الحرف فقط ويترك البايت الثاني
95+
** يجب مراجعة جدول بايتات نظام utf-8
96+
*/
397
int size_ = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
498
std::wstring strToWstr(size_, 0);
599
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &strToWstr[0], size_);
@@ -21,8 +115,6 @@ void file_run(wchar_t* _fileName) {
21115
std::string line;
22116

23117
std::ifstream fileContent(_fileName);
24-
//std::ifstream fileContent(L"../source/AlifCode.alif5"); // للتجربة فقط
25-
//_fileName = new wchar_t(L'test'); // للتجربة فقط
26118
if (!fileContent.is_open()) {
27119
prnt(L"لا يمكن فتح الملف او انه غير موجود - تاكد من اسم الملف -");
28120
exit(-1);
@@ -90,10 +182,6 @@ void terminal_run() {
90182
Parser parser = Parser(&lexer.tokens_, fileName, input_);
91183
parser.parse_terminal();
92184

93-
#ifndef _WIN64
94-
std::wcin.ignore(); // لمنع ارسال قيمة فارغة في المتغير input_
95-
#endif // !_WIN64
96-
97-
98185
}
99-
}
186+
}
187+
#endif // !_WIN64

source/Lexer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ class Lexer {
5151

5252
while (this->currentChar != L'\0')
5353
{
54-
if (this->currentChar == L'\n') // يجب ان يتم التحقق من السطر الجديد قبل المسافة او المسافة البادئة
54+
if (this->currentChar == L'\r')
55+
{
56+
this->advance();
57+
}
58+
else if (this->currentChar == L'\n') // يجب ان يتم التحقق من السطر الجديد قبل المسافة او المسافة البادئة
5559
{
5660
this->make_newline();
5761
}

0 commit comments

Comments
 (0)