Skip to content

Commit

Permalink
WIP: Switch to different JSON library
Browse files Browse the repository at this point in the history
  • Loading branch information
RauliL committed Nov 29, 2024
1 parent f8629ce commit 78a3ef7
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 66 deletions.
49 changes: 30 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,30 @@ INCLUDE(CMakePackageConfigHelpers)
INCLUDE(FetchContent)

FETCHCONTENT_DECLARE(
PlorthParser
OrderedMap
GIT_REPOSITORY
https://github.com/plorth/parser.git
https://github.com/tessil/ordered-map.git
GIT_TAG
v2.3.0
v1.1.0
)
FETCHCONTENT_DECLARE(
NlohmannJson
PeeloJson
GIT_REPOSITORY
https://github.com/nlohmann/json.git
https://github.com/peelonet/peelo-json.git
GIT_TAG
v3.11.2
v0.1.0
)
# TODO: Remove this. It appears that the JSON library also contains this.
FETCHCONTENT_DECLARE(
OrderedMap
PlorthParser
GIT_REPOSITORY
https://github.com/tessil/ordered-map.git
https://github.com/plorth/parser.git
GIT_TAG
v1.1.0
v2.3.0
)

FETCHCONTENT_MAKEAVAILABLE(PlorthParser)
FETCHCONTENT_MAKEAVAILABLE(NlohmannJson)
FETCHCONTENT_MAKEAVAILABLE(OrderedMap)
FETCHCONTENT_MAKEAVAILABLE(PeeloJson)
FETCHCONTENT_MAKEAVAILABLE(PlorthParser)

