0.简介
二元运算有很多,这里只用加法来举例子。
1.中间变量
在获得二元表达式的时候,要有中间结果在存储,所以要暗中处理中间结果。
一下是二元表达式计算中间代码的方法。
operand BinaryExpression::evaluation(std::vector<Quaternion>& stms, Memory& memory, NameTable& table)
{
operand result;
operand rightValue = right_expression->evaluation(stms, memory, table);
operand leftValue = left_expression->evaluation(stms, memory, table);
Token resType;
if (opt == TK_PLUS)
{
//类型判断
if (rightValue.type == KW_STRING || leftValue.type == KW_STRING)
{
//字符串不支持运算,报错
}
else if (rightValue.type == KW_INT || leftValue.type == KW_INT)
resType = KW_INT;
int addr = memory.malloc(4);
result = operand(resType,addr,true);
table.push(std::make_tuple(KW_INT,0,addr,"temp@"+addr));
stms.push_back(Quaternion(ADD,leftValue,rightValue,result));
}
return result;
}
2.结果
输入如下代码
int a;
int b = 99;
int c = 1

本文探讨了二元表达式的计算方法,重点介绍了如何处理加法运算中的中间结果,通过实例分析展示了中间代码生成的过程,包括类型判断、内存分配及结果存储。
最低0.47元/天 解锁文章
825

被折叠的 条评论
为什么被折叠?



