refactor, encapsulate
parent
b82f1c1f7e
commit
a79eaf8e16
@ -0,0 +1,75 @@
|
||||
// sprit-forth: A portable subroutine-threaded Forth.
|
||||
// Copyright (C) 2024 Clyne Sullivan <clyne@bitgloo.com>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Library General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for
|
||||
// more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Library General Public License
|
||||
// along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#include "executor.hpp"
|
||||
|
||||
Executor Exec;
|
||||
|
||||
Error Executor::executor(FuncList *list) noexcept
|
||||
{
|
||||
auto result = static_cast<Error>(setjmp(jmpbuf));
|
||||
FuncList body;
|
||||
|
||||
if (static_cast<int>(result) == 0) {
|
||||
result = Error::none;
|
||||
|
||||
// We are given the pointer to a list of function pointers.
|
||||
// Dereference once to retrieve the function pointer list.
|
||||
// We do not work with IP initially since it needs to be set to zero if
|
||||
// this is a top-level call/execution.
|
||||
body = *list;
|
||||
|
||||
// Enter the execution loop.
|
||||
goto entry;
|
||||
|
||||
// Execution continues so long as IP is not zero.
|
||||
// If this is a top-level execution of a pre-defined word, then IP will
|
||||
// remain zero'd and the loop will immediately exit.
|
||||
// If this is a defined word's execution, then its "call" will overwrite
|
||||
// IP (and push the initial zero-IP to the return stack); execution will
|
||||
// continue until we return to the zero-IP.
|
||||
while (ip) {
|
||||
// Retrieve next function pointer list.
|
||||
body = (FuncList)*++ip;
|
||||
entry:
|
||||
// Dereference `body` to get the first function in the list.
|
||||
// This is casted to take a FuncList as an argument since defined
|
||||
// words need to know their addresses so that they can perform
|
||||
// their "calls".
|
||||
// If the word is pre-defined then the argument will simply be
|
||||
// ignored.
|
||||
auto func = (void (*)(FuncList))*body;
|
||||
func(body);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Error Executor::execute1(Word *word) noexcept
|
||||
{
|
||||
// IP must initially be zero if executing a word at the top level.
|
||||
ip = nullptr;
|
||||
return executor(&word->list);
|
||||
}
|
||||
|
||||
[[noreturn]]
|
||||
void Executor::terminate(Error error) noexcept
|
||||
{
|
||||
std::longjmp(jmpbuf, static_cast<int>(error));
|
||||
}
|
||||
|
@ -0,0 +1,53 @@
|
||||
// sprit-forth: A portable subroutine-threaded Forth.
|
||||
// Copyright (C) 2024 Clyne Sullivan <clyne@bitgloo.com>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Library General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for
|
||||
// more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Library General Public License
|
||||
// along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#ifndef EXECUTOR_HPP
|
||||
#define EXECUTOR_HPP
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
#include <csetjmp>
|
||||
|
||||
class Executor
|
||||
{
|
||||
std::jmp_buf jmpbuf;
|
||||
|
||||
public:
|
||||
FuncList ip = nullptr;
|
||||
|
||||
/**
|
||||
* Begins execution with the given function pointer list.
|
||||
* @param list Function pointer list to execute
|
||||
*/
|
||||
[[nodiscard]]
|
||||
Error executor(FuncList *list) noexcept;
|
||||
|
||||
/**
|
||||
* Executes the given word by calling executor on its definition.
|
||||
* @param word The word to execute
|
||||
*/
|
||||
[[nodiscard]]
|
||||
Error execute1(Word *word) noexcept;
|
||||
|
||||
[[noreturn]]
|
||||
void terminate(Error error) noexcept;
|
||||
};
|
||||
|
||||
extern Executor Exec;
|
||||
|
||||
#endif // EXECUTOR_HPP
|
||||
|
Loading…
Reference in New Issue