Added PL/0 sample.

This commit is contained in:
yhirose 2015-07-30 17:26:06 -04:00
parent db5128a9a5
commit 38ac34d1ad
5 changed files with 459 additions and 2 deletions

View File

@ -20,5 +20,8 @@ target_link_libraries(calc2 pthread)
add_executable(calc3 example/calc3.cc)
target_link_libraries(calc3 pthread)
add_executable(pl0 example/pl0.cc)
target_link_libraries(pl0 pthread)
add_executable(culebra language/main.cc)
target_link_libraries(culebra pthread)

View File

@ -19,3 +19,6 @@ calc2 : calc2.cc ../peglib.h
calc3 : calc3.cc ../peglib.h
$(CC) -o calc3 $(CFLAGS) -I.. calc3.cc
pl0 : pl0.cc ../peglib.h
$(CC) -o pl0 $(CFLAGS) -I.. pl0.cc

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "calc", "calc.vcxproj", "{F85B641A-7538-4809-8175-C528FF632CF6}"
EndProject
@ -9,6 +9,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "calc2", "calc2.vcxproj", "{
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "calc3", "calc3.vcxproj", "{E6146F73-3B4C-4D4C-BC55-148930954434}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pl0", "pl0.vcxproj", "{6C5633BD-3CAE-498E-B0C6-ED90A1A99C47}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
@ -27,6 +29,10 @@ Global
{E6146F73-3B4C-4D4C-BC55-148930954434}.Debug|Win32.Build.0 = Debug|Win32
{E6146F73-3B4C-4D4C-BC55-148930954434}.Release|Win32.ActiveCfg = Release|Win32
{E6146F73-3B4C-4D4C-BC55-148930954434}.Release|Win32.Build.0 = Release|Win32
{6C5633BD-3CAE-498E-B0C6-ED90A1A99C47}.Debug|Win32.ActiveCfg = Debug|Win32
{6C5633BD-3CAE-498E-B0C6-ED90A1A99C47}.Debug|Win32.Build.0 = Debug|Win32
{6C5633BD-3CAE-498E-B0C6-ED90A1A99C47}.Release|Win32.ActiveCfg = Release|Win32
{6C5633BD-3CAE-498E-B0C6-ED90A1A99C47}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

353
example/pl0.cc Normal file
View File

