Simula Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is Simula?


Simulation Programming Language.


Released on 1962.


Superset of Algol 60.


Considered the first Object-Oriented Programming Language.


How to install Simula


We're going to use GNU Cim...a "c" implementation of the "Simula 67" programming language.

Here's how to download and install GNU Cim


Who uses Simula?


  • Old language...so no really used today...
  • Mostly used in academic and research environments
  • Influenced languages like C++, Java and Object Pascal

Starting out...


Simula doesn't have a REPL :(

Anyways...simply type "cim"

Comments


Comments are very simple

				
! This is a comment;
				

Yep...comments need an ending ";"

Variables


We need to specify the data type


Assignment is not the same for integers and strings


				
begin

    integer myVar;
    
    integer myConst = 5;
    
    text myText;
    
	
    myVar := 4;
    
!   myConst := 4  Error! Const cannot be changed;

    myText :- "Hello";
    
end
				

Conditionals


In Simula, the "if" statement is very simple...

				
begin

    integer int1;
    
    int1 := 3;
    
    if Int1 = 2 then OutText("Yes")
    
                else OutText("No");
                
end
				

If more than one line is needed

we can use a "BEGIN...END;"

Loops

We have "IF" and "WHILE" available

				
begin

    integer i;
    
    for i := 1 step 1 until 5 do
    
        begin
        
            OutInt(i,1);
            
            OutImage;
            
        end;
				

OutInt prints an integer, and the extra parameters is for spaces before printing...

OutImage prints an empty line...

				
begin

    integer i;
		
    while i < 6 do
    
        begin
        
            OutInt(i,1);
            
            OutImage;
            
            i := i + 1;		
            
        end;
        
end
				

Procedures


Procedures do not return a value

				
begin

    procedure sum(a, b);
    
    integer a, b;
    
    begin
    
        OutInt(a + b, 1);
        
    end;
	
	
    sum(2,3);
    
end
				

Functions


Functions can return a value...via the function name

				
begin

    integer result;
    

    integer procedure sum(a, b);
    
    integer a, b;
    
    begin
    
        sum := a + b;
        
    end;
    
	
    result := sum(2,3);
    
    OutInt(result,1);
    
end
				

Arrays


Arrays are fixed in size

Index start on 1

				
begin

    text array myArray(1:3);
    
    myArray(1) := "a";
    
    myArray(2) := "b";
    
end
				

Classes


Classes are declared differently from other languages...

				
begin

    class People(sName, iAge); 
    
    text sName; integer iAge;
    
    begin
    
        procedure Print;
        
        begin
        
            OutText("My name is ");
            
            OutText(sName);
            
            OutText(" and I'm ");
            
            OutInt(iAge, 2);
            
            OutText(" years old.");
            
        end of Print;
        
    end of People;
				

					
    ref(People) Person;
    
    Person :- new People("Blag",42);
    
    Person.Print;
    
end
				

This will simply print "My name is Blag and I'm 42 years old"...

Strings


The aren't too many String functions...

				
begin

    text myText;
    
    myText :- "This is a string";
    
    OutInt(myText.Length,2);
    
    OutImage;
    
    OutText(myText.sub(11,6));
    
end
				

First, we print how many characters do our String have...

Then we print a substring, which is in this case "string"

Fibonacci list sequence


Finally...we're going to make our first application...


Name your file "Fibonacci.sim"


we can run it by calling cim nameoffile.sim


				
begin

    integer num;
    
    text result;
    

    procedure fib(num, a, b);
    
    integer num, a, b;
    
    begin
    
        if a > 0 and num > 1 then
        
            begin
            
                OutInt(a+b,3);
                
                fib(num - 1, a + b, a);
                	
            end;
				
				
        if a = 0 then
        
            begin
            
                OutInt(a,3);
                
                OutInt(b,3);
                
                OutInt(a+b,3);
                
                fib(num - 1, a + b, b);
            end;
            
    end;
	
	
    OutText("Enter a number: ");
    
    OutImage;
    
    num := InInt;
    
    fib(num,0,1);
    
    OutImage;
    
