-
-
Save anonymous/8a84869f0817dbeaa11c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code is bad and you should feel bad.