本文整理汇总了C++中TokenStream::setToken方法的典型用法代码示例。如果您正苦于以下问题:C++ TokenStream::setToken方法的具体用法?C++ TokenStream::setToken怎么用?C++ TokenStream::setToken使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TokenStream
的用法示例。
在下文中一共展示了TokenStream::setToken方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: expression
double expression(TokenStream& tokenStream)
{
double result = term(tokenStream); // set result to value of term
Token token = tokenStream.getToken(); // get next token from tokenStream
while (true)
{
if (token.kind == '+')
{
result += term(tokenStream); // evaluate term and add to result
token = tokenStream.getToken(); // get next token from tokenStream
}
if (token.kind == '-')
{
result -= term(tokenStream); // evaluate term and subtract from result
token = tokenStream.getToken(); // get next token from tokenStream
}
if (token.kind != '+' && token.kind != '-')
{
tokenStream.setToken(token); // reset token into token stream
return result; // return result
}
}
}
示例2: calculate
void calculate()
{
const char quit = 'q'; // t.kind==quit means that t is a quit Token
const char print = ';'; // t.kind==print means that t is a print Token
const std::string prompt = "> ";
const std::string equals = "= "; // used to indicate that what follows is an evaluation
TokenStream tokenStream;
std::string enterToClose = "~~";
while (std::cin)
try
{
std::cout << prompt;
Token token = tokenStream.getToken();
if (token.kind == print)
token = tokenStream.getToken(); // eat ';'
if (token.kind == quit)
break;
tokenStream.setToken(token); // reset token into tokenStream
std::cout << equals << expression(tokenStream) << '\n';
}
catch (std::exception& e)
{
std::cerr << e.what() << '\n'; // write error message
tokenStream.ignore(print);
}
}
示例3: term
double term(TokenStream& tokenStream)
{
double result = primary(tokenStream); // set result to value of primary
Token token = tokenStream.getToken(); // get next token from tokenStream
while (true)
{
if (token.kind == '*')
{
result *= primary(tokenStream); // evaluate primary and multiply with result
token = tokenStream.getToken(); // get next token from tokenStream
}
else if (token.kind == '/')
{
double divisor = primary(tokenStream); // set divisor to value of primary
if (divisor == 0) error("divide by zero"); // check for error
result /= divisor; // divide result by divisor
token = tokenStream.getToken(); // get next token from tokenStream
}
else if (token.kind == '%') // modulo will only work on ints for this implementation
{
int numer = result; // cast result to int
int denom = primary(tokenStream); // cast denom to int
if (denom == 0) error("%: divide by zero"); // error check
result = numer % denom; // get result
token = tokenStream.getToken(); // get next token from tokenStream
}
else
{
tokenStream.setToken(token); // put token back into the Token stream
return result; // return result
}
}
}