-
Notifications
You must be signed in to change notification settings - Fork 2
Tutorial
After downloading Déjà Vu, go to the directory all the files are in. Then run:
python deja.py
Now you're running the interactive interpreter (based on the one Python has). You can use it to write Déjà Vu programs of any complexity, although they won't be saved, of course. Ctrl+D quits the interpreter, and Ctrl+C breaks of the line you're currently typing, for when you change your mind or something.
Back at the command line, running
python deja.py filename
Will try to run the file "filename" as a Déjà Vu program.
Restart the interactive interpreter for the rest of this tutorial.
This tutorial is intended for people having at least some exposure to languages like Python and Forth. Technical terms will be used, although I will try to make sure as many people as possible are able to benefit from this tutorial.
From now on, every command is assumed to be typed in the interactive interpreter.
Input is preceded by >>
or ..
prompt for clarity, output isn't.
So, a simple test:
>> . "Hello world!"
Hello world!
That was simple! Let's try something a bit more interesting next, shall we? How about some arithmetics?
>> . + 4 3
7
>> . - 8 1
7
>> . * 2 + 3 5
16
Woah, back up a little! What was that?
Well, in Déjà Vu, lines are evaluated backwards:
the function +
gets two arguments (4 and 3), and returns a single one
(7). The function .
prints its argument (7) on the screen.
Similarly, in the second line: the function -
gets two arguments (8 and 1), and returns a single one
(7). The function .
prints its argument (7) on the screen.
The last one is more complicated. +
now receives 3 and 5, returning 8. *
receives 2 and 8, returning 16.
This, by the way, is called "Polish notation".
Now that we know all this, we can go on to…
Repetition is boring. So if we can find ways to skip the repetion, that would be neat, wouldn't it?
>> func square x:
.. * x x # press tab at the start of this line
..
>> . square 10
100
Interesting. A lot of things happened here. Ending a line with :
signifies to Déjà Vu that it is a statement. What's more, statements
are followed by a block. Blocks are indented by a tab. Whenever you are inside a
block, the prompt changes to ..
, and Déjà Vu holds off running the code until
you say that you're done. You can tell the interpreter you're done by pressing
entering an empty line.
Another thing, you can start a comment by typing #
. Anything on the same line
following that is ignored. Useful, no?
Also, the keyword func
tells the interpreter to make a new function. It gets the name
square
, and takes a single argument, x
. The function runs * x x
and returns
the result of that calculation.
Now you can use square
just like +
, *
and -
.
Another interesting function:
>> func even number:
.. if % number 2: # odd number
.. 0 # press tab twice
.. else: # even number
.. 1
..
>> . even 7
0
>> . even 8
1
So what do we have here? %
is the module function: it returnes the rest after division. So, for example % 7 4
is 3, because 7 / 4 = 1, with a rest of 3. Let's check that: 4 * 1 + 3 = 7, yeah, that works out. More interestingly, %
returns zero if the first number is divisible by the second: % 8 4
is 0, and % x 2
is 0 if x
is even.
The keyword if
runs its body if the condition on that line returns something that isn't zero. Otherwise, if it has an else
statement following it, it executes that.
dup
is a function that duplicates the argument it got. So:
>> . . dup 1
1
1
It will come in handy when we check out the next flow control structure:
>> 1
>> while > 4 dup:
.. . dup
.. + 1
..
1
2
3
while
is similar to if
, only it keeps running its body as long as the condition is non-zero. Also, it can't have else
following it. (If you try, you get an error message.)
See if you can trace in your head what happened in the above example.
There is yet another form of basic flow control: For loops. They look like this:
>> for x range 1 4:
.. . x
..
1
2
3
This is a bit of an oddball. It lies somewhere between func
and while
. The first word following for
is special: it is the name of the "counter variable". You can use it like the arguments to functions. The rest of the line determine how often the body is run and the value the counter variable takes. range
is a function (comparable to a generator in Python, although it is a regular function) which takes two arguments, and returns, among other things, the first value for x
.
If you want more, you could check out the Syntax page, which goes more in-depth on the syntax, and the page on the Standard Library, which has all sorts of useful functions.