AALanguage
The best language for those who have nothing to do
Loading...
Searching...
No Matches
Semantic Class Reference

A helper class that performs basic actions on the semantics of the language. More...

#include <Semantic.h>

Collaboration diagram for Semantic:

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.
 
Identifierget_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 *, FunctionHasherfuncs
 Function hash table.
 
Type current_function = Type()
 Type of current function.
 

Private Attributes

TableIdentifierstid = new TableIdentifiers()
 
TableIdentifiersglobal_tid = new TableIdentifiers()
 

Detailed Description

A helper class that performs basic actions on the semantics of the language.

Definition at line 8 of file Semantic.h.

Member Function Documentation

◆ add_identifier()

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.

Parameters
identPointer to the new identifier
is_globalWhether 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().

◆ calc_expr_type()

Type Semantic::calc_expr_type ( Type first,
Type second )

Calculates the result of a conversion of two types.

Parameters
firstThe first type
secondThe second type
Returns
The resultant type to which both data types are cast.

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().

◆ check_operation()

bool Semantic::check_operation ( Type f,
Type s,
std::string operation )

◆ create_function()

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.

Parameters
nameFunction name
typeReturnable type
prefThe length of the prefix with no default value
initsVector-array with pointers to initialize variables in internal representation
ptrsVector-array with pointers to all argument identifiers
ptrFunction 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().

◆ create_table()

void Semantic::create_table ( bool new_func = false)

Creates a new TID and adds it as a child of the current one.

Parameters
new_funcWhether 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().

◆ del_table()

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().

◆ get_function()

Function Semantic::get_function ( std::string name,
std::vector< Type > params )

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.

Parameters
nameFunction name
paramsVector-array of standard argument types
Returns
The function found or throws an exception.

Definition at line 154 of file Semantic.cpp.

References funcs, and is_convertible().

Referenced by Analyzer::execute(), and Analyzer::field().

◆ get_identifier()

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.

Parameters
nameIdentifier name
Returns
Pointer to the required identifier.

Definition at line 112 of file Semantic.cpp.

References global_tid, TableIdentifiers::identifiers, TableIdentifiers::parent, and tid.

Referenced by Analyzer::field().

◆ init_array()

void Semantic::init_array ( ExprType type,
int size,
void *& value,
std::string name )

Initializes array memory with empty pointers.

Parameters
typeType of array elements
sizeArray size
valueReference to the array identifier value pointer
nameArray name

Definition at line 192 of file Semantic.cpp.

Referenced by Analyzer::var_definition(), and Analyzer::var_definitions().

◆ is_convertible()

bool Semantic::is_convertible ( Type first,
Type second,
bool is_func = false )

Checks if implicit type conversion is possible.

Parameters
firstThe first type
secondThe second type
is_funcFlag indicating that a type conversion of the function arguments is in progress.
Returns
Whether implicit type conversion is possible.

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().

◆ literal_to_type()

Type Semantic::literal_to_type ( LexicalAnalyzer & lex,
Token current_token )

Converts a string containing a literal to a Type.

Parameters
lexReference to the lexical analyzer used
current_tokenConvertible lexeme
Returns
The type of given literal.

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().

◆ string_to_type()

ExprType Semantic::string_to_type ( std::string str)

Translating a string with a data type name into ExprType.

Parameters
strString containing data type
Returns
ExprType corresponding to the internal representation.

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().

◆ type_to_string()

Member Data Documentation

◆ break_ptr

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().

◆ continue_ptr

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().

◆ current_function

Type Semantic::current_function = Type()

Type of current function.

Definition at line 147 of file Semantic.h.

Referenced by Analyzer::Analyzer(), create_function(), Analyzer::function(), and Analyzer::return_statement().

◆ exprs

◆ funcs

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().

◆ function_params_init

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().

◆ function_params_pref

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().

◆ global_tid

TableIdentifiers* Semantic::global_tid = new TableIdentifiers()
private

Definition at line 150 of file Semantic.h.

Referenced by add_identifier(), and get_identifier().

◆ is_in_function_header

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().

◆ params_ptrs

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().

◆ tid

TableIdentifiers* Semantic::tid = new TableIdentifiers()
private

Definition at line 149 of file Semantic.h.

Referenced by add_identifier(), create_function(), create_table(), del_table(), and get_identifier().

◆ was_function_assign

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().


The documentation for this class was generated from the following files: