aboutsummaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: e51f702a0901813c387a7190bb482001d5dc2d0a (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
/// sforth, an implementation of forth
/// Copyright (C) 2024  Clyne Sullivan <clyne@bitgloo.com>
///
/// This program is free software: you can redistribute it and/or modify it
/// under the terms of the GNU General Public License as published by the Free
/// Software Foundation, either version 3 of the License, or (at your option)
/// any later version.
///
/// This program 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 General Public License for
/// more details.
///
/// You should have received a copy of the GNU General Public License along
/// with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "sforth/forth.hpp"

#include <array>
#include <charconv>
#include <fstream>
#include <iostream>
#include <span>
#include <string>

constinit static sforth::forth<4096> forth {sforth::initialize<&forth>()};

static bool parse_stream(auto&, std::istream&, bool say_okay = false);

constinit static sforth::native_word<".", [](auto) {
    char buf[8 * sizeof(sforth::cell) + 1] = {};
    std::to_chars(buf, buf + sizeof(buf), forth.pop(), forth.base);
    std::cout << buf << ' ';
}> dot;
constinit static sforth::native_word<"U.", [](auto) {
    char buf[8 * sizeof(sforth::cell) + 1] = {};
    std::to_chars(buf, buf + sizeof(buf), std::bit_cast<sforth::addr>(forth.pop()), forth.base);
    std::cout << buf << ' ';
}, &dot> udot;
constinit static sforth::native_word<"EMIT", [](auto) {
    std::cout << static_cast<char>(forth.pop());
}, &udot> emit;
constinit static sforth::native_word<"INCLUDE", [](auto) {
    const auto w = forth.parse();
    std::string s {w.begin(), w.end()};
    std::ifstream file {s};
    parse_stream(forth, file);
}, &emit> incl;

int main(int argc, const char *argv[])
{
    std::span args (argv + 1, argc - 1);

    dot.next = std::exchange(forth.next, &incl);

    for (auto arg : args) {
        if (std::ifstream file {arg}; parse_stream(forth, file))
            return 0;
    }

    parse_stream(forth, std::cin, true);
}

bool parse_stream(auto &fth, std::istream& str, bool say_okay)
{
    std::string line;

    while (str.good()) {
        std::getline(str, line);
        if (!line.empty()) {
            if (sforth::isequal(line, "bye"))
                return true;

            try {
                fth.parse_line(line);
            } catch (sforth::error e) {
                std::cerr << sforth::error_string(e) << " in " << line << std::endl;
                continue;
            }
        }

        if (say_okay)
            std::cout << (fth.compiling ? "compiled" : "ok") << std::endl;
    }

    return false;
}