我一直在尝试制作一种小编程语言,但我不知道如何让我的解释器存储和调用变量
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VARIABLES 10
// Structure to store variables
struct Variable {
char name[50];
char value[256];
};
// Array to store variables
struct Variable variables[MAX_VARIABLES];
int variableCount = 0;
// Function to find a variable by name
struct Variable* findVariable(const char* name) {
for (int i = 0; i < variableCount; ++i) {
if (strcmp(variables[i].name, name) == 0) {
return &variables[i];
}
}
return NULL; // Variable not found
}
// Function to interpret commands
void interpretCommand(const char* command) {
if (strncmp(command, "console:write->", 15) == 0) {
// Extract the message within double quotes
const char* messageStart = strchr(command, '"');
const char* messageEnd = strrchr(command, '"');
if (messageStart != NULL && messageEnd != NULL && messageStart < messageEnd) {
// Print the message, replacing variables if present
for (const char* p = messageStart + 1; p < messageEnd; ++p) {
if (*p == '*') {
++p; // Move past '*'
const char* varStart = p;
while (*p != '*' && p < messageEnd) {
++p;
}
char varName[50];
strncpy(varName, varStart, p - varStart);
varName[p - varStart] = '\0';
struct Variable* variable = findVariable(varName);
if (variable != NULL) {
printf("%s", variable->value);
} else {
printf("Undefined variable: %s", varName);
}
} else {
putchar(*p);
}
}
putchar('\n');
} else {
printf("Invalid message format\n");
}
} else if (strncmp(command, "console:read->", 14) == 0) {
// Extract the prompt within double quotes
const char* promptStart = strchr(command, '"');
const char* promptEnd = strrchr(command, '"');
if (promptStart != NULL && promptEnd != NULL && promptStart < promptEnd) {
// Print the prompt and read user input
printf("%.*s", (int)(promptEnd - promptStart - 1), promptStart + 1);
// Read user input
char userInput[256]; // Adjust size as needed
fgets(userInput, sizeof(userInput), stdin);
} else {
printf("Invalid prompt format\n");
}
} else {
// Check if the command starts with "variableName->"
char* arrow = strstr(command, "->");
if (arrow != NULL) {
*arrow = '\0'; // Separate variable name and value
// Find or create a variable with the given name
struct Variable* variable = findVariable(command);
if (variable == NULL) {
if (variableCount < MAX_VARIABLES) {
strcpy(variables[variableCount].name, command);
variable = &variables[variableCount++];
} else {
printf("Maximum number of variables reached\n");
return;
}
}
// Copy the value to the variable
strcpy(variable->value, arrow + 2);
} else {
printf("Unknown command\n");
}
}
}
int main() {
// Open the file
FILE* file = fopen("King.roar", "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
char line[256]; // Assuming a maximum line length of 255 characters
// Read and interpret each line from the file
while (fgets(line, sizeof(line), file) != NULL) {
// Remove newline character if present
size_t len = strlen(line);
if (len > 0 && line[len - 1] == '\n') {
line[len - 1] = '\0';
}
// Interpret the command
interpretCommand(line);
}
// Close the file
fclose(file);
return 0;
}
这是我存储在 King.roar 中的示例代码
console:read->"Enter your name: "->name
console:read->"Enter your age: "->age
console:write->"Hello *name*!"
console:write->"You are *age* years old!"
由于某种原因,我只得到未定义的变量,我不知道数据是否未存储,或者调用不起作用
我尝试在互联网上查找,但没有找到任何相关内容。感谢您的帮助!
(免责声明:我没有尝试过)。
根据您的代码,您正在通过检查来检查变量
(变量名)->
(寻找 -> 并在箭头处将名字剪掉)。但是您的示例代码使用
->(变量名)
(->name, ->age),因此在箭头处截断将导致使用空字符串作为变量名称。尝试打印出您最终用于存储的变量的名称,并用 * 和 * 包围 - 我认为您最终会得到 **!