[http://pysch.sourceforge.net/] [index.html] [user.html] [developer.html] [ast.html]

Pysch: user's guide

S-expressions as XML

Pysch uses XML format to represent s-expressions. There are 12 elements in the format:

Examples:

(a b c)               <===>
  <li><sy>a</sy> <sy>b</sy> <sy>c</sy></li>
('a b . c)            <===>
  <li><li><sy>quote</sy> <sy>a</sy></li> <sy>b</sy> <do/> <sy>c</sy></li>
#(1 #\x "abc" #(#t))  <===>
  <ve><nu>1</nu> <ch>x</ch> <st>abc</st> <ve><bo>#t</bo></ve></ve>

Converting a Scheme program to XML

A program should be simplified and converted to XML before executing by Pysch. This task is performed by the script compiler.scm in the folder compiler. There are also wrappers run.sh and run2.sh.

Consider the sample program hello.scm in the folder compiler:

(let ((msg "Hello, World!\n"))
  (display msg))

In order to convert the code to XML, execute

$ cat hello.scm | bigloo -i compiler.scm

or

$ ./run.sh hello.scm

The script run.sh is a wrapper to the first command. In both cases the output looks so:

=====================
I: (let ((msg "Hello, World!\n")) (display msg))
;
<li><sy>let</sy><li><li><sy>msg</sy><st>Hello, World!&#xa;</st>\
  </li></li><li><sy>display</sy><sy>msg</sy></li></li>
;
;
;
((lambda (msg) (begin (display msg))) (begin "Hello, World!\n"))
C: <li><li><sy>lambda</sy><li><sy>msg</sy></li><li><sy>begin</sy>\
  <li><sy>display</sy><sy>msg</sy></li></li></li>\
  <li><sy>begin</sy><st>Hello, World!&#xa;</st></li></li>

The compiler.scm works as follows. It sequentially reads Scheme forms from the standard input. It prints a line of symbols “=” when starting a new form. The line with the prefix “I:” is the current input form. This line is followed by an XML representation of the current form. The compiler.sh expands the form and prints the result after the several empty comment lines. Then it starts a line with the “C:” prefix and prints the result as XML.

The run.sh is verbose and used to trace expansion. In order to make executable XML use another script, run2.sh:

$ ./run2.sh hello.scm >hello.xml
$ cat hello.xml 
<li><sy>begin</sy>
<li><li><sy>lambda</sy><li><sy>msg</sy></li><li><sy>begin</sy>\
  <li><sy>display</sy><sy>msg</sy></li></li></li>\
  <li><sy>begin</sy><st>Hello, World!&#xa;</st></li></li>
</li>

The run2.sh prints expanded forms as XML and puts them into the form “begin”.

REPL loop

REPL (Read Eval Print Loop) is a Scheme top-level program which allows interactive evaluation of Scheme expressions.

Pysch provides a very limited version of the REPL.

The REPL is provided by the program repl.py in the folder pysch. The program expects one of the letters as an argument: “s” (show), “c” (compile) and “e” (evaluate).

The action “s” reads an XML representation of a s-expression and prints back the s-expression itslef.

$ echo -n '<li><sy>quote</sy><sy>a</sy></li>' | python repl.py s
pysch>  (quote a)

The action “c” compiles an expression to an internal representation and prints a result.

$ echo -n '<li><sy>quote</sy><sy>a</sy></li>' | python repl.py c
pysch>  (constant a (halt))

The action “e” executes an expression and prints a result.

$ echo -n '<li><sy>quote</sy><sy>a</sy></li>' | python repl.py e
pysch>  a

Running a program and examples

The program eval.py in the folder pysch/pysch/vm can run an XML code produced by the script run2.sh:

$ cat ../../../compiler/hello.xml | python eval.py
Hello, World!
#unspecified

The string “Hello, World!” is the output from the program and the string “#unspecified” is the result of evaluation of the program.

There is also a set of examples. The folder examples contains:

These examples are ready to run using the make command:

$ make
cat ack.xml | (cd ../pysch/pysch/vm; python eval.py)
Ackermann(3,4) is 125
#unspecified
cat fac.xml | (cd ../pysch/pysch/vm; python eval.py)
Factorial of 7 is 5040
#unspecified
cat fib.xml | (cd ../pysch/pysch/vm; python eval.py)
10th Fibonacci number is 55
#unspecified

Evaluation of the Ackermann's function may take a while.

There is also an example of execution of a complex program. The folder doc contains the documentation for Pysch. The source of the documentation are Scheme files and SXSLT code for producing HTML. Pysch interprets SXSLT and so generates the documentation in the HTML format. You can run the generation process using the make command. It may take a while.


Author: Oleg Paraschenko
Hosted by:
SourceForge.net Logo