关于翻译我们要做什么
Project 3
P3
Interpreter – simple mode
Interaction with the environment, everything is known (table of symbols, stacks, virtual machine, etc.)
No definition of new functions, only the basic functions;
You have to separate the information!
Example
The function +
Takes two integers on the data stack, adds them and puts the result on the data stack.
In calculator mode, it must verify that the two objects to be added are of the correct type.
In compiled mode, this check is unnecessary, as it was done during compilation. There are two modes to predict! The interpreted mode and the compiled mode!
P4
Distribution of information
1 In the symbols table, we must put:
The name, number of parameters, their types and the type of results;
In the code (interpreted mode: to put the verification of the types, compiled mode: it is useless to put it);
2 In the virtual machine, you must
The code ;
And …?
P5
Base objects: will be defined in C language
1 The basic types (of the objects that will be on the data stack): the integers, the addresses of the strings
(Where the strings will be stored?);
2 Other types? Type recognition functions?
3 the basic functions: dup, drop, swap, ., count, type, =, *, +, -, etc. 4 the control structures will be reserved for the compilation …
P6
String problem
The usual storage in Forth (and therefore in LAC) is the length, followed by the ascii codes of the characters composing it.
Example
The string “Alain” will be stored in the form:
5 65 108 97 105 110
In language C, it will be stored in the form:
65 108 97 105 110 0
P7
Basic Words
dup (n – n n): duplicates the top of the data stack, if it is of type t, we obtain two objects of type t.
drop (n -): removes the top of the data stack.
swap (n1 n2 – n2 n1): exchanges the two elements on the top of the data stack.
. (e -): prints the integer that is on the top of the data stack and removes it. count (ad – ad ‘e): takes the string address at the top of the stack and replaces it with the start address of the string without the length and sets the length to the vertex (integer type).
type (ad e -): prints e characters whose codes are from ad.
= (E1 e2 – b): tests if e1 = e2 (two integers) and stacks a boolean.
P8
Simple choices
On the data stack, there will only be integers (which can be read as integers, string addresses, booleans, etc.)
On the return stack, there will only be integers (which will not be typed).
On the stack of types, there will be only integers (each type bearing a number). In the symbols table and the virtual machine, there will only be integers (to
simplify reading and writing). P9
Example of storage: function +
• In the symbol table
012345678
0 1 43 2 1 1 1 1 0
L + n1 t1 t2 n2 t3 c
Where l = 1 is the length of the name, 43 is the code of +, n1 the number of parameters on the data stack, t1 the type of the first parameter (on the top of the stack) – the integer type is encoded 1, T2 the type of the second parameter, n2 the number of output parameters, t3 its type and c where to find the + code in the virtual machine. (C is called the Cfa of + – Code Field Address).
• In the virtual machine
01
00
Where the first 0 indicates that it is a basic function (coded in C) and the second 0 its number among the basic functions.
P10
Example of storage: the swap function
In the symbol table
9 10 11 12 13 14 15 16 17 18 19 20 21
1 4 115 119 97 112 2 0 0 2 0 0 2
A l s w a p n1 t1 t2 n2 t3 t4 c
Where a = 1 denotes the address in the table of symbols where the name of the function previously defined (here +) begins, and c = 2 denotes the Cfa of swap. Any type was coded by 0.
In the virtual machine
23 01
P11
Balance sheet
We also need
A place to store strings;
A table containing all the basic functions;
Of structures for the symbol table, allowing you to know where you can start writing, where the last defined word is, and so on. And for the virtual machine too …
… where are the types defined? We put numbers, how to make new types?
Some functions (eg if) cannot be used in calculator mode, how to say it?
Some functions (eg if) have something to do at the time of compilation (which is often different what it does at runtime), how to say it?
P12
To do
Define language objects;
Programming an executor to read a virtual machine where there are only basic functions;
He must be able to run a chain of the kind
2 3 4 * swap 6 + -.
There must be an error at execution when there is a string of the kind
“Alain” 2 *
One must use the virtual machine