concise parser; >body, >in, source

llvm
Clyne 2 years ago
parent 6ef7f7ce85
commit bc118ad31f
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -36,8 +36,8 @@ yes 6.1.0480 <
6.1.0490 <# 6.1.0490 <#
yes 6.1.0530 = yes 6.1.0530 =
yes 6.1.0540 > yes 6.1.0540 >
6.1.0550 >BODY yes 6.1.0550 >BODY
6.1.0560 >IN yes 6.1.0560 >IN
6.1.0570 >NUMBER 6.1.0570 >NUMBER
yes 6.1.0580 >R yes 6.1.0580 >R
yes 6.1.0630 ?DUP yes 6.1.0630 ?DUP
@ -112,7 +112,7 @@ yes 6.1.2162 RSHIFT
6.1.2170 S>D 6.1.2170 S>D
6.1.2210 SIGN 6.1.2210 SIGN
6.1.2214 SM/REM 6.1.2214 SM/REM
6.1.2216 SOURCE yes 6.1.2216 SOURCE
yes 6.1.2220 SPACE yes 6.1.2220 SPACE
yes 6.1.2230 SPACES yes 6.1.2230 SPACES
yes 6.1.2250 STATE yes 6.1.2250 STATE

@ -31,12 +31,13 @@
: base 0 ; : base 0 ;
: _latest 1 cells ; : _latest 1 cells ;
: imm _latest @ dup @ 1 5 << | swap ! ;
: state 2 cells ; : state 2 cells ;
: decimal 1 1+ base ! 1010 base ! ; : postpone 1 3 cells ! ; imm
: _input 4 cells ;
: imm _latest @ dup @ 1 5 << | swap ! ; : decimal 1 1+ base ! 1010 base ! ;
: postpone 1 3 cells ! ; imm
: ['] ' postpone literal ; imm : ['] ' postpone literal ; imm
: [ 0 state ! ; imm : [ 0 state ! ; imm
: ] 1 state ! ; : ] 1 state ! ;
@ -113,7 +114,11 @@
2 cells + 2 cells +
['] _jmp over ! cell+ ['] _jmp over ! cell+
here swap ! ] ; here swap ! ] ;
: >body cell+ @ ;
: variable create 1 cells allot ; : variable create 1 cells allot ;
: constant create , does> ['] @ , postpone ; ; : constant create , does> ['] @ , postpone ; ;
( TODO fix compile-time does>... above should simply be "does> @ ;" ) ( TODO fix compile-time does>... above should simply be "does> @ ;" )
: >in _input 80 chars + cell+ _input @ - 4 chars - ;
: source _input @ 6 chars + >in 3 chars - swap ;

