Long time no see

This commit is contained in:
2026-02-21 10:47:00 +07:00
parent 0d54fe176e
commit b7df98a55c
198 changed files with 3249 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
15
+22
View File
@@ -0,0 +1,22 @@
global_var = 10
neg_num = -5
result = global_var + neg_num
user_age = int(input())
if global_var >= 5:
result = result + 1
if neg_num <= 0:
print("Negatine num\n")
counter = 0
while counter <= 3:
print(counter)
counter += 1
for i in range(10):
result = result + 1
print(result)
print(user_age)
+317
View File
@@ -0,0 +1,317 @@
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#define MAXINPUTARRAY 1000
#define MAXKNOWN 30
#define MAXROW 40
#define CMDLIST 4
#define CMDCOUNT 6
int it_is_what(wchar_t ch);
int main(){
setlocale(LC_ALL, "");
wchar_t massive[MAXINPUTARRAY] = {0};
wchar_t knownint[MAXKNOWN] = {0};
int CellsInMainArray = 0;
{ // GET ALL INPUT
wchar_t ch;
while((ch = getwchar()) != WEOF){
massive[CellsInMainArray]=ch;
CellsInMainArray++;
if(CellsInMainArray>=MAXINPUTARRAY)
return 1100;
}
}
{ // IS X(LETTER) EXISIST?
int k = 0;
while(k++ < CellsInMainArray){
if((massive[k] >= 'a' && massive[k] <= 'z') &&
massive[k+1]==' ' &&
!(massive[k-1] >= 'a' && massive[k-1] <= 'z')){
int mark = 1;
for(int i = 0;i<MAXKNOWN-1;i++){
if(massive[k]==knownint[i])mark=0;
}
if(mark){
putwchar(L'i'); putwchar(L'n');
putwchar(L't');putwchar(L' ');
putwchar(massive[k]); putwchar(' ');
putwchar('='); putwchar(' ');
putwchar('0'); putwchar(';'); putwchar('\n');
knownint[knownint[MAXKNOWN-1]] = massive[k];knownint[MAXKNOWN-1]++;
}
}
}
putwchar('\n');putwchar('\n');
}
int massiveCursor = 0;
wchar_t row[MAXROW] = {0};
int deathMark = 1;
do{
int command = 0;
int rowCursor = 0;
{// GET ROW
{ // CLEAR ROW
for(int i=0; i<MAXROW; i++) row[i] = ' ';
rowCursor = 0;
}
{ // GET ROW
while(massiveCursor < CellsInMainArray && rowCursor < MAXROW &&
massive[massiveCursor] != '\n' && row[rowCursor] != WEOF){
row[rowCursor] = massive[massiveCursor];
massiveCursor++;
rowCursor++;
if(massive[massiveCursor] == '\0' ||
massive[massiveCursor] == WEOF){
deathMark = 0;
}
}
massiveCursor++;
rowCursor++;
}
if(massiveCursor>MAXINPUTARRAY)return 0;
}
{ // TEST FOR COMMENT AND \t \n ' '
{
if(row[0]=='#'){
int i = 0;
putwchar('/');putwchar('/');
while(i++ < rowCursor)putwchar(row[i]);
putwchar('\n');
continue;
}
}
{
int i = 0;
if(row[0] == WEOF) return 0;
if(rowCursor<4){
while(i < rowCursor) putwchar(row[i++]);
continue;}
}
{
while(row[0]=='\t' || row[0] == ' '){
putwchar(row[0]);
for(int i = 0;i<rowCursor;i++){
row[i]=row[i+1];
}
rowCursor--;
}
}
}
{ //Test for see what command is it
wchar_t cmd[CMDLIST][CMDCOUNT] = {L"if",L"while",L"for",L"print"};
for(int i = 0;i<CMDLIST;i++){
int marker = 0;
int symbCount = 0;
for(int k = 0;cmd[i][k]!='\0';k++){
symbCount++;
}
for(int k = 0;k < symbCount;k++){
if(row[k]==cmd[i][k]){
marker++;
}
if (symbCount == marker && symbCount != 0){
command = i+1;
break;
}
}
if(command!= 0) break;
}
}
{//PRINT x = something;
if(it_is_what(row[0])==0 && // Enter if its letter
(it_is_what(row[2])==8 || // and logic
it_is_what(row[2])==9)){ // or arfimetic
wchar_t pyinput[] = L"int(input())";
int marker = 1,i=0;
while(pyinput[i++]!='\0'){
if(row[i+4]!=pyinput[i])marker=0;
}
if(marker){
wchar_t cinput[] = {L"scanf(\"%d\", &"};
i = 0;
while(cinput[i]!='\0')
putwchar(cinput[i++]);
putwchar(row[0]);putwchar(')');putwchar(';');putwchar('\n');
}
else{
for(int i = 0;i<rowCursor;i++){
if(row[i]=='\n')row[i]='\0';
putwchar(row[i]);
}
putwchar(';');putwchar('\n');
}
continue;
}
}
switch(command){ //COMMANDS
case 1:{ // IF if x > 0:
{ // PRINT PRE LETTERS
wchar_t word[] = L"if(";
int i = 0;
while(word[i]!='\0')putwchar(word[i++]);
}
{ // IF FUNC
int xpos = 3;
putwchar(row[xpos]);// x
putwchar(row[xpos+2]);// >
// >=
int tmp = 0;
if(it_is_what(row[xpos+3])==8){putwchar(row[xpos+3]);tmp++;}
// NUM
for(int i = xpos+4+tmp;i<rowCursor;i++){
if(it_is_what(row[i])==2) putwchar(row[i]);
}
}
{ // PRINT POST LETTERS
wchar_t word[] = L")\n";
int i = 0;
while(word[i]!='\0')putwchar(word[i++]);
}
break;
} // if(x > 0)
case 2:{ // WHILE while i < 10:
{ // PRINT PRE LETTERS
wchar_t word[] = L"while(";
int i = 0;
while(word[i]!='\0')putwchar(word[i++]);
}
{ // WHILE FUNC
int xpos = 6;
putwchar(row[xpos]);// i
putwchar(row[xpos+2]);// >
// >=
int tmp = 0;
if(it_is_what(row[xpos+3])==8){putwchar(row[xpos+3]);tmp++;}
// NUM
for(int i = xpos+4+tmp;i<rowCursor;i++){
if(it_is_what(row[i])==2) putwchar(row[i]);
}
}
{ // PRINT POST LETTERS
wchar_t word[] = L")\n";
int i = 0;
while(word[i]!='\0')putwchar(word[i++]);
}
break;
} // while(i < 10)
case 3:{ // FOR for i in range (10):
{ // PRINT PRE LETTERS
wchar_t word[] = L"for(int ";
int i = 0;
while(word[i]!='\0')putwchar(word[i++]);
}
{ // FOR FUNC
int xpos = 4;
putwchar(row[xpos]);
wchar_t word[] = L" = 0;i < ";
int i = 0;
while(word[i]!='\0')putwchar(word[i++]);
// NUM
for(int i = xpos+11;i<rowCursor;i++)
if(it_is_what(row[i])==2) putwchar(row[i]);
}
{ // PRINT POST LETTERS
wchar_t word[] = L";i++)\n";
int i = 0;
while(word[i]!='\0')putwchar(word[i++]);
}
break;
} // for(int i = 0;i<10;i++)
case 4:{ // PRINT print(L"letters")
{ // PRINT PRE LETTERS
wchar_t word[] = {L"printf(\""};
int i = 0;
while(word[i]!='\0')putwchar(word[i++]);
}
{ // PRINT FUNC
int xpos = 6;
if(it_is_what(row[xpos])==0){
wchar_t word[] = {L"%d\", "};
int i = 0;
while(word[i]!='\0')
putwchar(word[i++]);
putwchar(row[xpos]);
}
else{
xpos++;
while(row[xpos+1]!=')'){
if(it_is_what(row[xpos])==11)
putwchar('\\');
if(it_is_what(row[xpos])==12)
putwchar('%');
putwchar(row[xpos]);
xpos++;
}
putwchar('\"');
}
}
{ // PRINT POST LETTERS
wchar_t word[] = {L");\n"};
int i = 0;
while(word[i]!='\0')putwchar(word[i++]);
}
break;
}// printf(L"letters");
}
{ // ERROR HANDLE
if (command == 0 && row[0] != WEOF){
if(row[rowCursor]=='\n')row[rowCursor]='\0';
wchar_t err[] = L"ERROR CMD NOT FOUND!";
int k = 0;
while(err[k++]!='\0')putwchar(err[k-1]);
return 1;
}
}
}while(row[0] != WEOF && deathMark);
return 0;
}
int it_is_what(wchar_t ch){
if(ch>='a'&&ch<='z')return 0;
if(ch>='A'&&ch<='Z')return 1;
if(ch>='0'&&ch<='9')return 2;
if(ch == '=' || ch == '>' || ch == '<' || ch == '!')return 8;
if(ch == '*' || ch == '-' || ch == '+' || ch == '/')return 9;
if(ch == '\\' || ch == '\"' || ch == '\'')return 11;
if(ch == '%')return 12;
return -1;
}
+373
View File
@@ -0,0 +1,373 @@
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#define MAXINPUTARRAY 1000
#define MAXKNOWN 30
#define MAXKNOWNSYMB 32
#define MAXROW 80
#define CMDLIST 4
// --- GLOBAL DATA ---
wchar_t G_knownint[MAXKNOWN][MAXKNOWNSYMB] = {0};
int G_knownCount = 0;
// --- FUNCTION DECLARATIONS ---
int it_is_what(wchar_t ch);
int are_strings_equal(const wchar_t *s1, const wchar_t *s2);
int is_variable_known(const wchar_t *name);
void put_wchar_string(const wchar_t *s);
void put_char_string(const char *s);
// --- MAIN TRANSLATOR LOGIC ---
int main() {
setlocale(LC_ALL, "");
wchar_t massive[MAXINPUTARRAY] = {0};
int CellsInMainArray = 0;
{ // GET INPUT
wchar_t ch;
while ((ch = getwchar()) != WEOF) {
if (CellsInMainArray >= MAXINPUTARRAY) { return 1001; }
massive[CellsInMainArray++] = ch;
}
}
{ // PRE-SCAN: DECLARE ALL VARIABLES AS INT
put_char_string("#include <stdio.h>\n");
put_char_string("int main(){\n");
int k = 0;
while (k < CellsInMainArray) {
if (it_is_what(massive[k]) == 0 || it_is_what(massive[k]) == 1) {
wchar_t tempName[MAXKNOWNSYMB] = {0};
int len = 0;
int startK = k;
{ // EXTRACT NAME
while (startK < CellsInMainArray && (it_is_what(massive[startK]) >= 0)) {
if (len < MAXKNOWNSYMB - 1) tempName[len++] = massive[startK];
startK++;
}
}
{ // CHECK KEYWORDS AND DECLARE
int isKeyword = 0;
if (are_strings_equal(tempName, L"if") || are_strings_equal(tempName, L"while") ||
are_strings_equal(tempName, L"for") || are_strings_equal(tempName, L"print") ||
are_strings_equal(tempName, L"in") || are_strings_equal(tempName, L"range") ||
are_strings_equal(tempName, L"int") || are_strings_equal(tempName, L"input")) isKeyword = 1;
if (!isKeyword && tempName[0] != L'\0') {
if (!is_variable_known(tempName) && G_knownCount < MAXKNOWN) {
for (int c = 0; c < len; c++) G_knownint[G_knownCount][c] = tempName[c];
G_knownCount++;
// DECLARE VARIABLE
put_char_string("int ");
put_wchar_string(tempName);
put_char_string(" = 0;\n");
}
}
}
k = startK;
} else {
k++;
}
}
putwchar(L'\n');
}
// --- SECOND PASS: LEXICAL ANALYSIS AND TRANSLATION ---
int massiveCursor = 0;
wchar_t row[MAXROW] = {0};
int deathMark = 1;
int blockLevel = 0;
do {
if (massiveCursor >= CellsInMainArray) break;
int command = 0;
int rowCursor = 0;
int initialSpaceCount = 0;
{ // GET ROW AND INDENTATION
for (int i = 0; i < MAXROW; i++) row[i] = 0;
rowCursor = 0;
while (massiveCursor < CellsInMainArray && rowCursor < MAXROW - 1) {
wchar_t cur = massive[massiveCursor];
if (cur == L'\n') { massiveCursor++; break; }
if (cur == WEOF || massiveCursor == CellsInMainArray - 1) { deathMark = 0; }
row[rowCursor++] = cur;
massiveCursor++;
}
int i = 0;
while (row[i] == L' ' || row[i] == L'\t') { initialSpaceCount++; i++; }
// CLOSE BLOCK
if (blockLevel == 1 && initialSpaceCount == 0 && rowCursor > 0) {
put_char_string("}\n");
blockLevel = 0;
}
// PRINT INDENTATION
for (int j = 0; j < initialSpaceCount; j++) putwchar(L' ');
// TRIM ROW
if (initialSpaceCount > 0) {
for (int j = 0; j < rowCursor - initialSpaceCount; j++) {
row[j] = row[j + initialSpaceCount];
}
rowCursor -= initialSpaceCount;
for (int j = rowCursor; j < MAXROW; j++) row[j] = 0;
}
}
{ // HANDLE PYTHON COMMENTS (#)
int hashPos = -1;
for (int i = 0; i < rowCursor; i++) {
if (row[i] == L'#') { hashPos = i; break; }
}
if (hashPos != -1) {
put_char_string("//");
for (int k = 0; k < rowCursor; k++) {
if (row[k] == L'#') break;
putwchar(row[k]);
}
for (int k = hashPos + 1; k < rowCursor; k++) putwchar(row[k]);
putwchar(L'\n');
continue;
}
}
// SKIP EMPTY LINES
if (rowCursor == 0) {
putwchar(L'\n');
continue;
}
{ // DETERMINE COMMAND TYPE
wchar_t cmd_list[CMDLIST][10] = {L"if", L"while", L"for", L"print"};
for (int i = 0; i < CMDLIST; i++) {
int lenCmd = 0;
while (cmd_list[i][lenCmd] != L'\0') lenCmd++;
if (rowCursor >= lenCmd) {
int match = 1;
for (int k = 0; k < lenCmd; k++) {
if (row[k] != cmd_list[i][k]) { match = 0; break; }
}
if (match) {
int pos = lenCmd;
while(row[pos] == L' ') pos++;
if (i == 3) { // PRINT
if (row[pos] == L'(') { command = 4; break; }
} else { // IF, WHILE, FOR
int colonPos = -1;
for (int p = pos; p < rowCursor; p++) {
if (row[p] == L':') { colonPos = p; break; }
}
if (colonPos != -1) { command = i + 1; break; }
}
}
}
}
}
{ // HANDLE ASSIGNMENT / OPERATION (command == 0)
if (command == 0) {
int eqPos = -1;
int plusEqPos = -1;
int varNameEnd = -1;
for (int i = 0; i < rowCursor; i++) {
if (row[i] == L'=') {
eqPos = i;
if (i > 0 && row[i - 1] == L'+') plusEqPos = i;
varNameEnd = i - 1;
while(varNameEnd >= 0 && row[varNameEnd] == L' ') varNameEnd--;
break;
}
}
// If assignment found and not comparison
if (eqPos > 0 && row[eqPos + 1] != L'=' && plusEqPos == -1) {
// 1. Check for input pattern
wchar_t inputPattern[] = L"int(input())";
int isInput = 1;
int checkIdx = 0;
int valPos = eqPos + 1;
while (row[valPos] == L' ') valPos++;
while (inputPattern[checkIdx] != L'\0') {
if (row[valPos + checkIdx] != inputPattern[checkIdx]) { isInput = 0; break; }
checkIdx++;
}
if (isInput) {
// INPUT (scanf)
put_char_string("scanf(\"%d\", &");
for (int j = 0; j <= varNameEnd; j++) putwchar(row[j]);
put_char_string(");\n");
} else {
// REGULAR ASSIGNMENT (x = -5, x = a + 1)
for (int i = 0; i < rowCursor; i++) { putwchar(row[i]); }
put_char_string(";\n");
}
continue;
}
// Augmented Assignment (x += 1) or other unknown lines
for (int i = 0; i < rowCursor; i++) { putwchar(row[i]); }
put_char_string(";\n");
continue;
}
}
{ // TRANSLATE COMMAND (switch)
switch (command) {
case 1: // IF
case 2: // WHILE
case 3: { // FOR
wchar_t *start_word;
int len_cmd;
if (command == 1) { start_word = L"if("; len_cmd = 2; }
else if (command == 2) { start_word = L"while("; len_cmd = 5; }
else { start_word = L"for("; len_cmd = 3; }
put_wchar_string(start_word);
int pos = len_cmd;
while (row[pos] == L' ') pos++;
if (command == 3) { // FOR (i in range(10))
wchar_t iterVar[MAXKNOWNSYMB] = {0};
int ivLen = 0;
// GET ITERATOR NAME
while (row[pos] != L' ' && row[pos] != L'\0' && pos < rowCursor && row[pos] != L':') {
iterVar[ivLen++] = row[pos];
pos++;
}
// SKIP 'in range'
while (pos < rowCursor && row[pos] != L'(') pos++;
pos++;
// C FORMAT: for(i = 0; i < limit; i++)
put_wchar_string(iterVar); put_char_string(" = 0;");
put_wchar_string(iterVar); put_char_string(" < ");
// GET LIMIT
while (pos < rowCursor && row[pos] != L')' && row[pos] != L':') { putwchar(row[pos++]); }
put_char_string(";");
put_wchar_string(iterVar); put_char_string("++");
} else {
// IF / WHILE CONDITION (Handling x >= y conditions)
while (pos < rowCursor && row[pos] != L':') {
if (row[pos] == L'>' && row[pos+1] == L'=') {
put_char_string(" >= ");
pos += 2;
} else if (row[pos] == L'<' && row[pos+1] == L'=') {
put_char_string(" <= ");
pos += 2;
} else if (row[pos] == L'!' && row[pos+1] == L'=') {
put_char_string(" != ");
pos += 2;
} else if (row[pos] == L'=' && row[pos+1] == L'=') {
put_char_string(" == ");
pos += 2;
} else {
putwchar(row[pos++]);
}
}
}
put_char_string(")\n");
// OPEN BLOCK
put_char_string("{\n");
blockLevel = 1;
break;
}
case 4: { // PRINT (print(x) or print("hello"))
put_char_string("printf(");
int pos = 5;
while (row[pos] != L'(') pos++;
pos++;
int isString = (row[pos] == L'\"');
if (isString) {
putwchar(L'\"');
pos++;
while (pos < rowCursor && row[pos] != L')') {
if (row[pos] == L'\"' && isString) break;
putwchar(row[pos++]);
}
putwchar(L'\"');
} else {
put_char_string("\"%d\", ");
while (pos < rowCursor && row[pos] != L')') {
putwchar(row[pos++]);
}
}
put_char_string(");\n");
break;
}
}
}
} while (deathMark);
{ // FINAL BRACKETS
if (blockLevel == 1) {
put_char_string("}\n");
}
put_char_string("}\n");
}
return 0;
}
// --- AUXILIARY FUNCTION DEFINITIONS ---
void put_wchar_string(const wchar_t *s) {
int i = 0;
while (s[i] != L'\0') { putwchar(s[i++]); }
}
void put_char_string(const char *s) {
int i = 0;
while (s[i] != '\0') { putwchar(s[i++]); }
}
int it_is_what(wchar_t ch) {
if (ch >= L'a' && ch <= L'z') return 0;
if (ch >= L'A' && ch <= L'Z') return 1;
if (ch >= L'0' && ch <= L'9') return 2;
if (ch == L'_') return 3;
return -1;
}
int are_strings_equal(const wchar_t *s1, const wchar_t *s2) {
int i = 0;
while (s1[i] != L'\0' || s2[i] != L'\0') {
if (s1[i] != s2[i]) return 0;
i++;
}
return 1;
}
int is_variable_known(const wchar_t *name) {
if (name[0] == L'\0') return 1;
for (int r = 0; r < G_knownCount; r++) {
if (are_strings_equal(G_knownint[r], name)) {
return 1;
}
}
return 0;
}
Binary file not shown.
+43
View File
@@ -0,0 +1,43 @@
#include <stdio.h>
int main(){
int global_var = 0;
int neg_num = 0;
int result = 0;
int user_age = 0;
int Negatine = 0;
int num = 0;
int n = 0;
int counter = 0;
int i = 0;
global_var = 10 ;
neg_num = -5 ;
result = global_var + neg_num ;
scanf("%d", &user_age);
if(global_var >= 5)
{
result = result + 1;
}
if(neg_num <= 0)
{
printf("Negatine num\n");
}
counter = 0;
while(counter <= 3)
{
printf("%d", counter);
counter += 1;
}
for(i = 0;i < 10;i++)
{
result = result + 1 ;
}
printf("%d", result);
printf("%d", user_age);
}
Binary file not shown.
+5
View File
@@ -0,0 +1,5 @@
gcc ./main.c -o ./output/prog
./output/prog < ./input/test.py > output/output.c
bat ./output/output.c
gcc ./output/output.c -o ./output/output
./output/output < ./input/test.num