I made simple interpreter in C and now, I want to make my code better.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "def.h"
int main(int argc, char *argv[])
{
int i;
char ch[100];
char input[100];
char sinput[100];
char output[100];
int while_int;
if (argv[1] != NULL)
{
char *file_extension = strrchr(argv[1], '.');
if (file_extension == NULL)
{
printf("Error\nNo File Extension\n");
}
else if (strcmp(file_extension, ".pri") == FALSE)
{
FILE *fp = fopen(argv[1], "r");
fgets(ch, 100, fp);
fclose(fp);
}
}
do
{
if (argv[1] == NULL)
{
printf(">>> ");
scanf("%[^\n]%*c", ch);
}
if (ch[0] != '@' & argv[1] == NULL)
{
while_int = TRUE;
}
else
{
while_int = FALSE;
}
if (ch[0] == '"' & ch[strlen(ch) - 1] == '"')
{
if (strlen(ch) == 1 | strlen(ch) == 2)
{
printf("Error\nError Code: PRINTLANG.1\n");
ch[0] = '@';
}
else
{
chh(ch, output, 0, strlen(ch) - 1);
printf("%s\n", output);
ch[0] = '@';
}
}
if (ch[0] == 'p' & ch[1] == 'r' & ch[2] == 'i' & ch[3] == 'n' & ch[4] == 't')
{
if (ch[5] == '(' & ch[strlen(ch) - 1] == ')')
{
if (ch[6] == '"' & ch[strlen(ch) - 2] == '"')
{
if (strlen(ch) == 8 | strlen(ch) == 9)
{
printf("Error\nError Code: PRINTLANG.2\n");
ch[0] = '@';
}
else
{
chh(ch, output, 6, strlen(ch) - 2);
printf("%s\n", output);
ch[0] = '@';
}
}
else if (ch[6] == 'i' & ch[7] == 'n' & ch[8] == 'p' & ch[9] == 'u' & ch[10] == 't')
{
if (ch[11] == '(' & ch[strlen(ch) - 2] == ')')
{
if (strlen(ch) == 14)
{
scanf("%[^\n]%*c", input);
printf("%s\n", input);
ch[0] = '@';
}
else
{
if (ch[12] == '"' & ch[strlen(ch) - 3] == '"')
{
if (strlen(ch) == 16)
{
printf("Error\nError Code: PRINTLANG.3\n");
ch[0] = '@';
}
else
{
chh(ch, output, 12, strlen(ch) - 3);
printf("%s", output);
scanf("%[^\n]%*c", input);
printf("%s\n", input);
ch[0] = '@';
}
}
else
{
printf("Error\nError Code: PRINTLANG.4\n");
}
}
}
else
{
printf("Error\nError Code: PRINTLANG.5\n");
ch[0] = '@';
}
}
else
{
printf("Error\nError Code: PRINTLANG.6\n");
ch[0] = '@';
}
}
else
{
printf("Error\nError Code: PRINTLANG.7\n");
ch[0] = '@';
}
}
else if (ch[0] == 'e' & ch[1] == 'c' & ch[2] == 'h' & ch[3] == 'o')
{
if (ch[4] == ' ')
{
if (ch[5] == '"' & ch[strlen(ch) - 1] == '"')
{
chh(ch, output, 5, strlen(ch) - 1);
printf("%s\n", output);
ch[0] = '@';
}
else
{
printf("Error\nError Code: PRINTLANG.8\n");
ch[0] = '@';
}
}
else
{
printf("Error\nError Code: PRINTLANG.9\n");
ch[0] = '@';
}
}
else
{
ch[0] = '@';
}
}
while (while_int);
}
def.h:
#ifndef _DEF_H
# define _DEF_H
#endif
#define TRUE (1 == 1)
#define FALSE (1 == 0)
char *chh(char *ch, char *output, int x, int y)
{
int i;
for (i = x + 1; i < y; i++)
{
output[i - (x + 1)] = ch[i];
}
return output;
}