static void parseLine(Parser&, State&, const std::string&);
static void parseFile(Parser&, State&, std::istream&);
+static Parser parser;
+
int main(int argc, char *argv[])
{
MemDict dict;
State state (dict, readchar);
- Parser parser;
dict.initialize();
state.dict.writebyte(i++, file.get());
}
+#include <cstring>
void user_sys(State& state)
{
char buf[32] = {0};
std::cout << buf << ' ';
}
break;
+ case 5: // eval
+ {
+ auto oldip = state.ip;
+ std::jmp_buf oldjb;
+ memcpy(oldjb, state.jmpbuf, sizeof(std::jmp_buf));
+ state.ip = 0;
+ parser.parseSource(state);
+ memcpy(state.jmpbuf, oldjb, sizeof(std::jmp_buf));
+ state.ip = oldip;
+ }
+ break;
}
}
: imm _latest @ dup @ 1 5 << | swap ! ;
: immediate imm ;
: state 3 cells ;
-: >in 4 cells ;
+: _source 4 cells ;
+: >in 5 cells ;
: , here ! 1 cells allot ;
: min 2dup <= if drop else nip then ;
: max 2dup <= if nip else drop then ;
-: key >in @ 5 cells +
+: source _source @ 0 begin 2dup + c@ while char+ repeat ;
+: key _source @ >in @ +
begin dup c@ 0 = while _in repeat
c@ 1 >in +! ;
-: key? >in @ 5 cells + c@ 0 <> ;
+: key? _source @ >in @ + c@ 0 <> ;
: word here dup >r char+ >r
begin key? if key 2dup <> else 0 0 then while
r> tuck c! char+ >r repeat
: [char] char postpone literal ; imm
: ( begin [char] ) key <> while repeat ; imm
-: \ >in @ 5 cells +
+: \ _source @ >in @ +
begin dup c@ while 0 over c! char+ repeat drop ; imm
: type begin dup 0 > while swap dup c@ emit char+ swap 1- repeat 2drop ;
-1 constant true
0 constant false
-: source >in cell+ 0 begin 2dup + c@ while char+ repeat ;
-
: quit begin _rdepth 1 > while r> drop repeat postpone [ ;
: abort begin depth 0 > while drop repeat quit ;
: abort" postpone s" ['] rot ,
: marker create _latest @ , here , does>
dup @ _latest ! cell+ @ here swap - allot ;
: :noname 0 , here ] ;
+
+: evaluate _source @ >r >in @ >r
+ 0 >in ! _source ! 5 sys
+ r> >in ! r> _source ! ;
write(Here, Begin);
write(Latest, Begin);
write(Compiling, 0);
+ write(Source, Input + sizeof(Cell));
}
Addr Dictionary::allot(Cell amount) noexcept
Word Dictionary::input() noexcept
{
auto idx = read(Dictionary::Input);
- auto ch = readbyte(Dictionary::Input + sizeof(Cell) + idx);
+ auto src = read(Dictionary::Source);
+ auto ch = readbyte(src + idx);
if (ch) {
- Addr wordstart = Dictionary::Input + sizeof(Cell) + idx;
+ Addr wordstart = src + idx;
Addr wordend = wordstart;
do {
constexpr static Addr Here = sizeof(Cell);
constexpr static Addr Latest = sizeof(Cell) * 2;
constexpr static Addr Compiling = sizeof(Cell) * 3;
- constexpr static Addr Input = sizeof(Cell) * 4; // len data...
+ constexpr static Addr Source = sizeof(Cell) * 4;
+ constexpr static Addr Input = sizeof(Cell) * 5; // len data...
constexpr static Addr InputCells = 82; // bytes!
- constexpr static Addr Begin = sizeof(Cell) * 5 + InputCells;
+ constexpr static Addr Begin = sizeof(Cell) * 6 + InputCells;
void initialize();
constexpr static int UnknownWord = -1;
int parse(State&, const char *);
+ int parseSource(State&);
private:
- int parseSource(State&);
int parseWord(State&, Word);
int parseNumber(State&, Word);
};