misc/kforth: Initial import.

This commit is contained in:
2018-02-22 11:38:27 -08:00
parent 0fb6dce050
commit c4d78e17ff
13 changed files with 1083 additions and 0 deletions

7
linux/defs.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef __KF_LINUX_DEFS_H__
#define __KF_LINUX_DEFS_H__
#include <stddef.h>
#endif

79
linux/io.cc Normal file
View File

@@ -0,0 +1,79 @@
#include <iostream>
#include "../io.h"
#include "io.h"
char
Console::rdch()
{
std::cout.flush();
return getchar();
}
void
Console::wrch(char c)
{
std::cout << c;
}
size_t
Console::rdbuf(char *buf, size_t len, bool stopat, char stopch)
{
size_t n = 0;
char ch;
while (n < len) {
ch = this->rdch();
if (stopat && stopch == ch) {
break;
}
buf[n++] = ch;
}
return n;
}
void
Console::wrbuf(char *buf, size_t len)
{
for (size_t n = 0; n < len; n++) {
this->wrch(buf[n]);
}
}
// Line I/O
bool
Console::rdln(char *buf, size_t len, size_t *readlen) {
size_t n = 0;
char ch;
bool line = false;
while (n < len) {
ch = this->rdch();
if (ch == '\n') {
line = true;
break;
}
buf[n++] = ch;
}
if (nullptr != readlen) {
*readlen = n;
}
return line;
}
void
Console::wrln(char *buf, size_t len)
{
this->wrbuf(buf, len);
this->wrch(0x0a);
}

23
linux/io.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef __KF_IO_LINUX_H__
#define __KF_IO_LINUX_H__
#include "io.h"
#include "defs.h"
class Console : public IO {
public:
~Console() {};
char rdch(void);
void wrch(char c);
// Buffer I/O.
size_t rdbuf(char *buf, size_t len, bool stopat, char stopch);
void wrbuf(char *buf, size_t len);
// Line I/O
bool rdln(char *buf, size_t len, size_t *readlen);
void wrln(char *buf, size_t len);
private:
};
#endif // __KF_IO_LINUX_H__