@ -0,0 +1,353 @@
//
// pl0.cc - PL/0 interpreter (https://en.wikipedia.org/wiki/PL/0)
//
// Copyright (c) 2015 Yuji Hirose. All rights reserved.
// MIT License
//
#include <peglib.h>
#include <fstream>
#include <iostream>
using namespace peglib;
using namespace std;
auto grammar = R"(
program <- _ block '.' _
block <- const var procedure statement
const <- ('CONST' _ ident '=' _ number (',' _ ident '=' _ number)* ';' _)?
var <- ('VAR' _ ident (',' _ ident)* ';' _)?
procedure <- ('PROCEDURE' _ ident ';' _ block ';' _)*
statement <- (assignment / call / statements / if / while / out / in)?
assignment <- ident ':=' _ expression
call <- 'CALL' _ ident
statements <- 'BEGIN' _ statement (';' _ statement )* 'END' _
if <- 'IF' _ condition 'THEN' _ statement
while <- 'WHILE' _ condition 'DO' _ statement
out <- ('out' / 'write' / '!') _ expression
in <- ('in' / 'read' / '?') _ ident
condition <- odd / compare
odd <- 'ODD' _ expression
compare <- expression compare_op expression
compare_op <- < '=' / '#' / '<=' / '<' / '>=' / '>' > _
expression <- sign term (term_op term)*
sign <- < [-+]? > _
term_op <- < [-+] > _
term <- factor (factor_op factor)*
factor_op <- < [*/] > _
factor <- ident / number / '(' _ expression ')' _
ident <- < [a-z] ([a-z] / [0-9])* > _
number <- < [0-9]+ > _
~_ <- [ \t\r\n]*
)";
struct Environment
{
Environment(shared_ptr<Environment> outer = nullptr) : outer(outer) {}
int get_value(const string& ident) const {
try {
return get_constant(ident);
} catch (...) {
return get_variable(ident);
}
}
void set_variable(const string& ident, int val) {
if (variables.find(ident) != variables.end()) {
variables[ident] = val;
} else if (outer) {
return outer->set_variable(ident, val);
} else {
throw runtime_error("undefined variable");
}
}
int get_constant(const string& ident) const {
if (constants.find(ident) != constants.end()) {
return constants.at(ident);
} else if (outer) {
return outer->get_constant(ident);
} else {
throw runtime_error("undefined constants");
}
}
int get_variable(const string& ident) const {
if (variables.find(ident) != variables.end()) {
return variables.at(ident);
} else if (outer) {
return outer->get_variable(ident);
} else {
throw runtime_error("undefined variable");
}
}
map<string, int> constants;
map<string, int> variables;
map<string, shared_ptr<Ast>> procedures;
shared_ptr<Environment> outer;
};
struct Interpreter
{
void exec(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
switch (ast->tag) {
case "block"_: exec_block(ast, env); break;
case "statement"_: exec_statement(ast, env); break;
case "assignment"_: exec_assignment(ast, 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: throw logic_error("invalid Ast type");
}
}
private:
void exec_block(const shared_ptr<Ast> ast, shared_ptr<Environment> outer) {
// block <- const var procedure statement
auto env = make_shared<Environment>(outer);
const auto& nodes = ast->nodes;
exec_constants(nodes[0], env);
exec_variables(nodes[1], env);
exec_procedures(nodes[2], env);
exec(nodes[3], env);
}
void exec_constants(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// const <- ('CONST' _ ident '=' _ number(',' _ ident '=' _ number)* ';' _) ?
const auto& nodes = ast->nodes;
for (auto i = 0u; i < nodes.size(); i += 2) {
const auto& ident = nodes[i + 0]->token;
auto number = stoi(nodes[i + 1]->token);
env->constants[ident] = number;
}
}
void exec_variables(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// var <- ('VAR' _ ident(',' _ ident)* ';' _) ?
const auto& nodes = ast->nodes;
for (auto i = 0u; i < nodes.size(); i += 1) {
const auto& ident = nodes[i]->token;
env->variables[ident] = 0;
}
}
void exec_procedures(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// procedure <- ('PROCEDURE' _ ident ';' _ block ';' _)*
const auto& nodes = ast->nodes;
for (auto i = 0u; i < nodes.size(); i += 2) {
const auto& ident = nodes[i + 0]->token;
auto block = nodes[i + 1];
env->procedures[ident] = block;
}
}
void exec_statement(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// statement <-(assignment / call / statements / if / while / out / in) ?
if (!ast->nodes.empty()) {
exec(ast->nodes[0], env);
}
}
void exec_assignment(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// assignment <- ident ':=' _ expression
const auto& ident = ast->nodes[0]->token;
auto val = eval(ast->nodes[1], env);
env->set_variable(ident, val);
}
void exec_call(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// call <- 'CALL' _ ident
const auto& ident = ast->nodes[0]->token;
auto proc = env->procedures[ident];
exec_block(proc, env);
}
void exec_statements(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// statements <- 'BEGIN' _ statement (';' _ statement )* 'END' _
for (auto stmt: ast->nodes) {
exec(stmt, env);
}
}
void exec_if(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// if <- 'IF' _ condition 'THEN' _ statement
auto cond = eval_condition(ast->nodes[0], env);
auto stmt = ast->nodes[1];
if (cond) {
exec(stmt, env);
}
}
void exec_while(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// while <- 'WHILE' _ condition 'DO' _ statement
auto cond = ast->nodes[0];
auto stmt = ast->nodes[1];
auto ret = eval_condition(cond, env);
while (ret) {
exec(stmt, env);
ret = eval_condition(cond, env);
}
}
void exec_out(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// out <- '!' _ expression
auto val = eval(ast->nodes[0], env);
cout << val << endl;
}
void exec_in(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// in <- '?' _ ident
int val;
cin >> val;
const auto& ident = ast->nodes[0]->token;
env->variables[ident] = val;
}
bool eval_condition(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// condition <- odd / compare
const auto& node = ast->nodes[0];
switch (node->tag) {
case "odd"_: return eval_odd(node, env);
case "compare"_: return eval_compare(node, env);
default: throw logic_error("invalid Ast type");
}
}
bool eval_odd(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// odd <- 'ODD' _ expression
auto val = eval_expression(ast->nodes[0], env);
return val != 0;
}
bool eval_compare(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// compare <- expression compare_op expression
auto lval = eval_expression(ast->nodes[0], env);
auto op = peglib::str2tag(ast->nodes[1]->token.c_str());
auto rval = eval_expression(ast->nodes[2], env);
switch (op) {
case "="_: return lval == rval;
case "#"_: return lval != rval;
case "<="_: return lval <= rval;
case "<"_: return lval < rval;
case ">="_: return lval >= rval;
case ">"_: return lval > rval;
}
}
int eval(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
switch (ast->tag) {
case "expression"_: return eval_expression(ast, env);
case "term"_: return eval_term(ast, env);
case "ident"_: return eval_ident(ast, env);
case "number"_: return eval_number(ast, env);
default: throw logic_error("invalid Ast type");
}
}
int eval_expression(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// expression <- sign term (term_op term)*
auto sign = ast->nodes[0]->token;
auto sign_val = (sign.empty() || sign == "+") ? 1 : -1;
auto val = eval(ast->nodes[1], env) * sign_val;
const auto& nodes = ast->nodes;
for (auto i = 2u; i < nodes.size(); i += 2) {
auto ope = nodes[i + 0]->token[0];
auto rval = eval(nodes[i + 1], env);
switch (ope) {
case '+': val = val + rval; break;
case '-': val = val - rval; break;
}
}
return val;
}
int eval_term(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
// term <- factor (factor_op factor)*
auto val = eval(ast->nodes[0], env);
const auto& nodes = ast->nodes;
for (auto i = 1u; i < nodes.size(); i += 2) {
auto ope = nodes[i + 0]->token[0];
auto rval = eval(nodes[i + 1], env);
switch (ope) {
case '*':
val = val * rval;
break;
case '/':
if (rval == 0) {
throw runtime_error("divide by 0 error");
}
val = val / rval;
break;
}
}
return val;
}
int eval_ident(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
return env->get_value(ast->token);
}
int eval_number(const shared_ptr<Ast> ast, shared_ptr<Environment> env) {
return stol(ast->token);
}
};
bool read_file(const char* path, vector<char>& buff)
{
ifstream ifs(path, ios::in | ios::binary);
if (ifs.fail()) {
return false;
}
buff.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
if (!buff.empty()) {
ifs.seekg(0, ios::beg).read(&buff[0], static_cast<streamsize>(buff.size()));
}
return true;
}
int main(int argc, const char** argv)
{
if (argc < 2) {
cout << "usage: pl0 PATH [--ast]" << endl;
return 1;
}
auto path = argv[1];
vector<char> source;
if (!read_file(path, source)) {
cerr << "can't open the source file." << endl;
return -1;
}
peg parser(grammar);
parser.enable_ast(false, { "program", "statement", "statements", "term", "factor" });
shared_ptr<Ast> ast;
if (parser.parse_n(source.data(), source.size(), ast, path)) {
if (argc > 2 && string("--ast") == argv[2]) {
ast->print();
}
Interpreter interp;
auto env = make_shared<Environment>();
interp.exec(ast, env);
return 0;
}
cout << "syntax error..." << endl;
return -1;
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix

92
example/pl0.vcxproj Normal file
View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\peglib.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="pl0.cc" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{6C5633BD-3CAE-498E-B0C6-ED90A1A99C47}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>sample</RootNamespace>
<ProjectName>pl0</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>Ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>