|
|
@ -1,222 +1,138 @@
|
|
|
|
//
|
|
|
|
/**
|
|
|
|
/// @file dictionary.hpp
|
|
|
|
* Alee Forth: A portable and concise Forth implementation in modern C++.
|
|
|
|
/// @brief Defines the dictionary interface and common functionality.
|
|
|
|
* Copyright (C) 2023 Clyne Sullivan <clyne@bitgloo.com>
|
|
|
|
//
|
|
|
|
*
|
|
|
|
// Alee Forth: A portable and concise Forth implementation in modern C++.
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
// Copyright (C) 2023 Clyne Sullivan <clyne@bitgloo.com>
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
//
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
* (at your option) any later version.
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
*
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
// (at your option) any later version.
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
//
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
*
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
*/
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ALEEFORTH_DICTIONARY_HPP
|
|
|
|
#ifndef ALEEFORTH_DICTIONARY_HPP
|
|
|
|
#define ALEEFORTH_DICTIONARY_HPP
|
|
|
|
#define ALEEFORTH_DICTIONARY_HPP
|
|
|
|
|
|
|
|
|
|
|
|
#include "config.hpp"
|
|
|
|
|
|
|
|
#include "types.hpp"
|
|
|
|
|
|
|
|
#include "ctype.hpp"
|
|
|
|
#include "ctype.hpp"
|
|
|
|
|
|
|
|
#include "types.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @class Dictionary
|
|
|
|
* Dictionary entry format:
|
|
|
|
* @brief Provides an interface and essential funcitonality for a dictionary.
|
|
|
|
* - 1 information byte
|
|
|
|
* @details The core read and write functionality is left virtual so that
|
|
|
|
* bits 0..4: Length of name (L)
|
|
|
|
* dictionaries can be stored in any medium. So, this class cannot be used
|
|
|
|
* bit 5: Immediate?
|
|
|
|
* directly; the programmer must define a dictionary class that inherits this
|
|
|
|
* bits 6..15: Distance to next entry (negative)
|
|
|
|
* one.
|
|
|
|
* - L bytes of name
|
|
|
|
*
|
|
|
|
* - 0+ bytes for address alignment
|
|
|
|
* Dictionary entry format (for a 16-bit implementation):
|
|
|
|
* - 0+ bytes of entry's data...
|
|
|
|
* - One information cell:
|
|
|
|
|
|
|
|
* - bits 0..4: Length of name
|
|
|
|
|
|
|
|
* - bit 5: Set if word is immediate
|
|
|
|
|
|
|
|
* - bits 6..15: Distance (backwards) to the next entry
|
|
|
|
|
|
|
|
* - If bits 6..15 are all one-bits then "long" distance in following cell.
|
|
|
|
|
|
|
|
* - "Length" bytes of name
|
|
|
|
|
|
|
|
* - Zero or more bytes for address alignment
|
|
|
|
|
|
|
|
* - Zero or more bytes of the definition's contents
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class Dictionary
|
|
|
|
class Dictionary
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
/** Stores Numerical base to use for input/output. */
|
|
|
|
/**
|
|
|
|
|
|
|
|
* The beginning of the dictionary is used for "internal" variables.
|
|
|
|
|
|
|
|
*/
|
|
|
|
constexpr static Addr Base = 0;
|
|
|
|
constexpr static Addr Base = 0;
|
|
|
|
/** Stores the current `here` address. */
|
|
|
|
|
|
|
|
constexpr static Addr Here = sizeof(Cell);
|
|
|
|
constexpr static Addr Here = sizeof(Cell);
|
|
|
|
/** Stores the address of the most recently defined word. */
|
|
|
|
|
|
|
|
constexpr static Addr Latest = sizeof(Cell) * 2;
|
|
|
|
constexpr static Addr Latest = sizeof(Cell) * 2;
|
|
|
|
/** Stores a boolean indication of compiling state. */
|
|
|
|
|
|
|
|
constexpr static Addr Compiling = sizeof(Cell) * 3;
|
|
|
|
constexpr static Addr Compiling = sizeof(Cell) * 3;
|
|
|
|
/** Stores the address of the last execution token determined by colon. */
|
|
|
|
|
|
|
|
constexpr static Addr CompToken = sizeof(Cell) * 4;
|
|
|
|
constexpr static Addr CompToken = sizeof(Cell) * 4;
|
|
|
|
/** Stores the address of the current interpreter input source. */
|
|
|
|
|
|
|
|
constexpr static Addr Source = sizeof(Cell) * 5;
|
|
|
|
constexpr static Addr Source = sizeof(Cell) * 5;
|
|
|
|
/** Stores the size in bytes of the interpreter input source. */
|
|
|
|
|
|
|
|
constexpr static Addr SourceLen = sizeof(Cell) * 6;
|
|
|
|
constexpr static Addr SourceLen = sizeof(Cell) * 6;
|
|
|
|
/** Stores the dictionary's input buffer (a counted string). */
|
|
|
|
constexpr static Addr Input = sizeof(Cell) * 7; // len data...
|
|
|
|
constexpr static Addr Input = sizeof(Cell) * 7;
|
|
|
|
constexpr static Addr InputCells = 80; // bytes!
|
|
|
|
/** Stores the size of the dictionary's input buffer in bytes. */
|
|
|
|
|
|
|
|
constexpr static Addr InputCells = 80;
|
|
|
|
|
|
|
|
/** Stores the dictionary's "beginning" i.e. where new definitions begin. */
|
|
|
|
|
|
|
|
constexpr static Addr Begin = sizeof(Cell) * 8 + InputCells;
|
|
|
|
constexpr static Addr Begin = sizeof(Cell) * 8 + InputCells;
|
|
|
|
|
|
|
|
|
|
|
|
/** "Immediate" marker bit for a word's definition. */
|
|
|
|
|
|
|
|
constexpr static Cell Immediate = (1 << 5);
|
|
|
|
constexpr static Cell Immediate = (1 << 5);
|
|
|
|
/** Maximum "short" distance between two definitions. */
|
|
|
|
|
|
|
|
constexpr static Cell MaxDistance = (1 << (sizeof(Cell) * 8 - 6)) - 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns the value of the cell at the given address. */
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Dictionary data can be stored on any read-write interface.
|
|
|
|
|
|
|
|
* You must create a dictionary class that inherits Dictionary and
|
|
|
|
|
|
|
|
* implement these functions. See `memdict.hpp` for a simple block-of-
|
|
|
|
|
|
|
|
* memory implementation.
|
|
|
|
|
|
|
|
*/
|
|
|
|
virtual Cell read(Addr) const noexcept = 0;
|
|
|
|
virtual Cell read(Addr) const noexcept = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/** Writes the given value to the cell at the given address. */
|
|
|
|
|
|
|
|
virtual void write(Addr, Cell) noexcept = 0;
|
|
|
|
virtual void write(Addr, Cell) noexcept = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns the byte stored at the given address. */
|
|
|
|
|
|
|
|
virtual uint8_t readbyte(Addr) const noexcept = 0;
|
|
|
|
virtual uint8_t readbyte(Addr) const noexcept = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/** Writes the given byte to the given address. */
|
|
|
|
|
|
|
|
virtual void writebyte(Addr, uint8_t) noexcept = 0;
|
|
|
|
virtual void writebyte(Addr, uint8_t) noexcept = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns the total capacity of the dictionary in bytes. */
|
|
|
|
|
|
|
|
virtual unsigned long int capacity() const noexcept = 0;
|
|
|
|
virtual unsigned long int capacity() const noexcept = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Initializes essential dictionary values.
|
|
|
|
* Does initial dictionary setup, required before use for execution.
|
|
|
|
* Must be called before dictionary use.
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void initialize();
|
|
|
|
void initialize();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Gets the address stored in `here`.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
Addr here() const noexcept { return read(Here); }
|
|
|
|
Addr here() const noexcept { return read(Here); }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Sets the address stored in `here`.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
void here(Addr l) noexcept { write(Here, l); }
|
|
|
|
void here(Addr l) noexcept { write(Here, l); }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Gets the value of `latest`.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
Addr latest() const noexcept { return read(Latest); }
|
|
|
|
Addr latest() const noexcept { return read(Latest); }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Sets the value of `latest`.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
void latest(Addr l) noexcept { write(Latest, l); }
|
|
|
|
void latest(Addr l) noexcept { write(Latest, l); }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
// Aligns the given address.
|
|
|
|
* Aligns the given address to the next Cell boundary if necessary.
|
|
|
|
static Addr aligned(Addr);
|
|
|
|
* @param addr The address to align.
|
|
|
|
// Aligns `here`.
|
|
|
|
* @return The resulting aligned address.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static Addr aligned(Addr addr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Aligns `here` to the next Cell boundary if necessary.
|
|
|
|
|
|
|
|
* @return The new aligned address stored in `here`.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
Addr alignhere() noexcept;
|
|
|
|
Addr alignhere() noexcept;
|
|
|
|
|
|
|
|
// Advances `here` by the given number of bytes.
|
|
|
|
|
|
|
|
Addr allot(Cell) noexcept;
|
|
|
|
|
|
|
|
// Stores value to `here`, then adds sizeof(Cell) to `here`.
|
|
|
|
|
|
|
|
void add(Cell) noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Allocates memory by returning and then increasing the current `here`.
|
|
|
|
* Uses add() to store a new definition entry starting at `here`.
|
|
|
|
* @param count The number of bytes to increase `here` by.
|
|
|
|
* The entry does not become active until a semicolon is executed.
|
|
|
|
* @return The address stored in `here` before the increase.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
Addr allot(Cell count) noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Stores the given value to `here` then calls allot to "save" that cell.
|
|
|
|
|
|
|
|
* @param value The value to store.
|
|
|
|
|
|
|
|
* @see allot(Cell)
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void add(Cell value) noexcept;
|
|
|
|
void addDefinition(Word) noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Stores the beginning of a new word definition in the dictionary.
|
|
|
|
|
|
|
|
* The word must eventually have its definition concluded via semicolon.
|
|
|
|
|
|
|
|
* @param word The dictionary-stored name of the new word.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void addDefinition(Word word) noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Searches the dictionary for an entry for the given word.
|
|
|
|
* Searches the dictionary for an entry for the given word.
|
|
|
|
* @param word The dictionary-stored word to search for.
|
|
|
|
* Returns zero if not found.
|
|
|
|
* @return The beginning address of the word or zero if not found.
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
Addr find(Word word) noexcept;
|
|
|
|
Addr find(Word) noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Produces the execution token for the given dictionary entry.
|
|
|
|
* Given the address of a dictionary entry, produces the execution token
|
|
|
|
* @param addr The beginning address of a defined word.
|
|
|
|
* for that entry.
|
|
|
|
* @return The execution token for the given word.
|
|
|
|
|
|
|
|
* @see find(Word)
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
Addr getexec(Addr addr) noexcept;
|
|
|
|
Addr getexec(Addr) noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Reads the next word from the input buffer.
|
|
|
|
* Reads the next word from the input buffer.
|
|
|
|
* @return The next word or an empty word if one is not available.
|
|
|
|
* Returns an empty word if the buffer is empty or entirely read.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
Word input() noexcept;
|
|
|
|
Word input() noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Returns true if the dictionary's input buffer has immediately available
|
|
|
|
|
|
|
|
* data.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool hasInput() const noexcept;
|
|
|
|
bool hasInput() const noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Checks if the dictionary-stored word is equivalent to the given string.
|
|
|
|
* Checks if this dictionary's word is equivalent to the given string/size.
|
|
|
|
* @param word Dictionary-stored word to compare against.
|
|
|
|
|
|
|
|
* @param str String to compare to.
|
|
|
|
|
|
|
|
* @param size Size of the string to compare to.
|
|
|
|
|
|
|
|
* @return True if the two words are equivalent.
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bool equal(Word word, const char *str, unsigned size) const noexcept;
|
|
|
|
bool equal(Word, const char *, unsigned) const noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Checks if two words stored in this dictionary are equivalent.
|
|
|
|
* Checks if two words in this dictionary's word are equivalent.
|
|
|
|
* @param word1 First word to compare
|
|
|
|
|
|
|
|
* @param word2 Second word to compare
|
|
|
|
|
|
|
|
* @return True if the words are equivalent.
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
bool equal(Word word1, Word word2) const noexcept;
|
|
|
|
bool equal(Word, Word) const noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
// Used for case-insensitive comparison between two iterators.
|
|
|
|
* Generic equality comparison using our own case-insensitive comparator.
|
|
|
|
|
|
|
|
* Arguments and return value identical to std::equal.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<typename Iter1, typename Iter2>
|
|
|
|
template<typename Iter1, typename Iter2>
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
constexpr static bool equal(Iter1 b1, Iter1 e1, Iter2 b2) {
|
|
|
|
constexpr static bool equal(Iter1 b1, Iter1 e1, Iter2 b2) {
|
|
|
|
return std::equal(b1, e1, b2, eqchars);
|
|
|
|
return std::equal(b1, e1, b2, eqchars);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual ~Dictionary() {};
|
|
|
|
virtual ~Dictionary() = default;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
// Case-insensitive comparison.
|
|
|
|
* Case-insensitive character comparison used for dictionary lookup.
|
|
|
|
|
|
|
|
* @return True if the characters are equivalent.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
constexpr static bool eqchars(char c1, char c2) {
|
|
|
|
constexpr static bool eqchars(char c1, char c2) {
|
|
|
|
if (isalpha(static_cast<uint8_t>(c1)))
|
|
|
|
if (isalpha(static_cast<uint8_t>(c1)))
|
|
|
|
c1 |= 32;
|
|
|
|
c1 |= 32;
|
|
|
@ -225,6 +141,7 @@ private:
|
|
|
|
|
|
|
|
|
|
|
|
return c1 == c2;
|
|
|
|
return c1 == c2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ALEEFORTH_DICTIONARY_HPP
|
|
|
|
#endif // ALEEFORTH_DICTIONARY_HPP
|
|
|
|