Luke Gorrie (lukego) wrote,
Luke Gorrie

ed, man!

Yesterday I built an OLPC firmware image for the first time. It's easy and rumoured to be fairly safe, all you have to do is type this line from the top of the svn tree:
cd ./cpu/x86/pc/olpc/build; make clean; make
The scrolling speed is gratifyingly fast now that Mitch Bradley committed my Geode accelleration patch! (He rewrote it before doing so, but I'm a newbie so that's to be expected :-))

Now I'm trying to setup a self-sufficient Forth development environment on the XO. I'm quite well adjusted to the keyboard by now so there's no good reason to lug this Macbook around from cafe to cafe just to run Emacs.

The first thing I need is a convenient and persistent way to edit Forth programs on the XO. The programs should be stored on the 2GB SD-memory card that I added so that they're not overwritten when I install a new OLPC software image on the 1GB internal NAND flash. Mitch Bradley already sent me a lot of pointers to get started with file system access (I've put these on the OLPC Wiki as draft Forth Lesson #13) so it's only a matter of building some convenient "editing words" on top.

So I've hacked together something that I'm pretty happy with. Now I write ed foo and the openfirmware text editor (which is minimal but comfortably Emacsish) pops up with the contents of sd:\foo.fth. I edit and then press ^C to finish and optionally save the changes. If I want to evaluate the most recently edited source file I just type ev.

Here's the code:

0 0   2value   ed-file      \ filename being edited
10000 constant ed-maxsize   \ buffer size (max)
0     value    ed-size      \ amount of text in buffer
      create   ed-buf  ed-maxsize allot

: read-content ( adr len name$ -- sz )
   r/o open-file  if  0  else  dup >r  fgets  r> fclose  then
: write-content ( content$ name$ -- )
   2dup $delete-all  $create-file >r  " write" r@ $call-method  r> close-dev

: name>file ( $n -- $fn ) " sd:\" 2swap " .fth"  $cat2 $cat2 ;
: edit-string [ also hidden ] edit-file [ previous ] ;

: ed$   ( -- s$ ) ed-buf ed-size ;
: ed-read  ( -- ) ed-buf ed-maxsize ed-file read-content  is ed-size ;
: ed-write ( -- ) ed$ ed-file write-content ;
: ed-save? ( -- ) " Save?" confirmed? if ed-write then ;
: ed-edit  ( -- ) ed$ ed-maxsize edit-string is ed-size ;
: ed \ name ( -- )
   safe-parse-word name>file is ed-file
   ed-read ed-edit ed-save?
: re-ed ( -- ) ed-edit ed-save? ;
: ev    ( -- ) ed$ eval ;
This feels pretty good so far. I would like to know a way so that I could re-evaluate this source file to pick up new definitions without reallocating all the variables, so that I could preserve the existing state (like DEFVAR vs DEFPARAMETER in Lisp). I don't really understand how Forth people do interactive redefinition of functions yet.

Next target: reading and referencing openfirmware sources without the Macbook.

Tags: forth, olpc
  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.