Description of the tests¶
In order to incrementally build the compiler, a list of program tests has been proposed. This table summarizes what are the new elements of the Asl language that appear in each test with respect to the previous one.
Some indications on modifying compiler components are also given, but should be taken just as a guide.
Tests for parsing and type check¶
The following tests can help you to define the grammar of the whole ASL language and to complete the semantic analysis (SymbolsVisitor and TypeCheckVisitor modules). Some changes in the ASL grammar will produce errors executing g++ on CodeGenVisitor.cpp. Adjust this module when necessary.
Test |
New structures / new checks |
Changes in Asl grammar |
Changes in SymbolsVisitor |
Changes in TypeCheckVisitor |
Adjust in CodeGenVisitor |
---|---|---|---|---|---|
|
Other basic types, new operators, values, parenthesis, … Use of type coercion |
Update rule type and rule expr. New tokens, floats, … |
Update variable declaration (new basic types). See module TypesMgr |
Type check of new expressions. Type coercion from int to float (update both .h and .cpp) |
|
|
Multiple variable declarations in a single line. (new token true) |
Update variable declaration |
Update variable declaration |
(new value true) |
Fix access to the text of the first variable to avoid compilation error |
Additional statements |
|||||
|
while and return statements. else in conditionals. (new operator !=) |
Update rule statement |
Type check of new statements (update both .h and .cpp) Fix access to statement in if children (there are two now) |
Fix access to statement children in if (there are two now) to avoid compilation error |
|
Function declaration with parameters |
|||||
|
Functions may have parameters and return values |
Update function declaration (use new rules). Update return statement |
Update visitFunction to create the appropriate function type. See module TypesMgr. Hint: add new visit methods to process parameters |
||
Array access |
|||||
|
Array access in expressions and in l-value expressions |
Update rules left_expr and expr |
Add new visitors for array access in left_expr and in expr (update both .h and .cpp) |
Modify the visitor for rule left_expr to use the new visit method |
|
Function call |
|||||
|
Call to function as a form of expression. Functions without parameters |
Update rule expr to accept function calls |
Add new visitors for function call. Type check the expression appropriately (update both .h and .cpp) |
||
|
Call to function with parameters, without errors in arguments. No main function detection (automatic) |
Type check arguments (actual parameters) |
|||
|
Call to procedures with parameters, without errors in arguments. Inappropriate calls to procedures and functions, regardless of arguments |
Update statements |
Type check of function call and compute expression type. Type check arguments (actual parameters) |
||
|
Incorrect use of a function. l-value expressions in read |
Compute properly the IsLValue decoration of the identifiers (probably nothing) |
|||
Array declaration |
|||||
|
Array declarations |
New structured type. Hint: use 2 rules, type and basic_type |
Add new visit methods to process types. See module TypesMgr. Note: remember that functions only return basic types (update both .h and .cpp) |
Probably nothing |
|
|
Use of read statement |
Probably nothing if type check of array access is correct |
|||
Return statement |
|||||
|
Extensive use of return statement in procedures and functions, with and without type coercion. |
Note: remember that the first declaration of an identifier in a given scope prevails over the rest (on that scope) |
Update type check of return statement. Hint: the return type of the current function can be obtained from SymTab in visitFunction, and saved/retrieved with the methods setCurrentFunctionTy and getCurrentFunctionTy (available in TypeCheckVisitor). |
||
|
Check undeclared identifiers |
||||
Errors in parameters |
|||||
|
Check that parameters must be declared in the local scope, with re-declarations of symbols in the same scope. Also, function calls may have errors due to the type of the arguments |
Nothing if visitFunction
was properly updated in
the test |
In a function call, remember that arguments (actual parameters) must always be processed. Now, also type check actual vs. formal parameters See module SemErrors |
||
|
Similar to |
||||
|
Similar to |
||||
|
Similar to |
||||
|
Function and procedure calls may have a wrong number of arguments |
Type check function and procedure calls to detect also these errors. See module SemErrors |
|||
|
Use of operator modulo (%) |
(Update rule expr) |
Type check the operator modulo |
||
Operations on arrays |
|||||
|
Array assignments, arrays as parameters, type coercion, … Similar to previous tests |
Probably nothing |
Tests for code generation¶
The following tests can help you to complete the code generation (CodeGenVisitor module). Some changes may involve your ASL grammar and/or type check. Adjust Asl.g4 and TypeCheckVisitor module if necessary.
Test |
New structures / new code |
Adjust in Asl grammar |
Adjust in TypeCheckVisitor |
Changes in CodeGenVisitor |
---|---|---|---|---|
|
Multiple variable declarations in a single line. Other basic types, new operators, values, parenthesis,… Type coercion in some expressions. Extend write statement with new types |
Update variable declaration. Update code for write statement. Generate code for new expressions, with coercion int \(\rightarrow\) float in arithmetic operators. (Update both .h and .cpp). See module code |
||
|
Use of while statement. New operators >, < (without type coercion). Boolean values true and false |
If necessary, add value false |
If necessary, type check the new value |
Generate code for while statement, and for new expressions |
Function call with parameters |
||||
|
Functions may have parameters and return values. Call to a function as a form of expression (only parameters of basic types). Use of return expr statement (without type coercion) |
Update function declaration (visitFunction). Add new visitors for function call, and generate code for this expression appropriately (update both .h and .cpp). See module code |
||
|
Call to a procedure with parameters of basic types (without type coercion). New operator <= with type coercion |
Update procedure call to allow parameters. Relational operators with coercion int \(\rightarrow\) float |
||
Array declaration and access |
||||
|
Use of array type in declarations of local variables. Array access in expressions, and in l-value expressions. if-then-else statement. Function calls with type coercion in parameters |
If necessary, complete the if statement with else branch |
If necessary, complete the type check of the if statement |
Update assignment statement: now a value can be assigned to an array position. Update if statement. Update function call to allow coercion in actual parameters |
|
Type coercion in assignments, and in actual parameters of procedure calls. Use of read statement into non int expressions. Call to functions discarding the result (like a procedure) |
Update assignment to allow coercion. Update read statement. Update code generation in “procedure” calls |
||
Arrays as parameters |
||||
|
Use of parameters of type array: access to the value and to the address of an array position. Procedure calls with actual parameters of type array (passed by reference) |
Update procedure call to allow actual parameters of type array (passed by reference). Only local arrays are passed. |
||
|
Function calls with actual parameters of type array (passed by reference) |
Update function call to allow actual parameters of type array (passed by reference). Only local arrays are passed |
||
|
Definition and use of the function factorial |
Probably nothing |
||
|
Definition and use of the function prod_escalar (dot product of two arrays) |
Probably nothing, but check the pass of arrays as parameters |
||
|
Extend write statement with char expression. Operator modulo. Values of type char |
Update write statement. Add modulo operator in arithmetic expressions. Generate code for new values |
||
Array assignment |
||||
|
Array assignment (a = b), where a,b may be local variables and/or parameters |
Update assignment statement |
||
|
Additional checks of arrays passed as parameters, where the array is not the first param. |
Probably nothing |
||
|
Unary operator + |
Generate code for this expression |