create, does>, variables

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

@ -48,6 +48,7 @@ int main(int argc, char *argv[])
Parser parser;
dict.write(Dictionary::Base, 10);
dict.write(Dictionary::Latest, Dictionary::Begin);
dict.write(Dictionary::Compiling, 0);
dict.write(Dictionary::Postpone, 0);
@ -81,7 +82,7 @@ void parseLine(Parser& parser, State& state, std::string_view line)
if (r == ParseStatus::Finished) {
if (okay)
std::cout << "ok" << std::endl;
std::cout << (state.compiling() ? "compiled" : "ok") << std::endl;
} else {
std::cout << to_string(r) << ": " << line << std::endl;
}

@ -9,7 +9,7 @@ yes 6.1.0080 (
yes 6.1.0090 *
6.1.0100 */
6.1.0110 */MOD
yes 6.1.0120 +
yes 6.1.0120
yes 6.1.0130 +!
yes 6.1.0140 +LOOP
yes 6.1.0150 ,
@ -61,14 +61,14 @@ yes 6.1.0890 CELLS
yes 6.1.0895 CHAR
yes 6.1.0897 CHAR+
yes 6.1.0898 CHARS
6.1.0950 CONSTANT
yes 6.1.0950 CONSTANT
yes 6.1.0980 COUNT
yes 6.1.0990 CR
6.1.1000 CREATE
yes 6.1.1000 CREATE
yes 6.1.1170 DECIMAL
yes 6.1.1200 DEPTH
yes 6.1.1240 DO
6.1.1250 DOES>
yes 6.1.1250 DOES>
yes 6.1.1260 DROP
yes 6.1.1290 DUP
yes 6.1.1310 ELSE
@ -86,7 +86,7 @@ yes 6.1.1680 I
yes 6.1.1700 IF
yes 6.1.1710 IMMEDIATE (as "imm")
6.1.1720 INVERT
6.1.1730 J
6.1.1730 J
yes 6.1.1750 KEY
6.1.1760 LEAVE
yes 6.1.1780 LITERAL
@ -125,7 +125,7 @@ yes 6.1.2310 TYPE
6.1.2370 UM/MOD
6.1.2380 UNLOOP
yes 6.1.2390 UNTIL
6.1.2410 VARIABLE
yes 6.1.2410 VARIABLE
yes 6.1.2430 WHILE
yes 6.1.2450 WORD
yes 6.1.2490 XOR

@ -30,10 +30,13 @@
: 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 ! ;
@ -101,3 +104,16 @@
: 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> @ ;" )

@ -23,12 +23,12 @@ 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,
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
};
@ -169,8 +169,12 @@ void CoreWords::op_colon(State& state) {
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);
}
}
@ -202,14 +206,6 @@ void CoreWords::op_exit(State& state) {
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);
}
}
@ -218,12 +214,6 @@ void CoreWords::op_here(State& state) {
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();
@ -339,8 +329,6 @@ Func CoreWords::find(State& state, Word word)
return i >= 0 ? get(i & ~Compiletime) : nullptr;
}
struct corewords_run {};
bool CoreWords::run(int i, State& state)
{
i &= ~Compiletime;

@ -29,7 +29,7 @@ void user_sys(State&);
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
@ -50,7 +50,7 @@ private:
"_@\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";
@ -83,7 +83,6 @@ private:
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&);

@ -57,7 +57,7 @@ void Dictionary::addDefinition(Word word)
Addr Dictionary::find(Word word)
{
Addr lt = latest, oldlt;
Addr lt = latest(), oldlt;
do {
oldlt = lt;
const Cell l = read(lt);

@ -29,14 +29,17 @@ class Dictionary
{
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;

Loading…
Cancel
Save