AALanguage
The best language for those who have nothing to do
|
A helper class that performs basic actions on the semantics of the language. More...
#include <Semantic.h>
Public Member Functions | |
ExprType | string_to_type (std::string str) |
Translating a string with a data type name into ExprType. | |
bool | is_convertible (Type first, Type second, bool is_func=false) |
Checks if implicit type conversion is possible. | |
Type | calc_expr_type (Type first, Type second) |
Calculates the result of a conversion of two types. | |
std::string | type_to_string (Type type) |
Translates the Type to a string. | |
void | add_identifier (Identifier *ident, bool is_global=false) |
Adds a new identifier to the current table. | |
Identifier * | get_identifier (std::string name) |
Gets a pointer to an identifier by its name. | |
void | del_table () |
Deletes the current TID and returns to the parent table. | |
void | create_table (bool new_func=false) |
Creates a new TID and adds it as a child of the current one. | |
void | create_function (std::string name, Type type, int pref, std::vector< int > inits, std::vector< Identifier * > ptrs, int ptr) |
Creates a new function and adds it to the TID of functions. | |
Function | get_function (std::string name, std::vector< Type > params) |
Searches for a function with the specified name and set of argument types. | |
void | init_array (ExprType type, int size, void *&value, std::string name) |
Initializes array memory with empty pointers. | |
bool | check_operation (Type f, Type s, std::string operation) |
Checks whether the operation is applicable to two operand types. | |
Type | literal_to_type (LexicalAnalyzer &lex, Token current_token) |
Converts a string containing a literal to a Type. | |
Public Attributes | |
std::stack< std::pair< Type, bool > > | exprs |
A stack of current type expressions. Consists of pairs (Type, bool) = (Type of expression, whether lvalue). | |
std::stack< int * > | break_ptr |
A stack of pointers to the lowest level of the break operator in the internal representation. | |
std::stack< int * > | continue_ptr |
A stack of pointers to the lowest level of the continue operator in the internal representation. | |
std::vector< Identifier * > | params_ptrs |
Vector-array of pointers to the current set of function parameters. | |
int | function_params_pref = 0 |
The length of the current prefix with arguments with no default value in the function under consideration. | |
bool | is_in_function_header = false |
Flag to determine if you are inside the parameters of the function. | |
bool | was_function_assign = false |
Flag to determine if the parameters have started with the default value. | |
std::vector< int > | function_params_init |
Vector-array with pointers to the offset of initialization of parameters of the current function in the internal representation of the program. | |
std::unordered_map< Function, TableIdentifiers *, FunctionHasher > | funcs |
Function hash table. | |
Type | current_function = Type() |
Type of current function. | |
Private Attributes | |
TableIdentifiers * | tid = new TableIdentifiers() |
TableIdentifiers * | global_tid = new TableIdentifiers() |
A helper class that performs basic actions on the semantics of the language.
Definition at line 8 of file Semantic.h.
void Semantic::add_identifier | ( | Identifier * | ident, |
bool | is_global = false ) |
Adds a new identifier to the current table.
Checks that there is no identifier with the same name in the current scope and adds it.
ident | Pointer to the new identifier |
is_global | Whether the new identifier is global |
Definition at line 101 of file Semantic.cpp.
References global_tid, TableIdentifiers::identifiers, Identifier::name, params_ptrs, and tid.
Referenced by Analyzer::var_definition(), and Analyzer::var_definitions().
Calculates the result of a conversion of two types.
first | The first type |
second | The second type |
Definition at line 53 of file Semantic.cpp.
References Type::expr_type, and is_convertible().
Referenced by Analyzer::assignment_expression(), Analyzer::bitwise_and_expression(), Analyzer::bitwise_consequence_expression(), Analyzer::bitwise_or_expression(), Analyzer::bitwise_shift_expression(), Analyzer::bitwise_xor_expression(), Analyzer::multiply_expression(), and Analyzer::plus_expression().
Checks whether the operation is applicable to two operand types.
f | First operand |
s | Second operand |
operation | String of operation |
Definition at line 202 of file Semantic.cpp.
References Bool, Double, Type::expr_type, Float, is_convertible(), Large, String, UDouble, and UFloat.
Referenced by Analyzer::assignment_expression(), Analyzer::bitwise_and_expression(), Analyzer::bitwise_consequence_expression(), Analyzer::bitwise_or_expression(), Analyzer::bitwise_shift_expression(), Analyzer::bitwise_xor_expression(), Analyzer::comparison_expression(), Analyzer::equality_expression(), Analyzer::logical_and_expression(), Analyzer::logical_or_expression(), Analyzer::multiply_expression(), and Analyzer::plus_expression().
void Semantic::create_function | ( | std::string | name, |
Type | type, | ||
int | pref, | ||
std::vector< int > | inits, | ||
std::vector< Identifier * > | ptrs, | ||
int | ptr ) |
Creates a new function and adds it to the TID of functions.
Before adding a function to the hash table, it checks if the same hash exists. If an identical function was added earlier, it raises an exception.
name | Function name |
type | Returnable type |
pref | The length of the prefix with no default value |
inits | Vector-array with pointers to initialize variables in internal representation |
ptrs | Vector-array with pointers to all argument identifiers |
ptr | Function offset in internal representation |
Definition at line 142 of file Semantic.cpp.
References current_function, funcs, TableIdentifiers::identifiers, Function::name, and tid.
Referenced by Analyzer::function().
void Semantic::create_table | ( | bool | new_func = false | ) |
Creates a new TID and adds it as a child of the current one.
new_func | Whether the current variable is part of the function arguments |
Definition at line 135 of file Semantic.cpp.
References tid.
Referenced by Analyzer::for_statement(), Analyzer::function(), and Analyzer::statement().
void Semantic::del_table | ( | ) |
Deletes the current TID and returns to the parent table.
Definition at line 127 of file Semantic.cpp.
References TableIdentifiers::parent, and tid.
Referenced by Analyzer::statement().
Searches for a function with the specified name and set of argument types.
Searches the function first by prefixing argument types without a default value, and then by all values. If the function call is ambiguous, it throws an exception.
name | Function name |
params | Vector-array of standard argument types |
Definition at line 154 of file Semantic.cpp.
References funcs, and is_convertible().
Referenced by Analyzer::execute(), and Analyzer::field().
Identifier * Semantic::get_identifier | ( | std::string | name | ) |
Gets a pointer to an identifier by its name.
Checks if an identifier with the specified name exists in the current scope and returns a pointer to its instance of the Identifier class. The search is performed by traversing from the current TID through the parent tables.
name | Identifier name |
Definition at line 112 of file Semantic.cpp.
References global_tid, TableIdentifiers::identifiers, TableIdentifiers::parent, and tid.
Referenced by Analyzer::field().
void Semantic::init_array | ( | ExprType | type, |
int | size, | ||
void *& | value, | ||
std::string | name ) |
Initializes array memory with empty pointers.
type | Type of array elements |
size | Array size |
value | Reference to the array identifier value pointer |
name | Array name |
Definition at line 192 of file Semantic.cpp.
Referenced by Analyzer::var_definition(), and Analyzer::var_definitions().
Checks if implicit type conversion is possible.
first | The first type |
second | The second type |
is_func | Flag indicating that a type conversion of the function arguments is in progress. |
Definition at line 23 of file Semantic.cpp.
References Type::array_size, Bool, Byte, Char, Double, Type::expr_type, Float, Int, Type::is_array, Large, Long, Short, String, UDouble, UFloat, UInt, ULong, Unknown, UShort, and Void.
Referenced by calc_expr_type(), check_operation(), Analyzer::field(), get_function(), Analyzer::return_statement(), Analyzer::switch_statement(), Analyzer::var_definition(), and Analyzer::var_definitions().
Type Semantic::literal_to_type | ( | LexicalAnalyzer & | lex, |
Token | current_token ) |
Converts a string containing a literal to a Type.
lex | Reference to the lexical analyzer used |
current_token | Convertible lexeme |
Definition at line 225 of file Semantic.cpp.
References Bool, Byte, Char, Double, Float, Int, LexicalAnalyzer::integer, LexicalAnalyzer::is_literal(), LexicalAnalyzer::logical, Long, LexicalAnalyzer::real, Short, LexicalAnalyzer::string, String, LexicalAnalyzer::symbol, UDouble, UFloat, UInt, ULong, Unknown, UShort, and Token::value.
Referenced by Analyzer::construct_expression(), and Analyzer::switch_statement().
ExprType Semantic::string_to_type | ( | std::string | str | ) |
Translating a string with a data type name into ExprType.
str | String containing data type |
Definition at line 3 of file Semantic.cpp.
References Bool, Byte, Char, Double, Float, Int, Large, Long, Short, String, UDouble, UFloat, UInt, ULong, Unknown, UShort, and Void.
Referenced by Analyzer::convert_statement(), Analyzer::function(), Analyzer::var_definition(), and Analyzer::var_definitions().
std::string Semantic::type_to_string | ( | Type | type | ) |
Translates the Type to a string.
type | Type for conversion |
Definition at line 61 of file Semantic.cpp.
References Type::array_size, Bool, Byte, Char, Double, Type::expr_type, Float, Int, Type::is_array, Large, Long, Short, String, UDouble, UFloat, UInt, ULong, Unknown, UShort, and Void.
Referenced by Analyzer::assignment_expression(), Analyzer::bitwise_and_expression(), Analyzer::bitwise_consequence_expression(), Analyzer::bitwise_or_expression(), Analyzer::bitwise_shift_expression(), Analyzer::bitwise_xor_expression(), Analyzer::comparison_expression(), Analyzer::equality_expression(), Analyzer::for_statement(), Analyzer::if_statement(), Analyzer::logical_and_expression(), Analyzer::logical_or_expression(), Analyzer::multiply_expression(), Analyzer::plus_expression(), Analyzer::return_statement(), Analyzer::switch_statement(), Analyzer::unary_expression(), Analyzer::var_definition(), Analyzer::var_definitions(), and Analyzer::while_statement().
std::stack<int*> Semantic::break_ptr |
A stack of pointers to the lowest level of the break operator in the internal representation.
Definition at line 131 of file Semantic.h.
Referenced by Analyzer::for_statement(), Analyzer::goto_statement(), Analyzer::switch_statement(), and Analyzer::while_statement().
std::stack<int*> Semantic::continue_ptr |
A stack of pointers to the lowest level of the continue operator in the internal representation.
Definition at line 133 of file Semantic.h.
Referenced by Analyzer::for_statement(), Analyzer::goto_statement(), and Analyzer::while_statement().
Type of current function.
Definition at line 147 of file Semantic.h.
Referenced by Analyzer::Analyzer(), create_function(), Analyzer::function(), and Analyzer::return_statement().
std::stack<std::pair<Type, bool> > Semantic::exprs |
A stack of current type expressions. Consists of pairs (Type, bool) = (Type of expression, whether lvalue).
Definition at line 129 of file Semantic.h.
Referenced by Analyzer::assignment_expression(), Analyzer::bitwise_and_expression(), Analyzer::bitwise_consequence_expression(), Analyzer::bitwise_or_expression(), Analyzer::bitwise_shift_expression(), Analyzer::bitwise_xor_expression(), Analyzer::comparison_expression(), Analyzer::construct_expression(), Analyzer::convert_statement(), Analyzer::equality_expression(), Analyzer::expression(), Analyzer::field(), Analyzer::for_statement(), Analyzer::if_statement(), Analyzer::logical_and_expression(), Analyzer::logical_or_expression(), Analyzer::multiply_expression(), Analyzer::plus_expression(), Analyzer::print_statement(), Analyzer::rand_statement(), Analyzer::read_statement(), Analyzer::readln_statement(), Analyzer::return_statement(), Analyzer::statement(), Analyzer::strlen_statement(), Analyzer::switch_statement(), Analyzer::time_statement(), Analyzer::unary_expression(), Analyzer::var_definition(), Analyzer::var_definitions(), and Analyzer::while_statement().
std::unordered_map<Function, TableIdentifiers*, FunctionHasher> Semantic::funcs |
Function hash table.
Definition at line 145 of file Semantic.h.
Referenced by create_function(), and get_function().
std::vector<int> Semantic::function_params_init |
Vector-array with pointers to the offset of initialization of parameters of the current function in the internal representation of the program.
Definition at line 143 of file Semantic.h.
Referenced by Analyzer::function(), and Analyzer::parameter_list().
int Semantic::function_params_pref = 0 |
The length of the current prefix with arguments with no default value in the function under consideration.
Definition at line 137 of file Semantic.h.
Referenced by Analyzer::function(), Analyzer::parameter_list(), and Analyzer::var_definition().
|
private |
Definition at line 150 of file Semantic.h.
Referenced by add_identifier(), and get_identifier().
bool Semantic::is_in_function_header = false |
Flag to determine if you are inside the parameters of the function.
Definition at line 139 of file Semantic.h.
Referenced by Analyzer::parameter_list(), and Analyzer::var_definition().
std::vector<Identifier*> Semantic::params_ptrs |
Vector-array of pointers to the current set of function parameters.
Definition at line 135 of file Semantic.h.
Referenced by add_identifier(), and Analyzer::function().
|
private |
Definition at line 149 of file Semantic.h.
Referenced by add_identifier(), create_function(), create_table(), del_table(), and get_identifier().
bool Semantic::was_function_assign = false |
Flag to determine if the parameters have started with the default value.
Definition at line 141 of file Semantic.h.
Referenced by Analyzer::parameter_list(), and Analyzer::var_definition().