Applied clang-format

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

View File

@ -18,50 +18,51 @@ using namespace std;
* PEG Grammar * PEG Grammar
*/ */
auto grammar = R"( auto grammar = R"(
program <- _ block '.' _ program <- _ block '.' _
block <- const var procedure statement block <- const var procedure statement
const <- ('CONST' __ ident '=' _ number (',' _ ident '=' _ number)* ';' _)? const <- ('CONST' __ ident '=' _ number (',' _ ident '=' _ number)* ';' _)?
var <- ('VAR' __ ident (',' _ ident)* ';' _)? var <- ('VAR' __ ident (',' _ ident)* ';' _)?
procedure <- ('PROCEDURE' __ ident ';' _ block ';' _)* procedure <- ('PROCEDURE' __ ident ';' _ block ';' _)*
statement <- (assignment / call / statements / if / while / out / in)? statement <- (assignment / call / statements / if / while / out / in)?
assignment <- ident ':=' _ expression assignment <- ident ':=' _ expression
call <- 'CALL' __ ident call <- 'CALL' __ ident
statements <- 'BEGIN' __ statement (';' _ statement )* 'END' __ statements <- 'BEGIN' __ statement (';' _ statement )* 'END' __
if <- 'IF' __ condition 'THEN' __ statement if <- 'IF' __ condition 'THEN' __ statement
while <- 'WHILE' __ condition 'DO' __ statement while <- 'WHILE' __ condition 'DO' __ statement
out <- ('out' __ / 'write' __ / '!' _) expression out <- ('out' __ / 'write' __ / '!' _) expression
in <- ('in' __ / 'read' __ / '?' _) ident in <- ('in' __ / 'read' __ / '?' _) ident
condition <- odd / compare condition <- odd / compare
odd <- 'ODD' __ expression odd <- 'ODD' __ expression
compare <- expression compare_op expression compare <- expression compare_op expression
compare_op <- < '=' / '#' / '<=' / '<' / '>=' / '>' > _ compare_op <- < '=' / '#' / '<=' / '<' / '>=' / '>' > _
expression <- sign term (term_op term)* expression <- sign term (term_op term)*
sign <- < [-+]? > _ sign <- < [-+]? > _
term_op <- < [-+] > _ term_op <- < [-+] > _
term <- factor (factor_op factor)* term <- factor (factor_op factor)*
factor_op <- < [*/] > _ factor_op <- < [*/] > _
factor <- ident / number / '(' _ expression ')' _ factor <- ident / number / '(' _ expression ')' _
ident <- < [a-z] [a-z0-9]* > _ ident <- < [a-z] [a-z0-9]* > _
number <- < [0-9]+ > _ number <- < [0-9]+ > _
~_ <- [ \t\r\n]* ~_ <- [ \t\r\n]*
~__ <- ![a-z0-9_] _ ~__ <- ![a-z0-9_] _
)"; )";
/* /*
* 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,
stringstream ss; const string& msg) {
ss << path << ":" << ln << ":" << col << ": " << msg << endl; stringstream ss;
return ss.str(); ss << path << ":" << ln << ":" << col << ": " << msg << endl;
return ss.str();
} }
/* /*
@ -69,9 +70,8 @@ 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;
}; };
typedef AstBase<Annotation> AstPL0; typedef AstBase<Annotation> AstPL0;
@ -79,380 +79,461 @@ 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 {
auto ret = constants.count(ident) || variables.count(ident); auto ret = constants.count(ident) || variables.count(ident);
return ret ? true : (outer ? outer->has_symbol(ident) : false); return ret ? true : (outer ? outer->has_symbol(ident) : false);
} }
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,
// block <- const var procedure statement shared_ptr<SymbolScope> outer) {
auto scope = make_shared<SymbolScope>(outer); // block <- const var procedure statement
const auto& nodes = ast->nodes; auto scope = make_shared<SymbolScope>(outer);
constants(nodes[0], scope); const auto& nodes = ast->nodes;
variables(nodes[1], scope); constants(nodes[0], scope);
procedures(nodes[2], scope); variables(nodes[1], scope);
build_on_ast(nodes[3], scope); procedures(nodes[2], scope);
ast->scope = scope; build_on_ast(nodes[3], scope);
} ast->scope = scope;
}
static void constants(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void constants(const shared_ptr<AstPL0> ast,
// const <- ('CONST' __ ident '=' _ number(',' _ ident '=' _ number)* ';' _) ? shared_ptr<SymbolScope> scope) {
const auto& nodes = ast->nodes; // const <- ('CONST' __ ident '=' _ number(',' _ ident '=' _ number)* ';' _) ?
for (auto i = 0u; i < nodes.size(); i += 2) { const auto& nodes = ast->nodes;
const auto& ident = nodes[i + 0]->token; for (auto i = 0u; i < nodes.size(); i += 2) {
if (scope->has_symbol(ident)) { const auto& ident = nodes[i + 0]->token;
throw_runtime_error(nodes[i], "'" + ident + "' is already defined..."); if (scope->has_symbol(ident)) {
} throw_runtime_error(nodes[i], "'" + ident + "' is already defined...");
auto number = stoi(nodes[i + 1]->token); }
scope->constants.emplace(ident, number); auto number = stoi(nodes[i + 1]->token);
} scope->constants.emplace(ident, number);
} }
}
static void variables(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void variables(const shared_ptr<AstPL0> ast,
// var <- ('VAR' __ ident(',' _ ident)* ';' _) ? shared_ptr<SymbolScope> scope) {
const auto& nodes = ast->nodes; // var <- ('VAR' __ ident(',' _ ident)* ';' _) ?
for (auto i = 0u; i < nodes.size(); i += 1) { const auto& nodes = ast->nodes;
const auto& ident = nodes[i]->token; for (auto i = 0u; i < nodes.size(); i += 1) {
if (scope->has_symbol(ident)) { const auto& ident = nodes[i]->token;
throw_runtime_error(nodes[i], "'" + ident + "' is already defined..."); if (scope->has_symbol(ident)) {
} throw_runtime_error(nodes[i], "'" + ident + "' is already defined...");
scope->variables.emplace(ident); }
} scope->variables.emplace(ident);
} }
}
static void procedures(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void procedures(const shared_ptr<AstPL0> ast,
// procedure <- ('PROCEDURE' __ ident ';' _ block ';' _)* shared_ptr<SymbolScope> scope) {
const auto& nodes = ast->nodes; // procedure <- ('PROCEDURE' __ ident ';' _ block ';' _)*
for (auto i = 0u; i < nodes.size(); i += 2) { const auto& nodes = ast->nodes;
const auto& ident = nodes[i + 0]->token; for (auto i = 0u; i < nodes.size(); i += 2) {
auto block = nodes[i + 1]; const auto& ident = nodes[i + 0]->token;
scope->procedures[ident] = block; auto block = nodes[i + 1];
build_on_ast(block, scope); scope->procedures[ident] = block;
} build_on_ast(block, scope);
} }
}
static void assignment(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) { static void assignment(const shared_ptr<AstPL0> ast,
// assignment <- ident ':=' _ expression shared_ptr<SymbolScope> scope) {
const auto& ident = ast->nodes[0]->token; // assignment <- ident ':=' _ expression
if (scope->has_constant(ident)) { const auto& ident = ast->nodes[0]->token;
throw_runtime_error(ast->nodes[0], "cannot modify constant value '" + ident + "'..."); if (scope->has_constant(ident)) {
} else if (!scope->has_variable(ident)) { throw_runtime_error(ast->nodes[0],
throw_runtime_error(ast->nodes[0], "undefined variable '" + ident + "'..."); "cannot modify constant value '" + ident + "'...");
} } else if (!scope->has_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,
// call <- 'CALL' __ ident shared_ptr<SymbolScope> scope) {
const auto& ident = ast->nodes[0]->token; // call <- 'CALL' __ ident
if (!scope->has_procedure(ident)) { const auto& ident = ast->nodes[0]->token;
throw_runtime_error(ast->nodes[0], "undefined procedure '" + ident + "'..."); if (!scope->has_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,
const auto& ident = ast->token; shared_ptr<SymbolScope> scope) {
if (!scope->has_symbol(ident)) { const auto& ident = ast->token;
throw_runtime_error(ast, "undefined variable '" + ident + "'..."); if (!scope->has_symbol(ident)) {
} throw_runtime_error(ast, "undefined variable '" + ident + "'...");
} }
}
}; };
/* /*
* 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) {}
int get_value(const shared_ptr<AstPL0> ast, const string& ident) const { int get_value(const shared_ptr<AstPL0> ast, const string& ident) const {
auto it = scope->constants.find(ident); auto it = scope->constants.find(ident);
if (it != scope->constants.end()) { if (it != scope->constants.end()) {
return it->second; return it->second;
} else if (scope->variables.count(ident)) { } else if (scope->variables.count(ident)) {
if (variables.find(ident) == variables.end()) { if (variables.find(ident) == variables.end()) {
throw_runtime_error(ast, "uninitialized variable '" + ident + "'..."); throw_runtime_error(ast, "uninitialized variable '" + ident + "'...");
} }
return variables.at(ident); return variables.at(ident);
}
return outer->get_value(ast, ident);
} }
return outer->get_value(ast, ident);
}
void set_variable(const string& ident, int val) { void set_variable(const string& ident, int val) {
if (scope->variables.count(ident)) { if (scope->variables.count(ident)) {
variables[ident] = val; variables[ident] = val;
} else { } else {
outer->set_variable(ident, val); outer->set_variable(ident, val);
}
} }
}
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;
}; };
/* /*
* 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,
// block <- const var procedure statement shared_ptr<Environment> outer) {
exec(ast->nodes[3], make_shared<Environment>(ast->scope, outer)); // block <- const var procedure statement
} 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) {
if (!ast->nodes.empty()) { // statement <- (assignment / call / statements / if / while / out /
exec(ast->nodes[0], env); // in)?
} if (!ast->nodes.empty()) {
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,
// assignment <- ident ':=' _ expression shared_ptr<Environment> env) {
env->set_variable(ast->nodes[0]->token, eval(ast->nodes[1], env)); // assignment <- ident ':=' _ expression
} 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,
// call <- 'CALL' __ ident shared_ptr<Environment> env) {
exec_block(env->get_procedure(ast->nodes[0]->token), env); // call <- 'CALL' __ ident
} 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,
// statements <- 'BEGIN' __ statement (';' _ statement )* 'END' __ shared_ptr<Environment> env) {
for (auto stmt: ast->nodes) { // statements <- 'BEGIN' __ statement (';' _ statement )* 'END' __
exec(stmt, env); for (auto stmt : ast->nodes) {
} 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,
// if <- 'IF' __ condition 'THEN' __ statement shared_ptr<Environment> env) {
if (eval_condition(ast->nodes[0], env)) { // if <- 'IF' __ condition 'THEN' __ statement
exec(ast->nodes[1], env); if (eval_condition(ast->nodes[0], 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,
// while <- 'WHILE' __ condition 'DO' __ statement shared_ptr<Environment> env) {
auto cond = ast->nodes[0]; // while <- 'WHILE' __ condition 'DO' __ statement
auto stmt = ast->nodes[1]; auto cond = ast->nodes[0];
while (eval_condition(cond, env)) { auto stmt = ast->nodes[1];
exec(stmt, env); while (eval_condition(cond, env)) {
} exec(stmt, env);
} }
}
static void exec_out(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static void exec_out(const shared_ptr<AstPL0> ast,
// out <- ('out' __ / 'write' __ / '!' _) expression shared_ptr<Environment> env) {
cout << eval(ast->nodes[0], env) << endl; // out <- ('out' __ / 'write' __ / '!' _) expression
} 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,
// in <- ('in' __ / 'read' __ / '?' _) ident shared_ptr<Environment> env) {
int val; // in <- ('in' __ / 'read' __ / '?' _) ident
cin >> val; int val;
env->set_variable(ast->nodes[0]->token, val); cin >> 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,
// condition <- odd / compare shared_ptr<Environment> env) {
const auto& node = ast->nodes[0]; // condition <- odd / compare
switch (node->tag) { const auto& node = ast->nodes[0];
case "odd"_: return eval_odd(node, env); switch (node->tag) {
case "compare"_: return eval_compare(node, env); case "odd"_:
default: throw logic_error("invalid AstPL0 type"); return eval_odd(node, env);
} 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,
// odd <- 'ODD' __ expression shared_ptr<Environment> env) {
return eval_expression(ast->nodes[0], env) != 0; // odd <- 'ODD' __ expression
} 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,
// compare <- expression compare_op expression shared_ptr<Environment> env) {
const auto& nodes = ast->nodes; // compare <- expression compare_op expression
auto lval = eval_expression(nodes[0], env); const auto& nodes = ast->nodes;
auto op = peg::str2tag(nodes[1]->token.c_str()); auto lval = eval_expression(nodes[0], env);
auto rval = eval_expression(nodes[2], env); auto op = peg::str2tag(nodes[1]->token.c_str());
switch (op) { auto rval = eval_expression(nodes[2], env);
case "="_: return lval == rval; 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 "<="_:
default: throw logic_error("invalid operator"); return lval <= rval;
} 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,
// expression <- sign term (term_op term)* shared_ptr<Environment> env) {
const auto& nodes = ast->nodes; // expression <- sign term (term_op term)*
auto sign = nodes[0]->token; const auto& nodes = ast->nodes;
auto sign_val = (sign.empty() || sign == "+") ? 1 : -1; auto sign = nodes[0]->token;
auto val = eval(nodes[1], env) * sign_val; auto sign_val = (sign.empty() || sign == "+") ? 1 : -1;
for (auto i = 2u; i < nodes.size(); i += 2) { auto val = eval(nodes[1], env) * sign_val;
auto ope = nodes[i + 0]->token[0]; for (auto i = 2u; i < nodes.size(); i += 2) {
auto rval = eval(nodes[i + 1], env); auto ope = nodes[i + 0]->token[0];
switch (ope) { auto rval = eval(nodes[i + 1], env);
case '+': val = val + rval; break; switch (ope) {
case '-': val = val - rval; break; case '+':
} val = val + rval;
} break;
return val; case '-':
val = val - rval;
break;
}
} }
return val;
}
static int eval_term(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) { static int eval_term(const shared_ptr<AstPL0> ast,
// term <- factor (factor_op factor)* shared_ptr<Environment> env) {
const auto& nodes = ast->nodes; // term <- factor (factor_op factor)*
auto val = eval(nodes[0], env); const auto& nodes = ast->nodes;
for (auto i = 1u; i < nodes.size(); i += 2) { auto val = eval(nodes[0], env);
auto ope = nodes[i + 0]->token[0]; for (auto i = 1u; i < nodes.size(); i += 2) {
auto rval = eval(nodes[i + 1], env); auto ope = nodes[i + 0]->token[0];
switch (ope) { auto rval = eval(nodes[i + 1], env);
case '*': switch (ope) {
val = val * rval; case '*':
break; val = val * rval;
case '/': break;
if (rval == 0) { case '/':
throw_runtime_error(ast, "divide by 0 error"); if (rval == 0) {
} throw_runtime_error(ast, "divide by 0 error");
val = val / rval; }
break; val = val / rval;
} break;
} }
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,
return env->get_value(ast, ast->token); shared_ptr<Environment> env) {
} 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,
return stol(ast->token); shared_ptr<Environment> env) {
} return stol(ast->token);
}
}; };
/* /*
* 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; }
}
// Read a source file into memory // Read a source file into memory
auto path = argv[1]; auto path = argv[1];
vector<char> source; vector<char> source;
ifstream ifs(path, ios::in | ios::binary);
if (ifs.fail()) {
cerr << "can't open the source file." << endl;
return -1;
}
source.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
if (!source.empty()) {
ifs.seekg(0, ios::beg).read(&source[0], static_cast<streamsize>(source.size()));
}
// Setup a PEG parser
parser parser(grammar);
parser.enable_ast<AstPL0>();
parser.log = [&](size_t ln, size_t col, const string& msg) {
cerr << format_error_message(path, ln, col, msg) << endl;
};
// Parse the source and make an AST
shared_ptr<AstPL0> ast;
if (parser.parse_n(source.data(), source.size(), ast, path)) {
if (argc > 2 && string("--ast") == argv[2]) {
cout << ast_to_s(ast);
}
try {
SymbolTable::build_on_ast(ast);
Interpreter::exec(ast);
} catch (const runtime_error& e) {
cerr << e.what() << endl;
}
return 0;
}
ifstream ifs(path, ios::in | ios::binary);
if (ifs.fail()) {
cerr << "can't open the source file." << endl;
return -1; return -1;
}
source.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
if (!source.empty()) {
ifs.seekg(0, ios::beg)
.read(&source[0], static_cast<streamsize>(source.size()));
}
// Setup a PEG parser
parser parser(grammar);
parser.enable_ast<AstPL0>();
parser.log = [&](size_t ln, size_t col, const string& msg) {
cerr << format_error_message(path, ln, col, msg) << endl;
};
// Parse the source and make an AST
shared_ptr<AstPL0> ast;
if (parser.parse_n(source.data(), source.size(), ast, path)) {
if (argc > 2 && string("--ast") == argv[2]) {
cout << ast_to_s(ast);
}
try {
SymbolTable::build_on_ast(ast);
Interpreter::exec(ast);
} catch (const runtime_error& e) {
cerr << e.what() << endl;
}
return 0;
}
return -1;
} }
// vim: et ts=4 sw=4 cin cino={1s ff=unix // vim: et ts=2 sw=2 cin cino={1s ff=unix