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

Implement Fabulist Runtime #7

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
21fed35
refactor(runtime): Begin rewriting compiler
FiniteReality Feb 25, 2022
98835d8
feat(runtime): Add Story type
FiniteReality Feb 25, 2022
1ec3af5
feat(runtime): Add sections to story
FiniteReality Feb 25, 2022
c705398
feat(runtime): Wire up base API types
FiniteReality Feb 25, 2022
cf584dd
feat(runtime): Integrate gitversion into runtime
FiniteReality Feb 25, 2022
c43460e
feat(runtime): Start work on actions
FiniteReality Feb 28, 2022
e74f18b
feat(samples/cli): Rewrite CLI sample
FiniteReality Feb 28, 2022
d854f8c
refactor(compiler/cli): Fix include guard name
FiniteReality Feb 28, 2022
1cc01ec
feat: The runtime may actually work now
FiniteReality Mar 18, 2022
c7a61b1
fix(runtime): GCC errors, wheeee
FiniteReality Mar 18, 2022
6f4fa55
fix(compiler): Add lua_cpcall back to lua 5.2-5.4
FiniteReality Mar 18, 2022
eceadc0
fix(runtime): use path::string() as I hate UTF16
FiniteReality Mar 18, 2022
4fdcef6
feat(cli): Add win32 console implementation
FiniteReality Mar 18, 2022
020e5d6
fix(infra): Use CMAKE_CURRENT_SOURCE_DIR when adding module search paths
RubyNova May 11, 2022
804dd54
fix(CI): Fallback version if unable to get tags
FiniteReality May 14, 2022
3da9a27
feat: Run CI on MacOS, use struct for callbacks
FiniteReality Jun 3, 2022
bd80b7d
fix(runtime): Include <unordered_map> to fix MacOS
FiniteReality Jun 3, 2022
4e97124
fix(CI): Hide thirdparty warnings on mac
FiniteReality Jun 3, 2022
99a2d8b
Update save.cpp
Pheubel Sep 10, 2022
a885cae
Removed experimental flags and swapped BUILD_INTERFACE
capnkenny Nov 4, 2022
973aee5
fix(build): Added CMP0135 policy for CMake versions 3.24+
capnkenny Nov 4, 2022
28724b3
Add EOL
capnkenny Nov 4, 2022
798858f
fix(build): Removed experimental flags for MSVC
capnkenny Nov 4, 2022
39fc122
Merge branch 'novelrt:feature/runtime' into feature/runtime
Pheubel Nov 19, 2022
01c5804
Merge pull request #16 from Pheubel/feature/runtime
RubyNova Nov 20, 2022
7fe6ba5
feat(runtime): Allow cend() returns from queries
FiniteReality Jan 29, 2023
86b8ca8
feat(runtime): makeshift support for method calls
FiniteReality Jan 29, 2023
2df3c17
feat(compiler): Support primitive method calls
FiniteReality Jan 29, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
continue-on-error: ${{ matrix.lua == '5.1' }}
strategy:
matrix:
os: [ windows-latest, ubuntu-latest ]
os: [ windows-latest, ubuntu-latest, macos-latest ]
configuration: [ RelWithDebInfo, MinSizeRel ]
lua: [ "5.1", "5.2", "5.3", "5.4" ]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ windows-latest, ubuntu-latest ]
os: [ windows-latest, ubuntu-latest, macos-latest ]
lua: [ "5.1", "5.2", "5.3", "5.4" ]

steps:
Expand Down
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")