end
				

When we run it we're going to see...


Making an LCD Number App


This is my favorite application of all time...


Name your file "LED_Numbers.sim"


				
begin

    integer i, iNum;
    
    text Num, Line1, Line2, Line3, val;
    
    text array Led(1:10);
    

    Led(1) :- " _  | | |_| ";
    
    Led(2) :- "    |   |   ";
    
    Led(3) :- " _   _| |_  ";
    
    Led(4) :- " _   _|  _| ";
    
    Led(5) :- "    |_|   | ";
    
    Led(6) :- " _  |_   _| ";
    
    Led(7) :- " _  |_  |_| ";
    
    Led(8) :- " _    |   | ";
    
    Led(9) :- " _  |_| |_| ";
    
    Led(10) :- " _  |_|  _| ";
				
				
    i := 1;


    OutText("Enter a number: ");
    
    OutImage;
    
    InImage;
    
    inspect SysIn do
    
    begin
    
        Num :- Blanks(Image.Length);
        
        Num := Image
        
    end;
    
    Num :- Num.Strip;
    
	
    while i <= Num.Length do
    
        begin
        
            iNum := Num.sub(i,1).GetInt + 1;
				
				        
            val :- Led(iNum);
            
            Line1 :- Line1 & val.sub(1,4);
            
            Line2 :- Line2 & val.sub(5,4);
            
            Line3 :- Line3 & val.sub(9,4);
            
            i := i + 1;
            
        end;
        
	
    OutText(Line1);
    
    OutImage;
    
    OutText(Line2);
    
    OutImage;
    
    OutText(Line3);
    
    OutImage;
    
    OutImage;	
    
end
				

When we run it we're going to see...

Decimal to Romans


This App will create a Roman Numeral based on a decimal number


This will include some nice commands...


Name your file "Decimal_to_Roman.sim"


				
begin

    Boolean break;
    
    integer num, i;
    
    integer array keys(1:13);
    
    text array values(1:13);
    
	
    keys(1) := 1000; keys(2) := 900; keys(3) := 500;
    
    keys(4) := 400; keys(5) := 100; keys(6) := 90;
    
    keys(7) := 50; keys(8) := 40; keys(9) := 10;
    
    keys(10):= 9; keys(11) := 5; keys(12) := 4; 
    
    keys(13) := 1;
				
				 
    values(1) :- "M"; values(2) :- "CM"; 
    
    values(3) :- "D"; values(4) :- "LD"; 
    
    values(5) :- "C"; values(6) :- "XC";
    
    values(7) :- "L"; values(8) :- "CL"; 
    
    values(9) :- "X"; values(10) :- "IX"; 
    
    values(11) :- "V"; values(12) :- "IV";
    
    values(13) :- "I";
    
	
    OutText("Enter a number: ");
    
    OutImage;
    
    num := InInt;
				
				
    while num > 0 do
    
        begin
        
            Break := True;
            
            for i := 1 step 1 until 13 do
            
                begin
                
                    if num >= keys(i) and Break then
                    
                        begin
                        
                            OutText(values(i));
                            
                            num := num - keys(i);
                            
                            Break := False;
                            
                        end;
                        
                end;
                
       end;
       
end
				

When we run it we're going to see...

Random Names


This App will generate 100,000 10,000 1000 random pseudo-random names using two 16 elements arrays


We will measure the runtime.


Although keep in mind that is a C app...not a real Simula app.


