-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathintro.mi
156 lines (133 loc) · 5.66 KB
/
intro.mi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
dnl $ Id: $
dnl Copyright{2000-2024}: Albert van der Horst, HCC FIG Holland by GNU Public License
A Forth system is a database of small programs.
The database is called the dictionary.
The programs are called forthdefi({word})'s, or definitions.
The explanation of words from the dictionary is called
a glossary.
First of all, a Forth system is an environment that you enter by
running it: forthbreak() forthsamp({thisforth}) forthbreak() Like
in a Disk Operating System a forthdefi({word}) is executed
by typing its name, but unlike in a DOS several programs
can be specified on the same line, interspersed with
numbers. Also names can be anything, as long as they don't
contain spaces.
A program may leave one or more
results, and the next program can use it.
The latest result is used up first, hence the name lifo buffer.
(last in, first out).
For example:
forthexample(
{_HOSTED_(
{_HOSTED_LINUX_( {albert@@apple:~/forth/fig86 > thisforth}){}dnl
_HOSTED_MSDOS_({C:\ciforth>thisforth}){}dnl
_HOSTED_DPMI_({[After clicking on the thisforth icon]})}){}dnl
_BOOTED_({[After booting]})
_PROTECTED_(80386){}_REAL_(8088) ciforth beta ${R}CSfile: ci86.gnr,v $ ${R}evision: 6.122 $
1 2 + 7 *
OK
.
forthunderline(21 OK) })
1 2 and 7 are numbers and are just remembered as they are typed in.
forthsamp({OK}) and forthsamp({21 OK}) are the answer of the
computer.
forthcode(+) is a small program with an appropriate name.
It adds the two numbers that were entered the latest, in this
case 1 and 2. The result 3 remains, but 1 and 2 are consumed.
Note that a name can be anything, as long as it doesn't contain
spaces.
The program forthcode(*) multiplies the 3 and the 7 and the result is 21.
The program forthcode(.) prints this results. It could have
been put on the same line equally easily.
You will be curious about what are all those commands available.
Of course they are documented, but you can find the exact set
by typing forthcode({WORDS}) .
Programs can be added to the database by special programs: the so
called forthdefi({defining word})'s.
A defining word generally gets the name of the
new word from the input line.
For example: a constant is just a program that leaves always
the same value.
A constant is created in this way, by the defining word forthcode({CONSTANT}) :
forthexample({127 CONSTANT MONKEY 12 .
12 OK})
You can check that it has been added, by typing forthcode({WORDS}) again.
The above must not be read like:
forthbreak()
a number, two programs and again a number etc.... ,
forthbreak()
but as:
forthbreak()
a number, a program and a name that is consumed,
forthbreak()
and after that
life goes on. The forthsamp({12 .}) we put there for demonstration purposes,
to show that forthcode({CONSTANT}) reads ahead only one word.
On this single line we do two things, defining forthsamp({MONKEY}) and printing the
number 12.
We see that forthcode({CONSTANT}) like any other program consumes some
data, in this case the 127 that serves as an initial value for
the constant called forthvar({MONKEY}) .
You may get forthsamp({ constant ? ciforth ERROR # 12 : NOT RECOGNIZED }).
That is because you didn't type in the above precisely.
thisforth is case sensitive.
If you want to change that consult the section "Common problems".
(forthpxref({Errors})).
A very important defining word is forthcode({:}), with its closure
forthcode({;}).
forthexample({: TEST 1 2 + 7 * ; 21 .
21 OK })
In this case not only the name forthvar({TEST}) is consumed, but none
of the remaining numbers and programs are executed, up till the
semicolon forthcode({;}).
Instead they form a specification of what forthvar({TEST}) must do.
This state, where Forth is building up a definition, is called
forthdefi({compilation mode}) .
After the semicolon life continues as usual.
Note that forthcode({;}) is a program in itself too.
But it doesn't become part of forthcode({TEST}) . Instead it is executed
immediately.
It does little more than turning off compilation mode.
forthexample({TEST TEST + .
42 OK
: TEST+1 TEST 1 + . ; TEST+1
22 OK})
We see that forthvar({TEST}) behaves as a shorthand for the line up till
the semi colon, and that in its turn it can be used as a building
block.
The colon allows the Forth programmer to add new programs
easily and test them easily, by typing them at the keyboard.
It is considered bad style if a program is longer than
a couple of lines.
Indeed the inventor of Forth Chuck Moore has written splendid
applications with an average program length of about one line.
Cathedrals were built by laying stone upon stone,
never carved out of one rock.
The implementation of the language Forth you look at is old
fashioned, but simple. You as a user have to deal with only three
parts/files : this documentation, the executable program, and the
library file, a heap of small programs in source form.
_VERBOSE_({There may be several documentation files, but they contain the
same information in a different format.})
There is an ISO standard for Forth and this Forth doesn't fully
comply to it. Still by restricting yourself to the definitions marked
as ISO in the glossary, it is quite possible to write an application
that will run on any ISO-compliant system.
Because of the way Forth remembers numbers
you can always interrupt your work and continue.
For example
forthexample({: TEST-AGAIN
1 2 + [ 3 4 * . ]
12 OK
7 * ;
OK })
What happened here is that
some one asked you to calculate ``3 times 4'' while you
were busy with our test example. No sweat!
You switch from compilation mode
to normal (interpret) mode by forthcode({[}) , and back
by forthcode({]}).
In the meantime, as long as you don't leave numbers behind,
you can do anything.
(This doesn't apply to adding definitions, as you are
in the process of adding one already.)