1.0
Using the Prothon Interpreter
1.1
Interactive Console
When you have Prothon installed properly, you can
start the interpreter in interactive console mode by simply typing "prothon"
at the command prompt. Typing "prothon -h" will give you a
summary of available command-line options.
C:\>prothon
Prothon 0.1 Interactive Console, Build 500, May 17 2004 (Ctrl-D to exit)
>>>
As you can see from the console heading, you exit the interactive console by
typing Control-D at the beginning of any line that starts with the normal
>>> prompt. You may have to hit enter a few times to get to
this prompt.
In our tutorial we will use output from this console for most of the
examples. You can follow along by typing the same things into your Prothon
console. Even when large portions of code are loaded and run, this can
be done by loading files into the console and we have provided such
files for you to load and run with this tutorial.
Let's try something simple by using the console
as a calculator:
>>> 3**2 + 4**2
25
Two asterisks ( ** ) means "raise to the power of" so the
formula I typed was "3 squared plus 4 squared" and the console
echoed 25 (which we know is 5 squared). This shows the feature of the
console that it echoes the value of whatever result is left on the last
line typed.
You can also give commands. One that you will use often is the "print"
command:
>>> print "hello world"
hello world
The print command prints whatever arguments follow it. In this case it
is a simple string. This is a bit different than the last output of the
console because we commanded the output instead of just letting the console
default to showing us what value was left in the line.
You can also type in statements that require multiple
lines, such as the for statement:
>>> for i in 3:
... print i
...
0
1
2
Whenever you see the prompt with three dots "..." this means that the
console is just storing up your input and not running it yet. It knew
you weren't done yet after typing "for i in 3:" because the
line ended in a colon ( : ). In Prothon, whenever a line ends in a colon,
another line is expected to follow that is indented one level deeper
than the current one. This is the beginning of a "code block".
All the lines that start at this indent level are run together as one
block of code.
Note that the console tabs in one indent level whether you hit the
tab key or the space key. It also goes back one whole indent level when
you press backspace. When you are done entering a code block you must
enter one blank line to let the console know the block is finished.
Then Prothon runs the whole block of code at once. In this case it ran
the for loop three times. We will discuss the for loop more later.
1.2
Executing a Prothon File
Prothon source files are normal text files with any type of line endings
(Unix, Windows, or Mac) that have file names ending in ".pr".
Prothon is an interpreter so you can run the Prothon source file directly
without compiling it first. Just type "prothon file.pr" at
the command prompt. Set your working directory to the prothon/pr/turotorial
directory and try this:
C:\prothon\pr\tutorial>prothon tut1.pr
hello world
This complex program should look familiar. The contents of the tut1.pr
file is:
print "hello world"
You can see nothing special is needed in the source file.
Prothon programs can also have arguments. Just place
them after the name of the .pr file as you would any normal program
file:
C:\prothon\pr\tutorial>prothon tut2.pr hello world "test 1 2 3"
Argument: tut2.pr
Argument: hello
Argument: world
Argument: test 1 2 3
The source code for tut2.pr is:
# Prothon source file tut2.pr
for arg in argv_:
print "Argument:", arg
The arguments are placed in a Prothon list called argv__ with the program
name as the first item as is traditional. The for loop and lists will
be described later.
1.3
Using the Console With Files
If you add the -i option to the prothon command
line when starting a file, the interpreter will start an interactive
console session when the main program finishes. More importantly, the
variables will still remain from the main program for you to interrogate:
C:\Prothon\pr\tutorial>prothon -i tut2.pr hello world
Argument: tut2.pr
Argument: hello
Argument: world
Prothon 0.1 Interactive Console, Build 500, May 17 2004 (Ctrl-D to exit)
>>> print argv_[0]
tut2.pr
There is even a way to run the console at the
same time your file is executing. Here is a simple prothon program that
adds 1 to a variable called Main.timer every second:
# Prothon source file tut3.pr
for i in 1_000_000:
Main.timer = i
Sleep(1.0)
If you run this as a background program then you can examine the timer
variable in the console at the same time. The -t command line option starts
a prothon file executing as a background thread. Hint: to repeat a command
in the console, use the up arrow:
C:\Prothon\pr\tutorial>prothon -t tut3.pr -i
Prothon 0.1 Interactive Console, Build 500, May 17 2004 (Ctrl-D to exit)
>>> print Main.timer
5
>>> print Main.timer
7
>>> print Main.timer
8
>>>
You can actually start many files executing with the -s option and
the console all at once, but we won't show that here.
|