define type in forth

main
Clyne 3 days ago
parent 3246deb437
commit ff4e488ca0
Signed by: clyne
GPG Key ID: 7BA5A2980566A649

@ -65,18 +65,20 @@
dup cell+ allot dup cell+ allot
rot here swap ! rot here swap !
swap postpone literal postpone literal then ; immediate swap postpone literal postpone literal then ; immediate
: ." postpone s" state @ if postpone type else type then ; immediate : type ?dup if 0 do dup c@ emit 1+ loop then drop ;
: ." postpone s" state @ if postpone type else type then ; immediate
: ( begin [char] ) key = until ; immediate : ( begin [char] ) key = until ; immediate
: execute [ here 3 cells + ] literal ! [ ' _jmp , 0 , ] ; : execute [ here 3 cells + ] literal ! [ ' _jmp , 0 , ] ;
: move dup 0 <= if drop 2drop exit then : move dup 0 <= if drop 2drop exit then
>r 2dup < r> swap if >r 2dup < r> swap if
1- 0 swap do over i + c@ over i + c! -1 +loop 1- 0 swap do over i + c@ over i + c! -1 +loop
else else
0 do over i + c@ over i + c! loop 0 do over i + c@ over i + c! loop
then 2drop ; then 2drop ;
: fill -rot begin dup 0 > while : fill -rot begin dup 0 > while
>r 2dup c! char+ r> 1- repeat >r 2dup c! char+ r> 1- repeat
2drop drop ; 2drop drop ;

@ -40,17 +40,12 @@ constinit static sforth::native_word<"U.", [](auto) {
constinit static sforth::native_word<"EMIT", [](auto) { constinit static sforth::native_word<"EMIT", [](auto) {
std::cout << static_cast<char>(forth.pop()); std::cout << static_cast<char>(forth.pop());
}, &udot> emit; }, &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;
constinit static sforth::native_word<"INCLUDE", [](auto) { constinit static sforth::native_word<"INCLUDE", [](auto) {
const auto w = forth.parse(); const auto w = forth.parse();
std::string s {w.begin(), w.end()}; std::string s {w.begin(), w.end()};
std::ifstream file {s}; std::ifstream file {s};
parse_stream(forth, file); parse_stream(forth, file);
}, &type> incl; }, &emit> incl;
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {

Loading…
Cancel
Save