home *** CD-ROM | disk | FTP | other *** search
/ Global Amiga Experience / globalamigaexperience.iso / compressed / development / heliosdemodisk3.dms / heliosdemodisk3.adf / Tutorials / HeliOSLanguageTutorial1.doc < prev    next >
Text File  |  1994-12-18  |  74KB  |  2,079 lines

  1.  
  2. -----------------------------------
  3. Looking forward and getting started
  4. -----------------------------------
  5.  
  6. This introductory tutorial is intended to explain a few general ideas
  7. which may help you get started with HeliOS.
  8.  
  9. Because simple once-only statements of fact are not the best way of
  10. introducing a complex subject, we have provided tutorials which cover 
  11. similar ground in different ways.
  12.  
  13. Some of the ideas in this tutorial will be different expressions of
  14. ideas related in other tutorials, and some of the material will be new.
  15.  
  16. Hopefully this many-faceted approach will help make some of the more
  17. unfamiliar concepts accessible to most people, and once you are confident
  18. enough to write your own first programs you will be able to pick up
  19. further knowledge directly from the reference material as you need it.
  20.  
  21. So don't just say "Oh..we've already read this!" and gloss over things,
  22. unless you already are very confident of your understanding, because
  23. there may be some new ideas in here.....
  24.  
  25. Before explaining the fundamentals of HeliOS programming it is
  26. worthwhile looking briefly at the differences between computer
  27. programming languages.  This is important because HeliOS is very
  28. different from other languages with which you may be familiar
  29. and opens up many novel possibilities.
  30.  
  31. If you are already experienced with a computer language such as BASIC
  32. or PASCAL, you will need to "forget" many of the restrictions imposed
  33. by such languages.  HeliOS is a fluid, redefinable, linguistically
  34. oriented language, with much greater freedom of expression and very
  35. different "free form" programming style.
  36.  
  37. If you already use assembly language you will, in some ways, be
  38. more instantly at home with HeliOS, because it allows direct control
  39. of data storage in memory in a similar fashion to assembly language.
  40. In fact, HeliOS allows the integration of assembly language directly
  41. within "high level" HeliOS code, and HeliOS has many functions which
  42. are close to assembly language in concept.  HeliOS is also close to
  43. assembly language in potential speed and efficiency.
  44.  
  45. If you are new to programming, you will find that HeliOS will be
  46. rather easier to learn than the more restrictive old fashioned
  47. computer languages such as BASIC.  Coming fresh to HeliOS with no
  48. preconceptions, you will perhaps be better placed even than already
  49. experienced programmers to take advantage of all the novel new
  50. concepts used by HeliOS.
  51.  
  52. Since HeliOS as so different from other computer languages, let us
  53. assume you are new to programming and take things from the beginning.
  54.  
  55. Your first task is to learn a few simple and fundamental aspects of
  56. communication with your computer: think of it as learning to speak to
  57. someone who does not speak your own native language.
  58.  
  59. You will have to learn a new mode of communication, and interestingly
  60. you can also adapt HeliOS to use a form of linguistic expression chosen
  61. by you.  In a sense you could say that HeliOS allows you to train the
  62. computer to speak YOUR personal language.  This is quite an advanced
  63. capability which allows HeliOS programmers to develop a high degree
  64. of personal style and creativity.
  65.  
  66. ------------------------------------------------
  67. How HeliOS differs from other computer languages
  68. ------------------------------------------------
  69.  
  70. Using HeliOS you can actually build a "new" language which ideally
  71. suits your own style of programming, and which interfaces to the Amiga
  72. in the most efficient possible way.  HeliOS allows you to create new
  73. commands based on your own ideas and incorporating programming methods
  74. which you prefer.  Unlike most other computer languages HeliOS is an
  75. EXTENSIBLE language, so you literally program by extending the actual
  76. set of words you use to communicate with the computer.
  77.  
  78. Notice how much emphasis is being placed upon "communication" and the
  79. use of "words": this is very important, because HeliOS is really about
  80. inventing your own communication channel with the Amiga, based upon a
  81. mutual use of a shared vocabulary which YOU specify.  You could almost
  82. say that HeliOS is a "verbally" or "semantically" oriented language
  83. rather than a "number manipulating" purely "computational" one.
  84.  
  85. Already, if you are a BASIC or "C" programmer, you will perhaps be
  86. intrigued by what is a whole new world of programming possibilities.
  87.  
  88. There are many computer languages, all of which have strengths and
  89. weaknesses, and of course the computer which you use will have its
  90. own set of individual features.  Your task is really twofold: to master
  91. an effective means of communication and then to learn how to control
  92. all the different functions of the particular machine with which you
  93. are working.
  94.  
  95. Your choice of computer will determine the final results which you can
  96. achieve: some computers are better at certain tasks than others.  The
  97. Amiga is possibly the most versatile home computer of all, so you will
  98. certainly have many interesting avenues to explore.
  99.  
  100. Your choice of language will very much influence the ease and productivity
  101. of the task of programming, and your chosen language might be assessed in
  102. terms of two really vital measures of performance:
  103.  
  104. 1. Is the language capable of flexible control of ALL aspects of your
  105.    computer, or is it going to be incapable of performing certain tasks.
  106.  
  107.    For example, most high level languages lack speed, and are therefore
  108.    less capable than assembly language when it comes to creating games.
  109.    If you want to write game software without having to resort to assembly
  110.    language you need to choose a very fast high level language.
  111.  
  112. 2. Is the language flexible and easy to use from YOUR point of view.
  113.  
  114.    For example, ideally you need a language which makes it easy to do the
  115.    kinds of things you want to do with the computer.  You also need to be
  116.    comfortable with the general interface between you and the language in
  117.    terms of editing source code and program testing.
  118.  
  119. Computer languages vary in speed and power: most languages are called
  120. "high level" languages because they insulate you from dealing with "low
  121. level" technicalities to do with the actual operation of the computer
  122. hardware.  These languages usually have a certain trade-off of speed
  123. and flexibilty in favour of simplicity of use: they are biased towards
  124. making life easy for the programmer rather than being computer-efficient.
  125.  
  126. At the other end of the programming language "scale" is assembly language,
  127. which deals directly with the computer hardware and requires the human
  128. programmer to adapt to the needs of the machine rather than vice-versa.
  129. Assembly language is the most powerful language in terms of speed and
  130. efficiency, but it is quite difficult and unforgiving from the point of
  131. view of the programmer.
  132.  
  133. Another broad difference between languages is whether they are "compilers"
  134. or "interpreters".  A "compiler" has to first pre-process a complete set
  135. of program instructions and prepare an executable file which then has to be
  136. run to test the program.  This can be a slow and tedious process, and makes
  137. the task of the beginner so much harder because there is no facility for
  138. easy instant testing of small code fragments.
  139.  
  140. An "interpreter" on the other hand, allows you to type in a command and
  141. get an instant response: the language can instantly interpret what you type.
  142. This is excellent for beginners, because it is easy to test small sections
  143. of code and experiment to get things working correctly.
  144.  
  145. Traditionally interpreters were slow languages because all the program
  146. instructions were literally being interpreted as the program ran.  In
  147. recent times interpretive languages have often been equipped with add-on
  148. compilers which process the final program source code into an "executable"
  149. form which runs faster because it has been "pre-interpreted".  Most more
  150. modern and efficient languages are pure "compilers", and the special
  151. advantages of interpretive languages are no longer available to users of
  152. such languages.
  153.  
  154. So, being unrealistic and "hopeful", ideally you might be looking for an 
  155. easy to learn high level language, which is capable of assembly language 
  156. speed, full direct computer hardware control if required, and which has 
  157. the best features of both a compiler and an interpreter!
  158.  
  159. Many people will tell you this is an impossible dream.....
  160.  
  161. Certainly this requirement eliminiates virtually all languages available
  162. for the Amiga, but HeliOS actually does fulfill all these functions!
  163.  
  164. HeliOS is a very flexible interpreter AND compiler: a high level language
  165. with a natural speed nearly equivalent to assembly language.  HeliOS has a
  166. full capacity to control the computer hardware directly, and can even allow
  167. pure assembly language to be written within high level code.  Best of all,
  168. HeliOS has the most sophisticated and comfortable "human interface" of ANY
  169. language, so its powerful programming tools are available in an enjoyable
  170. and easily approachable form.
  171.  
  172. HeliOS will easily allow you to exploit every facility of the Amiga, but do
  173. not try to rush into doing complicated things too early.  The Amiga itself
  174. is very complex and sophisticated, and no matter how easily HeliOS allows
  175. you to write programs, you must take care to learn how the Amiga functions
  176. in detail before you can effectively control the computer.
  177.  
  178. This tutorial will teach you how to work with HeliOS, and you will soon
  179. learn to work with the Amiga operating system and hardware: then you will
  180. be free to create the most ambitious programs you can imagine.
  181.  
  182. However, that is for the future, and for now we will look at some simple
  183. ways to get to know the HeliOS language.
  184.  
  185. ---------------------------------
  186. Controlling the Amiga with HeliOS
  187. ---------------------------------
  188.  
  189. How does a computer language allow you to control a computer?  How do
  190. YOU control your chosen computer language?
  191.  
  192. In fact......just how does a simple digital machine manage to "come
  193. alive" and produce the fascinating sounds and graphical extravaganzas
  194. which we often see in Amiga software?
  195.  
  196. It does so, largely, by YOUR creative input, and to provide this you need
  197. tools to enable you to express yourself using this new creative medium.
  198. You then need to learn to use these tools creatively and confidently,
  199. so that you can turn the computer into an instrument for expressing your
  200. ideas effectively.
  201.  
  202. HeliOS provides you with an excellent toolkit, but if you don't master
  203. the fundamentals well, as in any artistic pursuit, the computer and the
  204. language will impose themselves on you to the point that you are forced
  205. to tread a tedious tightrope of obediance to rigid rules.  HeliOS gives
  206. you hundreds of different ways to do any job: the choice is yours, so
  207. learn how to use this range of choice for your own creative ends.
  208.  
  209. If YOU want to be in control, as you should be, you need first and
  210. foremost to have a good general idea of how the computer works.  You
  211. then need to have a sound understanding of the fundamental operation
  212. of your computer language.  You will gain experience and detailed
  213. knowledge quite easily and painlessly as you progress, and you should
  214. not try to learn too much in the way of technical details too soon.
  215.  
  216. Eventually, no doubt about it, you are going to have to do some detailed
  217. learning, but first of all you need an overview of the programming process
  218. and a few simple ground rules to get you started.  If you grasp the simple
  219. things and use sound programming practises, you will be able to enjoy
  220. rewarding results from even your first day of HeliOS programming.
  221.  
  222. Let us take an overview of our journey and then look at a few simple
  223. but fundamentally important programming concepts.
  224.  
  225. The Amiga computer has many functional sub-systems which can be likened
  226. to groups of people with various talents, each of whom uses a specific
  227. linguistic "dialect" designed to express concepts related to their task.
  228. It will be your job as a programmer to communicate with all these "people",
  229. each in their own idiom, and you will need to develop tools and methods
  230. with which to express your instructions.  For example, you would need one
  231. type of knowledge and technique for writing a sound synthesizer program,
  232. and you would need another for writing a graphics-intensive arcade game.
  233.  
  234. An ideal computer language is an "interface" consisting of pre-designed
  235. commands which are comprehensible to the programmer and which are also
  236. adaptable to meet the demands of communicating with every aspect of the
  237. host computer.  HeliOS has many specialised commands to help you deal with
  238. specific functions of the Amiga, and is probably the closest thing to a
  239. "total" Amiga control language which you will find.
  240.  
  241. Any programming language presents one face to you and another face to the
  242. machine: this is its job.  The less "work" that has to be done "in between"
  243. in forcing your commands into direct machine instructions, the better
  244. and more efficient will be the result.  HeliOS itself imposes a minimum
  245. of its own "character" and allows you to control all aspects of the Amiga
  246. very directly, in the way in which you choose.  This means that you need
  247. to UNDERSTAND what the computer is doing rather than simply entering lists
  248. of predesigned commands.  Don't worry: this isn't as hard as it sounds!
  249.  
  250. Most programming languages have developed over years into fairly rigid
  251. and limited systems which are not ideally adaptable to all aspects of
  252. controlling modern computers.  This tends to mean that the language has
  253. a very formal structure, and in fact many languages are only good at
  254. certain particular tasks.  HeliOS takes a different approach and allows
  255. you mould your own language for any programming requirement.
  256.  
  257. HeliOS allows you, the programmer, to recreate the language itself for
  258. any particular task you need to do.  In HeliOS, if you want to carry
  259. out a complex task, you first have to break down the task into sensible
  260. small "units", then you EXTEND the language by CREATING new commands to
  261. do the particular things you require.
  262.  
  263. Your new commands are named however YOU choose, so they instantly make
  264. sense to you and you remember them easily.  In other languages you are
  265. required always to remember fixed commands written by someone else, and
  266. to conform to rigidly inflexible command sets.
  267.  
  268. HeliOS, in short, provides you with a flexible and open ended way of
  269. developing your knowledge of the Amiga and your power to control it.
  270.  
  271. ----------------------
  272. A first HeliOS program
  273. ----------------------
  274.  
  275. Let us take, as a first example, the traditional computer language
  276. introductory program: getting the computer to say "Hello World!".
  277.  
  278. To do this in "C" you would need to write quite a lot of code to create a
  279. stand-alone program which would first need to set up the capacity for the
  280. computer to display a "screen" and to display text on this screen.
  281.  
  282. In HeliOS, even to write a "stand alone" program to do this would only be
  283. a matter of a few words: you could actually do it in one line of code!
  284.  
  285. However, HeliOS includes an "Interpreter" which already has a text display
  286. ready for use, so for a simple example all you need to do is switch to the
  287. HeiOS Interpreter screen and type your commands directly.
  288.  
  289. Like BASIC, HeliOS has simple commands to output text, but unlike BASIC
  290. HeliOS also has a very wide range of more sophisticated and powerful text
  291. commands to do many special operations.  However, for now, all we need use
  292. is a very simple text output function, and this can be done in HeliOS using
  293. the expression:
  294.  
  295.                        ." Hello World!"
  296.  
  297. Let us examine this simple expression, and firstly we can look at the
  298. initial two-character word at the start:
  299.  
  300.                        ."
  301.  
  302. The first part, the . character, is used by HeliOS to indicate that
  303. something is being "output": it is roughly equivalent to a shorthand form
  304. of the word "print".
  305.  
  306. The second part, the " character, is used to indicate that this "double
  307. quote mark" will be used to "delimit", or "enclose" the text we want
  308. to print.
  309.  
  310. This expression as a whole is saying, in a cryptic form, "print the text
  311. following enclosed by double quote marks to the screen".
  312.  
  313. In computer parlance we have a special name for the double quote character
  314. used to indicate the start and end of our text message: we call this a
  315. DELIMITER, because it "delimits" a text message surrounded by other words
  316. which have a different function.
  317.  
  318. Notice that we have emphasised the need to "enclose" text messages within
  319. delimiters: we cannot simply say:
  320.  
  321.                        ." Hello World!
  322.  
  323. without the final quotation mark.
  324.  
  325. This is important!
  326.  
  327. When you are telling the computer to do something with text you need to be 
  328. able to define where the text starts and stops, so that the computer can 
  329. tell the difference between COMMAND words and the actual words of your 
  330. embedded TEXT message.
  331.  
  332. To understand this, imagine that you had typed a set of commands at the
  333. Interpreter command line, and that one of these commands was an instruction
  334. to print some text to the screen.
  335.  
  336. You might have something like this:
  337.  
  338. COMMAND1 COMMAND2 ." This is my text to print" COMMAND3 COMMAND4 etc.
  339.                    ^                         ^
  340.               First delimiter       Second delimiter
  341.               = START of text       = END of text
  342.  
  343. You can see how the two " characters are used to enclose and distinguish
  344. the embedded text message from the commands before and after them.
  345.  
  346. This concept of "delimiters" is one you will meet often when using text
  347. embedded within program code.
  348.  
  349. Let us go back now to our "Hello World!" program:
  350.  
  351.                        ." Hello World!"
  352.  
  353. If you type this at the HeliOS command line, the Amiga will output the
  354. text message "Hello World!".  It is as simple as that, so why not try it
  355. now.....
  356.  
  357. Here you have typed in one single direct command, but you can of course
  358. type in a sequence of commands to do something a little more complicated.
  359.  
  360. Try typing the following (actually, you do not need to retype every word,
  361. just press "up-arrow" to recall your previous line of text and then edit
  362. the line as required):
  363.  
  364.   SCRCLR  ." Hello World!" WAITSPACE
  365.  
  366. This will do three different things, one after the other:
  367.  
  368. 1. Clear the text screen
  369. 2. Print "Hello World!"
  370. 3. Wait for you to press space.
  371.  
  372. Note that all computer programs are essentially simple sequences of
  373. operations, because the computer can actually only perform one function
  374. at any one time.
  375.  
  376.  
  377. Now try typing the following:
  378.  
  379. SCRCLR 12 12 CURPUT ." Hello World!"  WAITSPACE
  380.  
  381. This will set the text cursor position at "column 12, row 12" on the
  382. text screen before printing your message.
  383.  
  384. You have already written a short HeliOS program, but it is not very
  385. convenient to keep typing all this in at the command line every time.
  386.  
  387. We are now going to create a new single word command which will become
  388. a part of the HeliOS vocubulary.  This single word will the carry out all
  389. the commands in your small new program.
  390.  
  391. To create a new HeliOS command word we use two HeliOS functions expressed
  392. by use of the colon and semi-colon characters.  The colon character tells
  393. HeliOS that the next word will be the name of a new command, followed
  394. by a command definition.  The semi-colon character tells HeliOS that the
  395. new command definition is finished.
  396.  
  397. Let us create a new word called HELLO.
  398.  
  399.  :        HELLO      SCRCLR  12 12 CURPUT  ." Hello World!"  WAITSPACE   ;
  400.  ^        ^^^^^      <----------------------------------------------->   ^
  401.  Start    Name                        Definition                        End
  402.  
  403.  
  404. This has created a new word called HELLO.
  405.  
  406. Try entering this command line and then displaying the USER vocablulary.
  407.  
  408. You will see your new word HELLO listed as a HeliOS command word: you have
  409. already extended the HeliOS language and written a small program.
  410.  
  411. Now try typing just the word HELLO at the command line and press <Return>.
  412.  
  413. You will see that your new word does exactly what the original set of
  414. commands did when typed in individually.
  415.  
  416.                  ************************************
  417.                  Rules for creating new command words
  418.                  ************************************
  419.  
  420. Here, then, are the simple rules for creating new words:
  421.  
  422. 1. Place a "colon" (":") symbol with at least one space on each side.
  423.  
  424. 2. Put your new command name, followed by at least one space.
  425.  
  426. 3. Put your list of "internal" commands with spaces separating them.
  427.  
  428. 4. Place a "semicolon" (";") symbol with at least one space on each side.
  429.  
  430. We refer to a word definition such as this as a "colon definition".
  431.  
  432. This process of creating new commands is exactly what you do when writing
  433. all HeliOS programs: you keep on extending the language with ever more
  434. powerful commands, until you finally have one "master" command which uses
  435. all your previous constructions to carry out the main program.
  436.  
  437. Let us now make another command, called PRINT_RED.
  438.  
  439.  : PRINT_RED 6 FPENSET ;
  440.  
  441. This command sets the text colour to "colour 6", which will be RED unless
  442. you have changed your HeliOS colours.
  443.  
  444. You can now say:
  445.  
  446.   PRINT_RED HELLO
  447.  
  448. Getting more ambitious, we can make another new word:
  449.  
  450.  : PRINT_RED_HELLO   PRINT_RED HELLO ;
  451.  
  452. This is a compound word made up from our previous new commands, and you
  453. will see that HeliOS can freely mix any combination of your new words with
  454. its own CORE command set.  
  455.  
  456. Remember: there is no essential difference between HeliOS CORE functions 
  457. and the command words which you create yourself using colon definitions.
  458.  
  459. Can you see what the following expression does?
  460.  
  461.  : PRINT_BOLD_RED_HELLO   BOLDON PRINT_RED_HELLO ;
  462.  
  463. Try it!
  464.  
  465. And this....
  466.  
  467.  : FANCYHELLO   7 BPENSET PRINT_BOLD_RED_HELLO 2 BPENSET SCRCLR ;
  468.  
  469. The process of building new HeliOS commands should now be quite clear.
  470.  
  471. When you come to write a "real" program of course, you will not be using
  472. the command lines: you will be writing programs as text (or "source code")
  473. in an editor.
  474.  
  475. Many computer languages require source code to be written in a very strict
  476. format, but HeliOS is totally relaxed in its source code format, allowing
  477. you to choose a style which suits your own preference.
  478.  
  479. It is a good idea to use plenty of "white space" so that your programs
  480. are not cluttered and confusing, and it is best to make use of separate
  481. lines for distinct command sequences.
  482.  
  483. Look at this example of how you might turn all the above commands into
  484. a short HeliOS program in a text editor:
  485.  
  486.  : HELLO
  487.  
  488.  SCRCLR
  489.  12 12 CURPUT
  490.  ." Hello World!"
  491.  WAITSPACE
  492.  ;
  493.  
  494.  : PRINT_RED
  495.  
  496.  6 FPENSET
  497.  ;
  498.  
  499.  : PRINT_RED_HELLO
  500.  
  501.  PRINT_RED
  502.  HELLO
  503.  ;
  504.  
  505.  : PRINT_BOLD_RED_HELLO
  506.  
  507.  BOLDON
  508.  PRINT_RED_HELLO
  509.  ;
  510.  
  511.  : FANCYHELLO
  512.  
  513.  7 BPENSET
  514.  PRINT_BOLD_RED_HELLO
  515.  2 BPENSET
  516.  SCRCLR
  517.  ;
  518.  
  519. We have used UPPER CASE characters throughout, but you do not need to do so.
  520.  
  521. You could equally well have written FANCYHELLO as:
  522.  
  523.  : FancyHello
  524.  
  525.  7 BPenSet
  526.  Print_Bold_Red_Hello
  527.  2 BPenSet
  528.  ScrClr
  529.  ;
  530.  
  531. or even:
  532.  
  533.  : FancyHello   7 BPenSet  Print_Bold_Red_Hello  2 BPenSet  ScrClr  ;
  534.  
  535. This last option is actually quite a BAD idea, and with a little thought
  536. we can see why.
  537.  
  538. It is always best to use the sequence of lines within your source code
  539. to help express the sequential logic of the program: this makes it easy
  540. to follow what the program is doing.  The layout of your source code can,
  541. and should, help you comprehend the structure of your program.
  542.  
  543. There is also another good reason for using a line-by-line text format.
  544.  
  545. It is very useful in many cases to add notes to your programs, explaining
  546. what is happening at each stage.  This is called "commenting", and the
  547. addition of "comments" is vitally important once you start writing big
  548. complicated programs.  You may understand what a program is doing when you
  549. write it, but you will surely forget details when you return to it after
  550. some time has elapsed.
  551.  
  552. It is often very useful to add comments to each line, like this:
  553.  
  554.  : FancyHello                     \ Print red and yellow "Hello" message
  555.  
  556.  7 BPenSet                        \ Set background to "yellow"
  557.  Print_Bold_Red_Hello             \ Clear display and print message
  558.  2 BPenSet                        \ Reset background to white
  559.  ScrClr                           \ Clear display
  560.  ;
  561.  
  562. Notice that we use the "\" character to inform HeliOS that all text on
  563. the rest of that line is a comment.
  564.  
  565. HeliOS source code can also include longer multi-line comments.  You do
  566. this by enclosing the long text comment in brackets, like this:
  567.  
  568.  HELLO
  569.  
  570.  ( This is a long text comment, which can go on and on for as many lines
  571.   as I like.  I can carry on writing and HeliOS will ignore all this until
  572.   it finds a final ")" character, upon which it will carry on interpreting
  573.   the program. )
  574.  
  575.  FANCYHELLO
  576.  
  577. Perhaps you might have noticed something quite subtle: there was a bracket
  578. character WITHIN the comment which somehow HeliOS was clever enough to
  579. ignore.
  580.  
  581. How do you think HeliOS knew which bracket to use to determine the end of
  582. the comment?
  583.  
  584. The answer to this brings us to an important rule:
  585.  
  586. ALL HELIOS COMMAND WORDS MUST BE DELIMITED BY SPACES
  587.  
  588. This is very natural really, since it is just how we ourselves communicate.
  589.  
  590. After all, look what happens if we remove spaces from the line above:
  591.  
  592. Thisisverynaturalreally,sinceitisjusthowweourselvescommunicate.
  593.  
  594. HeliOS, like any normal person, requires its communications to be separated
  595. by spaces, and because this is such a natural consideration, it is quite
  596. unlikely that you will make the mistake of forgetting it.  However, if you
  597. do miss out a space between two words, HeliOS will merely tell you that
  598. it cannot understand and point out to you where it lost track of what you
  599. were saying.
  600.  
  601. Try typing this at the command line:
  602.  
  603.  : FancyHello   7 BPenSet  Print_Bold_Red_Hello  2 BPenSetScrClr  ;
  604.                                                          ^^
  605.                                                     Missing space
  606.  
  607.  
  608. You can see that HeliOS is really quite helpful, and pointed out where
  609. you had gone wrong without any untoward problems.
  610.  
  611. Going back to the case of the bracket within the comment, and indeed to
  612. comment markers in general, you should always remember that after the
  613. first bracket (which must have surrounding spaces) HeliOS will regard
  614. everything as a comment until it finds a final bracket with surrounding
  615. spaces.  If you were to place a ) character surrounded by spaces WITHIN
  616. your comment, HeliOS would assume that you wanted the comment to end there.
  617.  
  618. You should also take care not to write a line like this:
  619.  
  620.  7 BPenSet                        \Set background to "yellow"
  621.                                   ^^
  622.                                No space
  623.  
  624. If you miss out the space, HeliOS will not understand that you are writing
  625. a comment.  Try it and see......
  626.  
  627. One final point will lead us to the conclusion of this first lesson.
  628.  
  629. Perhaps you have noticed already that HeliOS will allow you to make several 
  630. new definitions of the same command.  In this case HeliOS warns you that 
  631. you have made multiple definitions, but will accept the new command in place 
  632. of the previous ones.
  633.  
  634. We will discuss this in more detail elsewhere, but for now we might point
  635. out that if you want to clear all old command definitions you simply type:
  636.  
  637.   FORGET **CORE**
  638.  
  639. This will cause HeliOS to "forget" all the new commands it has learned,
  640. and revert to its own in-built set of CORE functions.
  641.  
  642. Notice that once again HeliOS behaves in a fairly "human" way compared with
  643. most computer languages:  you teach it new words and use them in expressive
  644. ways similar to our own use of language, then you tell it to "forget".
  645.  
  646. ***************************************************************************
  647.  
  648. ---------------------------
  649. Simple programming strategy
  650. ---------------------------
  651.  
  652. We can now go on to a slightly more complex example, and in doing so we
  653. will look at simple ways of designing programs more efficiently.
  654.  
  655. Let us say that we want to print to the screen your name and address.
  656.  
  657. If we asked a person to do this type of task, we would assume that they
  658. knew how to write, how to use a pen, etc. etc., and we would be able to
  659. give them one simple command to do the job.
  660.  
  661. A computer on the other hand always has to be told explicitly what to
  662. do at each stage of the process.
  663.  
  664. The art of writing good computer software lies very much in developing
  665. a skillful understanding of how to break down complex tasks into several
  666. simple ones.  A good programmer can reduce a complex task to a relatively
  667. "intuitive" set of economical functions, whereas a bad programmer would
  668. probably write twice as much code to do the same job in a clumsy way.
  669.  
  670. Computer languages have many preset commands to carry out common tasks,
  671. and you need to have at your disposal a list of these commands.  This
  672. list of commands, in HeliOS, is called the DICTIONARY, and as you write
  673. programs you actually add commands of your own to this dictionary, thus
  674. effectively building your own personalised version of the language.
  675.  
  676. Let us assume we want to print this address:
  677.  
  678. Mr. W. Smith
  679. 12, Blitter Street
  680. AmigaTown
  681. ComputerLand
  682.  
  683.  
  684. Obviously, no computer language comes with a single built-in command which
  685. with one simple action will print your name and address at a chosen place
  686. on the screen and in chosen text colours and styles.  Even this very simple
  687. task requires a program to be written to do it, and our first task must be
  688. to work out a series of simple sub-commands to tell the computer how get the
  689. job done. Then we can create a new master-command called "PrintAddress".
  690.  
  691. Let us first make a preliminary list of what sort of things we want this
  692. command to do, in the form of a simple explicit step-by-step sequence:
  693.  
  694. 1.  Clear the page of previous text.
  695.  
  696.     On a computer the screen/page is not always empty!
  697.  
  698. 2.  Move the pen (or text cursor) to a certain place on the page.
  699.  
  700.     It would be nice to be able to specify this position as a parameter to
  701.     our new command PrintAddress.
  702.  
  703. 3.  It would be nice to be able to specify text style and colour, and for
  704.     this example we will use BOLD type in black for the name.
  705.  
  706. 4.  Print our name.
  707.  
  708. 5.  Move to the next line immediately below the start of the name.
  709.  
  710. 6.  It would be nice to be able to specify text style and colour, and for
  711.     this example we will use ITALIC type in black for the address.
  712.  
  713. 7.  Print the first line of the address.
  714.  
  715. 8.  Move to the next line immediately below......
  716.  
  717. 9.  Set text style.
  718.  
  719. 10. Print the second line of the address.
  720.  
  721. 11. Move to the next line immediately below......
  722.  
  723. 12. Set text style.
  724.  
  725. 13. Print the third line of the address.
  726.  
  727. Notice that we break down commands to the computer into very simple
  728. sequential operations.
  729.  
  730. Let us give names to these operations now.
  731.  
  732. 1.  ClearScreen
  733.  
  734. 2.  SetNamePosition
  735.  
  736. 3.  SetNameTextStyle
  737.  
  738. 4.  PrintName
  739.  
  740. 5.  SetAdd1Position
  741.  
  742. 6.  SetAdd1TextStyle
  743.  
  744. 7.  PrintAddress1
  745.  
  746. 8.  SetAdd2Position
  747.  
  748. 9.  SetAdd2TextStyle
  749.  
  750. 10. PrintAddress2
  751.  
  752. 11. SetAdd3Position
  753.  
  754. 12. SetAdd3TextStyle
  755.  
  756. 13. PrintAddress3
  757.  
  758. So, our program PrintAddress will look something like this:
  759.  
  760.  : PrintAddress
  761.  
  762.     ClearScreen
  763.     SetNamePosition
  764.     SetNameTextStyle
  765.     PrintName
  766.     SetAdd1Position
  767.     SetAdd1TextStyle
  768.     PrintAddress1
  769.     SetAdd2Position
  770.     SetAdd2TextStyle
  771.     PrintAddress2
  772.     SetAdd3Position
  773.     SetAdd3TextStyle
  774.     PrintAddress3
  775.   ;
  776.  
  777. You can imagine issuing these commands to a person (perhaps being a little
  778. more polite), and then watching the sequential series of commands being
  779. performed.
  780.  
  781. It is exactly the same with the computer.
  782.  
  783. Before we start issuing these commands we really ought to think a little
  784. to see if we have decided on the best way of doing things.  It is all
  785. very well instructing people or computers to do things, but it does make
  786. a tremendous difference if the commands which we issue are sensible.  With
  787. people, this makes them happier to carry out our plans, and with computers
  788. we simple get the job done quicker and more easily if we think things out
  789. carefully first.
  790.  
  791. Look again at our command list:
  792.  
  793.  
  794.               ClearScreen
  795.               SetNamePosition
  796.               SetNameTextStyle
  797.               PrintName
  798.               SetAdd1Position    *
  799.               SetAdd1TextStyle   +
  800.               PrintAddress1
  801.               SetAdd2Position    *
  802.               SetAdd2TextStyle   +
  803.               PrintAddress2
  804.               SetAdd3Position    *
  805.               SetAdd3TextStyle   +
  806.               PrintAddress3
  807.  
  808. Look at the lines marked with * and +.
  809.  
  810.  
  811. Now look again at another version:
  812.  
  813.         1     ClearScreen
  814.         2     SetNamePosition
  815.         3     SetNameTextStyle
  816.         4     PrintName
  817.         5     NextScreenLine     *
  818.         6     SetAddTextStyle    +
  819.         7     PrintAddress1
  820.         8     NextScreenLine     *
  821.         9     SetAddTextStyle    +
  822.         10    PrintAddress2
  823.         11    NextScreenLine     *
  824.         12    SetAddTextStyle    +
  825.         13    PrintAddress3
  826.  
  827. Notice that in the new version lines 5, 8, and 11 are all the same command,
  828. which we call NextScreenLine, and lines 6, 9 and 12 are also all the same
  829. command SetAddTextStyle.
  830.  
  831. This means that we will be able to re-use parts of our program, which will
  832. save time and effort.
  833.  
  834. We have managed to do this because we noticed that parts of the task could
  835. be described in a way which was repetitive and similar.
  836.  
  837. This is a very important part of designing computer software!
  838.  
  839. Now we have reduced the number of DIFFERENT commands we need to create
  840. from 13 to 9, but we can go further still:
  841.  
  842.         1     ClearScreen
  843.         2     SetNamePosition
  844.         3     SetNameTextStyle
  845.         4     PrintName
  846.         5     NextScreenLine
  847.         6     SetAddTextStyle
  848.         7     PrintAddress1
  849.         8     NextScreenLine
  850.         9     PrintAddress2
  851.         10    NextScreenLine
  852.         11    PrintAddress3
  853.  
  854. Here we still have 9 different commands, but we have removed the two last
  855. instances of "SetAddTextStyle" from our final program.  We can do this
  856. only because we know that once a text style is set in HeliOS, any further
  857. text will continue to be printed in that style.
  858.  
  859. We needed SPECIAL KNOWLEDGE to make this particular economy, and you will
  860. find that as you grow in experience you will on many occasions be able to
  861. make your programs more economical by "tricks" such as this.
  862.  
  863. We could go on making changes, but let us for now settle on this sequence
  864. of commands for our program.
  865.  
  866.  : PrintAddress
  867.  
  868.    ClearScreen
  869.    SetNamePosition
  870.    SetNameTextStyle
  871.    PrintName
  872.    NextScreenLine
  873.    SetAddTextStyle
  874.    PrintAddress1
  875.    NextScreenLine
  876.    PrintAddress2
  877.    NextScreenLine
  878.    PrintAddress3
  879.   ;
  880.  
  881.  
  882. Remember that we COULD write this as:
  883.  
  884.  : PrintAddress    ClearScreen SetNamePosition SetNameTextStyle PrintName
  885.  NextScreenLine SetAddTextStyle PrintAddress1 NextScreenLine PrintAddress2
  886.  NextScreenLine PrintAddress3 ;
  887.  
  888. Notice that this looks much more confused and is harder to read.
  889.  
  890. As we said above, because computers are sequential devices, operating on
  891. one command at a time, it is most useful to write programs with one command
  892. per line.  In this way it is much easier to see what is happening.
  893.  
  894. If all the sub-commands in PrintScreen were HeliOS CORE words we could
  895. proceed simply to write the program as expressed above.  However, looking
  896. at our definition of PrintAddress, we can see that HeliOS is going to need
  897. to know what we mean by several NEW sub-commands before it can carry out
  898. our instructions.  For example, we have not yet told HeliOS how to
  899. "ClearScreen", etc.
  900.  
  901. So, we now need to define all our new sub-command words, one at a time.
  902.  
  903. Let us do them in order, with ClearScreen first.
  904.  
  905. Look at this:
  906.  
  907.  :  ClearScreen
  908.  
  909.  SCRCLR
  910.  ;
  911.  
  912. You can see that ClearScreen has been defined as just one HeliOS CORE
  913. command called SCRCLR.
  914.  
  915. Actually, we are quite fortunate that HeliOS has this single word SCRCLR to
  916. clear a text screen: this means that our first definition is very simple!
  917.  
  918. The definition of ClearScreen above will create a new command word in the
  919. HeliOS dictionary called "ClearScreen", which actually does just the same
  920. as SCRCLR.  All we have achieved in this case is to make our new name for
  921. an existing word, but this is quite legitimate.  Whenever we now type
  922. "ClearScreen", either at the command line or in a program, HeliOS knows
  923. that we are wanting it to clear a text screen using its own command SCRCLR.
  924.  
  925. Actually, you can use this technique to rename any of the CORE HeliOS
  926. command words if you like.....
  927.  
  928. Let's now look at our second command:
  929.  
  930.  : SetNamePosition                  ( Column, Row - - - )
  931.  
  932.  CURPUT
  933.  CURSAVE
  934.  ;
  935.  
  936. This one is a little more interesting.
  937.  
  938. We here have two HeliOS internal commands which do the job we require:
  939. CURPUT will set up the text cursor position and CURSAVE will automatically
  940. remember it for future reference.
  941.  
  942. The HeliOS word CURPUT requires us to tell it where we want the cursor
  943. putting, which is quite reasonable, and we do this by simply writing the
  944. column and row position numbers before the command like this:
  945.  
  946. 20 20 CURPUT
  947.  
  948. This would place the cursor at column 20 row 20.
  949.  
  950. Quite easy!
  951.  
  952. The two numbers required by CURPUT to do its work are called "parameters",
  953. and the process of supplying these numbers is called "passing parameters".
  954.  
  955. You will soon see that HeliOS command words can pass parameters between
  956. themselves too, with one word passing its result to another, and so on.
  957.  
  958. Notice, if you are already a programmer in other languages, that HeliOS
  959. requirs no definitions of variables at this stage: you simple place the
  960. numerical parameters immediately before the command.
  961.  
  962. In fact, HeliOS uses a "Stack" for much of its parameter passing, and for
  963. now we will simply mention that a stack is a general storage place where
  964. any parameters can be stored until they are needed later.
  965.  
  966. When a HeliOS word requires parameters (in this case Column and Row), we
  967. have a conventional way of expressing this, which we call a "Stack diagram".
  968.  
  969. You perhaps noticed this in the definition of SetNamePosition:
  970.  
  971.  : SetNamePosition                  ( Column, Row - - - )
  972.                                     ^^^^^^^^^^^^^^^^^^^^^
  973.                                        Stack Diagram
  974.  
  975. Here is a generalised version of this stack diagram:
  976.  
  977.     ( Parameter1, Parameter2  - - - )
  978.  
  979. This tells us that the command in question takes two parameters and does
  980. not itself return any parameters.
  981.  
  982. Look at another general example of a Stack Diagram:
  983.  
  984.     ( Parameter1, Parameter2   - - -   Parameter3, Parameter4 )
  985.           ^           ^
  986.        1st Input  2nd Input   Command  1st Result  2nd Result
  987.        parameter  parameter            parameter   parameter
  988.  
  989. The "- - -" represents the operation of the command, the parameters on 
  990. the left represent things passed to the command before it operates, and
  991. parameters returned by the command are shown on the right.
  992.  
  993. You might have a word which takes two parameters and then returns one
  994. parameter itself.  For example "+" takes two numbers, adds them, and
  995. returns the sum of the two numbers as its result.
  996.  
  997. So "+" would have the following stack diagram:
  998.  
  999.     ( Parameter1, Parameter2  - - - Parameter3 )
  1000.  
  1001. or  ( Number1, Number2 - - - Result_Of_Addition )
  1002.  
  1003.  
  1004. Going back to CURPUT, which takes two column and row parameters, we can
  1005. notice something interesting: by putting CURPUT into our new command word
  1006. "SetNamePosition", this new word will also require the two column and row
  1007. parameters just like CURPUT.
  1008.  
  1009. This is easy to understand, if you realise that when HeliOS interprets
  1010. the SetNamePosition command, what it actually does is to perform the
  1011. sub-commands CURPUT and CURSAVE sequentially.
  1012.  
  1013. HeliOS says to itself "Ah, what do I do for SetNamePosition?  First I must
  1014. perform the CURPUT command, then the CURSAVE command".
  1015.  
  1016. So, "SetNamePosition" will require two parameters, and will have the
  1017. following "Stack diagram":
  1018.  
  1019. ( Column, Row - - - )
  1020.  
  1021. just like CURPUT.
  1022.  
  1023. You will see later how this works when you run the program.
  1024.  
  1025. Let's see how we are doing with our command list.
  1026.  
  1027.  : PrintAddress
  1028.  
  1029.    ClearScreen           - done
  1030.    SetNamePosition       - done
  1031.    SetNameTextStyle
  1032.    PrintName
  1033.    NextScreenLine
  1034.    SetAddTextStyle
  1035.    PrintAddress1
  1036.    NextScreenLine
  1037.    PrintAddress2
  1038.    NextScreenLine
  1039.    PrintAddress3
  1040.  ;
  1041.  
  1042. OK. Now let us make "SetNameTextStyle", something like this:
  1043.  
  1044.  : SetNameTextStyle
  1045.  
  1046.  1 FPENSET
  1047.  BOLDON
  1048.  ;
  1049.  
  1050. This time we use two more HeliOS sub-commands to make our new word.
  1051.  
  1052. The command FPENSET takes one parameter, which defines the colour of
  1053. the foreground text "pen".  In this case we set the text colour to "1",
  1054. which is BLACK.
  1055.  
  1056. Can you work out the stack diagram for FPENSET?
  1057.  
  1058. Here it is:     FPENSET    ( Pen - - - )
  1059.  
  1060. The command BOLDON switches HeliOS text mode to BOLD
  1061.  
  1062. You can see that SetNameTextStyle does not require any external parameters
  1063.  
  1064.  
  1065. The next command we need is "PrintName".
  1066.  
  1067.  : PrintName
  1068.  
  1069.  ." Mr. W. Smith"
  1070.  ;
  1071.  
  1072. This will print the text to the screen in the current text style and
  1073. colour.
  1074.  
  1075. Here we use again the previous simple method of including text in your
  1076. program (there are many other more sophisticated ways of doing this).
  1077.  
  1078. Let's look at our command list again.
  1079.  
  1080.  : PrintAddress
  1081.  
  1082.    ClearScreen           - done
  1083.    SetNamePosition       - done
  1084.    SetNameTextStyle      - done
  1085.    PrintName             - done
  1086.    NextScreenLine
  1087.    SetAddTextStyle
  1088.    PrintAddress1
  1089.    NextScreenLine
  1090.    PrintAddress2
  1091.    NextScreenLine
  1092.    PrintAddress3
  1093.  ;
  1094.  
  1095. Now we need:
  1096.  
  1097.  : NextScreenLine
  1098.  
  1099.  CURSET
  1100.  1 CURDN
  1101.  CURSAVE
  1102.  ;
  1103.  
  1104. This time we have three HeliOS commands.
  1105.  
  1106.  CURSET   - Restores the first CURSOR position we set.
  1107.  1 CURDN  - Moves the cursor down by one position.
  1108.  CURSAVE  - Saves the new cursor position.
  1109.  
  1110. Notice that we have specially designed this command so that it can be used
  1111. repetitively.  It was this "design feature" which allowed us to use this
  1112. one command in place of three separate commands in our "first version".
  1113.  
  1114. The next command, "SetAddTextStyle", is very similar to "SetNameTextStyle".
  1115.  
  1116.  : SetAddTextStyle
  1117.  
  1118.  BOLDOFF
  1119.  ITALON
  1120.  ;
  1121.  
  1122.  BOLDOFF - Switches off BOLD text.
  1123.  ITALON  - Switches on ITALIC text.
  1124.  
  1125. The commands "PrintAddress1" etc. are all very like "PrintName".
  1126.  
  1127.  : PrintAddress1
  1128.  
  1129.  ." 12, Blitter Street"
  1130.  ;
  1131.  
  1132.  : PrintAddress2
  1133.  
  1134.  ." ComputerLand"
  1135.  ;
  1136.  
  1137.  : PrintAddress3
  1138.  
  1139.  ." AmigaTown"
  1140.  ;
  1141.  
  1142. So, we have now created all the elements of our simple program, so let us
  1143. collect them all together:
  1144.  
  1145.  \ ****************
  1146.  \ Start of program
  1147.  \ ****************
  1148.  
  1149.  :  ClearScreen
  1150.  
  1151.  SCRCLR
  1152.  ;
  1153.  
  1154.  : SetNamePosition
  1155.  
  1156.  CURPUT
  1157.  CURSAVE
  1158.  ;
  1159.  
  1160.  : SetNameTextStyle
  1161.  
  1162.  1 FPENSET
  1163.  BOLDON
  1164.  ;
  1165.  
  1166.  : PrintName
  1167.  
  1168.  ." Mr. W. Smith"
  1169.  ;
  1170.  
  1171.  : NextScreenLine
  1172.  
  1173.  CURSET
  1174.  1 CURDN
  1175.  CURSAVE
  1176.  ;
  1177.  
  1178.  : SetAddTextStyle
  1179.  
  1180.  BOLDOFF
  1181.  ITALON
  1182.  ;
  1183.  
  1184.  : PrintAddress1
  1185.  
  1186.  ." 12, Blitter Street"
  1187.  ;
  1188.  
  1189.  : PrintAddress2
  1190.  
  1191.  ." ComputerLand"
  1192.  ;
  1193.  
  1194.  : PrintAddress3
  1195.  
  1196.  ." AmigaTown"
  1197.  ;
  1198.  
  1199.  : PrintAddress
  1200.  
  1201.    ClearScreen
  1202.    SetNamePosition
  1203.    SetNameTextStyle
  1204.    PrintName
  1205.    NextScreenLine
  1206.    SetAddTextStyle
  1207.    PrintAddress1
  1208.    NextScreenLine
  1209.    PrintAddress2
  1210.    NextScreenLine
  1211.    PrintAddress3
  1212.  ;
  1213.  
  1214.  \ **************
  1215.  \ End of program
  1216.  \ **************
  1217.  
  1218. Try compiling the small program above, either by:
  1219.  
  1220. 1. Highlighting it and pressing Amiga-e
  1221. 2. Cutting and pasting it into another editor, and running it from there.
  1222. 3. Typing it in at the command line or another editor.
  1223.  
  1224. Once you have compiled this code you can do a USER-vocabulary listing
  1225. and you will see each one of your new commands listed.
  1226.  
  1227. To see your new program working, type at the command line:
  1228.  
  1229. 12 12 PrintAddress WAITSPACE
  1230. ^^ ^^
  1231. Cursor position parameters.
  1232.  
  1233. Note that we have supplied the two parameters to PrintAddress.
  1234.  
  1235. This is interesting, because you will remember that we actually needed
  1236. these parameters originally to satisfy the requirements of the command
  1237. word CURPUT.
  1238.  
  1239. Then we included CURPUT in "SetNamePosition", so we realised that the
  1240. same two parameters would need to be supplied for this word, so that
  1241. they could in turn be passed to CURPUT.
  1242.  
  1243. Actually "SetNamePosition" is the SECOND command in our program
  1244. sequence, with "ClearScreen" coming first.  So what happens to the
  1245. two parameters now?
  1246.  
  1247. Think of it this way: when CURPUT, which is a sub-part of our program,
  1248. eventually executes, it will need two parameters, which it will pull out
  1249. of the general storage area we call the stack.  As far as CURPUT is
  1250. concerned, it does not care when or how the parameters got there, and
  1251. in fact it will fetch the last two numbers stored on the stack no
  1252. matter whether they are the correct ones or not.
  1253.  
  1254. It is YOUR job as a programmer to make sure that at the time when CURPUT
  1255. executes the last (top) two numbers on the stack are the required cursor
  1256. position parameters.
  1257.  
  1258. You can place these parameters on the HEliOS stack at any time: in fact
  1259. you can choose whether to type them in when you run the program, as above,
  1260. or include them in the program.
  1261.  
  1262. You COULD do any of these:
  1263.  
  1264.  
  1265.  : PrintAddress           : PrintAddress           : PrintAddress
  1266.  
  1267.    12 12                    ClearScreen              ClearScreen
  1268.    ClearScreen              12 12                    SetNamePosition
  1269.    SetNamePosition          SetNamePosition          12 12
  1270.    SetNameTextStyle         SetNameTextStyle         SetNameTextStyle
  1271.    PrintName                PrintName                PrintName
  1272.    NextScreenLine           NextScreenLine           NextScreenLine
  1273.    SetAddTextStyle          SetAddTextStyle          SetAddTextStyle
  1274.    PrintAddress1            PrintAddress1            PrintAddress1
  1275.    NextScreenLine           NextScreenLine           NextScreenLine
  1276.    PrintAddress2            PrintAddress2            PrintAddress2
  1277.    NextScreenLine           NextScreenLine           NextScreenLine
  1278.    PrintAddress3            PrintAddress3            PrintAddress3
  1279.  ;                        ;                        ;
  1280.  
  1281. Can you see why the first two examples are OK but the third one will fail?
  1282.  
  1283. Yes...the parameters are NOT on the stack when SetNamePosition executes,
  1284. but are placed there afterwards, which is too late.
  1285.  
  1286. This brings us to another important thing to remember:
  1287.  
  1288. IT IS THE PROGRAMMER'S RESPONSIBILTY ALWAYS TO ENSURE THAT CORRECT
  1289. PARAMETERS ARE PLACED ON THE STACK AT ALL TIMES
  1290.  
  1291. This is called "stack handling", and is a very vital part of your
  1292. initial learning of HeliOS.
  1293.  
  1294. HeliOS uses the stack because this is a very fast and efficient way
  1295. of passing parameters between commands.  You can actually use named
  1296. variables, or named constants, as you will soon see, but the stack
  1297. is fastest and in many ways easiest once you are used to it.
  1298.  
  1299. We will discuss the stack in depth later, but remember for now that
  1300. a good HeliOS programmer should always keep a clear idea of just what
  1301. is "on the stack" at any part of a program.
  1302.  
  1303. You can easily write yourself comments to help you with this if you
  1304. like:
  1305.  
  1306.  
  1307. : PrintAddress           \ Stack = Empty
  1308.  
  1309.   12 12                  \ Stack = 12 12
  1310.   ClearScreen            \ Stack = 12 12
  1311.   SetNamePosition        \ Stack = Empty
  1312.   SetNameTextStyle
  1313.   PrintName
  1314.   NextScreenLine
  1315.   SetAddTextStyle
  1316.   PrintAddress1
  1317.   NextScreenLine
  1318.   PrintAddress2
  1319.   NextScreenLine
  1320.   PrintAddress3
  1321. ;
  1322.  
  1323. Let us now look again at our original command line when typed in the
  1324. parameters as we ran the program:
  1325.  
  1326. 12 12 PrintAddress WAITSPACE
  1327.  
  1328. Why WAITSPACE?
  1329.  
  1330. The "WAITSPACE" will allow us to observe the results of the program
  1331. without anything else printing to the screen until <Space> is pressed.
  1332.  
  1333. This is quite useful, and you will probably find it convenient on many
  1334. occasions to use this command to halt a program temporarily.
  1335.  
  1336. ------------------------------------
  1337. Order of creation of HeliOS commands
  1338. ------------------------------------
  1339.  
  1340. Notice that when we collected together all our sub-commands into the
  1341. small program we constructed our main command "PrintAddress" last,
  1342. AFTER we had made all our sub-commands.
  1343.  
  1344. If you think about it this is obviously necessary, because you cannot
  1345. instruct HeliOS to use any command which you have not yet taught it!
  1346.  
  1347. In general using HeliOS you always progress from simple words using
  1348. HeliOS CORE functions to more complex compound words using commands
  1349. you have created earlier.  There are ways to avoid the necessity for
  1350. sequential command creation, but don't worry about this for now.
  1351.  
  1352. So this is another important rule:
  1353.  
  1354. HELIOS COMMANDS CAN ONLY CALL DIRECTLY EITHER CORE COMMANDS OR
  1355. COMMAND WORDS YOU HAVE DEFINED EARLIER IN THE PROGRAM
  1356.  
  1357. ***********************************************************************
  1358.  
  1359. --------------------------
  1360. Organisation and structure
  1361. --------------------------
  1362.  
  1363. In the example above we made a small attempt to improve the organisation
  1364. of our program between the stages of planning and final implementation.
  1365.  
  1366. Although the processing of information in a computer is essentially
  1367. sequential in nature, and your early programs will be simple sequences
  1368. of commands, it should soon become apparent that some degree of planning
  1369. and organisation is necessary to design an efficient program.
  1370.  
  1371. The fact is that computer programs contain structure, with small parts
  1372. of the whole working together in sometimes complex ways.  Once you begin
  1373. to write programs which can behave differently with different inputs,
  1374. you start to generate quite a high level of complexity.
  1375.  
  1376. There is never "only one way" to perform any complex task, and you will
  1377. often be faced with many ways of implementing a certain programming
  1378. requirement.
  1379.  
  1380. Always look carefully at the overall "structure" of your programs, and
  1381. at the way you have broken complex operations into smaller tasks.  This
  1382. is very important, and good organization goes together with sensible
  1383. and logical structuring of commands.  Of course this is a very personal
  1384. thing, and what is sensible to one person is idiotic to another!  In
  1385. spite of this, there IS often an overall optimum strategy which may be
  1386. determined by the constraints of the way the computer works.
  1387.  
  1388. In general you should try to balance economy of code with speed and
  1389. simplicity of operation: there will always be constraints and tradeoffs,
  1390. but you should always be in sufficient control to strike a good balance.
  1391.  
  1392. You should always PLAN your programs in such a way that there is minimal
  1393. duplication of effort, and you should always try to keep a view of the
  1394. greater whole as well as the individual parts.
  1395.  
  1396. In HeliOS programs you will:
  1397.  
  1398. 1. Break down large tasks into smaller sections.
  1399.  
  1400. 2. Choose appropriate and sensible names for these sub-commands in
  1401.    order to make your programs comprehensible and easily readable.
  1402.  
  1403. 3. Break down your sub-commands into still smaller and more manageable
  1404.    sections until you reach the lowest level of simple CORE commands.
  1405.  
  1406. Simple sequential programs are fairly straighforward in terms of logical
  1407. design, but you will encounter circumstances where your program has to 
  1408. behave differently according to external or internal conditions.  The
  1409. way sequential computer operations can be used to "adapt" with respect 
  1410. to time and different circumstances lies at the core of the fascination
  1411. of computer programming.
  1412.  
  1413. Computers would not be capable of nearly so many useful functions if they
  1414. could only carry out sequences of preprogrammed tasks.  In fact computers
  1415. are ingenious in that they can take "decisions" and modify their behaviour
  1416. depending upon the results of their own actions.
  1417.  
  1418. This "intelligent" behaviour comes, amazingly, from a simple use of
  1419. logical operations such as:
  1420.  
  1421.    Waiting for a certain condition to be "true".
  1422.  
  1423.    Doing one of several different things depending on a condition.
  1424.  
  1425.    Repeating "similar" or "modified" actions depending upon conditions.
  1426.  
  1427.    Counting and performing multiple tasks.
  1428.  
  1429.    etc. etc.
  1430.  
  1431. These considerations of "contingency" allow the simplest of computer
  1432. programs to have non-linear structures, and it is here that flexiblity
  1433. and intelligence on the part of the programmer come to the fore.
  1434.  
  1435. So, we have two forms of "structure" to consider in our early programming
  1436. efforts:
  1437.  
  1438. 1. Structure in terms of the breakdown of tasks into manageable "chunks",
  1439.    which really means structure in terms of code size and function.
  1440.  
  1441. 2. Structure in terms of logical behaviour, response to external or
  1442.    internal conditions, time related constraints etc.
  1443.  
  1444. Here is a simple HeliOS conditional construct, showing how a program
  1445. can easily interact with the outside world:
  1446.  
  1447.  : PressSpace?
  1448.  
  1449.  ." Please press a key."           \ Ask for user input
  1450.  
  1451.  CR                                \ CR = Carriage Return
  1452.  
  1453.  KEY                               \ Wait for a keypress
  1454.  
  1455.  32                                \ 32 = Code for <Space>
  1456.  =                                 \ Key = <Space>?
  1457.  IF
  1458.    ." Space was pressed!"          \ Yes!
  1459.  ELSE
  1460.    ." Space was not pressed!"      \ No!
  1461.  THEN
  1462.  
  1463.  CR                                \ CR = Carriage Return
  1464.  
  1465.  ." Thank you!"                    \ Finished!
  1466.  ;
  1467.  
  1468. Here you can see that one of two different logical paths will be taken
  1469. by the program depending on user input.
  1470.  
  1471. Look how HeliOS implemented the "conditional" construct of IF...ELSE...
  1472. THEN.
  1473.  
  1474. Here is what happens in the above program:
  1475.  
  1476. 1. We print a message and then do a "carriage return".  This is easy.
  1477.  
  1478. 2. We use the HeliOS CORE command KEY, which waits for user input and
  1479.    returns a numerical value depending upon which key was pressed.
  1480.  
  1481.    The stack disgram of KEY would be  ( - - - KeyValue )
  1482.  
  1483.    This means that after KEY there is one number, an input code, on
  1484.    the stack.
  1485.  
  1486. 3. The next entry in our program is "32", which puts the number 32 onto
  1487.    the parameter storage stack (on top of the input code).
  1488.  
  1489.    We now have two numbers on the HeliOS stack.
  1490.  
  1491. 4. The command "=" is used to compare two numbers which are fetched from
  1492.    the stack.
  1493.  
  1494.    As "=" gets the numbers from the stack it removes them, leaving the
  1495.    stack empty.
  1496.  
  1497.    When "=" has compared the two numbers, it will place a result code onto
  1498.    the stack.
  1499.  
  1500.    A "1" will be left on the stack if the two numbers were equal, and a
  1501.    "0" will be returned if they were unequal.
  1502.  
  1503.    The stack diagram of "=" is     ( number1 number2 - - - result )
  1504.                                                            0 or 1
  1505.  
  1506. 5. Now comes the important part: the use of the conditional "IF" command.
  1507.  
  1508.    The command "IF" removes a number from the stack and does two different
  1509.    things according to whether the number is zero or non-zero.
  1510.  
  1511.    If the number is non-zero, the code immediately after "IF" will be
  1512.    executed up to the point where either "ELSE" or "THEN" occur.
  1513.  
  1514.    If the number is zero, the code immediately after "IF" will be skipped
  1515.    and execution of code will continue immediately after the first occurence
  1516.    of "ELSE" or "THEN".
  1517.  
  1518. 6. The "ELSE" and "THEN" determine the code which will be executed after
  1519.    "IF", depending on conditions following the "=" comparison.
  1520.  
  1521. 7. The program completes by executing all the code after "THEN", in this
  1522.    case with a simple exit message.
  1523.  
  1524.  
  1525. Here is another way to use IF and THEN.
  1526.  
  1527.  : PressSpace?
  1528.  
  1529.  ." Please press a key."           \ Ask for user input
  1530.  
  1531.  CR                                \ CR = Carriage Return
  1532.  
  1533.  KEY                               \ Wait for a keypress
  1534.  
  1535.  32                                \ 32 = Code for <Space>
  1536.  =                                 \ Key = <Space>?
  1537.  IF
  1538.    ." Space was pressed!"          \ Yes!
  1539.  THEN
  1540.  
  1541.  CR                                \ CR = Carriage Return
  1542.  
  1543.  ." Thank you!"                    \ Finished!
  1544.  ;
  1545.  
  1546. This was actually simpler!
  1547.  
  1548. We simply used "IF" to determine whether or not to print our message,
  1549. and we have no alternative action defined by the "ELSE" command.
  1550.  
  1551. Look at these simple conditional constructs and make sure that you really
  1552. understand what is happening.
  1553.  
  1554. In particular, try to follow exactly what is happening to numbers on the
  1555. stack.
  1556.  
  1557. Here is the program again with stack comments:
  1558.  
  1559.  : PressSpace?
  1560.  
  1561.  ." Please press a key."         \ Ask for user input      Stack = Empty
  1562.  
  1563.  CR                              \ CR = Carriage Return    Stack = Empty
  1564.  
  1565.  KEY                             \ Wait for a keypress     Stack = Input
  1566.  
  1567.  32                              \ 32 = Code for <Space>   Stack = Input, 32
  1568.  =                               \ Key = <Space>?          Stack = Result
  1569.  IF                              \                         Stack = Empty
  1570.    ." Space was pressed!"        \ Yes!                        etc.
  1571.  ELSE
  1572.    ." Space was not pressed!"    \ No!
  1573.  THEN
  1574.  
  1575.  CR                              \ CR = Carriage Return
  1576.  
  1577.  ." Thank you!"                  \ Finished!
  1578.  ;
  1579.  
  1580. ----------------------------------
  1581. Functions, Data and Error Checking
  1582. ----------------------------------
  1583.  
  1584. Another way of analysing a program is in terms of the way "functions" are
  1585. used to operate on "data": whether a command "DOES" something (like the
  1586. addition function) or simply "IS" something (like a number which is added).
  1587.  
  1588. The syntax of some languages strictly controls how these different
  1589. categories are represented, to help you ensure that you write your code
  1590. in a way which prevents "incorrect" expressions.
  1591.  
  1592. HeliOS has no such enforced constraint, although you can if you wish impose
  1593. naming conventions for yourself, by using certain prefixes for certain
  1594. types of function etc..
  1595.  
  1596. Yet another abstraction is to specify the TYPE of "data/something" which
  1597. you are dealing with.  In some languages there is a very strict limitation
  1598. concerning which types of data each kind of function may operate upon.
  1599.  
  1600. This kind of "artificial" abstraction can have its uses, especially in
  1601. helping the computer language to "enforce" so-called "correct" programming
  1602. techniques.  However, checking all these things takes valuable computer
  1603. time and also enforces a rigid "straightjacket" on the programmer.
  1604.  
  1605. The fact is that all these "human" intellectual abstractions are basically
  1606. an imposition upon the way the computer works, since to a computer ALL
  1607. data is simply numerical and all processes obey a very simple binary logic.
  1608.  
  1609. Not only this, they can also soon become very irksome to an experienced
  1610. programmer who is accustomed to greater freedom.
  1611.  
  1612. HeliOS keeps things simple and efficient, staying close to the way the
  1613. computer CPU works and avoiding too many artificial constraints.
  1614.  
  1615. HeliOS, therefore, has no enforcement of such things as "data types", and
  1616. does very little by way of enforcement or error checking.  (Although since
  1617. HeliOS is extensible and user-definable you could build such a system for
  1618. yourself if you wished to do so!)
  1619.  
  1620. HeliOS provides a comprehensive and powerful set of logical tools such
  1621. as looping, timing, and conditional constructs.  It provides all the usual
  1622. data constructs such as constants, variables, text strings, data structures
  1623. and pointers.  It then leaves you to your own devices.........
  1624.  
  1625. HeliOS does, however, enforce "structured programming" in that it will not
  1626. easily allow you to jump from place to place forward and backward in your
  1627. code.  This is a good thing, because if allowed to do so most beginners
  1628. tend to write a tangled web of "spaghetti-logic" code and then often give
  1629. up before they have managed to disentangle themselves!
  1630.  
  1631. In general HeliOS programs progress logically and sequentially using simple
  1632. logical constructs such as IF....ELSE....THEN or BEGIN....UNTIL.  HeliOS
  1633. keeps things simple, free of constraint, and powerful.  It us down to you,
  1634. the programmer, to make sure that you use this freedom wisely, because
  1635. HeliOS will not waste time checking for your mistakes or forcing you to do
  1636. things "right".
  1637.  
  1638. ---------------------
  1639. The HeliOS Dictionary
  1640. ---------------------
  1641.  
  1642. Every command word which you create immediately becomes a fully fledged
  1643. part of the HeliOS command set, and can be used exactly like a HeliOS CORE
  1644. function.  The name of the command is immediately entered in the HeliOS
  1645. dictionary, as can be verified by performing a vocabulary listing.
  1646.  
  1647. When you create a new command that command is instantly "compiled", which
  1648. involves two things:
  1649.  
  1650. 1. HeliOS creates a special section of code which will be executed when
  1651.    your new command is called.
  1652.  
  1653. 2. HeliOS creates a new vocabulary entry.
  1654.  
  1655. These two actions, when repeated, build up two sections of information
  1656. in the computer's memory, and these two sections of information can be
  1657. saved and reloaded.
  1658.  
  1659. As you will see later, you can actually dispense with the "vocabulary"
  1660. data when "running" a stand-alone HeliOS program.  The vocabulary data
  1661. is really a simple reference listing of command words which is only
  1662. required when you are initially compiling a program or using the HeliOS
  1663. interpreter.
  1664.  
  1665. Here are two important statements which you should try to understand
  1666. and bear in mind as you work with HeliOS:
  1667.  
  1668. 1.  INTERPRETING : When you "interpret" a command, this actually
  1669.                    performs the command function and has no effect
  1670.                    on the dictionary.
  1671.  
  1672. 2.  COMPILING    : When you "compile" a command using a colon definition
  1673.                    the command function will NOT be performed.
  1674.  
  1675.                    The code which runs the new command function will be
  1676.                    compiled, along with the command name.
  1677.  
  1678. So:
  1679.  
  1680.   ." Hello World!"
  1681.  
  1682.   - Prints "Hello World!" and does not compile anything.
  1683.  
  1684.  
  1685.   : HELLO_WORLD  ." Hello World!" ;
  1686.  
  1687.   - Prints nothing but compiles a command called "HELLO_WORLD" which
  1688.     WHEN EXECUTED will print "Hello World!".
  1689.  
  1690. Remember that you can always examine the HeliOS vocabulary by using
  1691. the Interpreter menu functions or the command line functions:
  1692.  
  1693. VLIST   - Display CORE and USER vocabulary.
  1694. UVLIST  - Display USER vocabulary.
  1695. CVLIST  - Display CORE vocabulary.
  1696.  
  1697. Once a command word is in the dictionary, HeliOS will always understand
  1698. this word if you type it in your programs or at the command line.  In
  1699. fact, just like a person, HeliOS "understands" your instructions by
  1700. interpreting words, like this:
  1701.  
  1702. 1. HeliOS scans everything you write, one space-delimited word at a time.
  1703.  
  1704. 2. It then looks up the word in its dictionary.
  1705.  
  1706. 3. If it finds the word, it carries out the associated function.
  1707.  
  1708. 4. If it does not find the word in the dictionary HeliOS will try to
  1709.    find a matching symbol in its include files, and if it cannot do
  1710.    this it tries to interpret the word as a pure number.
  1711.  
  1712.    If all these options fail HeliOS issues a warning to you and stops
  1713.    the process of interpreting immediately.
  1714.  
  1715. To summarize, here are simple definitions of the "technical" terms
  1716. used when referring to program compilation/interpretation:
  1717.  
  1718. EXECUTION      = HeliOS takes a given word and carries out the
  1719.                  associated code.
  1720.  
  1721. INTERPRETATION = HeliOS reads your input and acts upon it by
  1722.                  executing the command.
  1723.  
  1724. COMPILATION    = HeliOS creates a new command and stores the name
  1725.                  and the associated functional code.
  1726.  
  1727. In case you are interested in more details, here is a brief description
  1728. of how HeliOS carries out compilation of a colon definition:
  1729.  
  1730. * HeliOS reads the input text and finds the colon character.
  1731.  
  1732. * It then EXECUTES the COLON command.
  1733.  
  1734. * The COLON code, when EXECUTED, does two things:
  1735.  
  1736.   1. It COMPILES the following word, the command name, into the vocabulary.
  1737.  
  1738.   2. It sets up an internal operational MODE such that HeliOS will now
  1739.      COMPILE all following words into the dictionary, until a SEMICOLON
  1740.      is encountered.
  1741.  
  1742.   3. It continues to COMPILE each successive word.
  1743.  
  1744.   4. It finds a SEMICOLON and terminates the new word definition,
  1745.      reverting back to INTERPRET mode.
  1746.  
  1747. -----------------------------------------
  1748. The concepts of RUN-TIME and COMPILE-TIME
  1749. -----------------------------------------
  1750.  
  1751. It will be more apparaent to you what these terms mean when you have
  1752. used HeliOS for some time, but even at this early stage we should give
  1753. a brief mention to these important concepts.
  1754.  
  1755. For any command word
  1756.  
  1757. COMPILE-TIME is the time when the word is compiled
  1758.  
  1759. RUN-TIME     is the time when the word is executed, either when a program
  1760.              executes or when a word is interpeted at the command line.
  1761.  
  1762. This sounds simple but there are subtleties to the situation!
  1763.  
  1764. For example, when a new word is being created, it is RUN-TIME for the
  1765. HeliOS command words which actually DO THE COMPILING, but COMPILE-TIME
  1766. for the new word itself.
  1767.  
  1768. In other words, the terms RUN_TIME and COMPILE-TIME relate to states of
  1769. particular words at a given time, and NOT the HeliOS system as a whole.
  1770.  
  1771. ----------------
  1772. The HeliOS Stack
  1773. ----------------
  1774.  
  1775. As mentioned above, the stack is a place wjhere HeliOS stores numbers
  1776. on a temporary basis.
  1777.  
  1778. It is rather as if HeliOS were to say to the stack "Here you are, please
  1779. hold these for me while I go and do something else!  Please give them to
  1780. the next person who asks for them.".
  1781.  
  1782. The stack, as its name implies, is rather like a pile of data items,
  1783. and each new item simply goes onto the top of the pile.
  1784.  
  1785. Look at this simple diagram:
  1786.  
  1787. a  <---- Top item
  1788. b  <---- 2nd item
  1789. c  <---- 3rd item
  1790. d  <---- 4th item
  1791.  
  1792. etc. etc.
  1793.  
  1794. Let us put just one number, the number 4, on the stack:
  1795.  
  1796. 4  <--- Top of stack
  1797.  
  1798. Now let us put just another number, the number 8, on the stack:
  1799.  
  1800. 8  <--- Top of stack
  1801. 4  <--- 2nd place on stack
  1802.  
  1803. Notice how each new number becomes the "top" number and displaces any
  1804. other numbers downwards.
  1805.  
  1806. If a computer, which is a simple minded creature, wants to do something
  1807. like an addition sum, it cannot do this quite so flexibly as we can.
  1808.  
  1809. Faced with the problem of adding 4 to 8 to give 12, we clever humans
  1810. might think something like this.
  1811.  
  1812. "Ah, first I see the number 4: lets see what we need to do with it."
  1813.  
  1814. "Oh, a "+" sign, so I need to find another number to add."
  1815.  
  1816. "There it is: 8.  I will add them together. The result is 12."
  1817.  
  1818. "Whether I write 12 down or just remember it, I KNOW the answer was 12".
  1819.  
  1820.  
  1821. Or we might look at it differently, like this:
  1822.  
  1823.  
  1824. "Ah, two numbers 4 and 8."
  1825.  
  1826. "What is it that I have to do with them?"
  1827.  
  1828. "Ah, I see, a "+" sign, so I will add them together to get 12"
  1829.  
  1830. "Whether I write 12 down or just remember it, I KNOW the answer was 12".
  1831.  
  1832.  
  1833. Or we might look at it differently again:
  1834.  
  1835. "Ah, a "+" sign."
  1836.  
  1837. "This needs two numbers to add, so what are they?"
  1838.  
  1839. " I see, 4 and 8, that gives 12."
  1840.  
  1841. "Whether I write 12 down or just remember it, I KNOW the answer was 12".
  1842.  
  1843. You might think all these are boringly similar and obvious, but a computer
  1844. does not even have this limited choice.
  1845.  
  1846. Putting it simply:
  1847.  
  1848. *   A computer has NO choices about how to carry out an operation.
  1849.  
  1850. *   A computer needs to have its DATA loaded FIRST into its processing
  1851.     unit's data storage space.
  1852.  
  1853. *   A computer carries out an operation like "+" by ASSUMING that data
  1854.     has ALREADY been stored in its processing unit data storage space.
  1855.  
  1856. *   A computer has no automatic MEMORY or KNOWLEDGE.
  1857.  
  1858. *   A computer simply places the result of its calculation in its
  1859.     processing unit's data storage space, then carries on and forgets
  1860.     what has happened.
  1861.  
  1862. So, to perform addition, a computer needs to:
  1863.  
  1864. 1. Get the first number
  1865. 2. Get the second number
  1866. 3. Add then together
  1867. 4. Store the result
  1868.  
  1869. The computer is quite inflexible and can work in no other way.
  1870.  
  1871. If you write an addition "sum" like this:
  1872.  
  1873. 4 + 8
  1874.  
  1875. and pass it to the computer (as some computer languages do) the computer
  1876. has to be made to carry out various extra manipulations INTERNALLY by
  1877. the computer language BEFORE it can do the calculation in the form it
  1878. can work with. This is slowing everything down because more work is
  1879. having to be done UNNECESSARILY by the computer.
  1880.  
  1881. This is something like what the computer language would have to do
  1882. internally, unseen by you:
  1883.  
  1884. 1. Read the first number.
  1885.  
  1886. 2. It does not know what to do with it yet, so it stores it.
  1887.  
  1888. 3. Read the "+" command.
  1889.  
  1890. 4. It looks up the "+" command to see what this command does when it
  1891.    executes.
  1892.  
  1893. 5. It sees that two numbers are required for this command.
  1894.  
  1895. 6. It stores or "remembers" that it is about to do a "+" operation.
  1896.  
  1897. 7. It gets the second number and stores it.
  1898.  
  1899. 8. It goes back to recall what command it was performing.
  1900.  
  1901. 9. It performs the addition.
  1902.  
  1903. On the other hand, if YOU supply the computer with its data in the form
  1904. in which it can handle it directly, you get much faster operation.
  1905.  
  1906. If you write the "sum" above like this:
  1907.  
  1908. 4 8 +
  1909.  
  1910. look how easy it is for the computer.
  1911.  
  1912. 1. Get a number onto the stack
  1913.  
  1914. 2. Get a number onto the stack
  1915.  
  1916. 3. Execute the "+" command, which takes two numbers off the stack and
  1917.    replaces the result on the stack.
  1918.  
  1919. The operation is over quickly and efficiently, the computer can go on
  1920. to do something else, and the result is automatically "remembered" on
  1921. the stack, where YOU can do something with it.
  1922.  
  1923. This means that computers can work more efficently, in general, by being
  1924. given numbers first, followed by function commands.
  1925.  
  1926. This is how HeliOS always works, because we are only interested in maximum
  1927. speed and efficiency: wherever the programmer can save the computer extra
  1928. work the program will run quicker.
  1929.  
  1930. The stack is a nice quick and simple "universal" storage area which is
  1931. organised internally in such a way the HeliOS can feed the computer's
  1932. central processing unit directly from the stack very quickly indeed.
  1933.  
  1934. The method of putting numbers on the stack BEFORE the operator function
  1935. is performed is called POSTFIX notation or REVERSE_POLISH notation.
  1936.  
  1937. The method of putting the operator IN-BETWEEN the number parameters
  1938. is called INFIX notation.
  1939.  
  1940. HeliOS uses postfix notation rather than infix notation for another
  1941. reason besides the one mentioned above.
  1942.  
  1943. Much of the power of HeliOS is derived from the fact that postfix
  1944. notation allows a very elegant and simple mechanism for passing
  1945. parameters between command functions.
  1946.  
  1947. Look at these examples:
  1948.  
  1949. The function  +  gets two numbers from the stack and adds them.
  1950.  
  1951. The function  FPENSET gets one number from the stack and sets a pen colour.
  1952.  
  1953. The function  CURPUT  gets two numbers from the stack and sets the cursor.
  1954.  
  1955. So we can have a word like this:
  1956.  
  1957. : FANCYPRINT 12 + CURPUT FPENSET ." Hello!" ;
  1958.  
  1959. with a stack diagram   ( Colour, Column, RowOffset - - - )
  1960.  
  1961. Look what happens here:
  1962.  
  1963. The function FANCYPRINT will expect three numbers already on the stack:
  1964.  
  1965. RowOffset  <- Top of stack
  1966. Column     <- 2nd on stack
  1967. Colour     <- 3rd on stack
  1968.  
  1969. It then puts the number 12 on TOP of the others:
  1970.  
  1971. 12         <- Top of stack
  1972. RowOffset  <- 2nd on stack
  1973. Column     <- 3rd on stack
  1974. Colour     <- 4th on stack
  1975.  
  1976. It then adds 12 to the RowOffset value, giving a cursor displacement
  1977. offset from "Row 12", which is where we want to place our text.
  1978.  
  1979. This "final" calculated Row value is placed back on the stack:
  1980.  
  1981. RowOffset+12 (= Calculated Row position) <- Top of stack
  1982. Column                                   <- 2nd on stack
  1983. Colour                                   <- 3rd on stack
  1984.  
  1985. The function CURPUT is then executed, and we know that CURPUT takes
  1986. two numbers from the stack and places the cursor at that position.
  1987.  
  1988. The stack now looks like this:
  1989.  
  1990. Colour                                   <- TOP of stack
  1991.  
  1992. The function FPENSET is then executed, and we know that FPENSET takes
  1993. one number from the stack and sets the text colour.
  1994.  
  1995. Finally the ." function prints the following text.
  1996.  
  1997. Look at this carefully to see how simply the parameter passing works,
  1998. then try to work out how it might work with INFIX notation.
  1999.  
  2000. You will probably agree that although it may be initially "strange"
  2001. compared to our habitual "human" use of INFIX notation, POSTFIX
  2002. notation is a great way of controlling a computer program!
  2003.  
  2004. Since HeliOS is concerned with helping you control your Amiga in the
  2005. most efficient way possible, you are going to HAVE to do your share of
  2006. the work and learn to use the stack and POSTFIX notation!
  2007.  
  2008. You will find this really very easy after the first few HeliOS
  2009. programming sessions, and you will notice that the HeliOS Interpreter
  2010. has a "real-time" permanent stack display to help you get used to
  2011. how the stack operates.
  2012.  
  2013. Try doing some simple arithmetic operations in the Interpreter and watch
  2014. the way the stack display changes.  Stick with this until you really are
  2015. confident that you have grasped the way the stack works, because this
  2016. understanding is absolutely vital to everything you will do in HeliOS.
  2017.  
  2018. You may find it useful to try these simple command words:
  2019.  
  2020.   .        Prints the number on top of the stack and removes it
  2021.  
  2022.   .s       Prints all the stack contents without changing anything
  2023.  
  2024.  DUP       Duplicates the top stack number
  2025.  
  2026.  SWAP      Exchanges the top two stack numbers
  2027.  
  2028.  ROT       Moves the 3rd number on the stack to the top
  2029.  
  2030.  OVER      Duplicates the 2nd number on the stack on top of the stack
  2031.  
  2032.  
  2033. The last four words are called stack manipulation words.
  2034.  
  2035. There are many of these stack manipulation words in the HeliOS CORE
  2036. dictionary.
  2037.  
  2038. Look at the dictionary now and read the notes concerning these words,
  2039. then try using a variety of them to become accustomed to the way the
  2040. stack can be manipulated.
  2041.  
  2042. Having done this you will have had some experience of what is called
  2043. "stack management".
  2044.  
  2045. Stack management is very important in HeliOS: you can imagine that 
  2046. in any language which uses stack parameter passing you will very often 
  2047. have the need to adjust and modify the stack.
  2048.  
  2049. This can be interesting and often even becomes quite addictive, with
  2050. many programmers taking delight in managing very complex stacks.
  2051.  
  2052. Do NOT attempt to work with large stack sizes!
  2053.  
  2054. The stack is designed for quick and simple parameter passing, and if
  2055. you find that things are getting complicated you should either break
  2056. your program down into simpler more manageable chunks or resort to
  2057. using named CONSTANTS and VARIABLES.
  2058.  
  2059. We will explain CONSTANTS and VARIABLES later, but basically these
  2060. allow you to manipulate numbers using names rather than simply
  2061. referring to a position on the stack.  You will find that in some
  2062. cases the stack is more efficient, and sometimes using CONSTANTS and
  2063. VARIABLES is better.  You will soon learn your own programming style
  2064. and preferences, but you should NOT try to avoid using the stack
  2065. by using methods you might be accustomed to from other programming
  2066. languages: the stack is a dynamic and efficient programming tool.
  2067.  
  2068. Finally here are two more terms which you will find used in reference
  2069. to the stack:
  2070.  
  2071.  
  2072. "Stack overflow"
  2073.  
  2074. This means that there are too many numbers on the stack.
  2075.  
  2076. "Stack underflow"
  2077.  
  2078. This means that there are too few numbers on the stack for a given function.
  2079.