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

A helper class for generating and executing the internal representation of the program. More...

#include <Poliz.h>

Collaboration diagram for Poliz:

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
 
Typecurrent_function
 

Detailed Description

A helper class for generating and executing the internal representation of the program.

Definition at line 12 of file Poliz.h.

Member Enumeration Documentation

◆ PolizType

Enumeration of all possible expression types in POLIZ.

Enumerator
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 

Definition at line 15 of file Poliz.h.

Constructor & Destructor Documentation

◆ Poliz() [1/2]

Poliz::Poliz ( )
inline

Default constructor.

Definition at line 24 of file Poliz.h.

◆ Poliz() [2/2]

Poliz::Poliz ( Type * current_function)

Constructor passing a pointer to the current function in semantic analysis.

Parameters
current_functionPointer to the Type of the current function

Definition at line 32 of file Poliz.cpp.

References current_function, and rnd.

◆ ~Poliz()

Poliz::~Poliz ( )

Destructor. Frees all dynamic memory of the internal representation.

Definition at line 36 of file Poliz.cpp.

References ADDRESS, lexes, and POINTER.

Member Function Documentation

◆ add_tid_values()

void Poliz::add_tid_values ( TableIdentifiers * current_tid,
bool is_first = true )
private

◆ address_to_value()

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.

Parameters
ptrA pointer that is part of a token of type ADDRESS.
Returns
Token with the type corresponding to the current value of the variable.

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.

Referenced by convert(), and execute().

◆ blank()

void Poliz::blank ( )

Adds a new blank token.

Definition at line 48 of file Poliz.cpp.

References BLANK, and lexes.

Referenced by Analyzer::for_statement(), Analyzer::if_statement(), Analyzer::parameter_list(), Analyzer::switch_statement(), and Analyzer::while_statement().

◆ call_function()

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.

Parameters
stReference to the stack of expression calculation results,
pReference 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().

◆ clear_tid_values()

void Poliz::clear_tid_values ( TableIdentifiers * current_tid)
private

◆ convert()

void * Poliz::convert ( std::pair< PolizType, void * > op,
PolizType to )

Converts an internal type 1 value to a type 2 value.

Parameters
opToken whose value will be converted
toFinal value type
Returns
A void* pointer to the converted value.

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

◆ copy_value()

void * Poliz::copy_value ( void * ptr)

Copies and returns the current value of the variable.

Parameters
ptrA pointer that is part of a token of type ADDRESS.
Returns
Pointer to the copied value.

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

◆ execute()

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.

Parameters
entry_funcThe function that is the entry point of the program. By default it has the name main.
Returns
POLIZ token, which is the result of the program operation - the return value of main or the call to exit().

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

◆ execute_operation() [1/2]

std::pair< PolizType, void * > Poliz::execute_operation ( std::pair< PolizType, void * > op,
std::string operation )

Performs an unary operation and returns its result.

Parameters
opOperand,
operationOperation string
Returns
A POLIZ token with the result of the operation.

◆ execute_operation() [2/2]

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.

Parameters
op1First operand,
op2Second operand
operationOperation string
Returns
A POLIZ token with the result of the operation.

Referenced by execute().

◆ get_size()

int Poliz::get_size ( )

Gets the current number of tokens in POLIZ.

Returns
Gets the current number of tokens in the internal representation.

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

◆ make_same()

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.

Parameters
op1First token operand
op2Second token operand
Returns
A pair (type, (op1, op2)) where type is the type of both operands, op1 and op2 are the original values converted to that type.

Definition at line 540 of file Poliz.cpp.

References BOOL_LITERAL, convert(), and literal_prior.

◆ operator[]()

std::pair< PolizType, void * > & Poliz::operator[] ( int ind)
inline

Helper operator to get a token by its number in the POLIZ arrayþ.

Parameters
indToken index
Returns
POLIZ token on the specified index.

Definition at line 140 of file Poliz.h.

References lexes.

◆ put_lex()

◆ ReplaceAll()

std::string Poliz::ReplaceAll ( std::string str,
const std::string & from,
const std::string & to )
private

Definition at line 3 of file Poliz.cpp.

Referenced by execute().

◆ restore_tid_values()

void Poliz::restore_tid_values ( )
private

Definition at line 1898 of file Poliz.cpp.

References tid_vals.

Referenced by execute().

◆ string_to_type()

ExprType Poliz::string_to_type ( std::string str)
private

Definition at line 12 of file Poliz.cpp.

References Bool, Byte, Char, Double, Float, Int, Large, Long, Short, String, UDouble, UFloat, UInt, ULong, Unknown, UShort, and Void.

Referenced by execute().

◆ type_to_poliz()

Poliz::PolizType Poliz::type_to_poliz ( ExprType type)

Converts ExprType to a valid PolizType.

Parameters
typeSource type of expression
Returns
Converted type that is part of PolizType.

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

Member Data Documentation

◆ current_function

Type* Poliz::current_function
private

Definition at line 150 of file Poliz.h.

Referenced by Poliz(), and put_lex().

◆ func_calls

std::stack<std::pair<int, PolizType> > Poliz::func_calls
private

Definition at line 146 of file Poliz.h.

Referenced by execute().

◆ global_lexes

std::vector<std::pair<PolizType, void*> > Poliz::global_lexes
private

Definition at line 147 of file Poliz.h.

Referenced by execute(), and put_lex().

◆ lexes

std::vector<std::pair<PolizType, void*> > Poliz::lexes
private

Definition at line 144 of file Poliz.h.

Referenced by blank(), execute(), get_size(), operator[](), put_lex(), and ~Poliz().

◆ literal_prior

int Poliz::literal_prior[12] = { 0, 2, 1, 11, 7, 5, 9, 3, 0, 6, 10, 12 }
private

Definition at line 145 of file Poliz.h.

Referenced by make_same().

◆ rnd

std::mt19937 Poliz::rnd
private

Definition at line 149 of file Poliz.h.

Referenced by execute(), and Poliz().

◆ tid_vals

std::stack<std::vector<std::pair<Identifier*, void*> > > Poliz::tid_vals
private

Definition at line 148 of file Poliz.h.

Referenced by add_tid_values(), and restore_tid_values().


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