concise wordwrap

main
Clyne 1 year ago
parent 1c097a3f53
commit 1eefd5f51c
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -9,9 +9,10 @@ all: sprit
msp430: CXX := msp430-elf-g++
msp430: AR := msp430-elf-ar
msp430: CXXFLAGS += -mmcu=msp430fr2476 -Os #-flto
msp430: LDFLAGS += -L/usr/msp430-elf/usr/include
msp430: sprit
msp430: CXXFLAGS += -mmcu=msp430fr2476 -Os -flto -ffunction-sections -fdata-sections
msp430: CXXFLAGS += -L/usr/msp430-elf/usr/include
msp430: LDFLAGS += -Tmsp430fr2476.ld -Wl,-gc-sections
msp430: source/libsprit.a
x86: CXXFLAGS += -m32
x86: sprit

@ -30,7 +30,7 @@ void jump(FuncList ip)
// LITERAL's run-time semantics: push the given value onto the stack.
static auto literall = WordWrap<[] {
push((Cell)*++IP);
}>();
}>;
void compileliteral()
{

@ -33,7 +33,7 @@ constexpr auto fexit = WordWrap<[] {
extern FuncList IP;
extern Cell rpop();
IP = reinterpret_cast<FuncList>(rpop());
}>();
}>;
void jump(FuncList ip); /** Jumps to the given instruction pointer. */
void compileliteral(); /** Compiles LITERAL into the current definition. */

@ -24,6 +24,7 @@
FuncList IP = nullptr;
std::array<Cell, DictSize> DICT;
Cell& BASE = DICT[DIdxBase];
Cell& HERE = DICT[DIdxHere];
Cell& LATEST = DICT[DIdxLatest];
Cell& STATE = DICT[DIdxState];

@ -22,7 +22,7 @@
constexpr Addr DS = 16; /** Data stack size */
constexpr Addr RS = 16; /** Return stack size */
constexpr auto DictSize = 4096u; /** Dictionary size */
constexpr auto DictSize = 2048u; /** Dictionary size */
constexpr Addr DIdxBase = 0;
constexpr Addr DIdxHere = 1;
@ -39,6 +39,7 @@ constexpr Addr DIdxBegin = DIdxInBuf + 80 * sizeof(char);
*/
extern std::array<Cell, DictSize> DICT;
extern Cell& BASE;
extern Cell& HERE; /** Linked to HERE's storage in DICT. */
extern Cell& LATEST; /** Linked to LATEST's storage in DICT. */
extern Cell& STATE; /** Linked to STATE's storage in DICT. */

