Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

Expressions

Jonathan50 edited this page Oct 17, 2018 · 13 revisions

A program in do-it is made of a sequence of expressions.

Do-it has these three types of expressions:

  • Self-evaluating expressions
  • Variable references
  • Procedure applications
  • Special forms

Self-evaluating expressions

A self-evaluating expression is a constant. Currently the only self-evaluating expressions are:

  • Fixnums (do-it currently doesn't bother to check that an integer will fit in a fixnum…), e.g. 4
  • Booleans, i.e. #t for true or #f for false
  • Characters, e.g. #\a
  • Strings of characters, e.g. "Hello, world!"

Variable references

A variable reference is simply a symbol (identifier) that is the name of the variable to be referenced.

Note: Variables in do-it only have dynamic extent.

Procedure applications

A procedure application takes the form (<operator> <operands>). <operator> is a symbol that is the name of a procedure. (Like LISP, do-it has separate namespaces for variables and procedures.) The <operands> can be any type of expression, and their values are passed as arguments to the procedure.

Do-it is call-by-value, so all the arguments are evaluated, then pushed onto the stack, and then the procedure is called.

Special forms

Most of the expression types in do-it are special forms. Special forms' "arguments" do not have to be expressions to be evaluated, and may be evaluated more than once.

(quote <datum>)

'<datum>

Quote returns <datum> without evaluating it. (Note that not all objects that Scheme can read are supported by do-it at runtime, currently only the same objects that are self-evaluating. Actually the only type of object in do-it is a machine word.) The second form is an abbreviation for the first form.

(begin <body>)

Begin evaluates all the expressions in <body> sequentially and returns the value of the last one.

(if <test> <consequent> <alternative>)

If <test> evaluates to #f or zero, then if <alternative> present it is evaluated, and if it is not present nothing happens. If <test> evaluates to #t, then <consequent> is evaluated.

(while <test> <body>)

While repeatedly evaluates <body> (as by begin, in fact the compiler adds a begin around the body), as long as <test> evaluates to true.

(return <expression>)

Return immediately exits from the procedure or program, returning the result of <expression> if it is present.

(defproc <name> (<parameters>) <body>)

Defproc defines a procedure, like LISP's defun. (Like LISP, do-it has seperate namespaces for variables and procedures.) It creates a procedure named <name>. When the procedure is called, the <parameters> are bound to their respective arguments and the <body> is executed as by begin. The parameters and all variables bound by the procedure are local to the procedure.

Note: defproc is (currently) only for use at the top level. Don't use it inside procedures.

(procedure <name>)

Procedure returns the address of the procedure named <name>, which can be called with the special form call.

(call <procedure pointer> <operands>)

Call evaluates <procedure pointer> and <operands>, then applies the procedure the value of <procedure pointer> points to to the values of the <operands>.

(defmacro <name> <parameters> <body>)

Define a macro named <name>, written in Scheme. Afterwards, when an expression like (<name> <datum> ...) is encountered, <body> is evaluated as Scheme code by the Scheme interpreter do-it is running on, with the <parameters> bound to the <datums>.

(defvar <name> <init>)

Defvar defines the variable <name>, and sets it to <init> if it is present. Note: Variables in do-it only have dynamic extent.

(set <name> <expression>)

Set is the assignment operator. It replaces the value of the variable <name> with the value of <expression>.

(for <init> <test> <step> <body>)

This is the same as

(begin
  <init>
  (while <test>
    <body>
    <step>))

e.g.

(for (set i 0) (<= i 20) (set i (+ i 1))
  (printf "%d" i)
  (newline))

will print all the integers from 0 to 20 (inclusive).