C-在解释器/编译器中需要时在结构中的另一种类型上使用指针
C- Using pointer on another type in structure when I need it in Interpret/ Compilator
我们正在编写 java 语言的解释(简化了很多,称为 IFJ16 语言)作为我们的团队项目。
我们编写的语言是纯 C 语言(我们不能使用任何 OOP)
这是问题所在:
在解释中我们需要读取 Instr 类型的结构
typedef struct Instr {
BTSNode *Id1; // Adress of first operand
BTSNode *Id2; // Adresss of second operand
BTSNode *Id3; // Adress where the result is added
InstrType type; // Type of instruction. InstrType is enum struct (like insDiv, insMux, ...)
}Instr;
BTSNode是二叉树中的一个节点,所有变量(类和函数都存储在这里)。结构:
typedef struct tBTSNode {
tableName key; // Key, variable/func,class name
NodeType nodeType; // Type (class, func, var)
union {
struct tBTSNode *functions; // Pointer on functions of class
int argNo; // Argument number if its function argument
} tBTSNode;
tabSymbol data; // Data
int inc; // Was var initialized?
struct tBTSNode *variables; // Pointer on function or class variables
struct tBTSNode *lptr; // Pointer on left subtree
struct tBTSNode *rptr; // Pointer on right subtree
} BTSNode, *tBTSNodePtr;
typedef struct tableSymbolVariable {
varType type; //In var case: type of var | in function case= return var type | In class case: NULL
varValue value; //In case of var: var value | In case of class and func: null
} tabSymbol, *tabSymbolPtr;
typedef enum {
var_int,
var_double,
var_string,
var_void,
} varType;
union {
int intValue;
double doubleValue;
char *stringValue;
} varValue;
因此 Interprer 获得了 指令栈(其简单堆栈由 Instructf 类型的 Instructionf 填充),已排序(因此第一个指令将首先执行,...)。在他执行堆栈的最后一项时通过循环解释 will(那时程序结束)。
所以问题来了。我们有例如指令
a = b + c;
很简单。结构看起来像(注意:那不是真正的代码,只是为了向您展示问题所在)
typedef struct Instr {
BTSNode *Id1 = adress of node with variable b
BTSNode *Id2 = adress of node with variable c
BTSNode *Id3 = adress of node with variable a (there will by the b+c result storaged)
InstrType type = insPlus
}Instr;
所以我将只调用我得到的 doMath 函数(当然我会检查类型是否正确。)
Id3->data.value = Id1->data.value Id2->data.value;
问题来了。如果我将示例更改为这个
会怎样
a = b + 30;
我该怎么办?使用 a 和 b 没有问题。我只会在二叉树中找到变量并将它们添加到 Id1 和 Id3 中。
但是我应该怎么处理30号呢?
我正在考虑更改 Instr 结构,因此 Id1-Td3 将是 void* 指针,在优先级分析中,我会将它们重新键入 BTSNode 指针(如果它们确实是指针)和数字(或字符串,... ) 在这种情况下只需重新键入 int* 指针。所以结构将是
typedef struct Instr {
BTSNode *Id1 = adress of node with variable b
Int *Id2 = adress of node with variable c
BTSNode *Id3 = adress of node with variable a (there will by the b+c result storaged)
InstrType type = insPlus
}Instr;
所以 我的问题是: 这是 "right" 解决方案吗?或者我可以做得更好更容易?
感谢您的回答和帮助。
您可以创建具有常量值的虚拟匿名变量,并像普通变量一样从指令节点引用它们。
我们正在编写 java 语言的解释(简化了很多,称为 IFJ16 语言)作为我们的团队项目。
我们编写的语言是纯 C 语言(我们不能使用任何 OOP)
这是问题所在:
在解释中我们需要读取 Instr 类型的结构
typedef struct Instr {
BTSNode *Id1; // Adress of first operand
BTSNode *Id2; // Adresss of second operand
BTSNode *Id3; // Adress where the result is added
InstrType type; // Type of instruction. InstrType is enum struct (like insDiv, insMux, ...)
}Instr;
BTSNode是二叉树中的一个节点,所有变量(类和函数都存储在这里)。结构:
typedef struct tBTSNode {
tableName key; // Key, variable/func,class name
NodeType nodeType; // Type (class, func, var)
union {
struct tBTSNode *functions; // Pointer on functions of class
int argNo; // Argument number if its function argument
} tBTSNode;
tabSymbol data; // Data
int inc; // Was var initialized?
struct tBTSNode *variables; // Pointer on function or class variables
struct tBTSNode *lptr; // Pointer on left subtree
struct tBTSNode *rptr; // Pointer on right subtree
} BTSNode, *tBTSNodePtr;
typedef struct tableSymbolVariable {
varType type; //In var case: type of var | in function case= return var type | In class case: NULL
varValue value; //In case of var: var value | In case of class and func: null
} tabSymbol, *tabSymbolPtr;
typedef enum {
var_int,
var_double,
var_string,
var_void,
} varType;
union {
int intValue;
double doubleValue;
char *stringValue;
} varValue;
因此 Interprer 获得了 指令栈(其简单堆栈由 Instructf 类型的 Instructionf 填充),已排序(因此第一个指令将首先执行,...)。在他执行堆栈的最后一项时通过循环解释 will(那时程序结束)。
所以问题来了。我们有例如指令
a = b + c;
很简单。结构看起来像(注意:那不是真正的代码,只是为了向您展示问题所在)
typedef struct Instr {
BTSNode *Id1 = adress of node with variable b
BTSNode *Id2 = adress of node with variable c
BTSNode *Id3 = adress of node with variable a (there will by the b+c result storaged)
InstrType type = insPlus
}Instr;
所以我将只调用我得到的 doMath 函数(当然我会检查类型是否正确。)
Id3->data.value = Id1->data.value Id2->data.value;
问题来了。如果我将示例更改为这个
会怎样a = b + 30;
我该怎么办?使用 a 和 b 没有问题。我只会在二叉树中找到变量并将它们添加到 Id1 和 Id3 中。 但是我应该怎么处理30号呢?
我正在考虑更改 Instr 结构,因此 Id1-Td3 将是 void* 指针,在优先级分析中,我会将它们重新键入 BTSNode 指针(如果它们确实是指针)和数字(或字符串,... ) 在这种情况下只需重新键入 int* 指针。所以结构将是
typedef struct Instr {
BTSNode *Id1 = adress of node with variable b
Int *Id2 = adress of node with variable c
BTSNode *Id3 = adress of node with variable a (there will by the b+c result storaged)
InstrType type = insPlus
}Instr;
所以 我的问题是: 这是 "right" 解决方案吗?或者我可以做得更好更容易?
感谢您的回答和帮助。
您可以创建具有常量值的虚拟匿名变量,并像普通变量一样从指令节点引用它们。