本文整理汇总了C++中ASTNode::kind方法的典型用法代码示例。如果您正苦于以下问题:C++ ASTNode::kind方法的具体用法?C++ ASTNode::kind怎么用?C++ ASTNode::kind使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ASTNode
的用法示例。
在下文中一共展示了ASTNode::kind方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: evalParameters
std::vector<Structure> Evaluator::evalParameters(const ASTNode& node) {
if (node.kind() == NodeKind::optionalParameterList) {
auto children = node.children();
if (children.size() == 0) {
return std::vector<Structure>();
}
assert(children.size() == 1);
return evalParameters(children[0]);
}
assert(node.kind() == NodeKind::parameterList);
std::vector<Structure> ret;
for (const auto& child : node.children()) {
ret.push_back(eval(child));
}
return ret;
}
示例2: evalProgram
Structure Evaluator::evalProgram(const ASTNode& node) {
Structure ret;
assert(node.kind() == NodeKind::program);
for (const auto& exp : node.children()) {
ret = eval(exp);
}
return ret;
}
示例3: evalLiteral
Structure Evaluator::evalLiteral(const ASTNode& node) {
assert(node.kind() == NodeKind::literal);
auto children = node.children();
assert(children.size() == 1);
const auto& child = children[0];
switch (child.kind()) {
case NodeKind::integer:
return Structure(child.integer());
case NodeKind::floatingPoint:
return Structure(child.floatingPoint());
default:
assert(child.kind() == NodeKind::string);
return Structure(child.string());
}
}
示例4: eval
Structure Evaluator::eval(const ASTNode& node) {
switch (node.kind()) {
case NodeKind::program:
return evalProgram(node);
case NodeKind::expression:
return evalExpression(node);
case NodeKind::identifier:
return evalIdentifier(node);
case NodeKind::literal:
return evalLiteral(node);
default:
// not implemented
assert(false);
}
}
示例5: evalLetExpression
Structure Evaluator::evalLetExpression(const ASTNode& node) {
assert(node.kind() == NodeKind::expression);
const auto children = node.children();
assert(children.size() == 2);
assert(children[0].kind() == NodeKind::identifier &&
children[0].string() == "let");
assert(children[1].kind() == NodeKind::optionalParameterList);
const auto parameterList = children[1].children()[0];
const auto id_with_value = parameterList.children();
assert(id_with_value.size() == 2);
const auto& id = id_with_value[0];
assert(id.kind() == NodeKind::identifier);
m_symbols[id.string()] = eval(id_with_value[1]);
return Structure(true);
}
示例6: evalIfExpression
Structure Evaluator::evalIfExpression(const ASTNode& node) {
assert(node.kind() == NodeKind::expression);
const auto children = node.children();
assert(children.size() == 2);
assert(children[0].kind() == NodeKind::identifier &&
children[0].string() == "if");
assert(children[1].kind() == NodeKind::optionalParameterList);
const auto parameters = children[1].children()[0].children();
assert(parameters.size() == 3);
const auto condition = this->eval(parameters[0]);
if (condition) {
return this->eval(parameters[1]);
} else {
return this->eval(parameters[2]);
}
}
示例7: evalLambdaExpression
Structure Evaluator::evalLambdaExpression(const ASTNode& node) {
assert(node.kind() == NodeKind::expression);
const auto& children = node.children();
assert(children.size() == 2);
assert(children[0].kind() == NodeKind::identifier &&
children[0].string() == "lambda");
assert(children[1].kind() == NodeKind::optionalParameterList);
const auto& parameterList = children[1].children()[0].children();
return Structure(static_cast<Structure::Function>(
[this, parameterList]
(const ASTNode& node, std::vector<Structure> params) {
SymbolTable symbols = m_symbols.augment(dictOfParams(parameterList, params));
Evaluator e(symbols);
const auto& body = parameterList[parameterList.size()-1];
return e.eval(body);
}
));
}
示例8: evalCondExpression
Structure Evaluator::evalCondExpression(const ASTNode& node) {
assert(node.kind() == NodeKind::expression);
const auto children = node.children();
assert(children.size() == 2);
assert(children[0].kind() == NodeKind::identifier &&
children[0].string() == "cond");
assert(children[1].kind() == NodeKind::optionalParameterList);
const auto parameters = children[1].children()[0].children();
if (parameters.size() % 2 != 0) {
throw RuntimeError(children[1], "cond must take an even number of parameters (pairs of condition and expression)");
}
for (size_t i=0; i<parameters.size(); i+=2) {
const auto condition = this->eval(parameters[i]);
if (condition) {
return this->eval(parameters[i+1]);
}
}
throw RuntimeError(children[1], "`cond` expression did not return a value (no condition evaluated to true)");
}
示例9: evalExpression
Structure Evaluator::evalExpression(const ASTNode& node) {
assert(node.kind() == NodeKind::expression);
const auto& children = node.children();
assert(children.size() == 2);
if (children[0].kind() == NodeKind::identifier) {
if (children[0].string() == "lambda") {
return this->evalLambdaExpression(node);
} else if (children[0].string() == "let") {
return this->evalLetExpression(node);
} else if (children[0].string() == "if") {
return this->evalIfExpression(node);
} else if (children[0].string() == "cond") {
return this->evalCondExpression(node);
}
}
Structure fn = eval(children[0]);
std::vector<Structure> params = evalParameters(children[1]);
return fn(node, params);
}