AALanguage
The best language for those who have nothing to do
|
A helper class for generating and executing the internal representation of the program. More...
#include <Poliz.h>
Public Types | |
enum | PolizType { GO , FGO , TGO , LABEL , ADDRESS , POINTER , GETARR , CALL , BLANK , SEMICOLON , LITERAL , USING , COMMA , ASSIGN , LOGICAL_OR , LOGICAL_AND , BITWISE_OR , BITWISE_XOR , BITWISE_CONS , BITWISE_AND , EQUALITY , CMP , BITWISE_SHIFT , PLUS , MULT , UNARY , SWITCH_CMP , SWITCH_POP , RET , BOOL_LITERAL , BYTE_LITERAL , CHAR_LITERAL , DOUBLE_LITERAL , FLOAT_LITERAL , INT_LITERAL , LONG_LITERAL , SHORT_LITERAL , STRING_LITERAL , UINT_LITERAL , ULONG_LITERAL , LARGE_LITERAL , STACK_PLUG , READ , PRINT , READLN , NORET , GETSTR , EXIT , CONVERT , RAND , STRLEN , TIME } |
Enumeration of all possible expression types in POLIZ. More... | |
Public Member Functions | |
Poliz () | |
Default constructor. | |
Poliz (Type *current_function) | |
Constructor passing a pointer to the current function in semantic analysis. | |
~Poliz () | |
Destructor. Frees all dynamic memory of the internal representation. | |
void | put_lex (std::pair< PolizType, void * > l) |
Adds a new token - value pair (POLIZ expression type, pointer to value). | |
void | blank () |
Adds a new blank token. | |
int | get_size () |
Gets the current number of tokens in POLIZ. | |
void * | convert (std::pair< PolizType, void * > op, PolizType to) |
Converts an internal type 1 value to a type 2 value. | |
std::pair< PolizType, std::pair< void *, void * > > | make_same (std::pair< PolizType, void * > op1, std::pair< PolizType, void * > op2) |
Performs transformations of two POLYZ tokens, bringing their values to the same type. | |
std::pair< PolizType, void * > | address_to_value (void *ptr) |
Converts a token of type ADDRESS to a token with a pointer to its value. | |
void * | copy_value (void *ptr) |
Copies and returns the current value of the variable. | |
PolizType | type_to_poliz (ExprType type) |
Converts ExprType to a valid PolizType. | |
std::pair< PolizType, void * > | execute_operation (std::pair< PolizType, void * > op1, std::pair< PolizType, void * > op2, std::string operation) |
Performs a binary operation and returns its result. | |
std::pair< PolizType, void * > | execute_operation (std::pair< PolizType, void * > op, std::string operation) |
Performs an unary operation and returns its result. | |
void | call_function (std::stack< std::pair< PolizType, void * > > &st, int &p) |
Executes a function call subject to stack constraint (recursion depth 5*10^5). | |
std::pair< PolizType, void * > | execute (Function entry_func) |
The main function that performs a walkthrough of the POLIZ token array. | |
std::pair< PolizType, void * > & | operator[] (int ind) |
Helper operator to get a token by its number in the POLIZ arrayþ. | |
Private Member Functions | |
ExprType | string_to_type (std::string str) |
std::string | ReplaceAll (std::string str, const std::string &from, const std::string &to) |
void | add_tid_values (TableIdentifiers *current_tid, bool is_first=true) |
void | clear_tid_values (TableIdentifiers *current_tid) |
void | restore_tid_values () |
Private Attributes | |
std::vector< std::pair< PolizType, void * > > | lexes |
int | literal_prior [12] = { 0, 2, 1, 11, 7, 5, 9, 3, 0, 6, 10, 12 } |
std::stack< std::pair< int, PolizType > > | func_calls |
std::vector< std::pair< PolizType, void * > > | global_lexes |
std::stack< std::vector< std::pair< Identifier *, void * > > > | tid_vals |
std::mt19937 | rnd |
Type * | current_function |
A helper class for generating and executing the internal representation of the program.
enum Poliz::PolizType |
Enumeration of all possible expression types in POLIZ.
Poliz::Poliz | ( | Type * | current_function | ) |
Constructor passing a pointer to the current function in semantic analysis.
current_function | Pointer to the Type of the current function |
Definition at line 32 of file Poliz.cpp.
References current_function, and rnd.
Poliz::~Poliz | ( | ) |
|
private |
Definition at line 1868 of file Poliz.cpp.
References add_tid_values(), TableIdentifiers::children, copy_value(), TableIdentifiers::identifiers, and tid_vals.
Referenced by add_tid_values(), call_function(), and execute().
std::pair< Poliz::PolizType, void * > Poliz::address_to_value | ( | void * | ptr | ) |
Converts a token of type ADDRESS to a token with a pointer to its value.
Gets the memory value reserved in the current Identifier of a token of type ADDRESS.
ptr | A pointer that is part of a token of type ADDRESS. |
Definition at line 548 of file Poliz.cpp.
References Bool, BOOL_LITERAL, Byte, BYTE_LITERAL, Char, CHAR_LITERAL, Double, DOUBLE_LITERAL, Type::expr_type, Float, FLOAT_LITERAL, Int, INT_LITERAL, Large, LARGE_LITERAL, Long, LONG_LITERAL, Identifier::name, Short, SHORT_LITERAL, String, STRING_LITERAL, Identifier::type, UInt, UINT_LITERAL, ULong, ULONG_LITERAL, and Identifier::value.
void Poliz::blank | ( | ) |
Adds a new blank token.
Definition at line 48 of file Poliz.cpp.
Referenced by Analyzer::for_statement(), Analyzer::if_statement(), Analyzer::parameter_list(), Analyzer::switch_statement(), and Analyzer::while_statement().
void Poliz::call_function | ( | std::stack< std::pair< PolizType, void * > > & | st, |
int & | p ) |
Executes a function call subject to stack constraint (recursion depth 5*10^5).
The CALL token is used to determine how many parameters will have the specified value and how many will receive the default value. For this prefix, sets the values in the TID of the function and changes the pointer to the current instruction.
st | Reference to the stack of expression calculation results, |
p | Reference to an integer offset pointer in internal representation |
Definition at line 1342 of file Poliz.cpp.
References add_tid_values(), clear_tid_values(), convert(), Function::params_init, Function::params_ptrs, Function::ptr, STACK_PLUG, Function::TID, and type_to_poliz().
Referenced by execute().
|
private |
Definition at line 1883 of file Poliz.cpp.
References TableIdentifiers::children, clear_tid_values(), and TableIdentifiers::identifiers.
Referenced by call_function(), and clear_tid_values().
Converts an internal type 1 value to a type 2 value.
op | Token whose value will be converted |
to | Final value type |
Definition at line 54 of file Poliz.cpp.
References ADDRESS, address_to_value(), BOOL_LITERAL, BYTE_LITERAL, CHAR_LITERAL, DOUBLE_LITERAL, FLOAT_LITERAL, INT_LITERAL, LARGE_LITERAL, LONG_LITERAL, SHORT_LITERAL, STRING_LITERAL, UINT_LITERAL, and ULONG_LITERAL.
Referenced by call_function(), execute(), Analyzer::execute(), and make_same().
void * Poliz::copy_value | ( | void * | ptr | ) |
Copies and returns the current value of the variable.
ptr | A pointer that is part of a token of type ADDRESS. |
Definition at line 582 of file Poliz.cpp.
References Bool, Byte, Char, Double, Type::expr_type, Float, Int, Large, Long, Identifier::name, Short, String, Identifier::type, UInt, ULong, and Identifier::value.
Referenced by add_tid_values().
std::pair< Poliz::PolizType, void * > Poliz::execute | ( | Function | entry_func | ) |
The main function that performs a walkthrough of the POLIZ token array.
Each instruction is analyzed according to its POLIZ type.
entry_func | The function that is the entry point of the program. By default it has the name main. |
Definition at line 1364 of file Poliz.cpp.
References add_tid_values(), ADDRESS, address_to_value(), ASSIGN, BLANK, Bool, BOOL_LITERAL, Byte, BYTE_LITERAL, CALL, call_function(), Char, CHAR_LITERAL, COMMA, CONVERT, convert(), Double, DOUBLE_LITERAL, execute_operation(), EXIT, Type::expr_type, FGO, Float, FLOAT_LITERAL, func_calls, GETARR, GETSTR, global_lexes, GO, Int, INT_LITERAL, LABEL, Large, LARGE_LITERAL, lexes, LITERAL, Long, LONG_LITERAL, NORET, POINTER, PRINT, Function::ptr, RAND, READ, READLN, ReplaceAll(), restore_tid_values(), RET, rnd, SEMICOLON, Short, SHORT_LITERAL, STACK_PLUG, String, STRING_LITERAL, string_to_type(), STRLEN, SWITCH_CMP, SWITCH_POP, TGO, Function::TID, TIME, Function::type, type_to_poliz(), UInt, UINT_LITERAL, ULong, ULONG_LITERAL, UNARY, and UShort.
Referenced by Analyzer::execute().
std::pair< PolizType, void * > Poliz::execute_operation | ( | std::pair< PolizType, void * > | op, |
std::string | operation ) |
Performs an unary operation and returns its result.
op | Operand, |
operation | Operation string |
std::pair< PolizType, void * > Poliz::execute_operation | ( | std::pair< PolizType, void * > | op1, |
std::pair< PolizType, void * > | op2, | ||
std::string | operation ) |
Performs a binary operation and returns its result.
Converts the input operands to a common type and performs the desired operation.
op1 | First operand, |
op2 | Second operand |
operation | Operation string |
Referenced by execute().
int Poliz::get_size | ( | ) |
Gets the current number of tokens in POLIZ.
Definition at line 51 of file Poliz.cpp.
References lexes.
Referenced by Analyzer::for_statement(), Analyzer::function(), Analyzer::if_statement(), Analyzer::parameter_list(), Analyzer::switch_statement(), and Analyzer::while_statement().
std::pair< Poliz::PolizType, std::pair< void *, void * > > Poliz::make_same | ( | std::pair< PolizType, void * > | op1, |
std::pair< PolizType, void * > | op2 ) |
Performs transformations of two POLYZ tokens, bringing their values to the same type.
The conversion is performed using extensional transformations based on "prioritization" of types.
op1 | First token operand |
op2 | Second token operand |
Definition at line 540 of file Poliz.cpp.
References BOOL_LITERAL, convert(), and literal_prior.
|
inline |
void Poliz::put_lex | ( | std::pair< PolizType, void * > | l | ) |
Adds a new token - value pair (POLIZ expression type, pointer to value).
l | The value of the new token |
Definition at line 42 of file Poliz.cpp.
References current_function, global_lexes, and lexes.
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::exit_statement(), Analyzer::expression(), Analyzer::field(), Analyzer::for_statement(), Analyzer::function(), Analyzer::goto_statement(), Analyzer::if_statement(), Analyzer::logical_and_expression(), Analyzer::logical_or_expression(), Analyzer::may_be_semicolon(), Analyzer::multiply_expression(), Analyzer::namespace_definition(), Analyzer::plus_expression(), Analyzer::rand_statement(), Analyzer::read_statement(), Analyzer::readln_statement(), Analyzer::return_statement(), Analyzer::semicolon(), Analyzer::strlen_statement(), Analyzer::switch_statement(), Analyzer::time_statement(), Analyzer::unary_expression(), Analyzer::var_definition(), Analyzer::var_definitions(), and Analyzer::while_statement().
|
private |
|
private |
|
private |
Poliz::PolizType Poliz::type_to_poliz | ( | ExprType | type | ) |
Converts ExprType to a valid PolizType.
type | Source type of expression |
Definition at line 615 of file Poliz.cpp.
References BLANK, Bool, BOOL_LITERAL, Byte, BYTE_LITERAL, Char, CHAR_LITERAL, Double, DOUBLE_LITERAL, Float, FLOAT_LITERAL, Int, INT_LITERAL, Large, LARGE_LITERAL, Long, LONG_LITERAL, Short, SHORT_LITERAL, String, STRING_LITERAL, UInt, UINT_LITERAL, ULong, and ULONG_LITERAL.
Referenced by call_function(), and execute().
|
private |
|
private |
|
private |
|
private |
Definition at line 144 of file Poliz.h.
Referenced by blank(), execute(), get_size(), operator[](), put_lex(), and ~Poliz().
|
private |
Definition at line 145 of file Poliz.h.
Referenced by make_same().
|
private |
|
private |
Definition at line 148 of file Poliz.h.
Referenced by add_tid_values(), and restore_tid_values().