HOME Try Sather Post Messages

3. Playing with Sather


1. Introduction

I will explain haw to write Sather codes and to compile them in this section.

2. Hello World!

Let's start with "Hello World" as usual ([code 1]).
[code 1]
01:     -- a simple "Hello World" program
02:     
03:     class MAIN is
04:        main is
05:           #OUT +  "Hello World!\n";
06:        end;
07:     end;
You can compile and execute the source like as follows:
$ sacomp hello.sa -o hello
$ ./hello
Hello World!
You can compile several source files simultaneously by listing the files.
$ sacomp foo1.sa foo2.sa foo3.sa -o foo
The compiler name is sacomp. You can name the executable using the -o option. See Compiler Man Page about the options of sacomp. The compiler name used in the link is cs. Replace it to sacomp when you read the document.

Corresponding C source codes are produced by -output_C and -only_C options. You can compile the produced C codes for other platforms. In the case of win32+cygwin, you can make the executable by just calling make command.

The sabrowse is the Sather code browser, which shows the source and relations among classes as shown in figures 1 and 2. As Sather support multiple inheritance, the browser is useful to write large scale programs.

$ sabrowse hello.sa

Fig. 1. A Sather source code.


Fig. 2. The relation among classes.

3. Variables

3.1 Basic data types or classes

Following table shows basic data types.
Type Initial Value explanation
BOOL false boolean, true or false.
INT 0 signed integer, whose size is more than 32 bit.
INTI 0i arbitrary large integers
FLT 0.0 IEEE 754-1984 single format 32-bit floating point.
FLTD 0.0d IEEE 754-1984 double format 64-bit floating point.
CHAR '\0' Characters
STR "" Strings

3.2. Valuable declaration

The forms of variable declaration and assignment are like as follows:
variable name : data type; -- the reverse way from that of the C language
left_hand_side := right_hand_side

--
-- Example:
--

b0:BOOL := true;

ia:INT := 10;
ib:INT := -321;
ic:INT := 13_456_367_443;    -- You can insert '_' for easy reading
id:INT := 0b11001;           -- binary, equal to 25 of decimal base
ie:INT := 0o37323;           -- Oct
if:INT := 0x_ea_75_67;       -- Hex

ig:INTI := -4325i;           -- an arbitrary size integer
ih:INTI := 0o33784;

fa:FLT := 0.0;
fb:FLTD := 3.1345e-2d;

ca:CHAR := 'a';
cb:CHAR := '\n';               -- newline

sa:STR := "Hi";
You can declare several variables simultaneously if they have same type.
a,b,c :INT;
x,y,z :FLT;

3.3. ARRAY

Type ARRAY represents array.
                                          -- C style
a:ARRAY{INT}:=#(10);                      -- int a[10];
a[0] := 100;                              -- a[0] = 100;
i:INT := a[1];                            -- int i = a[1];

s:ARRAY{STR}:= |"foo", "bar", "baz"|;     -- char* s[] = {"foo", "bar", "baz"};
s.has("foo") → true

notes

  1. '#' is an operator to make a instance. The argument value 10 is the size of the ARRAY instance made here.
  2. Literals of ARRAY have form like | item1, item2, ..... |.
  3. ARRAY class has has(obj) method, which returns true if the array has obj.

4. Defining methods

[code 2] shows a simple program that calculate factorials.
[code 2]
01:     -- a simple factorial program
02:     
03:     class MAIN is
04:        main(av: ARRAY{STR}) is
05:           n:INT;
06:           if av.size = 2 then
07:     	 n := av[1].cursor.get_int;
08:           #OUT +  fact(n) + "\n";
09:           else
10:     	 #ERR + "Usage: fact N\n";
11:           end;
12:        end;
13:     
14:        fact(n:INT):INT is
15:           if n=0 then return 1;
16:           else return n * fact(n-1);
17:           end;
18:        end;
19:     end;
Following shows the form of defining method. You should give a method name, a list of arguments, and return data type.
method_name(parm1:TYPE1, parm2:TYPE2):RETURN_TYPE is
.......
.......
end;

notes

  1. The argument of the main function is ARRAY{STR} type.
  2. Use str.cursor.get_int to convert STR into INT.

5. Branching

Use if or case blocks for branching.

5.1. if block

Following shows the format of the if block
if if_test then
    if_body
elsif elsif_test then
    elsif_body
else
    else_body
end;
  1. You can omit elsif and else blocks.
  2. You can add elsif blocks as much as you need.
Example:
01:     -- example of if: iftest
02:     
03:     class MAIN is
04:        main(argv: ARRAY{STR}) is
05:           a1, s1: STR;
06:           flag : ARRAY{CHAR} := |'a', 'b', 'c'|;
07:           if argv.size = 2 then
08:     	 a1 := argv[1];
09:     	 if a1[0] = '-' then
10:     	    if flag.has(a1[1]) then  print_out(a1);
11:     	    else #OUT + "This option is not supported.\n";
12:     	    end;
13:     	 else show_usage;
14:     	 end;
15:           else show_usage;
16:           end;
17:        end;
18:     
19:        print_out(s:STR) is
20:           #OUT + "Option " + s + " is selected.\n"; 
21:        end;
22:     
23:        show_usage is
24:           #OUT + "Usage iftest -[abc]\n";
25:        end;
26:     end;
$ sacomp iftest.sa -o iftest
$ ./iftest -a
Option -a is selected.
$ ./iftest -d
This option is not supported.
$ ./iftest a
Usage iftest -[abc].

5.2. case blocks

Following is the form of the case blocks.
case parameter
when values_1 then
    procedure_1
when values_2 then
    procedure_2
else
    else_procedure
end;
  1. You can use several values after when.
  2. You can add the when blocks as much as you need.
  3. The else block is called if the value of parameter does not match all the rest.
The example above can be implemented more easily by using a case block.
01:     -- example of case: casetest
02:     
03:     class MAIN is
04:        main(argv: ARRAY{STR}) is
05:           a1, s1: STR;
06:     --       flag : ARRAY{CHAR} := |'a', 'b', 'c'|;
07:           if argv.size = 2 then
08:     	 a1 := argv[1];
09:     	 if a1[0] = '-' then
10:     --	    if flag.has(a1[1]) then  print_out(a1);
11:     	    case a1[1]
12:     	    when 'a', 'b', 'c' then print_out(a1);
13:     	    else #OUT + "This option is not supported.\n";
14:     	    end;
15:     	 else show_usage;
16:     	 end;
17:           else show_usage;
18:           end;
19:        end;
20:     
21:        print_out(s:STR) is
22:           #OUT + "Option " + s + " is selected.\n"; 
23:        end;
24:     
25:        show_usage is
26:           #OUT + "Usage iftest -[abc]\n";
27:        end;
28:     end;

6. Summary

I have talked about the syntax of Sather and how to compile Sather codes, quickly.

Next section is about the iterators.


HOME Try Sather Post Messages