ADD_LIBRARY(
plorth
Expand Down Expand Up @@ -70,11 +69,19 @@ ADD_LIBRARY(
src/value/word.cpp
)

TARGET_COMPILE_OPTIONS(
plorth
PRIVATE
-Wall -Werror
)
IF(MSVC)
TARGET_COMPILE_OPTIONS(
plorth
INTERFACE
/W4 /WX
)
ELSE()
TARGET_COMPILE_OPTIONS(
plorth
INTERFACE
-Wall -Werror
)
ENDIF()

TARGET_COMPILE_FEATURES(
plorth
Expand All @@ -87,13 +94,17 @@ TARGET_INCLUDE_DIRECTORIES(
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
# TODO: Figure out what's wrong with this.
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/build/_deps/orderedmap-src/include>
)

TARGET_LINK_LIBRARIES(
plorth
PUBLIC
NlohmannJson
OrderedMap
# TODO: Figure out what's wrong with this too.
# OrderedMap
PeeloJson
PeeloUnicode
PlorthParser
)

Expand Down
4 changes: 2 additions & 2 deletions include/plorth/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
*/
#pragma once

#include <nlohmann/json.hpp>
#include <peelo/json/value.hpp>
#include <plorth/parser/position.hpp>

namespace plorth
{
using json = nlohmann::json;
using json = peelo::json::value::ptr;

/**
* Creates an JSON object from given source code position. The created object
Expand Down
2 changes: 1 addition & 1 deletion include/plorth/value/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
#pragma once

#include <iostream>
#include <memory>

#include <plorth/json.hpp>
Expand Down Expand Up @@ -129,7 +130,6 @@ namespace plorth::value
std::u32string to_string(enum type type);

std::ostream& operator<<(std::ostream&, type);
std::ostream& operator<<(std::ostream&, const base*);
std::ostream& operator<<(std::ostream&, const ref&);

inline bool
Expand Down
12 changes: 7 additions & 5 deletions src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,23 @@ namespace plorth
json
to_json(const parser::position& position)
{
json result;
peelo::json::object::container_type result;

if (!position.file.empty())
{
result["file"] = peelo::unicode::encoding::utf8::encode(position.file);
result[U"file"] = std::make_shared<peelo::json::string>(position.file);
}
if (position.line > 0)
{
result["line"] = position.line;
result[U"line"] = std::make_shared<peelo::json::number>(position.line);
}
if (position.column > 0)
{
result["column"] = position.column;
result[U"column"] = std::make_shared<peelo::json::number>(
position.column
);
}

return result;
return std::make_shared<peelo::json::object>(result);
}
}
8 changes: 4 additions & 4 deletions src/value/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ namespace plorth::value
json
array::to_json() const
{
std::vector<json> result;
peelo::json::array::container_type elements;

result.reserve(m_elements.size());
elements.reserve(m_elements.size());
for (const auto& element : m_elements)
{
result.push_back(element->to_json());
elements.push_back(element->to_json());
}

return result;
return std::make_shared<peelo::json::array>(elements);
}

std::u32string
Expand Down
13 changes: 0 additions & 13 deletions src/value/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,6 @@ namespace plorth::value
return os;
}

std::ostream&
operator<<(std::ostream& os, const base* value)
{
if (value)
{
os << peelo::unicode::encoding::utf8::encode(value->to_string());
} else {
os << "<no value>";
}

return os;
}

std::ostream&
operator<<(std::ostream& os, const ref& value)
{
Expand Down
2 changes: 1 addition & 1 deletion src/value/boolean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace plorth::value
json
boolean::to_json() const
{
return m_value;
return std::make_shared<peelo::json::boolean>(m_value);
}

std::u32string
Expand Down
11 changes: 7 additions & 4 deletions src/value/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,19 @@ namespace plorth::value
json
error::to_json() const
{
peelo::json::object::container_type properties;
json result;

result["code"] = static_cast<int>(m_code);
result["message"] = peelo::unicode::encoding::utf8::encode(m_message);
properties[U"code"] = std::make_shared<peelo::json::number>(
static_cast<int>(m_code)
);
properties[U"message"] = std::make_shared<peelo::json::string>(m_message);
if (m_position)
{
result["position"] = plorth::to_json(*m_position);
properties[U"position"] = plorth::to_json(*m_position);
}

return result;
return std::make_shared<peelo::json::object>(properties);
}

std::u32string
Expand Down
8 changes: 6 additions & 2 deletions src/value/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,13 @@ namespace plorth::value
{
if (is_int())
{
return std::get<int_type>(m_container);
return std::make_shared<peelo::json::number>(
std::get<int_type>(m_container)
);
} else {
return std::get<real_type>(m_container);
return std::make_shared<peelo::json::number>(
std::get<real_type>(m_container)
);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/value/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,14 @@ namespace plorth::value
json
object::to_json() const
{
json result;
peelo::json::object::container_type properties;

for (const auto& property : m_properties)
{
result[peelo::unicode::encoding::utf8::encode(property.first)]
= property.second->to_json();
properties[property.first] = property.second->to_json();
}

return result;
return std::make_shared<peelo::json::object>(properties);
}

std::u32string
Expand Down
6 changes: 3 additions & 3 deletions src/value/quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,18 @@ namespace plorth::value
if (is_compiled())
{
const auto& values = std::get<std::vector<ref>>(m_container);
std::vector<json> result;
peelo::json::array::container_type result;

result.reserve(values.size());
for (const auto& value : values)
{
result.push_back(value->to_json());
}

return result;
return std::make_shared<peelo::json::array>(result);
}

return "native quote";
return std::make_shared<peelo::json::string>(U"native quote");
}

std::u32string
Expand Down
2 changes: 1 addition & 1 deletion src/value/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace plorth::value
json
string::to_json() const
{
return peelo::unicode::encoding::utf8::encode(m_value);
return std::make_shared<peelo::json::string>(m_value);
}

std::u32string
Expand Down
8 changes: 4 additions & 4 deletions src/value/symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ namespace plorth::value
json
symbol::to_json() const
{
json result;
peelo::json::object::container_type result;

result["id"] = peelo::unicode::encoding::utf8::encode(m_id);
result[U"id"] = std::make_shared<peelo::json::string>(m_id);
if (m_position)
{
result["position"] = plorth::to_json(*m_position);
result[U"position"] = plorth::to_json(*m_position);
}

return result;
return std::make_shared<peelo::json::object>(result);
}

std::u32string
Expand Down
6 changes: 3 additions & 3 deletions src/value/word.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ namespace plorth::value
json
word::to_json() const
{
std::vector<json> result;
peelo::json::array::container_type result;

result.reserve(2);
result.push_back("->");
result.push_back(std::make_shared<peelo::json::string>(U"->"));
result.push_back(m_symbol->to_json());

return result;
return std::make_shared<peelo::json::array>(result);
}

std::u32string
Expand Down

0 comments on commit 78a3ef7

Please sign in to comment.