Fixed build errors.

This commit is contained in:
yhirose 2015-08-27 22:26:34 -04:00
parent 568da082dc
commit 337741e17e
6 changed files with 24 additions and 30 deletions

View File

@ -55,7 +55,7 @@ int main(int argc, const char** argv)
shared_ptr<Ast> ast;
if (parser.parse(expr, ast)) {
ast = AstOptimizer(true).optimize(ast);
AstPrint::print(ast);
print_ast(ast);
cout << expr << " = " << eval(*ast) << endl;
return 0;
}

View File

@ -228,7 +228,7 @@ int repl(shared_ptr<Environment> env, bool print_ast)
auto ret = run("(repl)", env, line.c_str(), line.size(), val, msgs, ast);
if (ret) {
if (print_ast) {
AstPrint::print(ast);
peg::print_ast(ast);
}
cout << val << endl;
linenoise::AddHistory(line.c_str());
@ -297,7 +297,7 @@ int main(int argc, const char** argv)
}
return -1;
} else if (print_ast) {
AstPrint::print(ast);
peg::print_ast(ast);
}
}

View File

@ -437,7 +437,7 @@ int main(int argc, const char** argv)
shared_ptr<AstPL0> ast;
if (parser.parse_n(source.data(), source.size(), ast, path)) {
if (argc > 2 && string("--ast") == argv[2]) {
AstPrint::print(ast);
print_ast(ast);
}
try {
SymbolTable::build_on_ast(ast);
@ -452,3 +452,4 @@ int main(int argc, const char** argv)
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix

View File

@ -97,7 +97,7 @@ int main(int argc, const char** argv)
}
ast = peg::AstOptimizer(opt_optimize_ast_nodes).optimize(ast);
peg::AstPrint::print(ast);
peg::print_ast(ast);
} else {
if (!parser.parse_n(source.data(), source.size())) {
return -1;

View File

@ -24,7 +24,7 @@ function<void (size_t, size_t, const string&)> makeJSONFormatter(string& json)
};
}
bool parse_grammar(const string& text, peglib::peg& peg, string& json)
bool parse_grammar(const string& text, peg::parser& peg, string& json)
{
peg.log = makeJSONFormatter(json);
json += "[";
@ -33,7 +33,7 @@ bool parse_grammar(const string& text, peglib::peg& peg, string& json)
return ret;
}
bool parse_code(const string& text, peglib::peg& peg, string& json)
bool parse_code(const string& text, peg::parser& peg, string& json)
{
peg.log = makeJSONFormatter(json);
json += "[";
@ -57,7 +57,7 @@ int main(void)
string grammarResult;
string codeResult;
peglib::peg peg;
peg::parser peg;
auto ret = parse_grammar(grammarText, peg, grammarResult);
if (ret && peg) {

View File

@ -2039,30 +2039,23 @@ struct AstBase : public Annotation
std::shared_ptr<AstBase<Annotation>> parent;
};
struct AstPrint
{
template <typename T>
static void print(const std::shared_ptr<T>& ptr, int level = 0) {
const auto& ast = *ptr;
for (auto i = 0; i < level; i++) { std::cout << " "; }
if (ast.is_token) {
std::cout << "- " << name(ast) << ": '" << ast.token << "'" << std::endl;
} else {
std::cout << "+ " << name(ast) << std::endl;
}
for (auto node : ast.nodes) { print(node, level + 1); }
template <typename T>
void print_ast(const std::shared_ptr<T>& ptr, int level = 0) {
const auto& ast = *ptr;
for (auto i = 0; i < level; i++) { std::cout << " "; }
std::string name;
if (ast.name == ast.original_name) {
name = ast.name;
} else {
name = ast.original_name + " (" + ast.name + ")";
}
private:
template <typename T>
static std::string name(const T& ast) {
if (ast.name == ast.original_name) {
return ast.name;
} else {
return ast.original_name + " (" + ast.name + ")";
}
if (ast.is_token) {
std::cout << "- " << name << ": '" << ast.token << "'" << std::endl;
} else {
std::cout << "+ " << name << std::endl;
}
};
for (auto node : ast.nodes) { print_ast(node, level + 1); }
}
struct AstOptimizer
{