本文整理汇总了C++中NodePtr::Add方法的典型用法代码示例。如果您正苦于以下问题:C++ NodePtr::Add方法的具体用法?C++ NodePtr::Add怎么用?C++ NodePtr::Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NodePtr
的用法示例。
在下文中一共展示了NodePtr::Add方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Function
void Parser::Function(NodePtr node)
{
Expect(Token::Func);
Expect(Token::Ident);
auto name = Last();
auto fun = NewNode(Node::Function);
fun->Add(name);
Expect(Token::OpenParan);
auto args = NewNode(Node::None);
fun->Add(args);
if (Try(Token::Ident))
{
args->Add(Consume());
while (Try(Token::Comma))
{
Consume();
args->Add(Expect(Token::Ident));
}
}
Expect(Token::CloseParan);
Block(fun);
node->Add(fun);
}
示例2: AddBlock
void Parser::AddBlock(NodePtr node)
{
// TODO: this doesnt make sense with our brace parsing
auto block = NewNode(Node::Block);
Block(block);
node->Add(block);
}
示例3: For
void Parser::For(NodePtr block)
{
if (!Try(Token::For))
return;
Consume();
auto f = NewNode(Node::For);
if (!Expression())
{
CreateError("For what?");
return;
}
if (Try(Token::In))
{
Consume();
f->Add(Pop());
if (!Expression())
{
CreateError("For each in what?");
return;
}
f->Add(Pop());
}
else
{
Expect(Token::Semi);
f->Add(Pop());
if (!Expression())
{
CreateError("When does the for statement stop?");
return;
}
f->Add(Pop());
Expect(Token::Semi);
if (!Expression())
{
CreateError("What happens when a for statement ends?");
return;
}
f->Add(Pop());
}
Expect(Token::NewLine);
AddBlock(f);
block->Add(f);
}
示例4: While
void Parser::While(NodePtr block)
{
auto w = NewNode(Consume());
if (!Expression())
{
CreateError("While what?");
return;
}
w->Add(Pop());
block->Add(w);
}
示例5: IfCondition
void Parser::IfCondition(NodePtr block)
{
if (!Try(Token::If))
return;
Consume();
Expect(Token::OpenParan);
// get the test expression
if (!Expression())
{
CreateError("If what?");
return;
}
NodePtr condition = Pop();
Expect(Token::CloseParan);
// get the true-clause
NodePtr trueClause = NewNode(Node::Block);
Block(trueClause);
// make the conditional node in AST
NodePtr cond = NewNode(Node::Conditional);
cond->Add(condition);
cond->Add(trueClause);
// if there's an else, add it as well
if (Try(Token::Else))
{
Consume();
NodePtr falseClause = NewNode(Node::Block);
Block(falseClause);
cond->Add(falseClause);
}
block->Add(cond);
}
示例6: Block
void Parser::Block(NodePtr node)
{
if (!Try(Token::OpenBrace))
return;
Consume();
auto block = NewNode(Node::Block);
Push(block);
while (Statement(block))
{
if (Try(Token::CloseBrace))
{
Consume();
break;
}
}
Pop();
node->Add(block);
}
示例7: Statement
bool Parser::Statement(NodePtr block)
{
switch (Current().type)
{
case Token::OpenBrace:
{
Block(block);
return true;
}
case Token::Assert:
{
Consume();
if (!Expression())
{
Fail(Lexer::CreateErrorMessage(Current(), "Assert needs an expression to test"));
return false;
}
auto ass = NewNode(Consume());
ass->Add(Pop());
Push(ass);
Expect(Token::Semi);
goto finis;
}
case Token::Return:
case Token::Yield:
{
auto ret = NewNode(Consume());
if (Expression())
ret->Add(Pop());
block->Add(ret);
Expect(Token::Semi);
goto finis;
}
case Token::While:
{
While(block);
return true;
}
case Token::For:
{
For(block);
return true;
}
case Token::If:
{
IfCondition(block);
return true;
}
case Token::Func:
{
Function(block);
return true;
}
}
if (!Expression())
return false;
// a block does not require a semicolon?
//Expect(Token::Semi);
block->Add(Pop());
finis:
return true;
}