系列入口:编程实战:类C语法的编译型脚本解释器(系列)-CSDN博客
前文已经介绍了编译入口,核心就是语句,本文介绍语句的编译。
一、代码概览
代码太长,直接贴:
bool GetSentence(CTokens& tokens, T_VARIABLE_S& vars, Sentence& sentence, Token*& pToken, size_t& pos)
{
sentence.clear();
Expression* pExpression;
if (tokens.IsPosNotToken(pos))return false;
sentence.source_start = tokens.TokenStart(pos);
sentence.source_end = tokens.TokenStart(pos);
tokens.MoveCurrentToken(pToken, pos);
switch (pToken->type)
{
case Token::IDENTIFIER:
case Token::NUMBER:
case Token::STRING:
case Token::OPERATOR:
sentence.type = Sentence::EXPRESSION;
pExpression = GetExpression(tokens, vars, NULL, pos, ";");
if (!tokens.IsPosNotToken(pos) && tokens.IsDelimiter(pos, ";"))
{
tokens.MoveNextToken(pToken, pos);
}
sentence.expressions.reserve(1);
sentence.expressions.push_back(*pExpression);
break;
case Token::DELIMITER:
if (pToken->text == "{")
{
sentence.type = Sentence::BLOCK;
tokens.MoveNextToken(pToken, pos);
while (true)
{
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,块语句未结束");
if (tokens.IsDelimiter(pos, "}"))
{
tokens.MoveNextToken(pToken, pos);
break;
}
Sentence tmpsentence;
if (!GetSentence(tokens, vars, tmpsentence, pToken, pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "获取块语句的语句出错");
sentence.sentences.reserve(sentence.sentences.size() + 1);
sentence.sentences.push_back(tmpsentence);
}
}
else if (pToken->text == "}")
{
CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的块语句结束标记");
}
else if (pToken->text == ";")
{
sentence.type = Sentence::NULLSENTENCE;
tokens.MoveNextToken(pToken, pos);
}
else
{
CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "尚未支持的分隔符");
}
break;
case Token::KEYWORD:
if (pToken->text == "return")
{
sentence.type = Sentence::RETURN;
tokens.MoveNextToken(pToken, pos);
pExpression = GetExpression(tokens, vars, NULL, pos, ";");
Expression null_expression;
if (NULL == pExpression)
{
pExpression = &null_expression;
}
if (!tokens.IsPosNotToken(pos) && tokens.IsDelimiter(pos, ";"))
{
tokens.MoveNextToken(pToken, pos);
}
sentence.expressions.reserve(1);
sentence.expressions.push_back(*pExpression);
}
else if (pToken->text == "if")
{
sentence.type = Sentence::IF;
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待左括号");
if (!tokens.IsOperator(pos, "("))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待左括号");
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待条件表达式");
if (NULL == (pExpression = GetExpression(tokens, vars, NULL, pos, ")")))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "条件表达式为空");
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待右括号");
if (!tokens.IsOperator(pos, ")"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待右括号");
sentence.expressions.reserve(1);
sentence.expressions.push_back(*pExpression);
tokens.MoveNextToken(pToken, pos);
sentence.sentences.reserve(2);
Sentence if_sentence;
if (!GetSentence(tokens, vars, if_sentence, pToken, pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "分析if块的执行语句出错");
sentence.sentences.push_back(if_sentence);
if (!tokens.IsPosNotToken(pos))
{
if (pToken->type == Token::KEYWORD && pToken->text == "else")
{
tokens.MoveNextToken(pToken, pos);
Sentence else_sentence;
if (!GetSentence(tokens, vars, else_sentence, pToken, pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "分析else块的执行语句出错");
sentence.sentences.push_back(else_sentence);
}
}
}
else if (pToken->text == "do")
{
sentence.type = Sentence::DO;
tokens.MoveNextToken(pToken, pos);
sentence.sentences.reserve(1);
Sentence if_sentence;
if (!GetSentence(tokens, vars, if_sentence, pToken, pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "分析do块的执行语句出错");
sentence.sentences.push_back(if_sentence);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待while");
if (pToken->type != Token::KEYWORD || pToken->text != "while")CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待while");
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待左括号");
if (!tokens.IsOperator(pos, "("))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待左括号");
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待条件表达式");
if (NULL == (pExpression = GetExpression(tokens, vars, NULL, pos, ")")))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "条件表达式为空");
tokens.MoveCurrentToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待右括号");
if (!tokens.IsOperator(pos, ")"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待右括号");
sentence.expressions.reserve(1);
sentence.expressions.push_back(*pExpression);
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待分号");
if (!tokens.IsDelimiter(pos, ";"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待分号");
tokens.MoveNextToken(pToken, pos);
}
else if (pToken->text == "while")
{
sentence.type = Sentence::WHILE;
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待左括号");
if (!tokens.IsOperator(pos, "("))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待左括号");
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待条件表达式");
if (NULL == (pExpression = GetExpression(tokens, vars, NULL, pos, ")")))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "条件表达式为空");
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待右括号");
if (!tokens.IsOperator(pos, ")"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待右括号");
sentence.expressions.reserve(1);
sentence.expressions.push_back(*pExpression);
tokens.MoveNextToken(pToken, pos);
sentence.sentences.reserve(1);
Sentence if_sentence;
if (!GetSentence(tokens, vars, if_sentence, pToken, pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "分析while块的执行语句出错");
sentence.sentences.push_back(if_sentence);
}
else if (pToken->text == "for")
{
sentence.type = Sentence::FOR;//第一个分号前面是一个语句
sentence.expressions.reserve(2);
sentence.sentences.reserve(2);
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待左括号");
if (!tokens.IsOperator(pos, "("))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待左括号");
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待初始化表达式");
Sentence init_sentence;
if (!GetSentence(tokens, vars, init_sentence, pToken, pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "分析for块的初始化语句出错");
sentence.sentences.push_back(init_sentence);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待条件表达式");
if (NULL == (pExpression = GetExpression(tokens, vars, NULL, pos, ";")))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "获取条件表达式出错");
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待分号");
if (!tokens.IsDelimiter(pos, ";"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待分号");
sentence.expressions.push_back(*pExpression);
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待循环递增表达式");
if (NULL == (pExpression = GetExpression(tokens, vars, NULL, pos, ")")))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "获取循环递增表达式出错");
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待右括号");
if (!tokens.IsOperator(pos, ")"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待右括号");
sentence.expressions.push_back(*pExpression);
tokens.MoveNextToken(pToken, pos);
Sentence for_sentence;
if (!GetSentence(tokens, vars, for_sentence, pToken, pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "分析for块的执行语句出错");
sentence.sentences.push_back(for_sentence);
}
else if ("break" == pToken->text)
{
sentence.type = Sentence::BREAK;
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待分号");
if (!tokens.IsDelimiter(pos, ";"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待分号");
tokens.MoveNextToken(pToken, pos);
}
else if ("continue" == pToken->text)
{
sentence.type = Sentence::CONTINUE;
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待分号");
if (!tokens.IsDelimiter(pos, ";"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待分号");
tokens.MoveNextToken(pToken, pos);
}
else if ("long" == pToken->text || "double" == pToken->text || "int" == pToken->text || "float" == pToken->text || "string" == pToken->text)
{//声明语句
sentence.type = Sentence::DECLARE;
Variable var;
if ("long" == pToken->text || "int" == pToken->text)var.type = Variable::LONG;
else if ("double" == pToken->text || "float" == pToken->text)var.type = Variable::DOUBLE;
else var.type = Variable::STRING;
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待变量名");
if (tokens.IsDelimiter(pos, ";"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的分号,期待变量名");
if (pToken->type != Token::IDENTIFIER)CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待标识符");
sentence.expressions.resize(1);
sentence.expressions[0].type = Expression::DEFINE;
sentence.expressions[0].VariableName = pToken->text;
sentence.expressions[0].m_variable = var;
sentence.expressions[0].source_start = sentence.source_start;
sentence.expressions[0].source_end = tokens.TokenStart(pos + 1);
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))
{
}
else if (tokens.IsOperator(pos, "="))
{//带有初始值
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待初始化表达式");
if (NULL == (pExpression = GetExpression(tokens, vars, NULL, pos, ";")))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "获取初始化表达式出错");
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待分号");
if (!tokens.IsDelimiter(pos, ";"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待分号");
tokens.MoveNextToken(pToken, pos);
sentence.expressions[0].AddLeftOperand(*pExpression);
}
else if (tokens.IsOperator(pos, "("))
{//函数
for (long i = 0; i < (long)m_functions.size(); ++i)
{
if (m_functions[i].script_name == sentence.expressions[0].VariableName)CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "函数重名");
}
m_functions.resize(m_functions.size() + 1);
CScript* pFunction = &m_functions[m_functions.size() - 1];
pFunction->FromParent(*this, sentence.expressions[0].VariableName);
pFunction->return_type = var.type;
pFunction->count_global_variable = vars.getGlobalCount();
tokens.MoveNextToken(pToken, pos);
while (!tokens.IsOperator(pos, ")"))
{
Variable param;
if ("long" == pToken->text || "int" == pToken->text)param.type = Variable::LONG;
else if ("double" == pToken->text || "float" == pToken->text)param.type = Variable::DOUBLE;
else if("string"== pToken->text)param.type = Variable::STRING;
else CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待参数类型");
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待参数名");
if (pToken->type != Token::IDENTIFIER)CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待标识符");
if (!pFunction->m_EnvVariables.AddVariable(pToken->text, param))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "函数参数重名");
if (NULL!=vars.FindVariable(pToken->text))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "函数参数与全局变量或环境变量重名");
tokens.MoveNextToken(pToken, pos);
if (tokens.IsPosNotToken(pos))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未期待的脚本结束,期待)");
if (tokens.IsOperator(pos, ","))tokens.MoveNextToken(pToken, pos);
else break;
}
if (!tokens.IsOperator(pos, ")"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待)");
tokens.MoveNextToken(pToken, pos);
if (!tokens.IsDelimiter(pos, "{"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待{");
tokens.MoveNextToken(pToken, pos);
T_VARIABLE_S tmpvars;
tmpvars.FromParentVars(vars, pFunction->count_global_variable, &pFunction->m_EnvVariables);
if (!pFunction->Build(tokens, tmpvars, pos))
{
m_msg += "编译失败\r\n";
return false;
}
if (!tokens.IsDelimiter(pos, "}"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待}");
tokens.MoveNextToken(pToken, pos);
if (!tokens.IsDelimiter(pos, ";"))CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待分号");
tokens.MoveNextToken(pToken, pos);
return GetSentence(tokens, vars, sentence, pToken, pos);
}
else if (!tokens.IsDelimiter(pos, ";"))
{
CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "期待分号,一个语句只能定义一个变量");
}
}
else
{
CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "非期待的的关键字,是否前面缺少分号?");
}
break;
default:
CException::Throw(__FILE__, __LINE__, m_source, tokens.TokenStart(pos), "未支持的标记类型");
break;
}
sentence.source_end = tokens.TokenStart(pos);
return sentence.source_end != sentence.source_start;
}
(我还没写完)