-( : variable create 0 , ; )
-( : create here const ; )
-
: . 0 sys ;
: emit 1 sys ;
: cells 2 * ;
: over 1 pick ;
+: rot >r swap r> swap ;
: -rot rot rot ;
: nip swap drop ;
: tuck swap over ;
: char bl word cell+ c@ ;
: [char] char postpone literal ; imm
+: ( begin [char] ) key <> while repeat ; imm
+
: type begin dup 0 > while swap dup c@ emit char+ swap 1- repeat 2drop ;
: s" state @ if ['] _jmp , here 0 , then
[char] " word count
: constant create , does> ['] @ , postpone ; ;
( TODO fix compile-time does>... above should simply be "does> @ ;" )
--1 constant true
-0 constant false
+-1 constant true
+0 constant false
: >in _input 80 chars + cell+ _input @ - 4 chars - ;
: source _input @ 6 chars + >in 3 chars - swap ;
Func CoreWords::get(int index)
{
static const Func ops[WordCount] = {
- op_drop, op_dup, op_swap, op_pick, op_sys,
- op_add, op_sub, op_mul, op_div, op_mod,
- /*10*/ op_peek, op_poke, op_rot, op_pushr, op_popr,
- op_eq, op_lt, op_allot, op_and, op_or,
- /*20*/ op_xor, op_shl, op_shr, op_comment, op_colon,
- op_semic, op_here, op_const, op_depth, op_key,
- /*30*/ op_exit, op_tick, op_execute, op_jmp, op_jmp0,
- op_lit, op_literal, op_rdepth
+ op_drop, op_dup, op_swap, op_pick, op_sys,
+ op_add, op_sub, op_mul, op_div, op_mod,
+ /*10*/ op_peek, op_poke, op_pushr, op_popr, op_eq,
+ op_lt, op_allot, op_and, op_or, op_xor,
+ /*20*/ op_shl, op_shr, op_colon, op_semic, op_here,
+ op_depth, op_key, op_exit, op_tick, op_execute,
+ /*30*/ op_jmp, op_jmp0, op_lit, op_literal, op_rdepth
};
return index >= 0 && index < WordCount ? ops[index] : nullptr;
state.dict.write(addr, state.pop());
}
-void CoreWords::op_rot(State& state) {
- std::swap(state.pick(2), state.pick(1));
- std::swap(state.pick(1), state.pick(0));
-}
-
void CoreWords::op_pushr(State& state) {
state.pushr(state.pop());
}
state.top() >>= a;
}
-void CoreWords::op_comment(State& state) {
- do {
- op_key(state);
- } while (state.pop() != ')');
-}
-
void CoreWords::op_colon(State& state) {
Word word = state.dict.input();
while (word.size() == 0) {
state.push(state.dict.here);
}
-void CoreWords::op_const(State& state)
-{
- Word word = state.dict.input();
- while (word.size() == 0) {
- state.input(state);
- word = state.dict.input();
- }
-
- state.pushr(state.dict.alignhere());
- state.dict.addDefinition(word);
- state.dict.add(findi("_lit"));
- state.dict.add(state.pop());
- op_semic(state);
-}
-
void CoreWords::op_lit(State& state)
{
state.push(state.beyondip());
class CoreWords
{
public:
- constexpr static std::size_t WordCount = 38;
+ constexpr static std::size_t WordCount = 35;
constexpr static Cell Immediate = (1 << 5);
constexpr static Cell Compiletime = (1 << 6);
constexpr static char wordsarr[] =
"drop\0dup\0swap\0pick\0sys\0"
"+\0-\0*\0/\0%\0"
- "_@\0_!\0rot\0>r\0r>\0"
+ "_@\0_!\0>r\0r>\0"
"=\0<\0allot\0&\0|\0"
- "^\0<<\0>>\0(\1:\1"
- ";\1here\0const\0depth\0"
+ "^\0<<\0>>\0:\1"
+ ";\1here\0depth\0"
"key\0exit\0'\0execute\0_jmp\0"
"_jmp0\0_lit\0literal\1_rdepth\0";
static void op_mod(State&);
static void op_peek(State&);
static void op_poke(State&);
- static void op_rot(State&); // : rot >r swap r> swap ;
static void op_pushr(State&);
static void op_popr(State&);
static void op_eq(State&);
static void op_xor(State&);
static void op_shl(State&);
static void op_shr(State&);
- static void op_comment(State&);
static void op_colon(State&);
static void op_semic(State&);
static void op_here(State&);
- static void op_const(State&);
static void op_lit(State&);
static void op_depth(State&);
static void op_rdepth(State&);
static void op_execute(State&);
static void op_jmp(State&);
static void op_jmp0(State&);
- static void op_literal(State&); // : literal ['] _lit , , ; imm
+ static void op_literal(State&);
};
#endif // ALEEFORTH_COREWORDS_HPP