McCal Editor is designed for editing McCal Script. It is activated from Function Viewer Source View.
McCal Editor has a similar keypad to that of McCal calculator, but some differeces exist. The editor has the
button for carriage return, instead of . It also has "up" and "down" cursor keys.At the actionbar for McCal Editor, there are icons for starting McCal Viewer and Function Viewer. These viewers are read-only: users cannot change, for example, variables and macros.
McCal Script is a simple script language built in McCal Calculator. It is designed for small footprint and automatic detection of data types.
McCal supports many data types: Integer, Floating point, Complex number, and String. It also handles arrays consisting of any combination of those types. Automatic detection of data types are used for optimal calculation.
Integers are 64bit wide, and floating point numbers are double precision (64-bit). Results of calculation between two integers are integers when possible, but converted to floating point numbers if necessary.
Addition is the only operation allowed for strings. The addition of two strings results in the joined string. Addition of a string and a number results in the joined string after converting the number to the corresponding string.
Comparison operators return 0 for false, and 1 for true.
A McCal Script source program is a synthesis of literal numbers, variables, operators, functions, and reserved words. The McCal Script interpreter first breaks the given source code down to meaningful tokens (or words), and evaluates the code.
As shown below, the first character of a token is important for tokenization.
token type | format | Examples | Decimal number | integer and floating point | 100.0, 1E-8 (equal to 1×10-8) | Binary number | 0b(prefix)+[0-1], integer-only | 0b10011001 | Octal number | 0(prefix)+[0-7], integer-only | 0377 | Hexadecimal number | 0x(prefix)+[0-9A-Fa-f] | 0xFFFF, 0x7f, 0x0.1p-6 (equals to 1×16-1x2-6) | String | enclosed by "" | "Hello, World!" | Variable | lower-case character | a, b, z | Global variable | @(prefix)+alphanumeric word | @length, @1stName, @a | Operator | (non-alphanumeric) | +, ×, >=, && | Function | #Package.function(a,r,g,s) | #sin(x), #Myprog.test(10,100) | Reserved word | Capitalized word | For, Print, Dim |
Any type of data listed above is stored in a variable. In McCal Script, no need for declaration of data types.
Unlike other programming languages, variable is designated by a single character, such as 'a' and 'x'.
Multiplication operator (*) can be omitted. In the example below, 'abc' and 'a*b*c' return the same result.
a=2; b=4; c=-2 ▶ -2 abc ▶ -16 a*b*c ▶ -16 a(b+c) ▶ 4
Please note that a variable with an upper case character sometimes causes a problem with hidden multiplication operator.
AB
In the above example, McCal reports an error complaining that there is no keyward "AB" in the system.
A*B A B
These two cases return the muliplication product of 'A' and 'B'.
For global variables, you can use words by adding a '@' character.
@1stName = "John"; @2ndName="Doe" ▶ "Doe" @1stName + " " + @2ndName ▶ "John Doe"
McCal Viewer only shows global variables.
In the present implementation, up to three-dimensional arrays are supported.
McCal can handle mathematical Matrix calculations, such as addition, subtraction, and multiplication, using two-dimensional arrays.
A user can reserve the space for an array with predefined size, using the "Dim" keyword. In the example below, one-, two-, and three dimensional arrays are declared. Each element is set to integer zero.
Dim a[100] Dim a[100][3] Dim a[100][3][2]
In "Dim" construct, it is possible to initialize the array using brackets and values, as shown below. Without size, the array has the same size of the initializing data.
Dim a=[[0.5,0],[0,2]] ▶ array[2][2]
Dim a[100][2]=[[0.5,0],[0,2]] ▶ array[100][2] Dim a[2][2]=[[1,0,3],[0,2,4]] ▶ array[2][2]
McCal operators are similar to those in Java or C, but some are McCal-specific.
McCal-specific operators are the power operator (^), and the factorial operator (!). In Java or C, those characters are used for exclusive OR and logical NOT, respectively. McCal, thus, uses different characters for exclusive OR (|^) and logical NOT (!!).
Priority | Operator | Format | Name | Connection |
---|---|---|---|---|
0 | () | (y) | Parenthesis | |
1 | () | x(y) | function argument list | left |
[] | x[y] | Array index | left | |
2 | ! | x! | factorial operator | left |
3 | ^ | x^y | power operator | left |
4 | + | +x | unary + operator | right |
- | -x | unary - operator | right | |
!! | !!x | logical NOT | right | |
5 | xy | omitted binomial multiplication | right | |
6 | * | x * y | binomial × (multiplication) | left |
/ | x / y | binomial ÷ (division) | left | |
% | x % y | modulus | left | |
7 | + | x + y | binomial + (addition) | left |
- | x - y | binomial - (subtraction) | left | |
8 | < | x < y | larger than comparison | left |
<= | x <= y | larger or equal comparison | left | |
> | x > y | greater than comparison | left | |
>= | x >= y | greater or equal comparison | left | |
9 | == | x == y | equal comparison | left |
!= | x != y | not equal comparison | left | |
10 | & | x & y | bitwise AND | left |
11 | |^ | x |^ y | bitwise exclusive OR | left |
12 | | | x | y | bitwise OR | left |
13 | && | x && y | logical AND | left |
14 | || | x || y | logical OR | left |
15 | = | x = y | assignment operator | right |
⇒ | ⇒ y | post assignment | right | |
16 | , | x , y | comma operator | left |
There are two types of function in McCal: system built-in and user-defined.
McCal Viewer groups related system functions into a 'Category' for easy access, whereas a set of related user-defined functions can be put together into a 'Package'.
A function name starts with '#'. The following table shows the naming convention for system- and user defined- functions.
Function name example | Description |
---|---|
#sin(x) | System function. A system function name starts with a lower case character. |
#Pkg.fn(x) | User-defined function. Its name consisits of a capitalized package name and a function name, separated by a period. |
System functions has no package name.
McCal Script supports BASIC-like conditional branching and loop controls. These constructs encompass multiple lines, and supported only inside of a function, not calculator main window.
If statement
If a>0 Then Print "a is positive" ElseIf a == 0 Then Print "a is zero" Else Print "a is negative" EndIf
For statement
Dim a[20] For c=0 to 19 Step 2 a[c] = "No." + c Next
Do ~ Loop While, Do ~ Until statement
Dim a[20][2] c = 0 Do a[c][0] = "square of " + c c=c+1 Loop While c < 20 c = 0 Do a[c][1] = c^2 c=c+1 Loop Untile c == 20
Do While ~ Loop, Do Until ~ Loop statement
Dim a[20][2] c = 0 Do While c < 20 a[c][0] = "square of " + c c=c+1 Loop c = 0 Do Until c == 20 a[c][1] = c^2 c=c+1 Loop
Exit statement
Forcing exit of the inner-most loop
Continue statementSkip to the end of a loop
Return statementExits from the function, and returns the value(s) of the statement.
It's possible to return multiple values using a "Return" statement.
Return a, b, c
These returned values are catched using a variable list, shown below.
[x,y,z] = #Example.returnTest(A) [x,y] = #Example.returnTest(A) x = #Example.returnTest(A)
In the second and the third example above, some of the values in the "Return" statement are discarded. 'x' gets the first value ('a'9 in the "Return" statement in this example.
[x,y,z,a] = #Example.returnTest(A)
When the number of variables in the list exceeds the number of returned values, McCal reports an error.
The list of words reserved for declaration, command, and flow control.
Plese note that these words are capitalized.
Word | Example | Description |
Func | Func #Pkg.fn(x,y) {...} | Declaration of a user-defined function |
Dim | Dim a[100][2] | Reserves the space for the given size. |
Print a, a+b | Print the values | |
Println | Println a, a+b | Print the values and a new line character. |
If, Then, ElseIf, Else, EndIf | If a>0 Then ... ElseIf a==0 Then ... Else ... EndIf | conditional branching. 'ElseIf' and 'Else' lines are optional. |
For, To, Step, Next | For k=1 To 10 Step 2 ... Next | For loop. 'Step' is optional. |
Do, While, Until, Loop | (See 'Flow control of execution') | While, Until loop |
Break | Break | Exit the inner-most loop |
Continue | Continue | Skip to the end of the loop. |
Return | Return [expression] | Exits the function, and returns the value. |
The scope of variables is explained in this chapter.
In the McCal calculator main window, variables are treated as 'global', and stored in a special area called global name space. McCal Viewer can see only global variables.
When a function is called, values are passed to the function (call by value). Each function call leads to the creation of an isolated local variable name space. The local variables are accessible only from the code of the respective function.
In the above example,
(1) values of two arguments,'1' and 'a+1', are calculated and the resulting values are passed to the function (Func1).
(2) At the Func1 side, a region for local variables for each function-call is allocated,
(3) then the local variables in the argument list, 'a' and 'b', are created.
(4) Finally, the passed values are copied to them.
(5) Execution of the code of the function starts.
When a variable name is referenced in a function, McCal searches the variable name in the local variable region. Thus, these local variables are independent from those in the global name space, and invisible from outside.
McCal has a mechanism to access the global variables from the inside of a user function. When '@' (scope modifier) is placed before a variable name, McCal assumes the variable is a member of the global name place. For instance, '@c' referes to the global variable 'c', and the global variable is, in this way, readabe/writable from the inside of a function.
When Func1() is finished, its local variables are removed from the system. To save any calculated values in Func1, the user needs to 'Return' a value, or to write values into global variables, although the latter is not recommended due to unintended side-effects.
The repetitive functions, such as #tiems and #while explained in chapter 1.13, takes one or more code blocks. McCal uses the caller's name space for evaluation of the code blocks. Even the counter varable 'k' belongs to the caller's name space.