Start from a main routine

From Rosetta Code
Revision as of 20:47, 13 July 2013 by rosettacode>PKai (C#/Omit, F#/Omit)
Task
Start from a main routine
You are encouraged to solve this task according to the task description, using any language you may know.

Some languages (like Gambas and Visual Basic) support two startup modes. Applications written in these languages start with an open window that waits for events, and it is necessary to do some trickery to cause a main procedure to run instead. Data driven or event driven languages may also require similar trickery to force a startup procedure to run.

The task is to demonstrate the steps involved in causing the application to run a main procedure, rather than an event driven window at startup.

Languages that always run from main() can be omitted from this task.

Ada

In Ada, the "Main" procedure doesn't have to follow a special naming scheme. Any procedure will do.

<lang ada>with Ada.Text_IO; procedure Foo is begin

  Ada.Text_IO.Put_Line("Bar");

end Foo;</lang>

AutoHotkey

AutoHotkey always starts at the top of the script. A main() function can be called from there, or event hooks set.

AWK

The awk language is data driven. However, it does support the use of begin blocks, so we could use one of those to provide us with a main startup procedure:

<lang awk>BEGIN {

 # This is our main startup procedure
 print "Hello World!"

}</lang>

Erlang

When started Erlang enters a REPL (read-eval-print loop). To call a function called main in the module m you do: erl -run m main argument1 argument 2 ...

Forth

Forth still runs the interpreter when given a file to include in order to compile the source, but you can avoid the interactive interpreter by invoking an entry point ("main") then calling BYE to exit. <lang forth>include foo.fs ...

main ... ;

main bye</lang>

This pattern is also used (e.g. GNU Forth) to interpret a Forth snippet from the command line.

<lang forth>$ gforth -e "2 2 + . bye"</lang>

Furthermore, professional Forth systems like PFE and SwiftForth have a means to make a "turnkey" application which omits the interactive interpreter, suitable for installing on a third-party system. The command would take an entry point and target executable name:

<lang forth>' main turnkey app.exe</lang>

Gambas

In Gambas, to make an application startup from a main routine:

  • Create a new module called MMain
  • In the MMain module, create a public sub called Main as follows:

<lang gambas>PUBLIC SUB Main()

 ' This is the start of the program

END</lang>

  • Right click the MMain module, then select Startup class from the context menu

J

J, by default, starts an event loop.

If a file name is specified on the command line, that file is executed before dropping into the event loop.

Thus, if the script issues an exit command, that will happen before the event loop executes.

If you want the script to exit even when it hits an error, you can use an immex phrase, which will be the first thing executed by the event loop, before it prompts.

Mathematica

Mathematica automatically starts a REPL (read-eval-print loop), which is a kind of event loop. If that is not desired, pass -run on the command line. Note that if Mathematica is called via MathLink from within an external program, then the main loop has to be defined, which will usually differ from the standard one.

PARI/GP

GP scripts start from the top of the script. PARI code starts from the main function.

Perl

Same as Perl 6. <lang perl6>BEGIN {...} # as soon as parsed CHECK {...} # end of compile time INIT {...} # beginning of run time END {...} # end of run time</lang>

Perl 6

When executed with the standard setting, Perl 6 code always runs the mainline code automatically, followed by the MAIN function if you have one. However, it's possible to start up with an alternate setting that might want to create its own event loop or MAIN. In such cases you can always capture control at various phases with blocks that we call "phasers": <lang perl6>BEGIN {...} # as soon as parsed CHECK {...} # end of compile time INIT {...} # beginning of run time END {...} # end of run time</lang>

PicoLisp

PicoLisp automatically starts a REPL (read-eval-print loop), which is a kind of event loop. If that is not desired, call (wait), or pass -wait on the command line. Per convention, the GUI event loop is started by calling (go), or by passing -go on the command line.

PureBasic

PureBasic is procedural and any code which is not part of a procedure is considered 'main' code. This code also does not use any explicit syntax (i.e. a 'main' module) to cause it to execute and it always executes first.

Racket

Racket can be configured to run a REPL, run a main function, or just run top-level expressions. A main function can be run by executing racket -tm program.rkt.

<lang racket>

  1. /usr/bin/env racket -tm
  2. lang racket

(provide main) (define (main . args) (displayln "Hello World!")) </lang>

REXX

The closest REXX has to this type of behavior is when a REXX program starts,
then executes (as per this discusion, say) an XEDIT session, and
then re-directs commands to the XEDIT session via the ADDRESS command.
XEDIT has native (built-in) support for the REXX language as a macro language.
The XEDIT mentioned above runs on the VM/CMS operating system. <lang rexx>/*REXX*/ address 'XEDIT'

 .
 .
 .

[XEDIT commands here.]

 .
 .
 .</lang>

sed

A sed program repeats itself for each line of input, but your program can begin with commands that address line 1. (This requires that your input has a line 1. If your input is empty file, like /dev/null, then it is impossible to run commands.)

<lang sed># This code runs only for line 1. 1 { i\ Explain-a-lot processed this file and i\ replaced every period with three exclamation points!!! i\

}

  1. This code runs for each line of input.

s/\./!!!/g</lang>

Tcl

If a Tcl interpreter (such as tclsh or wish) is started without a script file, it will (typically) provide an interactive command prompt that supports read-eval-print-loop functionality. However, if a script file is supplied the file is executed and then the program either exits or, if Tk is in use, the application waits, servicing events, until the last window is deleted.

Visual Basic

In Visual Basic to make an application startup from a main routine:

  • Create a new module called MMain.bas
  • Create a new subroutine in the new module as follows:

<lang vb>SUB Main()

 ' This is the start of the program

END</lang>

  • From the menu in the application development environment, choose: File, Project Options.
  • Ensure that MMain.bas is selected, by clicking in the list
  • From the pulldown list, choose "Sub Main"
  • Click the OK button.

ZX Spectrum Basic

On the ZX Spectrum, there is no main function as such. However a saved program can be made to start running from a particular line number by providing the line number as a parameter to the save command. The following example will save the program in memory so that it starts running from line 500:

<lang zxbasic>SAVE "MYPROG" LINE 500: REM For a program with main code starting at line 500</lang> {{omit from|C S