Initial draft of stack-based machine for JS.
The idea I have is that I'll translate/compile JavaScript into a rudimentary Forth like language. This sketch forms the basis for that language's engine.
This commit is contained in:
84
js4g/StackMachine.h
Normal file
84
js4g/StackMachine.h
Normal file
@ -0,0 +1,84 @@
|
||||
static_assert( __cplusplus >= 2023'02 );
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Dillo/Dillo.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <deque>
|
||||
|
||||
namespace Dillo::Hydrogen::JavaScriptForge ::detail:: StackMachine_m
|
||||
{
|
||||
inline namespace exports
|
||||
{
|
||||
class StackMachine;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Machine state for JS4G (Java Script Forth Grinder, AKA: JavaScriptForge JS4G).
|
||||
*
|
||||
* This class represents the state of a JavaScript execution, post translation to
|
||||
* the forth grinder format.
|
||||
*/
|
||||
class exports::StackMachine
|
||||
{
|
||||
private:
|
||||
// The main runtime stack of the forth engine.
|
||||
// It grows on the "back".
|
||||
// A `std::deque` is used instead of `std::vector`, because
|
||||
// it helps cut down on large allocations, by subdividing things.
|
||||
std::deque< std::string > stack;
|
||||
|
||||
// TODO: Should these tokens be binary?
|
||||
// Upside: compressed.
|
||||
// Downside: more memory management in the machine end?
|
||||
std::deque< std::string > tokens;
|
||||
|
||||
using Integer= std::int64_t;
|
||||
using Float= long double;
|
||||
|
||||
|
||||
std::map< std::string, std::string > words;
|
||||
|
||||
// Which side of the current conditional to take.
|
||||
enum ConditionalState { If, Else };
|
||||
std::vector< ConditionalState > conditionals;
|
||||
|
||||
ConditionalState currentState;
|
||||
|
||||
void runWord( std::string_view word );
|
||||
|
||||
std::string pop();
|
||||
const std::string &peek();
|
||||
|
||||
void push( std::string s );
|
||||
|
||||
template< typename T >
|
||||
T pop();
|
||||
|
||||
template< typename T >
|
||||
void push( const T &t );
|
||||
|
||||
std::ostream &output;
|
||||
|
||||
public:
|
||||
StackMachine( std::ostream &output= std::cout );
|
||||
|
||||
void
|
||||
loadProgram( std::deque< std::string > tokens )
|
||||
{
|
||||
this->tokens= std::move( tokens );
|
||||
}
|
||||
|
||||
void run();
|
||||
};
|
||||
}
|
||||
|
||||
namespace Dillo::Hydrogen::JavaScriptForge::inline exports::inline StackMachine_m
|
||||
{
|
||||
using namespace detail::StackMachine_m::exports;
|
||||
}
|
||||
Reference in New Issue
Block a user