Skip to content

Commit

Permalink
Initial commit - cpp_stm_free copy-paste
Browse files Browse the repository at this point in the history
  • Loading branch information
graninas committed Mar 23, 2019
0 parents commit 6682063
Show file tree
Hide file tree
Showing 22 changed files with 1,814 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.user
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2019, Alexander Granin
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
C++ Monadic Parsers library
=========================================


Requirements
------------

- GCC 7.2

14 changes: 14 additions & 0 deletions cpp_parsec.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# parsec targets

win32: CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../lib/cpp_parsec/cpp_parsec/release/ -lcpp_parsec
else:win32: CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../lib/cpp_parsec/cpp_parsec/debug/ -lcpp_parsec
else:unix: LIBS += -L$$OUT_PWD/../../lib/cpp_parsec/cpp_parsec/ -lcpp_parsec

INCLUDEPATH += $$PWD/../../lib/cpp_parsec/cpp_parsec
DEPENDPATH += $$PWD/../../lib/cpp_parsec/cpp_parsec

win32-g++: CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/cpp_parsec/cpp_parsec/release/libcpp_parsec.a
else:win32-g++: CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/cpp_parsec/cpp_parsec/debug/libcpp_parsec.a
else:win32:!win32-g++: CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/cpp_parsec/cpp_parsec/release/cpp_parsec.lib
else:win32:!win32-g++: CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/cpp_parsec/cpp_parsec/debug/cpp_parsec.lib
else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../lib/cpp_parsec/cpp_parsec/libcpp_parsec.a
8 changes: 8 additions & 0 deletions cpp_parsec.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
TEMPLATE = subdirs

SUBDIRS += \
cpp_parsec \
cpp_parsecTest

OTHER_FILES += \
cpp_parsec.pri
30 changes: 30 additions & 0 deletions cpp_parsec/cpp_parsec.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
QT -= core gui

TARGET = cpp_parsec
TEMPLATE = lib
CONFIG += staticlib

CONFIG += c++1z

SOURCES += \
ps/context.cpp

HEADERS += \
ps/tvar.h \
ps/context.h \
ps/psf/functor.h \
ps/psf/psf.h \
ps/impl/runtime.h \
ps/types.h \
ps/church/psl.h \
ps/church/interpreter.h \
ps/church/functor.h \
ps/church/ps.h \
ps/ps.h

unix {
target.path = /usr/lib
INSTALLS += target
}

DISTFILES +=
36 changes: 36 additions & 0 deletions cpp_parsec/ps/church/functor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef PS_CHURCH_FUNCTOR_H
#define PS_CHURCH_FUNCTOR_H

#include "psl.h"

namespace ps
{
namespace church
{

template <typename A, typename B>
PSL<B> fmap(
const std::function<B(A)>& f,
const PSL<A>& church)
{
PSL<B> n;

n.runF = [=](
const std::function<Any(B)>& p,
const std::function<Any(psf::PSF<Any>)>& s)
{
auto composed = [=](const A& a)
{
return p(f(a));
};

return church.runF(composed, s);
};

return n;
}

} // namespace church
} // namespace ps

#endif // PS_CHURCH_FUNCTOR_H
124 changes: 124 additions & 0 deletions cpp_parsec/ps/church/interpreter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#ifndef PS_CHURCH_INTERPRETER_H
#define PS_CHURCH_INTERPRETER_H

#include "psl.h"
#include "../context.h"
#include <cstring>

namespace ps
{
namespace church
{

// Forward declaration
template <typename Ret>
struct StmfVisitor;

template <typename A>
RunResult<A> runPSF(
AtomicRuntime& runtime,
const psf::PSF<A>& psf)
{
StmfVisitor<A> visitor(runtime);
std::visit(visitor, psf.psf);
return visitor.result;
}

template <typename A>
RunResult<A> runPSL(AtomicRuntime& runtime,
const PSL<A>& psl)
{
std::function<Any(A)>
pureAny = [](const A& a) { return a; }; // cast to any

std::function<Any(psf::PSF<Any>)> g
= [&](const psf::PSF<Any>& psf)
{
auto runnerResult = runPSF(runtime, psf);
if (runnerResult.retry)
{
throw std::runtime_error("Retry");
}
if (!runnerResult.result.has_value())
{
throw std::runtime_error("No result.");
}
return runnerResult.result.value();
};

A result;
try
{
Any anyResult = psl.runF(pureAny, g);
result = std::any_cast<A>(anyResult);
}
catch(std::runtime_error err)
{
if (strcmp(err.what(), "Retry") == 0)
{
return RunResult<A> { true, std::nullopt };
}
throw err;
}
return RunResult<A> { false, result };
}

template <typename Ret>
struct StmfVisitor
{
AtomicRuntime& _runtime;

StmfVisitor(AtomicRuntime& runtime)
: _runtime(runtime)
{
}

RunResult<Ret> result;

template <typename A>
void operator()(const psf::NewTVar<A, Ret>& f)
{
auto tvarId = _runtime.newId();

// std::cout << "<" << _runtime.getUStamp() << "> NewTVar. Id: " << tvarId << ", name: " << f.name << std::endl;

TVarHandle tvarHandle { _runtime.getUStamp(), f.val, true };
_runtime.addTVarHandle(tvarId, tvarHandle);
TVarAny tvar { f.name, tvarId };
result.retry = false;
result.result = f.next(tvar);
}

template <typename A>
void operator()(const psf::ReadTVar<A, Ret>& f)
{
// std::cout << "<" << _runtime.getUStamp() << "> ReadTVar. Id: " << f.tvar.id << ", name: " << f.tvar.name << std::endl;

TVarHandle tvarHandle = _runtime.getTVarHandle(f.tvar.id);
result.retry = false;
result.result = f.next(tvarHandle.data);
}

template <typename A>
void operator()(const psf::WriteTVar<A, Ret>& f)
{
// std::cout << "<" << _runtime.getUStamp() << "> WriteTVar. Id: " << f.tvar.id << ", name: " << f.tvar.name << std::endl;

_runtime.setTVarHandleData(f.tvar.id, f.val);
result.retry = false;
result.result = f.next(unit);
}

template <typename A>
void operator()(const psf::Retry<A, Ret>&)
{
// std::cout << "<" << _runtime.getUStamp() << "> Retry." << std::endl;

result = { true, std::nullopt };
}
};

} // namespace church
} // namespace ps

#endif // PS_CHURCH_INTERPRETER_H
Loading

0 comments on commit 6682063

Please sign in to comment.