Name your file "Random_Names.sim"


				
begin

    integer seed, ans, counter, i;
    
    integer aux_seed, aux_ans;
    
	
    text array Names(1:16);
    
    text array Last_Names(1:16);
    
    text array Full_Names(1:1000);
    
	
    seed := 0; ans := 0; counter := 0;
    
    aux_seed := 0; aux_ans := 0;
    
	
    Names(1) :- "Anne"; Names(2) :- "Gigi"; 
    
    Names(3) :- "Blag"; Names(4) :- "Juergen"; 
    
    Names(5) :- "Marek"; Names(6) :- "Ingo";
    
    Names(7) :- "Lars"; Names(8) :- "Julia"; 
				
				
    Names(9) :- "Danielle"; Names(10) :- "Rocky"; 
    
    Names(11) :- "Julien"; Names(12) :- "Uwe";
    
    Names(13) :- "Myles"; Names(14) :- "Mike"; 
    
    Names(15) :- "Steven"; Names(16) :- "Fanny";
    
	
    Last_Names(1) :- "Hardy"; Last_Names(2) :- "Read"; 
    
    Last_Names(3) :- "Tejada"; Last_Names(4) :- "Schmerder"; 
    
    Last_Names(5) :- "Kowalkiewicz"; Last_Names(6) :- "Sauerzapf";
    
    Last_Names(7) :- "Karg"; Last_Names(8) :- "Satsuta"; 
    
    Last_Names(9) :- "Keene"; Last_Names(10) :- "Ongkowidjojo"; 
    
    Last_Names(11) :- "Vayssiere"; Last_Names(12) :- "Kylau";
    
    Last_Names(13) :- "Fenlon"; Last_Names(14) :- "Flynn"; 
    
    Last_Names(15) :- "Taylor"; Last_Names(16) :- "Tan";
				
				
    for i := 1 step 1 until 1000 do
    
    begin
    
        counter := i;
        
        seed := 7 * MOD(counter,101);
        
        ans := MOD((seed - 1),16) + 1;
        
        aux_seed := 7 * MOD(counter,4);
        
        aux_ans := MOD((aux_seed - 1),16) + 1;
        
        Full_Names(i) :- Names(ans) & " " & Last_Names(aux_ans);
        
    end;
    
	
end 
				

When we run it we're going to see...


Pretty fast...for a C app...

The idea was to use 100,000 items...but an overflow was produced...so only 1000 was allowed -:(

Counting Letters


In this example we're going to read a file and count how many time a letter appears...


Call your file "countletters.sim".


Create a file called "readme.txt" with the following text...


"This is a text file that we're going to read using Simula"


				
begin

    Boolean break;
    
    ref(InFile) Inf;
    
    text T;
    
    text array Letters(1:60);
    
    integer array Counter(1:60);
    
    integer i, ind;
    
	
    integer procedure checkKey(key, break);
    
    text key;
    
    Boolean break;
    
	begin
	
        integer i;
				
				
        for i := 1 step 1 until 60 do
        
        begin
        
            if break then
            
            begin
            
                if Letters(i) = key then 
                
                    begin
                    
                        checkKey := i;
                        
                        break := False;
                        
                   end
                   
                   else checkKey := 0;
                   
               end;
               
        end;
        
    end;
				
				
    inf :- new InFile("readme.txt");
    
    inf.Open(Blanks(60));
    
    inf.InImage;
    
    T :- Inf.Image;
    
    T :- T.Strip;
    

    for i := 1 step 1 until T.Length do
    
    begin
    
        break := True;
        
        ind := checkKey(T.sub(i,1), break);
        
        if ind > 0 then Counter(ind) := Counter(ind) + 1
        
                   else 
				
				
                   begin
                   
                       Letters(i) :- T.sub(i,1);
                       
                       Counter(i) := 1;
                       
                   end;
                   
		
    end;
    
	
    for i := 1 step 1 until 60 do
    
    begin
    
        if Letters(i) <> "" then
        
        begin
        
            OutText(Letters(i));
            
            OutText("  -->");
            
            OutInt(Counter(i),2);
            
            OutImage;
            
        end;
        
    end;
    
end
				

When we run it we're going to see...


That's it for now...


Simula is a nice and interesting language


It's also a historic languagage...


Everybody should know at least the basics...


Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...