Skip to content

Instantly share code, notes, and snippets.

Created March 14, 2015 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8a84869f0817dbeaa11c to your computer and use it in GitHub Desktop.
Save anonymous/8a84869f0817dbeaa11c to your computer and use it in GitHub Desktop.
ast::expr::NodePtr Parser::ParseTerm(TokenIterator& it, const TokenIterator& end)
{
using namespace ast::expr;
auto currToken = *it;
switch(currToken)
{
case BinLiteral:
case OctLiteral:
case HexLiteral:
case DecLiteral:
{
detail::IncrIter(it);
return NodePtr(new IntegralConstantNode(currToken.value()));
}
case Identifier:
{
auto name = std::to_string(it->value());
detail::IncrIter(it);
return NodePtr(new VariableNode(name));
}
case ParenthesisOpen:
{
detail::IncrIter(it);
auto subExpr = ParseExpr(it, end);
detail::Expect(ParenthesisClose, it);
return subExpr;
}
default: // TODO: What exactly are we expecting here?
{
throw SyntaxErrorException();
}
}
}
ast::expr::NodePtr Parser::ParseFunctionCall(TokenIterator& it, const TokenIterator& end)
{
using namespace ast::expr;
if (it->id() == Identifier && detail::IsNextToken(it, end, ParenthesisOpen))
{
auto name = std::to_string(it->value());
detail::IncrIter(it);
detail::Expect(ParenthesisOpen, it);
std::vector<NodePtr> args;
while (it->id() != ParenthesisClose)
{
args.push_back(ParseTerm(it, end));
if (it->id() == Comma)
detail::IncrIter(it);
}
detail::Expect(ParenthesisClose, it);
return NodePtr(new FunctionCallNode(name, args));
}
else
{
return ParseTerm(it, end);
}
}
ast::expr::NodePtr Parser::ParseMulDiv(TokenIterator& it, const TokenIterator& end)
{
using namespace ast::expr;
auto lhs = ParseFunctionCall(it, end);
for (;;)
{
if (it->id() == Multiply || it->id() == Divide)
{
detail::IncrIter(it);
lhs = NodePtr(new BinaryOpNode(static_cast<TokenType>(it->id()), lhs, ParseFunctionCall(it, end)));
}
else
{
break;
}
}
return lhs;
}
@rightfold
Copy link

Your code is bad and you should feel bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment