Description of the IMP language
Requisitos de finalización
Comments
- starts by // until the end of the line
- starts by /* and finishes at the first occurrence of */
Expressions (e)
- signed integers constants
- booleans : true and false
- variables (x,y) : starts with a lowercase alphabetical character or the character _, possibly continues with alphabetical characters, digits or _
- binary infix operations (only +, * and <) e1 op e2
- function calls f(e1,..,en)
- expressions between parentheses (e)
Instructions (i)
- putchar(e) : print the character whose ASCII code is the value of e
- e; : evaluate the expression e
- x=e; put the value of expression e in variable x
- if (e) { li1 } else { li2 } : conditional
- while (e) { li } : while loop
- return(e); : exit a function call with the value of e
- lists of instructions (li) are just sequences of instructions
Programs
- A sequence of global variables declarations var id;
- A sequence of function declarations : includes formal parameters x1,..,xn, local variables y1,..yk and body as a list of instructions
function f (x1,..,xn) {var y1; ... var yk; li }
A function called main (expecting 1 argument) will be executed when the program is called.
Example
var retour;
var espace;
function main(arg) {
var i;
i = 0;
retour = 10;
espace = 32;
while (i < arg+1) {
affiche_ligne(i, arg);
putchar(retour);
i = i + 1;
}
}
function affiche_ligne(i, n) {
var j;
j = 0;
while (j < n+1) {
if (i*i + j*j < n*n) {
putchar(46);
} else {
putchar(35);
}
putchar(espace);
j = j+1;
}
}
Última modificación: domingo, 28 de septiembre de 2025, 18:24