Table of Contents
You can invoke the Gamma engine by typing
[sh]$ gamma
at the shell prompt. (If you are using Gamma with Photon, you should type phgamma.)
It will return the following Gamma prompt:
Gamma>
Now you can start writing instructions to Gamma and get an immediate response. If you define a variable a without assigning it a value, Gamma will respond with the message that the symbol is undefined and suggest debugging:
Gamma> a;
Symbol is undefined: a
debug 1>
Type Ctrl - D to return to the Gamma prompt and assign a a value:
Gamma> a = 5;
5
Gamma>
This time Gamma responds with the value assigned to the variable. In Gamma a variable must be assigned a value. The library function undefined_p can be used to test if a variable is defined:
Gamma> undefined_p (b); t Gamma> b = 1; 1 Gamma> undefined_p (b); nil Gamma>
This function returns t for true and nil for false. The objects t and nil are discussed in more detail in the Logical Types section of the Basic Data Types and Mechanisms chapter.
A function is defined in Gamma using a function statement:
Gamma> function MyFunc (a) { a *= 10;}
(defun MyFunc (a) (*= a 10))
Gamma>
Gamma returns the function definition in Lisp syntax. It reflects the fact that Gamma is using the Lisp engine internally. Basically, Lisp displays functions as lists, surrounded by parentheses. The first word in every Gamma function definition is defun because that is the Lisp function for defining functions. After that, the function name is listed, followed by its arguments and code, which is also in Lisp format.
The MyFunc function called with 12 as its argument will return the value 120, as follows:
Gamma> MyFunc (12);
120
Gamma>
Notice that no type specification is used. An important feature of Gamma is that it is an abstractly typed language, making it unnecessary to specify the type of a data object in order to be able to use it. This does not mean that objects do not have types, but rather the system does not require that the type of an object be known until the code actually executes. This allows a function to return entirely different types, depending on what the calculation produces.
For example, a MIN function could be defined as:
function min (x, y) { if (x < y) x; else y; }
This function will return an integer or floating point number depending on the types of the arguments. The arguments do not need to be the same type. Gamma automatically type-casts them, favoring the smallest possible memory allocation. However, the less-than comparison will fail if both arguments are not numeric types.
Finally, return statements are not necessary in Gamma. Looking at the function MyFunc, we see it returns 120, the result of multiplying a by 10. Any Gamma function that executes successfully always returns a value, which is the result of evaluating the last expression in the function. This return value determines the value and type of the function.
This topic is discussed in greater detail in Function Definitions in the Functions and Program Structure chapter.
Copyright © 1995-2010 by Cogent Real-Time Systems, Inc. All rights reserved.