You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.9 KiB
C++

1 month ago
/// 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"
1 month ago
#include <array>
#include <fstream>
#include <iostream>
#include <span>
#include <string>
constinit static sforth::forth<4096> forth {sforth::initialize<&forth>()};
1 month ago
static void putu(sforth::addr v)
{
char buf[32] = {};
auto ptr = buf + sizeof(buf);
*--ptr = '\0';
do {
*--ptr = "0123456789abcdefghijklmnopqrstuvwxyz"[v % forth.base];
} while (v /= forth.base);
std::cout << ptr << ' ';
}
constinit static sforth::native_word<".", [](auto) {
sforth::addr v = forth.pop();
if (v & (1 << (8 * sizeof(sforth::cell) - 1)))
std::cout << '-';
putu(v);
}> dot;
constinit static sforth::native_word<"U.", [](auto) {
putu(forth.pop());
}, &dot> udot;
constinit static sforth::native_word<"EMIT", [](auto) {
std::cout << static_cast<char>(forth.pop());
}, &udot> emit;
constinit static sforth::native_word<"TYPE", [](auto) {
const unsigned u = forth.pop();
const auto caddr = reinterpret_cast<const char *>(forth.pop());
std::cout << std::string_view{caddr, u};
}, &emit> type;
static bool parse_stream(auto&, std::istream&, bool say_okay = false);
1 month ago
int main(int argc, const char *argv[])
{
std::span args (argv + 1, argc - 1);
dot.next = std::exchange(forth.next, &type);
1 month ago
for (auto arg : args) {
if (std::ifstream file {arg}; parse_stream(forth, file))
1 month ago
return 0;
}
parse_stream(forth, std::cin, true);
1 month ago
}
bool parse_stream(auto &fth, std::istream& str, bool say_okay)
1 month ago
{
std::string line;
while (str.good()) {
std::getline(str, line);
if (!line.empty()) {
for (auto& ch : line) {
if (ch >= 'a' && ch <= 'z')
ch = ch - 'a' + 'A';
}
if (line == "BYE")
return true;
1 month ago
try {
fth.parse_line(line);
} catch (sforth::error e) {
std::cerr << sforth::error_string(e) << " in " << line << std::endl;
1 month ago
continue;
}
}
if (say_okay)
std::cout << (fth.compiling ? "compiled" : "ok") << std::endl;
1 month ago
}
return false;
}