系列入口:编程实战:类C语法的编译型脚本解释器(系列)-CSDN博客
前文已经介绍了编译入口,核心就是语句,本文介绍语句的编译。
目录
一、代码概览
二、辅助函数
2.1 tokens.IsPosNotToken(pos)
2.2 tokens.MoveCurrentToken(pToken, pos)
2.3 tokens.IsDelimiter(pos, ";")
2.4 tokens.MoveNextToken(pToken, pos)
2.5 tokens.IsOperator(pos, "(")
三、流程分析
一、代码概览
代码太长,直接贴:
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;
}
整个过程通过pos贯穿,pos是当前的Token位置,大部分函数使用的都是pos的引用,在内部会修改pos,函数返回时pos总是处于下一个未处理的Token位置。
二、辅助函数
里面用到一些功能比较小的函数。
2.1 tokens.IsPosNotToken(pos)
这个判断是否pos已经到达结束位置。
2.2 tokens.MoveCurrentToken(pToken, pos)
这个将pToken指向pos处的Token,方便后面使用。
2.3 tokens.IsDelimiter(pos, ";")
这个函数判断pos是否是给定的分隔符,需要同时判断Token类型和文本。
2.4 tokens.MoveNextToken(pToken, pos)
同时将pToken和pos指向下一个Token。
2.5 tokens.IsOperator(pos, "(")
这个函数判断是否是给定的操作符,需要同时判断Token类型和文本。
三、流程分析
流程处理相对简单,根据获取到的第一个Token的类型做分支处理,每个分支根据各自的语法要求获取语句或变量。
如果是标识符、数字、字符串、操作符,就是一个表达式,调用GetExpression()函数获取一个表达式,生成一个表达式语句:
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;
剩下的就是关键字,对关键字再分别处理,虽然稍微负责一些,但用到的东西和上面的差不多。
剩下的未介绍的部分就是表达式编译了。
(这里是结束,但不是整个系列的结束)