@ -28,8 +28,7 @@ Func CoreWords::get(int index)
/*20*/ op_xor, op_shl, op_shr, op_comment, op_colon, /*20*/ op_xor, op_shl, op_shr, op_comment, op_colon,
op_semic, op_here, op_const, op_depth, op_key, op_semic, op_here, op_const, op_depth, op_key,
/*30*/ op_exit, op_tick, op_execute, op_jmp, op_jmp0, /*30*/ op_exit, op_tick, op_execute, op_jmp, op_jmp0,
op_lit, op_literal, op_lit, op_literal
op_jump
}; };
return index >= 0 && index < WordCount ? ops[index] : nullptr; return index >= 0 && index < WordCount ? ops[index] : nullptr;
@ -162,7 +161,6 @@ void CoreWords::op_comment(State& state) {
} }
void CoreWords::op_colon(State& state) { void CoreWords::op_colon(State& state) {
if (state.compiling()) {
Word word = state.dict.input(); Word word = state.dict.input();
while (word.size() == 0) { while (word.size() == 0) {
state.input(state); state.input(state);
@ -175,7 +173,7 @@ void CoreWords::op_colon(State& state) {
(state.dict.read(start) & 0x1F) | (state.dict.read(start) & 0x1F) |
((start - state.dict.latest()) << 6)); ((start - state.dict.latest()) << 6));
state.dict.latest(start); state.dict.latest(start);
} state.compiling(true);
} }
void CoreWords::op_tick(State& state) { void CoreWords::op_tick(State& state) {
@ -243,12 +241,6 @@ void CoreWords::op_literal(State& state)
} }
} }
void CoreWords::op_jump(State& state)
{
state.pushr(state.ip + sizeof(Cell));
op_jmp(state);
}
void CoreWords::op_jmp(State& state) void CoreWords::op_jmp(State& state)
{ {
state.ip = state.beyondip() - sizeof(Cell); state.ip = state.beyondip() - sizeof(Cell);

@ -29,9 +29,7 @@ void user_sys(State&);
class CoreWords class CoreWords
{ {
public: public:
constexpr static std::size_t VisibleWordCount = 37; // size constexpr static std::size_t WordCount = 37;
constexpr static auto HiddenWordJump = VisibleWordCount; // index
constexpr static auto WordCount = VisibleWordCount + 1; // size
constexpr static Cell Immediate = (1 << 5); constexpr static Cell Immediate = (1 << 5);
constexpr static Cell Compiletime = (1 << 6); constexpr static Cell Compiletime = (1 << 6);
@ -85,7 +83,6 @@ private:
static void op_here(State&); static void op_here(State&);
static void op_const(State&); static void op_const(State&);
static void op_lit(State&); static void op_lit(State&);
static void op_jump(State&);
static void op_depth(State&); static void op_depth(State&);
static void op_key(State&); static void op_key(State&);
static void op_exit(State&); static void op_exit(State&);

@ -52,47 +52,37 @@ ParseStatus Parser::parseSource(State& state)
ParseStatus Parser::parseWord(State& state, Word word) ParseStatus Parser::parseWord(State& state, Word word)
{ {
// TODO unify core-word and defined-word parsing/execution. int ins, imm;
if (auto i = CoreWords::findi(state, word); i >= 0) { ins = CoreWords::findi(state, word);
auto p = state.dict.read(Dictionary::Postpone); if (ins < 0) {
auto imm = (i & CoreWords::Compiletime); ins = state.dict.find(word);
if (state.compiling() || p) { if (ins <= 0) {
if (p || !imm) { return parseNumber(state, word);
state.dict.add(i & ~CoreWords::Compiletime); } else {
imm = state.dict.read(ins) & CoreWords::Immediate;
if (p) ins = state.dict.getexec(ins);
state.dict.write(Dictionary::Postpone, 0);
} else if (imm) {
CoreWords::run(i & ~CoreWords::Compiletime, state);
} }
} else { } else {
if (state.dict.equal(word, ":")) imm = ins & CoreWords::Compiletime;
state.compiling(true); ins &= ~CoreWords::Compiletime;
CoreWords::run(i & ~CoreWords::Compiletime, state);
} }
} else if (auto j = state.dict.find(word); j > 0) {
auto e = state.dict.getexec(j);
auto p = state.dict.read(Dictionary::Postpone);
if (state.compiling() || p) {
auto imm = state.dict.read(j) & CoreWords::Immediate;
if (p || !imm) { if (state.dict.read(Dictionary::Postpone)) {
state.dict.add(CoreWords::HiddenWordJump); state.dict.add(ins);
state.dict.add(e);
if (p)
state.dict.write(Dictionary::Postpone, 0); state.dict.write(Dictionary::Postpone, 0);
} else if (imm) { } else if (state.compiling() && !imm) {
state.execute(e); state.dict.add(ins);
}
} else { } else {
state.execute(e); state.execute(ins);
} }
} else {
return ParseStatus::Finished;
}
ParseStatus Parser::parseNumber(State& state, Word word)
{
char buf[word.size() + 1]; char buf[word.size() + 1];
for (unsigned i = 0; i < word.size(); ++i) for (unsigned i = 0; i < word.size(); ++i)
buf[i] = state.dict.readbyte(word.start + i); buf[i] = state.dict.readbyte(word.start + i);
@ -109,12 +99,11 @@ ParseStatus Parser::parseWord(State& state, Word word)
} else { } else {
state.push(l); state.push(l);
} }
return ParseStatus::Finished;
} else { } else {
std::cout << "word not found: " << buf << std::endl; std::cout << "word not found: " << buf << std::endl;
return ParseStatus::NotAWord; return ParseStatus::NotAWord;
} }
}
return ParseStatus::Finished;
} }

@ -31,6 +31,7 @@ public:
private: private:
ParseStatus parseSource(State&); ParseStatus parseSource(State&);
ParseStatus parseWord(State&, Word); ParseStatus parseWord(State&, Word);
ParseStatus parseNumber(State&, Word);
}; };
#endif // ALEEFORTH_PARSER_HPP #endif // ALEEFORTH_PARSER_HPP

Loading…
Cancel
Save