This shows you the differences between two versions of the page.
|
alf:teme:tema4_en [2020/04/21 21:31] alexandru.radovici |
alf:teme:tema4_en [2020/04/25 12:15] (current) alexandru.radovici |
||
|---|---|---|---|
| Line 56: | Line 56: | ||
| symbol_table: {...}, // the symbol_table | symbol_table: {...}, // the symbol_table | ||
| ast: [...], // the ast with the type for very node that returns a value | ast: [...], // the ast with the type for very node that returns a value | ||
| - | error_list [] // the error list | + | errors [] // the error list |
| } | } | ||
| </code> | </code> | ||
| Line 64: | Line 64: | ||
| The symbol table is represented by a Javascript object, where each property is the name of a context. In the example below you | The symbol table is represented by a Javascript object, where each property is the name of a context. In the example below you | ||
| can see there are two contexts: | can see there are two contexts: | ||
| - | * `script` - the context for the main script | + | * //script// - the context for the main script |
| - | * `function_sum` - the context generated by the `sum` function | + | * //function_sum// - the context generated by the `sum` function |
| <code javascript> | <code javascript> | ||
| Line 181: | Line 181: | ||
| </code> | </code> | ||
| + | ==== Context names ==== | ||
| + | * ''script'' - script | ||
| + | * ''function'' - function_''title'' | ||
| + | * ''if_then'' - if_''line_where_the_if_then_was_defined'' | ||
| + | * ''loop_when'' - do_while_''line_where_the_loop_when_was_defined'' | ||
| + | * ''loop_go'' - while_''line_where_the_loop_go_was_defined'' | ||
| + | |||
| + | ===== The new AST ===== | ||
| + | The AST from the parser has to be transformed so that it does not contain any variable, function or type definitions. | ||
| + | |||
| + | The new AST is a list of objects, each object being either the script either a function. | ||
| + | |||
| + | <code javascript> | ||
| + | [ | ||
| + | // the script | ||
| + | "script": { | ||
| + | statements: [ | ||
| + | ... | ||
| + | ] | ||
| + | }, | ||
| + | // a function | ||
| + | "function_title": { | ||
| + | statements: [ | ||
| + | ... | ||
| + | ] | ||
| + | } | ||
| + | ] | ||
| + | </code> | ||
| + | |||
| + | ==== Variable definitions ==== | ||
| + | The variable definitions will be written to the symbol table. The definition will be deleted from the AST (it will not be placed in the | ||
| + | statements, as it has no actual action). If the definition is with an attribution ''@var x:int <- 7;'', it will be replaced with | ||
| + | an attribution node equivalent to ''x <- 7;''. | ||
| + | |||
| + | ==== Type definitions ==== | ||
| + | Structs and vectors are type definitions. These will be written to the ''types'' part in the symbol table. | ||
| + | |||
| + | For structs, if the properties have default values, every struct variable declaration will be replaced with attributions for | ||
| + | all the properties. There is an [[https://github.com/UPB-FILS/alf/blob/master/Devoir/semantic/verify/semantic/4_struct/definition_with_values_and_variable.alf|example]] in the repository. | ||
| ===== Verify Types in the AST ===== | ===== Verify Types in the AST ===== | ||
| Line 191: | Line 230: | ||
| * return | * return | ||
| * element_of_vector | * element_of_vector | ||
| - | * prop | + | * property |
| - | * dispatch | + | * function_call |
| + | |||
| + | All the other elements have the return type ''none''. If there is a type error (eg. float * string) it will be ''error''. | ||
| Set type the by adding a parameter type in the node. | Set type the by adding a parameter type in the node. | ||
| Line 198: | Line 239: | ||
| <note> | <note> | ||
| When searching for a variable, the algorithm is: | When searching for a variable, the algorithm is: | ||
| - | * if it is a function context, search the local variables | + | * search the local context |
| - | * if not found, search the parameters | + | * if not found, search the parent context |
| - | * if not found, search the struct context local variables | + | |
| - | + | ||
| - | * if it is the module context, search the global variables | + | |
| </note> | </note> | ||
| === Example === | === Example === | ||
| <code javascript> | <code javascript> | ||
| - | { | + | { |
| - | "id": "expr", | + | "id": "expr", |
| - | "op": "=", | + | "op": "+", |
| - | "left": { | + | "left": { |
| - | "id": "expr", | + | "id": "value", |
| - | "op": "mod", | + | "type": "int", |
| - | "left": { | + | "value": 2, |
| - | "id": "identifier", | + | "line": 3, |
| - | "value": "n", | + | "symbol_table": "script" |
| - | "line": 8, | + | }, |
| - | "symbol": 1, | + | "right": { |
| - | "type": "int" | + | "id": "value", |
| - | }, | + | "type": "int", |
| - | "right": { | + | "value": 3, |
| - | "id": "identifier", | + | "line": 3, |
| - | "value": "i", | + | "symbol_table": "script" |
| - | "line": 8, | + | }, |
| - | "symbol": 1, | + | "line": 3, |
| - | "type": "int" | + | "symbol_table": "script", |
| - | }, | + | "type": "int" |
| - | "line": 8, | + | |
| - | "symbol": 1, | + | |
| - | "t": "int" | + | |
| - | }, | + | |
| - | "right": { | + | |
| - | "id": "value", | + | |
| - | "type": "int", | + | |
| - | "value": 0, | + | |
| - | "line": 8, | + | |
| - | "symbol": 1 | + | |
| - | }, | + | |
| - | "line": 8, | + | |
| - | "symbol": 1, | + | |
| - | "type": "bool" | + | |
| } | } | ||
| </code> | </code> | ||
| Line 274: | Line 298: | ||
| { | { | ||
| type: // string with the error type | type: // string with the error type | ||
| - | line: // the line number in the source (starting at 1) | + | elements: { |
| - | elements: // items for the error, each type of error has different items | + | // items for the error, each type of error has different items |
| + | line: // the line where the error is | ||
| + | }, | ||
| text: // the error text message | text: // the error text message | ||
| } | } | ||
| </code> | </code> | ||
| + | |||
| ==== Error type ==== | ==== Error type ==== | ||
| Line 289: | Line 316: | ||
| <code javascript> | <code javascript> | ||
| { | { | ||
| - | variable: // variable name | + | variable: // variable name, |
| } | } | ||
| </code> | </code> | ||
| Line 313: | Line 340: | ||
| </code> | </code> | ||
| - | === STRUCT_ELEMENT_ALREADY_DEFINED === | + | === STRUCT_PROPERTY_ALREADY_DEFINED === |
| The error occurs when a struct element definition is repeated. | The error occurs when a struct element definition is repeated. | ||
| == Elements == | == Elements == | ||
| { | { | ||
| - | struct: // struct type name | + | type: // struct type title |
| - | element: // element name | + | title: // property title |
| } | } | ||
| Line 328: | Line 355: | ||
| <code javascript> | <code javascript> | ||
| { | { | ||
| - | vector: // array type name | + | array: // array type name |
| - | low_index: // the lower index value | + | length: // the negative length |
| - | high_index: // the higher index value | + | |
| } | } | ||
| </code> | </code> | ||
| Line 343: | Line 369: | ||
| variable: // the name of the variable with the unresolved type | variable: // the name of the variable with the unresolved type | ||
| } | } | ||
| - | // struct element | + | // struct property |
| { | { | ||
| struct: // name of struct type | struct: // name of struct type | ||
| - | element: // name of struct element with the unresolved type | + | property: // name of struct element with the unresolved type |
| } | } | ||
| </code> | </code> | ||
| - | === UNDEFINED_MESSAGE === | + | === UNDEFINED_FUNCTION === |
| - | The error occurs when a function call is made to a function that is not defned. | + | The error occurs when a function call is made to a function that is not defined. |
| == Elements == | == Elements == | ||
| <code javascript> | <code javascript> | ||
| { | { | ||
| - | id: // message name | + | title: // function title |
| } | } | ||
| </code> | </code> | ||
| Line 367: | Line 393: | ||
| <code javascript> | <code javascript> | ||
| { | { | ||
| - | variable: // variable name | + | variable: // variable title |
| } | } | ||
| </code> | </code> | ||
| Line 376: | Line 402: | ||
| == Elements == | == Elements == | ||
| { | { | ||
| - | variable: // the variable that has that type | + | value: // the type that is undefined |
| - | type: // the type that is undefined | + | |
| } | } | ||
| Line 386: | Line 411: | ||
| <code javascript> | <code javascript> | ||
| { | { | ||
| - | struct: // struct name | + | type: // struct title |
| - | property: // element name | + | title: // property title |
| } | } | ||
| </code> | </code> | ||
| Line 411: | Line 436: | ||
| </code> | </code> | ||
| === ARRAY_INDEX_TYPE === | === ARRAY_INDEX_TYPE === | ||
| - | The error occurs when an index for an array is not a number or symbol | + | The error occurs when an index for an array is not an int |
| == Elements == | == Elements == | ||
| <code javascript> | <code javascript> | ||
| { | { | ||
| - | vector: // array type | + | type: // the index type |
| - | index: // supplied index type | + | |
| } | } | ||
| </code> | </code> | ||
| Line 452: | Line 476: | ||
| op: // if, while, repeat | op: // if, while, repeat | ||
| } | } | ||
| - | // for | + | // attribution (including for variable) |
| { | { | ||
| - | exp: // expression type | + | to: // to type, |
| - | op: "for" | + | op: "<-", |
| - | element: // variable, from, to or step | + | from: // from type |
| } | } | ||
| - | // value_of_function | + | // return |
| { | { | ||
| - | op: "value" | + | op: "return" |
| to: // return type | to: // return type | ||
| from: // provided type | from: // provided type | ||
| } | } | ||
| - | // is | + | // iteration (for i in exp go) |
| { | { | ||
| - | op: "is" | + | op: "iteration" |
| - | to: // to type | + | value: // the exp type |
| - | from: // from type | + | |
| } | } | ||
| - | // struct element type is undefined | + | // typecast |
| { | { | ||
| - | struct: // struct type, | + | op: "typecast", |
| - | element: // name of struct element | + | to: // to type, |
| + | from: // from type | ||
| } | } | ||
| - | // variable type is undefined | ||
| - | { | ||
| - | variable: // name of the variable | ||
| - | } | ||
| - | </code> | ||
| === LEXICAL === | === LEXICAL === | ||