Sunday, March 21, 2010

Some Forth string words

I have been experimenting with pForth, and composed some words to manipulate strings:

: str: create 0 , 0 , ;      \ create a string variable
: >str ( a n str -- ) 2! ;     \ store a string in a string variable
: str> ( str -- a n ) 2@ ;     \ retrieve a string onto the stack
: .str ( str -- ) str> type ;  \ print a string

\ example usage
str: greeting                  \ define a string 
s" hello world" greeting >str  \ set it to a simple string
greeting .str                  \ print the string

It's very Noddy, I know.

Saturday, March 20, 2010

PortablE

PortablE is an implementation of AmigaE for Windows, Amiga OS3, OS4, AROS & MorphOS. Using PEGCC, which included in the distribution, you can compile native executables. These executables run without needing to distribute additional libraries.

Wednesday, March 17, 2010

Chicken scheme installation report (mingw-msys)



The following is an installation report of attempting to compile
chicken from git using mingw-msys on 17-Mar-2010. The topmost entry in
the git log is:
commit 590e58d9cfaed30d1b79abcc8bab4c13b80691ce
Author: felix 
Date:   Fri Mar 12 07:51:21 2010 +0100
same for chicken-status and chicken-uninstall

Here are the steps I followed to create chicken:
git clone http://chicken.wiki.br/git/chicken-core.git
cd chicken-core
make PLATFORM=mingw-msys confclean
make PLATFORM=mingw-msys PREFIX=/usr/local bootstrap
make PLATFORM=mingw-msys PREFIX=/usr/local CHICKEN=./chicken-boot
make PLATFORM=mingw-msys PREFIX=/usr/local install

During the execution of the last command, it is reported:
Error: (directory) cannot open directory: "/usr/local/lib/chicken/5"
However, this doesn't really seem to be  a problem, because if I type:
ls /usr/local/lib/chicken/5
I obtain the listing:
chicken.import.so          posix.import.so           srfi-14.import.so
csi.import.so              regex.import.so           srfi-18.import.so
data-structures.import.so  scheme.import.so          srfi-4.import.so
extras.import.so           setup-api.import.so       srfi-69.import.so
files.import.so            setup-api.so              tcp.import.so
foreign.import.so          setup-download.import.so  types.db
irregex.import.so          setup-download.so         utils.import.so
lolevel.import.so          srfi-1.import.so
ports.import.so            srfi-13.import.so


The interpreter works OK. If I type:
csi
I am greeted with the prompt:
CHICKEN
(c)2008-2010 The Chicken Team
(c)2000-2007 Felix L. Winkelmann
Version 4.4.1
windows-mingw32-x86 [ manyargs dload ptables ]
compiled 2010-03-17 on EMEAGBRABSLT001 (MINGW32_NT-5.1)

Compilation is problematical, though. If I have the file hello.scm:
(write-line "chicken scheme says hello")
and type:
csc hello.scm
I obtain the response:
The system cannot find the path specified.
Error: shell command terminated with non-zero exit status 1: 
""\usr\local\bin\chicken.exe" "hello.scm" -output-file "hello.c""
The problem is due to the use of backslashes. 

It should be noted that I am typing the commands from MSYS shell,
where forward slashes are expected. If I run csi and csc from a normal
DOS shell, then I get the same responses. In DOS, although the
backslashes are correct, the filepath isn't. It's actually in:
C:\msys\1.0\local\bin

One philosophical problem to resolve is whether chicken development
should be done from a DOS shell, or a MSYS shell. I am inclined to say
the latter, as that is the normal development environment for MINGW.



Monday, March 15, 2010

block/return in scheme

Continuations in Scheme - they're not necessarily all that hard. You can use them to prematurely return from a function, as in the following example:

(call-with-current-continuation
(lambda (return)
(begin
(display "One ")
(return #t)
(display "Two ")
#f)))


In the above example, it would print "One ", and bail out by returning the value #t. The remainder of the lambda is never executed. Note that 'return' is a paramater, so you could choose whatever name you wanted to.

It is useful to have some syntactic sugar for the above. A standard name is 'let-cc', defined as follows:

(define-syntax let-cc
(syntax-rules ()
((let-cc variable . body)
(call-with-current-continuation
(lambda (variable) . body)))))


I can then type things more conveniently:

(let-cc return
(display "One ")
(return #t)
(display "Two ")
#f)


Note that my choice of use of the word 'return' is still arbitrary. It's very flexible, because I can have any number of 'named' blocked, and return to whatever part of the program I am interested in.

Suppose I want to give up some flexibility, and that I want 'return' to be part of my syntax. I want a block/return (similar to Lisp's block nil) construction. 'return' means that I want to return from the block. To put it in more concrete terms, I want to be able to write:

(block
(write-line "this is printed")
(return "this is returned")
(write-line "this is never printed"))


A macro which satisfies this is:

(define-syntax block
(lambda (x r c)
(let ((body (cdr x)))
`(call-with-current-continuation
(lambda (return) ,@body)))))


The above was written for Chicken Scheme. Other Schemes might not support it - it's certainly do-able in other schemes - but the implementation might be rather hairier (check out the cls link below, for example).

Further reading:

Sunday, March 14, 2010

Scheme from Scratch - Introduction

Scheme from Scratch - Introduction

The design and implementation of programming languages fascinates me. A programming language enables a text file to both accurately communicate to another person a problem’s solution and control a computer’s execution. The influence a language’s feature set has on the way a programmer thinks about problems and their solutions is an endlessly captivating idea. The world of programming languages is amazing.

I’m particularly interested in implementing higher-level languages in lower-level languages. I think I was attracted to Math in school because the construction of new ideas based on a small set of axioms seems like good, clear thinking. Building up to a high-level programming language from below has the same appeal for me.

chibi-scheme - Project Hosting on Google Code

chibi-scheme - Project Hosting on Google Code

Chibi-Scheme is a very small but mostly complete R5RS Scheme implementation using a reasonably fast custom VM. Chibi-Scheme tries as much as possible not to trade its small size by cutting corners, and provides full continuations, both low and high-level hygienic macros based on syntactic-closures, string ports and exceptions. Chibi-Scheme is written in highly portable C and supports multiple VM instances running simultaneously.