Files
2026-04-25 17:23:04 +07:00

318 lines
10 KiB
C

#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;
}