@ -100,13 +100,10 @@ struct WordSet
* and used for a word's definition.
*/
template<auto... funcs>
auto WordWrap = [] {
constexpr static Func list[1] = {
+[] { (funcs(), ...); }
};
return list;
};
constexpr auto WordWrap = []() noexcept {
constexpr static auto wrapper = +[] { (funcs(), ...); };
return &wrapper;
}();
enum class Error : int {
none = 1,

@ -33,59 +33,59 @@ static void discard() { auto v = pop(); (void)v; }
static void tobool() { if (*sp()) *sp() = -1; }
constinit WordSet words (
Word("[", WordWrap<[] { STATE = 0; }>()).markImmediate(),
Word("]", WordWrap<[] { STATE = -1; }>()),
Word("@", WordWrap<peek>()),
Word("c@", WordWrap<peek, [] { *sp() &= 0xFF; }>()),
Word("!", WordWrap<[] { auto a = (Cell *)pop(); *a = pop(); }>()),
Word("c!", WordWrap<[] { auto a = (char *)pop(); *a = pop(); }>()),
Word("_d", WordWrap<[] { *sp() += (Cell)DICT.data(); }>()),
Word("_jmp", WordWrap<[] { jump((FuncList)*++IP); }>()),
Word("[", WordWrap<[] { STATE = 0; }>).markImmediate(),
Word("]", WordWrap<[] { STATE = -1; }>),
Word("@", WordWrap<peek>),
Word("c@", WordWrap<peek, [] { *sp() &= 0xFF; }>),
Word("!", WordWrap<[] { auto a = (Cell *)pop(); *a = pop(); }>),
Word("c!", WordWrap<[] { auto a = (char *)pop(); *a = pop(); }>),
Word("_d", WordWrap<[] { *sp() += (Cell)DICT.data(); }>),
Word("_jmp", WordWrap<[] { jump((FuncList)*++IP); }>),
Word("_jmp0", WordWrap<[] {
++IP;
if (pop() == 0)
jump((FuncList)*IP);
}>()),
Word(",", WordWrap<commaSP>()),
Word("emit", WordWrap<[] { std::putchar(pop()); }>()),
Word("key", WordWrap<[] { push(key()); }>()),
Word("key?", WordWrap<[] { push(haskey()); }, tobool>()),
Word("execute", WordWrap<[] { (void)executor((FuncList *)pop()); }>()),
Word(":", WordWrap<colon>()),
Word(";", WordWrap<semic>()).markImmediate(),
}>),
Word(",", WordWrap<commaSP>),
Word("emit", WordWrap<[] { std::putchar(pop()); }>),
Word("key", WordWrap<[] { push(key()); }>),
Word("key?", WordWrap<[] { push(haskey()); }, tobool>),
Word("execute", WordWrap<[] { (void)executor((FuncList *)pop()); }>),
Word(":", WordWrap<colon>),
Word(";", WordWrap<semic>).markImmediate(),
Word("exit", fexit),
Word("drop", WordWrap<discard>()),
Word("dup", WordWrap<[] { push(*sp()); }>()),
Word("swap", WordWrap<[] { std::swap(*sp(), *(sp() - 1)); }>()),
Word("pick", WordWrap<[] { auto t = *(sp() - *sp() - 1); *sp() = t; }>()),
Word("cells", WordWrap<[] { *sp() *= sizeof(Cell); }>()),
Word("+", WordWrap<[] { *(sp() - 1) += *sp(); }, discard>()),
Word("-", WordWrap<[] { *(sp() - 1) -= *sp(); }, discard>()),
Word("*", WordWrap<[] { *(sp() - 1) *= *sp(); }, discard>()),
Word("/", WordWrap<[] { *(sp() - 1) /= *sp(); }, discard>()),
Word("mod", WordWrap<[] { *(sp() - 1) %= *sp(); }, discard>()),
Word("=", WordWrap<[] { *(sp() - 1) = *(sp() - 1) == *sp(); }, discard, tobool>()),
Word("<", WordWrap<[] { *(sp() - 1) = *(sp() - 1) < *sp(); }, discard, tobool>()),
Word("or", WordWrap<[] { *(sp() - 1) |= *sp(); }, discard>()),
Word("and", WordWrap<[] { *(sp() - 1) &= *sp(); }, discard>()),
Word("xor", WordWrap<[] { *(sp() - 1) ^= *sp(); }, discard>()),
Word("lshift", WordWrap<[] { *(sp() - 1) <<= *sp(); }, discard>()),
Word("rshift", WordWrap<[] { *(sp() - 1) >>= *sp(); }, discard>()),
Word(">r", WordWrap<[] { rpush(pop()); }>()),
Word("r>", WordWrap<[] { push(rpop()); }>()),
Word("immediate", WordWrap<[] { ((Word *)LATEST)->markImmediate(); }>()),
Word("aligned", WordWrap<[] { *sp() = aligned(*sp()); }>()),
Word("align", WordWrap<align>()),
Word("literal", WordWrap<[] { if (STATE) compileliteral(); }>()).markImmediate(),
Word("\'", WordWrap<tick>()),
Word("_i", WordWrap<[] { *sp() = ((Word *)*sp())->immediate(); }, tobool>()),
Word("[']", WordWrap<tick, compileliteral>()).markImmediate(),
Word("compile,", WordWrap<peek, commaSP>()),
Word("word", WordWrap<word>()),
Word("drop", WordWrap<discard>),
Word("dup", WordWrap<[] { push(*sp()); }>),
Word("swap", WordWrap<[] { std::swap(*sp(), *(sp() - 1)); }>),
Word("pick", WordWrap<[] { auto t = *(sp() - *sp() - 1); *sp() = t; }>),
Word("cells", WordWrap<[] { *sp() *= sizeof(Cell); }>),
Word("+", WordWrap<[] { *(sp() - 1) += *sp(); }, discard>),
Word("-", WordWrap<[] { *(sp() - 1) -= *sp(); }, discard>),
Word("*", WordWrap<[] { *(sp() - 1) *= *sp(); }, discard>),
Word("/", WordWrap<[] { *(sp() - 1) /= *sp(); }, discard>),
Word("mod", WordWrap<[] { *(sp() - 1) %= *sp(); }, discard>),
Word("=", WordWrap<[] { *(sp() - 1) = *(sp() - 1) == *sp(); }, discard, tobool>),
Word("<", WordWrap<[] { *(sp() - 1) = *(sp() - 1) < *sp(); }, discard, tobool>),
Word("or", WordWrap<[] { *(sp() - 1) |= *sp(); }, discard>),
Word("and", WordWrap<[] { *(sp() - 1) &= *sp(); }, discard>),
Word("xor", WordWrap<[] { *(sp() - 1) ^= *sp(); }, discard>),
Word("lshift", WordWrap<[] { *(sp() - 1) <<= *sp(); }, discard>),
Word("rshift", WordWrap<[] { *(sp() - 1) >>= *sp(); }, discard>),
Word(">r", WordWrap<[] { rpush(pop()); }>),
Word("r>", WordWrap<[] { push(rpop()); }>),
Word("immediate", WordWrap<[] { ((Word *)LATEST)->markImmediate(); }>),
Word("aligned", WordWrap<[] { *sp() = aligned(*sp()); }>),
Word("align", WordWrap<align>),
Word("literal", WordWrap<[] { if (STATE) compileliteral(); }>).markImmediate(),
Word("\'", WordWrap<tick>),
Word("_i", WordWrap<[] { *sp() = ((Word *)*sp())->immediate(); }, tobool>),
Word("[']", WordWrap<tick, compileliteral>).markImmediate(),
Word("compile,", WordWrap<peek, commaSP>),
Word("word", WordWrap<word>),
Word("_b", WordWrap<[] {
std::putchar('#'); // Gives a good breakpoint spot for gdb
}>()),
Word(".", WordWrap<[] { std::cout << pop() << ' '; }>())
}>),
Word(".", WordWrap<[] { std::cout << pop() << ' '; }>)
);
void getinput()

Loading…
Cancel
Save