project(Fabulist
VERSION 0.1.0
Expand Down Expand Up @@ -40,8 +44,8 @@ if(FABULIST_COMPILER)
endif()

if(FABULIST_RUNTIME)
message(WARNING "Fabulist runtime is currently broken - refusing to build runtime")
# add_subdirectory(runtime)
message(STATUS "Building runtime")
add_subdirectory(runtime)
endif()

if(FABULIST_SAMPLES)
Expand Down
10 changes: 10 additions & 0 deletions cmake/modules/GitVersion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ function(extract_git_version prefix)
execute_process(
COMMAND "${GIT_PROGRAM}" describe --tags "${_git_tag_sha}"
OUTPUT_VARIABLE _git_tag
RESULT_VARIABLE _git_tag_success
)

if(NOT _git_tag_success EQUAL 0)
set(${prefix}_VERSION_STRING "0.0.0-unknown" PARENT_SCOPE)
set(${prefix}_VERSION_MAJOR 0 PARENT_SCOPE)
set(${prefix}_VERSION_MINOR 0 PARENT_SCOPE)
set(${prefix}_VERSION_PATCH 0 PARENT_SCOPE)
return()
endif()

execute_process(
COMMAND "${GIT_PROGRAM}" rev-parse --short HEAD
OUTPUT_VARIABLE _git_sha
Expand Down
6 changes: 3 additions & 3 deletions compiler/cli/console.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef ARGS_HPP
#define ARGS_HPP
#ifndef CONSOLE_HPP
#define CONSOLE_HPP

#include <optional>
#include <ostream>
Expand Down Expand Up @@ -36,4 +36,4 @@ std::optional<parsed_arguments> parse_arguments(int argc, char const** argv);

}

#endif /* ARGS_HPP */
#endif /* CONSOLE_HPP */
2 changes: 1 addition & 1 deletion compiler/cli/console/parse_getopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std::ostream& cli::detail::usage(std::ostream& stream,
std::optional<cli::parsed_arguments> cli::parse_arguments(
int argc, char const** argv)
{
cli::parsed_arguments result;
cli::parsed_arguments result{};

struct option options[] = {
{ "output", optional_argument, nullptr, 'o' },
Expand Down
3 changes: 2 additions & 1 deletion compiler/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ int main(int argc, char const** argv)
}
catch (std::exception& e)
{
std::cerr << e.what() << "\n";
std::cerr << "error parsing " << path << ":\n";
std::cerr << e.what();
std::cerr << cli::error << "compilation halted.\n";
return 1;
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ include(GitVersion)

add_library(compiler
$<$<AND:$<VERSION_GREATER_EQUAL:${FABULIST_LUA_VERSION},5.1>,$<VERSION_LESS:${FABULIST_LUA_VERSION},5.2>>:compatibility/lua5.1.cpp>
$<$<AND:$<VERSION_GREATER_EQUAL:${FABULIST_LUA_VERSION},5.2>,$<VERSION_LESS:${FABULIST_LUA_VERSION},5.3>>:compatibility/lua5.2.cpp>
$<$<AND:$<VERSION_GREATER_EQUAL:${FABULIST_LUA_VERSION},5.3>,$<VERSION_LESS:${FABULIST_LUA_VERSION},5.4>>:compatibility/lua5.3.cpp>
$<$<AND:$<VERSION_GREATER_EQUAL:${FABULIST_LUA_VERSION},5.4>,$<VERSION_LESS:${FABULIST_LUA_VERSION},5.5>>:compatibility/lua5.4.cpp>

compiler/convenience.cpp

Expand All @@ -16,10 +19,11 @@ add_library(compiler
compiler/setup.cpp

compiler/setup/actions.cpp
compiler/setup/actions/call.cpp
compiler/setup/actions/jump.cpp
compiler/setup/actions/options.cpp

compiler/setup/base.cpp
compiler/setup/options.cpp
compiler/setup/section.cpp
compiler/setup/speaker.cpp

Expand Down
12 changes: 12 additions & 0 deletions compiler/lib/compatibility/lua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ int lua_load(lua_State* L, lua_Reader reader, void* data, char const* source, ch
void luaL_setfuncs(lua_State* L, luaL_Reg const* reg, int nup);
void lua_resume(lua_State* L, lua_State* from, int nargs);

#elif LUA_VERSION_NUM == 502

int lua_cpcall(lua_State* L, lua_CFunction func, void* ud);

#elif LUA_VERSION_NUM == 503

int lua_cpcall(lua_State* L, lua_CFunction func, void* ud);

#elif LUA_VERSION_NUM == 504

int lua_cpcall(lua_State* L, lua_CFunction func, void* ud);

#endif

#endif /* LUA_HPP */
15 changes: 15 additions & 0 deletions compiler/lib/compatibility/lua5.2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// DO NOT INCLUDE "lua.hpp" HERE! THIS WILL BREAK EVERYTHING!

#include <lua.h>
#include <lauxlib.h>

static_assert(LUA_VERSION_NUM == 502, "Do not compile this outside of Lua 5.1");

int lua_cpcall(lua_State* L, lua_CFunction func, void* ud)
{
lua_pushcfunction(L, func);

lua_pushlightuserdata(L, ud);

return lua_pcall(L, 1, 0, 0);
}
15 changes: 15 additions & 0 deletions compiler/lib/compatibility/lua5.3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// DO NOT INCLUDE "lua.hpp" HERE! THIS WILL BREAK EVERYTHING!

#include <lua.h>
#include <lauxlib.h>

static_assert(LUA_VERSION_NUM == 503, "Do not compile this outside of Lua 5.1");

int lua_cpcall(lua_State* L, lua_CFunction func, void* ud)
{
lua_pushcfunction(L, func);

lua_pushlightuserdata(L, ud);

return lua_pcall(L, 1, 0, 0);
}
15 changes: 15 additions & 0 deletions compiler/lib/compatibility/lua5.4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// DO NOT INCLUDE "lua.hpp" HERE! THIS WILL BREAK EVERYTHING!

#include <lua.h>
#include <lauxlib.h>

static_assert(LUA_VERSION_NUM == 504, "Do not compile this outside of Lua 5.1");

int lua_cpcall(lua_State* L, lua_CFunction func, void* ud)
{
lua_pushcfunction(L, func);

lua_pushlightuserdata(L, ud);

return lua_pcall(L, 1, 0, 0);
}
40 changes: 34 additions & 6 deletions compiler/lib/compiler/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using namespace fabulist::compiler;

struct reader {
std::istream& stream;
std::string name;
std::array<char, 1024> buffer;
};

Expand Down Expand Up @@ -68,15 +69,42 @@ int error_handler(lua_State* L)
return 1;
}

void compiler::parse(std::istream& stream, std::string name)
int parse_main(lua_State* L)
{
reader reader{stream, {}};
void* data = lua_touserdata(L, 1);
auto* reader = static_cast<struct reader*>(data);

lua_pushcclosure(L, error_handler, 0);

if (lua_load(L, read_func, reader, reader->name.c_str(), "t"))
{
error_handler(L);
lua_error(L);
}
else if (lua_pcall(L, 0, 0, -2))
{
lua_error(L);
}

lua_pushcclosure(_pimpl->state, error_handler, 0);
lua_pushvalue(L, LUA_REGISTRYINDEX);
lua_pushliteral(L, "return");
lua_rawget(L, -2);

if (!lua_isnil(L, -1))
{
lua_pushliteral(L, "detected that an action may be missed from a section");
error_handler(L);
lua_error(L);
}

return 0;
}

void compiler::parse(std::istream& stream, std::string name)
{
reader reader{stream, name, {}};

int status = lua_load(_pimpl->state, read_func, &reader, name.c_str(), "t");
if (status) error_handler(_pimpl->state);
else status = lua_pcall(_pimpl->state, 0, 0, -2);
int status = lua_cpcall(_pimpl->state, parse_main, &reader);

if (status)
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib/compiler/save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ nlohmann::json table_to_json(lua_State* L)
nlohmann::json result = std::accumulate(
values.begin(), values.end(),
nlohmann::json{},
[is_array](nlohmann::json& result,
[is_array](nlohmann::json result,
decltype(values)::const_reference value)
{
if (is_array)
Expand Down
3 changes: 0 additions & 3 deletions compiler/lib/compiler/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ static lua_CFunction state_setup_actions[] = {

&setup_state,

&setup_speaker,
&setup_section,
&setup_options,

&setup_actions,

nullptr
Expand Down
4 changes: 3 additions & 1 deletion compiler/lib/compiler/setup/actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "common.hpp"

#define ACTIONS \
ACTION(jump)
ACTION(call) \
ACTION(jump) \
ACTION(options)

enum class action
{
Expand Down
37 changes: 37 additions & 0 deletions compiler/lib/compiler/setup/actions/call.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "../actions.hpp"

int setup_parameters(lua_State* L);

template <>
int call_action<action::call>(lua_State* L)
{
luaL_checkstring(L, 1);

lua_createtable(L, 0, 2);
lua_pushliteral(L, "method");
lua_pushvalue(L, 1);
lua_settable(L, -3);

lua_pushliteral(L, "type");
lua_pushliteral(L, "call");
lua_settable(L, -3);

lua_pushliteral(L, "parameters");
lua_newtable(L);
lua_settable(L, -3);

lua_pushcclosure(L, &setup_parameters, 1);
return 1;
}

int setup_parameters(lua_State* L)
{
luaL_checktype(L, 1, LUA_TTABLE);

lua_pushvalue(L, lua_upvalueindex(1));
lua_pushliteral(L, "parameters");
lua_pushvalue(L, 1);
lua_settable(L, -3);

return 1;
}
18 changes: 4 additions & 14 deletions compiler/lib/compiler/setup/actions/jump.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
#include <string_view>

#include "../actions.hpp"

constexpr std::pair<std::string_view, std::string_view> values[] = {
{"type", "action"},
{"action", "jump"}
};

template <>
int call_action<action::jump>(lua_State* L)
{
luaL_checkstring(L, 1);

lua_createtable(L, 0, 1 + (int)std::size(values));
lua_createtable(L, 0, 2);
lua_pushliteral(L, "section");
lua_pushvalue(L, 1);
lua_settable(L, -3);

for (auto& pair : values)
{
lua_pushlstring(L, pair.first.data(), pair.first.size());
lua_pushlstring(L, pair.second.data(), pair.second.size());
lua_settable(L, -3);
}
lua_pushliteral(L, "type");
lua_pushliteral(L, "jump");
lua_settable(L, -3);

return 1;
}
Loading