Applied clang-format

This commit is contained in:
yhirose 2017-08-04 07:03:44 -04:00
parent bb0c7c4522
commit 7c24c30104

View File

@ -58,7 +58,8 @@ auto grammar = R"(
/* /*
* Utilities * Utilities
*/ */
string format_error_message(const string& path, size_t ln, size_t col, const string& msg) { string format_error_message(const string& path, size_t ln, size_t col,
const string& msg) {
stringstream ss; stringstream ss;
ss << path << ":" << ln << ":" << col << ": " << msg << endl; ss << path << ":" << ln << ":" << col << ": " << msg << endl;
return ss.str(); return ss.str();
@ -69,8 +70,7 @@ string format_error_message(const string& path, size_t ln, size_t col, const str
*/ */
struct SymbolScope; struct SymbolScope;
struct Annotation struct Annotation {
{
shared_ptr<SymbolScope> scope; shared_ptr<SymbolScope> scope;
}; };
@ -79,8 +79,7 @@ typedef AstBase<Annotation> AstPL0;
/* /*
* Symbol Table * Symbol Table
*/ */
struct SymbolScope struct SymbolScope {
{
SymbolScope(shared_ptr<SymbolScope> outer) : outer(outer) {} SymbolScope(shared_ptr<SymbolScope> outer) : outer(outer) {}
bool has_symbol(const string& ident) const { bool has_symbol(const string& ident) const {
@ -89,43 +88,60 @@ struct SymbolScope
} }
bool has_constant(const string& ident) const { bool has_constant(const string& ident) const {
return constants.count(ident) ? true : (outer ? outer->has_constant(ident) : false); return constants.count(ident) ? true : (outer ? outer->has_constant(ident)
: false);
} }
bool has_variable(const string& ident) const { bool has_variable(const string& ident) const {
return variables.count(ident) ? true : (outer ? outer->has_variable(ident) : false); return variables.count(ident) ? true : (outer ? outer->has_variable(ident)
: false);
} }
bool has_procedure(const string& ident) const { bool has_procedure(const string& ident) const {
return procedures.count(ident) ? true : (outer ? outer->has_procedure(ident) : false); return procedures.count(ident) ? true : (outer ? outer->has_procedure(ident)
: false);
} }
map<string, int> constants; map<string, int> constants;
set<string> variables; set<string> variables;
map<string, shared_ptr<AstPL0>> procedures; map<string, shared_ptr<AstPL0>> procedures;
private: private:
shared_ptr<SymbolScope> outer; shared_ptr<SymbolScope> outer;
}; };
void throw_runtime_error(const shared_ptr<AstPL0> node, const string& msg) { void throw_runtime_error(const shared_ptr<AstPL0> node, const string& msg) {
throw runtime_error(format_error_message(node->path, node->line, node->column, msg)); throw runtime_error(
format_error_message(node->path, node->line, node->column, msg));
} }
struct SymbolTable struct SymbolTable {
{ static void build_on_ast(const shared_ptr<AstPL0> ast,
static void build_on_ast(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope = nullptr) { shared_ptr<SymbolScope> scope = nullptr) {
switch (ast->tag) { switch (ast->tag) {
case "block"_: block(ast, scope); break; case "block"_:
case "assignment"_: assignment(ast, scope); break; block(ast, scope);
case "call"_: call(ast, scope); break; break;
case "ident"_: ident(ast, scope); break; case "assignment"_:
default: for (auto node: ast->nodes) { build_on_ast(node, scope); } break; assignment(ast, scope);
break;
case "call"_:
call(ast, scope);
break;
case "ident"_:
ident(ast, scope);
break;
default:
for (auto node : ast->nodes) {
build_on_ast(node, scope);
}
break;
} }
} }
private: private:
static void block(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> outer) { static void block(const shared_ptr<AstPL0> ast,
shared_ptr<SymbolScope> outer) {
// block <- const var procedure statement // block <- const var procedure statement
auto scope = make_shared<SymbolScope>(outer); auto scope = make_shared<SymbolScope>(outer);
const auto& nodes = ast->nodes; const auto& nodes = ast->nodes;
@ -136,7 +152,8 @@ private:
ast->scope = scope; ast->scope = scope;
} }
static void constants(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void constants(const shared_ptr<AstPL0> ast,
shared_ptr<SymbolScope> scope) {
// const <- ('CONST' __ ident '=' _ number(',' _ ident '=' _ number)* ';' _) ? // const <- ('CONST' __ ident '=' _ number(',' _ ident '=' _ number)* ';' _) ?
const auto& nodes = ast->nodes; const auto& nodes = ast->nodes;
for (auto i = 0u; i < nodes.size(); i += 2) { for (auto i = 0u; i < nodes.size(); i += 2) {
@ -149,7 +166,8 @@ private:
} }
} }
static void variables(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void variables(const shared_ptr<AstPL0> ast,
shared_ptr<SymbolScope> scope) {
// var <- ('VAR' __ ident(',' _ ident)* ';' _) ? // var <- ('VAR' __ ident(',' _ ident)* ';' _) ?
const auto& nodes = ast->nodes; const auto& nodes = ast->nodes;
for (auto i = 0u; i < nodes.size(); i += 1) { for (auto i = 0u; i < nodes.size(); i += 1) {
@ -161,7 +179,8 @@ private:
} }
} }
static void procedures(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void procedures(const shared_ptr<AstPL0> ast,
shared_ptr<SymbolScope> scope) {
// procedure <- ('PROCEDURE' __ ident ';' _ block ';' _)* // procedure <- ('PROCEDURE' __ ident ';' _ block ';' _)*
const auto& nodes = ast->nodes; const auto& nodes = ast->nodes;
for (auto i = 0u; i < nodes.size(); i += 2) { for (auto i = 0u; i < nodes.size(); i += 2) {
@ -172,25 +191,31 @@ private:
} }
} }
static void assignment(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void assignment(const shared_ptr<AstPL0> ast,
shared_ptr<SymbolScope> scope) {
// assignment <- ident ':=' _ expression // assignment <- ident ':=' _ expression
const auto& ident = ast->nodes[0]->token; const auto& ident = ast->nodes[0]->token;
if (scope->has_constant(ident)) { if (scope->has_constant(ident)) {
throw_runtime_error(ast->nodes[0], "cannot modify constant value '" + ident + "'..."); throw_runtime_error(ast->nodes[0],
"cannot modify constant value '" + ident + "'...");
} else if (!scope->has_variable(ident)) { } else if (!scope->has_variable(ident)) {
throw_runtime_error(ast->nodes[0], "undefined variable '" + ident + "'..."); throw_runtime_error(ast->nodes[0],
"undefined variable '" + ident + "'...");
} }
} }
static void call(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void call(const shared_ptr<AstPL0> ast,
shared_ptr<SymbolScope> scope) {
// call <- 'CALL' __ ident // call <- 'CALL' __ ident
const auto& ident = ast->nodes[0]->token; const auto& ident = ast->nodes[0]->token;
if (!scope->has_procedure(ident)) { if (!scope->has_procedure(ident)) {
throw_runtime_error(ast->nodes[0], "undefined procedure '" + ident + "'..."); throw_runtime_error(ast->nodes[0],
"undefined procedure '" + ident + "'...");
} }
} }
static void ident(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void ident(const shared_ptr<AstPL0> ast,
shared_ptr<SymbolScope> scope) {
const auto& ident = ast->token; const auto& ident = ast->token;
if (!scope->has_symbol(ident)) { if (!scope->has_symbol(ident)) {
throw_runtime_error(ast, "undefined variable '" + ident + "'..."); throw_runtime_error(ast, "undefined variable '" + ident + "'...");
@ -201,8 +226,7 @@ private:
/* /*
* Environment * Environment
*/ */
struct Environment struct Environment {
{
Environment(shared_ptr<SymbolScope> scope, shared_ptr<Environment> outer) Environment(shared_ptr<SymbolScope> scope, shared_ptr<Environment> outer)
: scope(scope), outer(outer) {} : scope(scope), outer(outer) {}
@ -229,10 +253,11 @@ struct Environment
shared_ptr<AstPL0> get_procedure(const string& ident) const { shared_ptr<AstPL0> get_procedure(const string& ident) const {
auto it = scope->procedures.find(ident); auto it = scope->procedures.find(ident);
return it != scope->procedures.end() ? it->second : outer->get_procedure(ident); return it != scope->procedures.end() ? it->second
: outer->get_procedure(ident);
} }
private: private:
shared_ptr<SymbolScope> scope; shared_ptr<SymbolScope> scope;
shared_ptr<Environment> outer; shared_ptr<Environment> outer;
map<string, int> variables; map<string, int> variables;
@ -241,61 +266,89 @@ private:
/* /*
* Interpreter * Interpreter
*/ */
struct Interpreter struct Interpreter {
{ static void exec(const shared_ptr<AstPL0> ast,
static void exec(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env = nullptr) { shared_ptr<Environment> env = nullptr) {
switch (ast->tag) { switch (ast->tag) {
case "block"_: exec_block(ast, env); break; case "block"_:
case "statement"_: exec_statement(ast, env); break; exec_block(ast, env);
case "assignment"_: exec_assignment(ast, env); break; break;
case "call"_: exec_call(ast, env); break; case "statement"_:
case "statements"_: exec_statements(ast, env); break; exec_statement(ast, env);
case "if"_: exec_if(ast, env); break; break;
case "while"_: exec_while(ast, env); break; case "assignment"_:
case "out"_: exec_out(ast, env); break; exec_assignment(ast, env);
case "in"_: exec_in(ast, env); break; break;
default: exec(ast->nodes[0], env); break; case "call"_:
exec_call(ast, env);
break;
case "statements"_:
exec_statements(ast, env);
break;
case "if"_:
exec_if(ast, env);
break;
case "while"_:
exec_while(ast, env);
break;
case "out"_:
exec_out(ast, env);
break;
case "in"_:
exec_in(ast, env);
break;
default:
exec(ast->nodes[0], env);
break;
} }
} }
private: private:
static void exec_block(const shared_ptr<AstPL0> ast, shared_ptr<Environment> outer) { static void exec_block(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> outer) {
// block <- const var procedure statement // block <- const var procedure statement
exec(ast->nodes[3], make_shared<Environment>(ast->scope, outer)); exec(ast->nodes[3], make_shared<Environment>(ast->scope, outer));
} }
static void exec_statement(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static void exec_statement(const shared_ptr<AstPL0> ast,
// statement <- (assignment / call / statements / if / while / out / in)? shared_ptr<Environment> env) {
// statement <- (assignment / call / statements / if / while / out /
// in)?
if (!ast->nodes.empty()) { if (!ast->nodes.empty()) {
exec(ast->nodes[0], env); exec(ast->nodes[0], env);
} }
} }
static void exec_assignment(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static void exec_assignment(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// assignment <- ident ':=' _ expression // assignment <- ident ':=' _ expression
env->set_variable(ast->nodes[0]->token, eval(ast->nodes[1], env)); env->set_variable(ast->nodes[0]->token, eval(ast->nodes[1], env));
} }
static void exec_call(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static void exec_call(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// call <- 'CALL' __ ident // call <- 'CALL' __ ident
exec_block(env->get_procedure(ast->nodes[0]->token), env); exec_block(env->get_procedure(ast->nodes[0]->token), env);
} }
static void exec_statements(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static void exec_statements(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// statements <- 'BEGIN' __ statement (';' _ statement )* 'END' __ // statements <- 'BEGIN' __ statement (';' _ statement )* 'END' __
for (auto stmt: ast->nodes) { for (auto stmt : ast->nodes) {
exec(stmt, env); exec(stmt, env);
} }
} }
static void exec_if(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static void exec_if(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// if <- 'IF' __ condition 'THEN' __ statement // if <- 'IF' __ condition 'THEN' __ statement
if (eval_condition(ast->nodes[0], env)) { if (eval_condition(ast->nodes[0], env)) {
exec(ast->nodes[1], env); exec(ast->nodes[1], env);
} }
} }
static void exec_while(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static void exec_while(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// while <- 'WHILE' __ condition 'DO' __ statement // while <- 'WHILE' __ condition 'DO' __ statement
auto cond = ast->nodes[0]; auto cond = ast->nodes[0];
auto stmt = ast->nodes[1]; auto stmt = ast->nodes[1];
@ -304,61 +357,82 @@ private:
} }
} }
static void exec_out(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static void exec_out(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// out <- ('out' __ / 'write' __ / '!' _) expression // out <- ('out' __ / 'write' __ / '!' _) expression
cout << eval(ast->nodes[0], env) << endl; cout << eval(ast->nodes[0], env) << endl;
} }
static void exec_in(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static void exec_in(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// in <- ('in' __ / 'read' __ / '?' _) ident // in <- ('in' __ / 'read' __ / '?' _) ident
int val; int val;
cin >> val; cin >> val;
env->set_variable(ast->nodes[0]->token, val); env->set_variable(ast->nodes[0]->token, val);
} }
static bool eval_condition(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static bool eval_condition(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// condition <- odd / compare // condition <- odd / compare
const auto& node = ast->nodes[0]; const auto& node = ast->nodes[0];
switch (node->tag) { switch (node->tag) {
case "odd"_: return eval_odd(node, env); case "odd"_:
case "compare"_: return eval_compare(node, env); return eval_odd(node, env);
default: throw logic_error("invalid AstPL0 type"); case "compare"_:
return eval_compare(node, env);
default:
throw logic_error("invalid AstPL0 type");
} }
} }
static bool eval_odd(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static bool eval_odd(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// odd <- 'ODD' __ expression // odd <- 'ODD' __ expression
return eval_expression(ast->nodes[0], env) != 0; return eval_expression(ast->nodes[0], env) != 0;
} }
static bool eval_compare(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static bool eval_compare(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// compare <- expression compare_op expression // compare <- expression compare_op expression
const auto& nodes = ast->nodes; const auto& nodes = ast->nodes;
auto lval = eval_expression(nodes[0], env); auto lval = eval_expression(nodes[0], env);
auto op = peg::str2tag(nodes[1]->token.c_str()); auto op = peg::str2tag(nodes[1]->token.c_str());
auto rval = eval_expression(nodes[2], env); auto rval = eval_expression(nodes[2], env);
switch (op) { switch (op) {
case "="_: return lval == rval; case "="_:
case "#"_: return lval != rval; return lval == rval;
case "<="_: return lval <= rval; case "#"_:
case "<"_: return lval < rval; return lval != rval;
case ">="_: return lval >= rval; case "<="_:
case ">"_: return lval > rval; return lval <= rval;
default: throw logic_error("invalid operator"); case "<"_:
return lval < rval;
case ">="_:
return lval >= rval;
case ">"_:
return lval > rval;
default:
throw logic_error("invalid operator");
} }
} }
static int eval(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static int eval(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
switch (ast->tag) { switch (ast->tag) {
case "expression"_: return eval_expression(ast, env); case "expression"_:
case "term"_: return eval_term(ast, env); return eval_expression(ast, env);
case "ident"_: return eval_ident(ast, env); case "term"_:
case "number"_: return eval_number(ast, env); return eval_term(ast, env);
default: return eval(ast->nodes[0], env); case "ident"_:
return eval_ident(ast, env);
case "number"_:
return eval_number(ast, env);
default:
return eval(ast->nodes[0], env);
} }
} }
static int eval_expression(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static int eval_expression(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// expression <- sign term (term_op term)* // expression <- sign term (term_op term)*
const auto& nodes = ast->nodes; const auto& nodes = ast->nodes;
auto sign = nodes[0]->token; auto sign = nodes[0]->token;
@ -368,14 +442,19 @@ private:
auto ope = nodes[i + 0]->token[0]; auto ope = nodes[i + 0]->token[0];
auto rval = eval(nodes[i + 1], env); auto rval = eval(nodes[i + 1], env);
switch (ope) { switch (ope) {
case '+': val = val + rval; break; case '+':
case '-': val = val - rval; break; val = val + rval;
break;
case '-':
val = val - rval;
break;
} }
} }
return val; return val;
} }
static int eval_term(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static int eval_term(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
// term <- factor (factor_op factor)* // term <- factor (factor_op factor)*
const auto& nodes = ast->nodes; const auto& nodes = ast->nodes;
auto val = eval(nodes[0], env); auto val = eval(nodes[0], env);
@ -397,11 +476,13 @@ private:
return val; return val;
} }
static int eval_ident(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static int eval_ident(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
return env->get_value(ast, ast->token); return env->get_value(ast, ast->token);
} }
static int eval_number(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static int eval_number(const shared_ptr<AstPL0> ast,
shared_ptr<Environment> env) {
return stol(ast->token); return stol(ast->token);
} }
}; };
@ -409,8 +490,7 @@ private:
/* /*
* Main * Main
*/ */
int main(int argc, const char** argv) int main(int argc, const char** argv) {
{
if (argc < 2) { if (argc < 2) {
cout << "usage: pl0 PATH [--ast]" << endl; cout << "usage: pl0 PATH [--ast]" << endl;
return 1; return 1;
@ -427,7 +507,8 @@ int main(int argc, const char** argv)
} }
source.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg())); source.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
if (!source.empty()) { if (!source.empty()) {
ifs.seekg(0, ios::beg).read(&source[0], static_cast<streamsize>(source.size())); ifs.seekg(0, ios::beg)
.read(&source[0], static_cast<streamsize>(source.size()));
} }
// Setup a PEG parser // Setup a PEG parser
@ -455,4 +536,4 @@ int main(int argc, const char** argv)
return -1; return -1;
} }
// vim: et ts=4 sw=4 cin cino={1s ff=unix // vim: et ts=2 sw=2 cin cino={1s ff=unix