-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
105 lines (87 loc) · 2.86 KB
/
main.c
File metadata and controls
105 lines (87 loc) · 2.86 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* The function interprets a MeowScript program by incrementing, decrementing, or outputting ASCII
* values based on specific keywords.
*
* @param program The `program` parameter is a pointer to a character array (string) that represents
* the MeowScript program to be interpreted.
*/
void interpretMeowScript(const char* program) {
int asciiValue = 0;
const char* meow = "meow";
const char* hiss = "hiss";
const char* purr = "purr";
while (*program != '\0') {
if (strstr(program, meow) == program) {
asciiValue++;
program += 4;
} else if (strstr(program, hiss) == program) {
asciiValue--;
program += 4;
} else if (strstr(program, purr) == program) {
putchar(asciiValue);
program += 4;
} else {
program++;
}
}
}
int interactive() {
char buffer[256];
char *inputHistory = NULL;
size_t totalSize = 0;
printf("MeowScript's Interactive Mode :3 (Ctrl+D to exit):\n> ");
while(fgets(buffer, sizeof(buffer), stdin) != NULL) {
size_t bufferSize = strlen(buffer);
inputHistory = realloc(inputHistory, totalSize + bufferSize + 1);
if(inputHistory == NULL) {
perror("Error reallocating memory for input history");
return 1;
}
strcpy(inputHistory + totalSize, buffer);
totalSize += bufferSize;
interpretMeowScript(inputHistory);
printf("\n> ");
}
return 0;
}
/**
* This C program reads a file containing a MeowScript program, interprets the program, and prints the
* output.
*
* @param argc The `argc` parameter is an integer that represents the number of command-line arguments
* passed to the program. It stands for "argument count".
* @param argv The `argv` parameter is an array of strings that represents the command-line arguments
* passed to the program. In this case, `argv` is expected to have two elements:
*
* @return The main function is returning an integer value. In this case, it is returning 0, indicating
* successful execution of the program.
*/
int main(int argc, char *argv[]) {
if (argc != 2) {
return interactive();
}
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
perror("Error opening the file");
return 1;
}
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
char *meowScriptProgram = (char *)malloc(size + 1);
if (meowScriptProgram == NULL) {
perror("Error allocating memory for the program");
fclose(file);
return 1;
}
fread(meowScriptProgram, 1, size, file);
fclose(file);
meowScriptProgram[size] = '\0';
interpretMeowScript(meowScriptProgram);
printf("\n");
free(meowScriptProgram);
return 0;
}