Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json library. #219

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed num
Empty file.
9 changes: 8 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SUBDIRS = verifier vm loader element ffi

bin_PROGRAMS = slim
bin_PROGRAMS = slim sscheck

slim_LDADD = -llmn_loader \
-llmn_verifier \
Expand Down Expand Up @@ -87,3 +87,10 @@ slim_OBJS =

arch.h ../lib/config.lmn: genconfig
$(SHELL) ./genconfig

sscheck_SOURCES = ssc/main.cpp env.cpp

sscheck_LDADD = -llmn_elm
sscheck_LDFLAGS = -L./element
sscheck_CXXFLAGS = -std=c++11 -DCOMMIT_ID=\"`git show -s --format=%h`\"
sscheck_DEPENDENCIES = ./element/liblmn_elm.a
3 changes: 2 additions & 1 deletion src/element/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ liblmn_elm_a_SOURCES = \
st.cpp st.h \
util.cpp util.h \
vector.cpp vector.h \
life_time.cpp
life_time.cpp \
json/value.cpp

68 changes: 6 additions & 62 deletions src/element/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,71 +36,13 @@
* $Id: alloc.c,v 1.3 2008/09/19 05:18:17 taisuke Exp $
*/

#include "error.h"
#include "lmntal.h"
#include "lmntal_thread.h"
#include "memory_pool.h"
#include "util.h"
#include "vector.h"
#include "vm/vm.h"

/*----------------------------------------------------------------------
* memory allocation for atom
*/

static memory_pool **atom_memory_pools[128];

void mpool_init() {
int i, core_num, arity_num;
arity_num = ARY_SIZEOF(atom_memory_pools);
core_num = lmn_env.core_num;
for (i = 0; i < arity_num; i++) {
atom_memory_pools[i] =
(memory_pool **)malloc(sizeof(memory_pool *) * core_num);
memset(atom_memory_pools[i], 0, sizeof(memory_pool *) * core_num);
}
}

LmnSymbolAtomRef lmn_new_atom(LmnFunctor f) {
LmnSymbolAtomRef ap;
int arity, cid;
arity = LMN_FUNCTOR_ARITY(lmn_functor_table, f);
cid = env_my_thread_id();

if (atom_memory_pools[arity][cid] == 0) {
atom_memory_pools[arity][cid] = memory_pool_new(LMN_SATOM_SIZE(arity));
}
ap = (LmnSymbolAtomRef)memory_pool_malloc(atom_memory_pools[arity][cid]);
ap->set_functor(f);
ap->set_id(0);

return ap;
}

void lmn_delete_atom(LmnSymbolAtomRef ap) {
int arity, cid;

env_return_id(ap->get_id());
#include "error.h"

arity = LMN_FUNCTOR_ARITY(lmn_functor_table, ap->get_functor());
cid = env_my_thread_id();
memory_pool_free(atom_memory_pools[arity][cid], ap);
}
#include "lmntal.h"

void free_atom_memory_pools(void) {
unsigned int i, j, arity_num, core_num;

arity_num = ARY_SIZEOF(atom_memory_pools);
core_num = lmn_env.core_num;
for (i = 0; i < arity_num; i++) {
for (j = 0; j < core_num; j++) {
if (atom_memory_pools[i][j]) {
memory_pool_delete(atom_memory_pools[i][j]);
}
}
free(atom_memory_pools[i]);
}
}
#include <cstring>
#include <cstdlib>

/*----------------------------------------------------------------------
* memory allocation for membrane
Expand Down Expand Up @@ -156,6 +98,8 @@ void *lmn_realloc(void *p, size_t num) {

void lmn_free(void *p) { free(p); }

// new, delete演算子オーバーロード

void* operator new(std::size_t num) {
return lmn_malloc(num);
}
Expand Down
26 changes: 15 additions & 11 deletions src/element/element.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,33 @@
*/

#include "clock.h"
#include "conditional_ostream.hpp"
#include "error.h"
#include "exception.hpp"
#include "file_util.h"
#include "internal_hash.h"
#include "instruction.hpp"
#include "internal_hash.h"
#include "life_time.hpp"
#include "lmnstring.h"
#include "lmntal_thread.h"
#include "memory_pool.h"
#include "optional.hpp"
#include "port.h"
#include "process_util.h"
#include "queue.h"
#include "st.h"
#include "util.h"
#include "vector.h"
#include "scope.hpp"
#include "range_remove_if.hpp"
#include "re2c/buffer.hpp"
#include "re2c/cfstream_buffer.hpp"
#include "re2c/file_buffer.hpp"
#include "exception.hpp"
#include "variant.hpp"
#include "conditional_ostream.hpp"
#include "optional.hpp"
#include "range_remove_if.hpp"
#include "scope.hpp"
#include "st.h"
#include "stack_trace.hpp"
#include "life_time.hpp"
#include "util.h"
#include "variant.hpp"
#include "vector.h"

