: chars ;
: base 0 ;
-: state 2 ;
+: _latest 1 cells ;
+: state 2 cells ;
: decimal 1 1+ base ! 1010 base ! ;
-: postpone 1 4 ! ; imm
+: imm _latest @ dup @ 1 5 << | swap ! ;
+
+: postpone 1 3 cells ! ; imm
: ['] ' postpone literal ; imm
: [ 0 state ! ; imm
: ] 1 state ! ;
: type begin dup 0 > while swap dup c@ emit char+ swap 1- repeat ;
: ." [char] " word count type ;
+
+: create align here bl word count nip cell+ allot align
+ ['] _lit , here 3 cells + , ['] exit , 0 ,
+ dup @ 31 & over _latest @ - 6 << or over ! _latest ! ;
+: does> _latest @
+ dup @ 31 & + cell+ aligned
+ 2 cells +
+ ['] _jmp over ! cell+
+ here swap ! ] ;
+
+: variable create 1 cells allot ;
+: constant create , does> ['] @ , postpone ; ;
+( TODO fix compile-time does>... above should simply be "does> @ ;" )
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,
- op_peek, op_poke, op_rot, op_pushr, op_popr,
+ /*10*/ op_peek, op_poke, op_rot, op_pushr, op_popr,
op_eq, op_lt, op_allot, op_and, op_or,
- op_xor, op_shl, op_shr, op_comment, op_colon,
- op_semic, op_here, op_imm, op_const, op_depth,
- op_key, op_exit, op_tick, op_execute, op_jmp,
- op_jmp0, op_lit, op_literal,
+ /*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_jump
};
word = state.dict.input();
}
- state.pushr(state.dict.alignhere());
+ const auto start = state.dict.alignhere();
state.dict.addDefinition(word);
+ state.dict.write(start,
+ (state.dict.read(start) & 0x1F) |
+ ((start - state.dict.latest()) << 6));
+ state.dict.latest(start);
}
}
void CoreWords::op_semic(State& state) {
if (state.compiling()) {
state.dict.add(findi("exit"));
-
- auto begin = state.popr();
-
- state.dict.write(begin,
- (state.dict.read(begin) & 0x1F) |
- ((begin - state.dict.latest) << 6));
-
- state.dict.latest = begin;
state.compiling(false);
}
}
state.push(state.dict.here);
}
-void CoreWords::op_imm(State& state)
-{
- state.dict.write(state.dict.latest,
- state.dict.read(state.dict.latest) | Immediate);
-}
-
void CoreWords::op_const(State& state)
{
Word word = state.dict.input();
return i >= 0 ? get(i & ~Compiletime) : nullptr;
}
-struct corewords_run {};
-
bool CoreWords::run(int i, State& state)
{
i &= ~Compiletime;
class CoreWords
{
public:
- constexpr static std::size_t VisibleWordCount = 38; // size
+ constexpr static std::size_t VisibleWordCount = 37; // size
constexpr static auto HiddenWordJump = VisibleWordCount; // index
constexpr static auto WordCount = VisibleWordCount + 1; // size
"_@\0_!\0rot\0>r\0r>\0"
"=\0<\0allot\0&\0|\0"
"^\0<<\0>>\0(\0:\1"
- ";\1here\0imm\0const\0depth\0"
+ ";\1here\0const\0depth\0"
"key\0exit\0'\0execute\0_jmp\0"
"_jmp0\0_lit\0literal\1";
static void op_colon(State&);
static void op_semic(State&);
static void op_here(State&);
- static void op_imm(State&);
static void op_const(State&);
static void op_lit(State&);
static void op_jump(State&);
{
public:
constexpr static Addr Base = 0;
- constexpr static Addr Compiling = sizeof(Cell);
- constexpr static Addr Postpone = sizeof(Cell) * 2;
- constexpr static Addr Input = sizeof(Cell) * 3; // len data...
+ constexpr static Addr Latest = sizeof(Cell);
+ constexpr static Addr Compiling = sizeof(Cell) * 2;
+ constexpr static Addr Postpone = sizeof(Cell) * 3;
+ constexpr static Addr Input = sizeof(Cell) * 4; // len data...
constexpr static Addr InputCells = 80; // bytes!
- constexpr static Addr Begin = sizeof(Cell) * 4 + InputCells;
+ constexpr static Addr Begin = sizeof(Cell) * 5 + InputCells;
Addr here = Begin;
- Addr latest = Begin;
+
+ Addr latest() { return read(Latest); }
+ void latest(Addr l) { write(Latest, l); }
virtual Cell read(Addr) const = 0;
virtual void write(Addr, Cell) = 0;