Difference between revisions of "Hello World"

From vishap oberon compiler
Jump to navigation Jump to search
Line 1: Line 1:
 
{{language navigation|Hello World}}
 
{{language navigation|Hello World}}
  
Now let's write a "hello world" program and compile it using voc.
+
==using module Console==
  
Create a new file named <code>foo.Mod</code> and insert in it the following code.
+
Create a new file named <code>hello.Mod</code>:
  
 
<source lang="oberon2" style="font-size:10pt">
 
<source lang="oberon2" style="font-size:10pt">
 
MODULE hello;
 
MODULE hello;
  
IMPORT Console;
+
  IMPORT Console;
  
BEGIN
+
  BEGIN
  
Console.String ("Hello World!");
+
    Console.String ("Hello World!");
Console.Ln
+
    Console.Ln
  
END hello.
+
  END hello.
 
</source>
 
</source>
  
Line 33: Line 33:
 
Now let's compile it!
 
Now let's compile it!
  
<code>voc -m foo.Mod</code>
+
<code>voc -m hello.Mod</code>
  
 
and now let's run it!
 
and now let's run it!
Line 44: Line 44:
 
</pre>
 
</pre>
  
End article.
+
== using Texts and Oberon.Log ==
 +
 
 +
this is a more traditional way in a spirit of Oberon system.
 +
 
 +
<source lang="oberon2" style="font-size:10pt">
 +
MODULE Hello;
 +
IMPORT Oberon, Texts;
 +
  VAR W: Texts.Writer;
 +
PROCEDURE World*;
 +
  BEGIN
 +
    Texts.WriteString(W, "Hello World!");
 +
    Texts.WriteLn(W);
 +
    Texts.Append(Oberon.Log, W.buf);
 +
    Oberon.DumpLog;
 +
  END World;
 +
 
 +
BEGIN
 +
  Texts.OpenWriter(W);
 +
  World
 +
END Hello.
 +
</source>
 +
 
 +
There are two small differences with Oberon system version:
 +
 
 +
* in Oberon system you can call from the shell Hello.World function, while in Linŭ there is only one program entrance point (main), and in the main module it's a section which starts with BEGIN keyword. Thus you need to call World procedure from that part.
 +
 
 +
* We have a function Oberon.DumpLog, in order to dump Oberon.Log to console.
 +
 
 +
This may be considered a more portable way, because not all systems have console (windows?), and Oberon.Log can be dumped to the different space.

Revision as of 16:25, 24 February 2016

Deutsch (de) | English (en) | հայերեն (hy)

using module Console

Create a new file named hello.Mod:

MODULE hello;

  IMPORT Console;

  BEGIN

    Console.String ("Hello World!");
    Console.Ln

  END hello.

Which means

MODULE hello — The module's name is hello.

IMPORT Console — Import the module Console.

BEGIN — Here we start our code!

Console.String ("Hello World!"); — Module Console, Procedure String (which is going to show a sting on our console) Hello World!
Console.Ln; — Print a line on the Console.

END hello. — End of the module hello, and let's not forget the dot. (:

Now let's compile it!

voc -m hello.Mod

and now let's run it!

$ ./hello
Hello World!

$

using Texts and Oberon.Log

this is a more traditional way in a spirit of Oberon system.

MODULE Hello;
 IMPORT Oberon, Texts;
   VAR W: Texts.Writer;
 PROCEDURE World*;
   BEGIN
     Texts.WriteString(W, "Hello World!");
     Texts.WriteLn(W);
     Texts.Append(Oberon.Log, W.buf);
     Oberon.DumpLog;
  END World;

 BEGIN
   Texts.OpenWriter(W);
   World
 END Hello.

There are two small differences with Oberon system version:

  • in Oberon system you can call from the shell Hello.World function, while in Linŭ there is only one program entrance point (main), and in the main module it's a section which starts with BEGIN keyword. Thus you need to call World procedure from that part.
  • We have a function Oberon.DumpLog, in order to dump Oberon.Log to console.

This may be considered a more portable way, because not all systems have console (windows?), and Oberon.Log can be dumped to the different space.