The interactive mode

When the Shang executable program is invoked, a Shang session is launched. The initial mode of the session is an interactive programming environment, in which Shang commands are executed and answers displayed.

In the interactive mode the interpreter displays the prompt sign ``»'' to indicate that it is waiting for the user to enter a command.

>>
When the user types a command after the prompt and hits Enter, the interpreter will examine the command. If it contains no syntax error and can be carried out, the interpreter will perform some necessary calculations and show the result and display the prompt sign again.
>> cos(pi)
   -1
>>
The interpreter recognizes pi as a system defined global variable that stores the value of the mathematics constant , therefore evaluates cos(pi) to the cosine of . Otherwise, if it doesn't recognize a symbol, it will print an error message and wait for the next command.
>> cos(pi)
   -1
>> cos(PI)
   Error: line 2, symbol "PI" not defined
>>

A session can be viewed as a stack whose top level is the interactive mode. If user-defined functions are invoked, the session enters lower levels of the stack. The local variables defined in the interactive mode cannot be accessed on lower levels of the stack. Each level of the stack has its own work space to store local variables and won't interfere with each other. When a function call is finished, the work space is deleted and the active level of the stack is restored to the previous level. For example, during the execution of the following commands, the two variables both named x belong to different levels of stack and won't interfere with each other.

>> x = 10;
>> f = function y -> z
            x = sqrt(1 + y^2);
            z = 1 / x;
       end
>> f(10)
    0.09950371902
>> x
    10

oz 2009-12-22