#include "json/conv.hpp"
#include "json/exception.hpp"
#include "json/value.hpp"

#endif /* LMN_ELEMENT_H */
112 changes: 112 additions & 0 deletions src/element/json/conv.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* conv.hpp
*
* Copyright (c) 2019, Ueda Laboratory LMNtal Group
* <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the Ueda Laboratory LMNtal Group 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
* OWNER 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.
*/

#ifndef SLIM_ELEMENT_JSON_CONV_HPP
#define SLIM_ELEMENT_JSON_CONV_HPP

#include "value.hpp"

namespace slim {
namespace element {

/**
* Convert to JSON value.
*
* Allowed inputs are integers, floating points, booleans, std::string,
* std::vector, std::map, and std::unordered_map.
*
* Usage:
* json::value(42) --> 42
* std::vector<int> v = {1, 2, 3};
* json::value(v) --> [1, 2, 3]
*
* If you want to convert user-defined types, all you have to do is to define
* 'to_json' function.
*
* Example:
* using namespace slim::element;
* struct Point { int x, y; };
* json_t to_json(const Point &p) {
* return json::to_json(std::vector<int>({p.x, p.y}));
* }
*/
template <class T, typename std::enable_if<std::is_integral<T>::value &&
!std::is_same<T, bool>::value,
std::nullptr_t>::type = nullptr>
inline json_t to_json(const T &v) {
return json::integer(v);
}
template <class T, typename std::enable_if<std::is_floating_point<T>::value,
std::nullptr_t>::type = nullptr>
inline json_t to_json(const T &v) {
return json::real(v);
}
template <class T, typename std::enable_if<std::is_same<T, bool>::value,
std::nullptr_t>::type = nullptr>
inline json_t to_json(T v) {
return json::boolean(v);
}
inline json_t to_json(const std::string &v) { return json::string(v); }
template <class T> inline json_t to_json(const std::vector<T> &v) {
json::value_ptr<json::array> ary(new json::array);
ary->value.reserve(v.size());
for (auto &u : v)
ary->value.push_back(to_json(u));
return ary;
}
template <class T>
inline json_t to_json(const std::unordered_map<std::string, T> &v) {
json::value_ptr<json::object> res(new json::object);
for (auto &p : v) {
res->value[p.first] = to_json(p.second);
}
return res;
}
template <class T> inline json_t to_json(const std::map<std::string, T> &v) {
json::value_ptr<json::object> res(new json::object);
for (auto &p : v) {
res->value[p.first] = to_json(p.second);
}
return res;
}
inline json_t to_json(const json_t &v) { return v; }
inline json_t to_json(json_t &&v) { return std::move(v); }

} // namespace element
} // namespace slim

#endif /* SLIM_ELEMENT_JSON_CONV_HPP */
67 changes: 67 additions & 0 deletions src/element/json/exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* exception.hpp
*
* Copyright (c) 2019, Ueda Laboratory LMNtal Group
* <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the Ueda Laboratory LMNtal Group 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
* OWNER 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.
*/

#ifndef SLIM_ELEMENT_JSON_EXCEPTION_HPP
#define SLIM_ELEMENT_JSON_EXCEPTION_HPP

#include <istream>
#include <stdexcept>
#include <string>

namespace slim {
namespace element {
namespace json {
struct parse_error {
virtual const char *what() const noexcept = 0;
};

struct overflow_error : parse_error, std::overflow_error {
overflow_error(std::istream::pos_type pos)
: std::overflow_error("occurred at " + std::to_string(pos)) {}
const char *what() const noexcept { return std::overflow_error::what(); }
};
struct syntax_error : parse_error, std::runtime_error {
syntax_error(const std::string &what_arg, std::istream::pos_type pos)
: std::runtime_error(what_arg + "(at " + std::to_string(pos) + ")") {}
const char *what() const noexcept { return std::runtime_error::what(); }
};

} // namespace json
} // namespace element
} // namespace slim

#endif /* SLIM_ELEMENT_JSON_EXCEPTION_HPP */
Loading