首页 > 解决方案 > 如何在 GForth 中保存程序/单词

问题描述

有没有办法将我定义的单词保存到文件中,以便以后继续试验?

到目前为止,我找到了从控制台复制+粘贴定义的唯一方法,如果它们仍然可见的话。


我是从头开始的,所以我犯了很多错误并在以后更正它们,我想保存我之前定义的单词并在下一个会话中重复使用它们。

这是一个简单的例子:

Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
:hello ."hello"; 
:1: Undefined word
>>>:hello<<< ."hello";
Backtrace:
$7F29BBAB3A00 throw 
$7F29BBAC9C98 no.extensions 
$7F29BBAB3CC0 interpreter-notfound1 
: hello ."hello"; 
:2: Undefined word
: hello >>>."hello";<<<
Backtrace:
$7F29BBAB3A00 throw 
$7F29BBAC9C20 no.extensions 
$7F29BBAB7338 compiler-notfound1 
: hello ." hello";  ok
: 2hello hello hello ;  ok
2hello  hellohello ok
: hello ." hello "; \ added space to end redefined hello   ok
2hello  hellohello ok                   
: 2hello hello hello ; redefined 2hello   ok
2hello  hello hello  ok
bye

现在我有工作hello(最后有空格)和工作2hello(使用编辑hello) - 可以说,我有更多的麻烦来解决2hello定义形式和定义hello现在不在屏幕上。

  1. 有没有办法保存hello并保存2hello到文件中,我可以在第二天使用它来制作更复杂的单词?
  2. 它可以是文本文件吗,所以我可以使用一些编辑器(比如vim)来清理所有错误的定义并评论那些我想保留的?

我想以文件welcome.fth 结束:

: hello \ -- ; say hello and space at the end of word, to be able simply concatenate that 
    ." hello ";
: 2hello \ -- ; repeats hello two times
    hello hello ; 

第二天就能来,继续发现

Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
include welcome.fth  ok
2hello  hello hello  ok
\ now I can continue with learning

标签: gforth

解决方案


首先,gforth 将其历史记录保存在 ~/.gforth-history 文件中,任何文本编辑器都可以获取该文件以供我写作

其次,可以使用向上箭头访问历史记录,并使用编辑器保存文件,我可以爬回去并将我的输入保存到文件中。

GNU FORTH 有编辑器吗?

只需将位置设置0 t为第一行,将我的代码的最后一行添加到那里,在 as 之前插入新行i并以这种方式填写所有其他行重新开始。insertilinsert line

然后flush将缓冲区写入磁盘,下次使用此文件use test.bl并使用0 load.

(或在编辑器中将块文件转换为普通文本include test.bl

我可以在不离开 gforth 的情况下做到这一点,所以它对我有用。

(最终我可以以某种方式改进它,因为它也写在了 :)


推荐阅读