aboutsummaryrefslogtreecommitdiffstats
path: root/source/core.cpp
blob: cc7c267e39efd4e7e4a421b25a2759441a1a79e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// sprit-forth: A portable subroutine-threaded Forth.
// Copyright (C) 2023  Clyne Sullivan <clyne@bitgloo.com>
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
// more details.
//
// You should have received a copy of the GNU Library General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.

#include "core.hpp"
#include "state.hpp"

#include <cctype>
#include <cstring>

void jump(FuncList ip)
{
    // IP is incremented before its next execution.
    IP = ip - 1;
}

// LITERAL's run-time semantics: push the given value onto the stack.
static auto literall = WordWrap<[] {
    push((Cell)*++IP);
}>();

void compileliteral()
{
    comma((Cell)literall);
    comma(pop());
}

bool haskey()
{
    return DICT[DIdxSrcLen] > 0;
}

void addkey(int k)
{
    --DICT[DIdxSource];
    ++DICT[DIdxSrcLen];

    auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]);
    *ptr = static_cast<char>(k);
}

int key()
{
    // Block until input is available.
    while (!haskey())
        getinput();

    auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]);
    ++DICT[DIdxSource];
    --DICT[DIdxSrcLen];

    return *ptr;
}

Cell *comma(Cell n)
{
    const auto ptr = reinterpret_cast<Cell *>(HERE);
    *ptr = n;
    HERE += sizeof(Cell);
    return ptr;
}

Addr aligned(Addr addr)
{
    return (addr + (sizeof(Cell) - 1)) & ~(sizeof(Cell) - 1);
}

void align()
{
    HERE = aligned(HERE);
}

static void readword(int ch)
{
    int k;
    do {
        k = key();
    } while (k == ch);

    // Collect the word's text.
    char *ptr;
    do {
        ptr = reinterpret_cast<char *>(HERE);
        *ptr = k;
        ++HERE;

        if (!haskey())
            break;

        k = key();
    } while (k != ch);

    // Add a null terminator.
    ptr = reinterpret_cast<char *>(HERE);
    *ptr = '\0';
    ++HERE;
}

void word()
{
    auto here = (char *)HERE;
    ++HERE;

    readword(*sp());

    here[0] = strlen(here + 1);
    HERE = (Cell)here;
    *sp() = HERE;
}

void colon()
{
    // Collect (and store) the word's name.
    align();
    auto name = HERE;
    readword(' ');
    align();

    // Build the Word structure.
    comma(HERE + 4 * sizeof(Cell)); // exec ptr
    comma(name);                    // name ptr
    push((Cell)comma(0));           // link (to be set by semic())
    comma(0);                       // immediate

    // The word's execution begins with a prologue that technically performs
    // the "call" to this word.
    // By including this in the word's definition, execution can avoid caring
    // about if it is running words or routines (i.e. pre-defined words).
    comma((Cell)+[](FuncList *ip) {
        ++ip;
        rpush((Cell)IP);
        jump((FuncList)*ip);
    });
    // The actual function list will begin one Cell beyond here.
    comma(HERE + sizeof(Cell));

    DICT[DIdxCompXt] = *sp() - 2 * sizeof(Cell);

    // Enter compiling state.
    STATE = -1;
}

void semic()
{
    // Add exit routine.
    comma((Cell)fexit);

    // Complete the new word's linkage to make it usable.
    auto link = (Cell *)pop();
    *link = LATEST;
    LATEST = (Cell)(link - 2);

    // Exit compilation state.
    STATE = 0;
}

// TODO define in Forth? ": ' bl word find drop ;"
void tick()
{
    // Get the name to look up.
    auto name = (char *)HERE;
    readword(' ');

    // Look up the name and push the result.
    int len = HERE - (Cell)name - 1;
    auto word = find(name, len);
    push((Cell)word);

    // Deallocate `name`.
    HERE = (Cell)name;
}