core package

This is the most important package in Cnerator, which provides the core functionality. In this package, you can find the Abstract Syntax Tree (AST) program representation, the generators of each syntactic construct, type inference routines, probability specifications and different utilities provided by the system.

Submodules

core.ast module

This module holds the Abstract Syntax Tree (AST) program representation. Upon generation, the program is represented as an AST that is finally stored as output C source files. That functionality is provided by the to_str method.

class core.ast.ASTNode[source]

Bases: object

class core.ast.Array(_type, size=0, pointer_literal=False, pointer_declaration=False)[source]

Bases: core.ast.Pointer

generate_literal(from_declaration, size=0)[source]
property name
class core.ast.ArrayAccessExpression(left, right, c_type)[source]

Bases: core.ast.BinaryASTNode

to_str(indent: int = 0)[source]
class core.ast.Assignment(left, op, right, c_type)[source]

Bases: core.ast.BinaryASTNode

to_str(indent: int = 0)[source]
class core.ast.BinaryASTNode[source]

Bases: core.ast.ASTNode

property left
property right
class core.ast.BinaryExpression(left, op, right, c_type)[source]

Bases: core.ast.BinaryASTNode

to_str(indent: int = 0)[source]
class core.ast.Block(statements: list)[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.Bool(*args, **kwargs)[source]

Bases: core.ast.Type, core.ast.Singleton

generate_literal(**kwargs)[source]
name = 'bool'
values = ('true', 'false')
class core.ast.Break[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.CastExpression(exp, c_type)[source]

Bases: core.ast.UnaryExpression

class core.ast.Do(statements: List[core.ast.ASTNode], condition: core.ast.ASTNode)[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.Double(*args, **kwargs)[source]

Bases: core.ast.NumericType

name = 'double'
range = (2.3e-308, 1.7e+308)
class core.ast.Float(*args, **kwargs)[source]

Bases: core.ast.NumericType

name = 'float'
range = (1.2e-38, 3.4e+38)
class core.ast.For(initialization: core.ast.ASTNode, condition: core.ast.ASTNode, increment: core.ast.ASTNode, statements: List[core.ast.ASTNode])[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.FuncProc[source]

Bases: object

Func = 1
Proc = 2
class core.ast.Function(name, return_type, param_types)[source]

Bases: core.ast.ASTNode

get_param_by_type(c_type)[source]
prototype()[source]
property stmts
to_str(indent: int = 0)[source]
class core.ast.If(condition: core.ast.ASTNode, if_statements: List[core.ast.ASTNode], else_statements: List[core.ast.ASTNode])[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.Invocation(func_name, arguments, return_type, _is_stmt)[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.Label(label)[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.Literal(value, type)[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.LongDouble(*args, **kwargs)[source]

Bases: core.ast.NumericType

literal_suffix = 'L'
name = 'long double'
range = (2.3e-308, 1.7e+308)
class core.ast.NumericType(*args, **kwargs)[source]

Bases: core.ast.Type, core.ast.Singleton

generate_literal(**kwargs)[source]
class core.ast.Pointer(type)[source]

Bases: core.ast.Type

generate_literal(from_declaration)[source]
property name
class core.ast.Program[source]

Bases: core.ast.ASTNode

property functions
get_functions_by_return_type(return_type)[source]
stringify_parts()[source]
to_str(indent: int = 0)[source]
class core.ast.Return(exp=None, c_type=None)[source]

Bases: core.ast.UnaryASTNode

to_str(indent: int = 0)[source]
class core.ast.SignedChar(*args, **kwargs)[source]

Bases: core.ast.NumericType

name = 'signed char'
range = (-128, 127)
class core.ast.SignedInt(*args, **kwargs)[source]

Bases: core.ast.NumericType

name = 'signed int'
range = (-2147483648, 2147483647)
class core.ast.SignedLongInt(*args, **kwargs)[source]

Bases: core.ast.NumericType

literal_suffix = 'L'
name = 'signed long int'
range = (-2147483648, 2147483647)
class core.ast.SignedLongLongInt(*args, **kwargs)[source]

Bases: core.ast.NumericType

literal_suffix = 'LL'
name = 'signed long long int'
range = (-9223372036854775808, 9223372036854775807)
class core.ast.SignedShortInt(*args, **kwargs)[source]

Bases: core.ast.NumericType

name = 'signed short int'
range = (-32768, 32767)
class core.ast.Singleton(*args, **kwargs)[source]

Bases: object

class core.ast.Struct(name, *fields)[source]

Bases: core.ast.Type

add_field(field, _type)[source]
check_circular_reference(_type)[source]
generate_literal(from_declaration)[source]
get_field_by_type(_type)[source]
get_type_by_field(field)[source]
has_type(_type)[source]
property name
prototype()[source]
property struct_references
class core.ast.StructAccessExpression(op, exp, field, c_type)[source]

Bases: core.ast.UnaryASTNode

to_str(indent: int = 0)[source]
class core.ast.Switch(condition: core.ast.ASTNode, cases: Dict[core.ast.Literal, List[core.ast.ASTNode]], default: List[core.ast.ASTNode])[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.TernaryASTNode[source]

Bases: core.ast.ASTNode

property exp_1
property exp_2
property exp_3
class core.ast.TernaryExpression(exp_1, exp_2, exp_3, op, c_type)[source]

Bases: core.ast.TernaryASTNode

to_str(indent: int = 0)[source]
class core.ast.Type[source]

Bases: object

to_str(indent: int = 0)[source]
class core.ast.UnaryASTNode[source]

Bases: core.ast.ASTNode

property exp
class core.ast.UnaryExpression(op, exp, c_type, post_op=False)[source]

Bases: core.ast.UnaryASTNode

to_str(indent: int = 0)[source]
class core.ast.UnsignedChar(*args, **kwargs)[source]

Bases: core.ast.NumericType

literal_suffix = 'U'
name = 'unsigned char'
range = (0, 255)
class core.ast.UnsignedInt(*args, **kwargs)[source]

Bases: core.ast.NumericType

literal_suffix = 'U'
name = 'unsigned int'
range = (0, 4294967295)
class core.ast.UnsignedLongInt(*args, **kwargs)[source]

Bases: core.ast.NumericType

literal_suffix = 'UL'
name = 'unsigned long int'
range = (0, 4294967295)
class core.ast.UnsignedLongLongInt(*args, **kwargs)[source]

Bases: core.ast.NumericType

literal_suffix = 'ULL'
name = 'unsigned long long int'
range = (0, 18446744073709551615)
class core.ast.UnsignedShortInt(*args, **kwargs)[source]

Bases: core.ast.NumericType

literal_suffix = 'U'
name = 'unsigned short int'
range = (0, 65535)
class core.ast.Variable(value)[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
class core.ast.Void(*args, **kwargs)[source]

Bases: core.ast.Type, core.ast.Singleton

name = 'void'
class core.ast.While(condition: core.ast.ASTNode, statements: List[core.ast.ASTNode])[source]

Bases: core.ast.ASTNode

to_str(indent: int = 0)[source]
core.ast.expression_to_str(expression: object) → str[source]

Converts one expression (it could be an AST node) to a string

core.ast.generate_string_literal(size, from_delcaration, type)[source]
core.ast.get_operators(c_type, _type)[source]
core.ast.global_variable(c_type, number)[source]
core.ast.indent_str(indent_number: int) → str[source]

Given a number of indentation, returns the string to be used as prefix for indentation

core.ast.is_compound_statement(statement: core.ast.ASTNode) → bool[source]

Returns if an ASTNode is a coumpund statement

core.ast.literal_to_str(literal: object) → str[source]

Variable may be a Variable object or just a string; it returns its name as string

core.ast.local_variable(c_type, number)[source]
core.ast.name_struct_field(c_type, number)[source]
core.ast.param(number)[source]
core.ast.reference_paths_to_struct(struct, structs)[source]
core.ast.statements_to_str(statements: List[core.ast.ASTNode], indent: int) → str[source]

Takes a list of statements and returns a string with their C representation

core.ast.structs_references_graph(structs)[source]
core.ast.variable_to_var_name(variable: object) → str[source]

Variable may be a Variable object or just a string; it returns its name as string

core.generators module

Module aimed at generating the different elements of the output C program. Each syntax construct is encapsulated into a generate_ <syntax construct> function. First generate_program is called, which in turn calls other generators until the whole program is generated. The probabilities of all the syntactic constructs are considered. Program representation is return by each generator, represented as AST nodes.

core.generators.generate_basic_exp(program, function, c_type)[source]
core.generators.generate_basic_lvalue(program, function, c_type)[source]
core.generators.generate_basic_stmt(program: core.ast.Program, function: core.ast.Function)core.ast.ASTNode[source]

Generates a statement with no block inside of it

core.generators.generate_compound_stmt(program: core.ast.Program, function: core.ast.Function, stmt_depth: int)core.ast.ASTNode[source]

Generates a compound statement (with blocks inside of it), given the maximum depth for blocks

core.generators.generate_expression(program, function, exp_type, exp_depth_prob)[source]
core.generators.generate_expression_arity_1(program, function, exp_type, operator, default_generator_1, generator_1_depth_prob, **kwargs)[source]
core.generators.generate_expression_arity_2(program, function, exp_type, operator, default_generator_1, generator_1_depth_prob, default_generator_2, generator_2_depth_prob, **kwargs)[source]
core.generators.generate_expression_arity_3(program, function, exp_type, operator, default_generator_1, generator_1_depth_prob, default_generator_2, generator_2_depth_prob, default_generator_3, generator_3_depth_prob, **kwargs)[source]
core.generators.generate_expression_incdec(program, function, exp_type, operator)[source]
core.generators.generate_expression_invocation(program, function, return_type, exp_depth_prob)[source]
core.generators.generate_function(program, function, return_type)[source]
core.generators.generate_function_param_types(program, function)[source]
core.generators.generate_global_var(program, function, c_type)[source]
core.generators.generate_literal(program, function, literal_type, from_declaration=False)[source]
core.generators.generate_local_var(program, function, c_type)[source]
core.generators.generate_lvalue(program, function, exp_type, exp_depth_prob)[source]
core.generators.generate_param_var(program, function, c_type)[source]
core.generators.generate_program()[source]
core.generators.generate_program_with_function_distribution(distribution, args, remove_unwanted_functions=True)[source]
core.generators.generate_stmt_assignment(program, function)[source]
core.generators.generate_stmt_augmented_assignment(program, function)[source]
core.generators.generate_stmt_block(program: core.ast.Program, function: core.ast.Function, stmt_depth: int)core.ast.Block[source]

Generates a block statement

core.generators.generate_stmt_do(program: core.ast.Program, function: core.ast.Function, stmt_depth: int)core.ast.Do[source]

Generates a do statement

core.generators.generate_stmt_expression_stmt(program, function)[source]

Generates an expression statement: <expression> ;

core.generators.generate_stmt_for(program: core.ast.Program, function: core.ast.Function, stmt_depth: int)core.ast.For[source]

Generates a for statement

core.generators.generate_stmt_func(program: core.ast.Program, function: core.ast.Function, stmt_depth: Optional[int] = None)core.ast.ASTNode[source]

Generates a statement inside a function

core.generators.generate_stmt_if(program: core.ast.Program, function: core.ast.Function, stmt_depth: int)core.ast.If[source]

Generates an if / else statement

core.generators.generate_stmt_incdec(program, function)[source]
core.generators.generate_stmt_invocation(program, function, invoked_func=None)[source]
core.generators.generate_stmt_return(program, function, exp=None)[source]
core.generators.generate_stmt_switch(program: core.ast.Program, function: core.ast.Function, stmt_depth: int)core.ast.Switch[source]

Generates a switch statement

core.generators.generate_stmt_while(program: core.ast.Program, function: core.ast.Function, stmt_depth: int)core.ast.While[source]

Generates a while statement

core.generators.generate_type(program, function, new_type_cls=None, old_type_obj=None)[source]
core.generators.generate_type_array(program, function, old_type)[source]
core.generators.generate_type_pointer(program, function, old_type)[source]
core.generators.generate_type_struct(program, function, old_type)[source]

core.operators module

This module defines the way expressions are built, relative to the syntax of the C operators. You can modify then to change the way expressions are created. However, do not change their names, since a naming convention is used (i.e., reflection).

core.operators_types module

This module defines the types of C experessions, relative to the syntax of the C operators. It only contains data, no computation. Data are two dictionaries: by_operands (depending on the operator) and by_return (returned by an operator).

core.probs module

This file defines the probability for creating each part of the program. Probability may be defined manually, or using the helper functions in probs_helper. They are expressed as dictionaries: {value: probability of that value}. The sum of the probabilities must be 1.0. For a fixed value, just set its probability to 1.0. The doc:Dict[str,str] documents all the probabilities meanings to be shown to the user.

core.probs.does_this_probability_exist(probability_name: str) → bool[source]
core.probs.set_probabilities(probabilities: Dict[str, dict]) → None[source]

Modifies the global variables of this module, that is the default probabilities of Cnerator

core.probs_helper module

These functions are used to define the probability of the different syntax constructs of the C language. Probabilities are defined as Dict[object, float], where the key are the different values to be generated and the content defines the probability for each one. All the probabilities (contents of the dictionary) must sum 0.0.

core.probs_helper.compute_equal_prob(keys: set, base: float = 1.0) → Dict[object, float][source]

Given a list of elements (keys) returns a dictionary with the equal probabilities for all the elements

core.probs_helper.compute_inverse_proportional_prob(population: Dict[object, float], base: float = 1.0) → Dict[object, float][source]

Given a dictionary of elements and their inverse proportional occurrence, returns a dictionary with its probability distribution. For example, {‘a’:1, ‘b’:2, c:’2’} returns {‘a’:0.5, ‘b’:0.25, ‘c’:0.25}

core.probs_helper.compute_normal_prob(mean: int, stdev: int, base: float = 1.0, number: int = 30) → Dict[object, float][source]

Given the mean and standard deviation, returns a dictionary a normal distribution

core.probs_helper.compute_proportional_prob(population: Dict[object, float], base: float = 1.0) → Dict[object, float][source]

Given a dictionary of elements and their proportional occurrence, returns a dictionary with its probability distribution. For example, {‘a’:1, ‘b’:2, c:’1’} returns {‘a’:0.25, ‘b’:0.5, c:’0.25’}

core.probs_helper.random_value(probabilities: Dict[object, float])[source]

Returns one of the values in probabilities (key) given its probability (content)

core.type_inference module

Different functions to infer the type of a syntax construct. This module heavily depends on the data provided in the operators_types module.

core.type_inference.infer_operands_type(program, function, arity, operator, ret_t)[source]
core.type_inference.infer_return_type(program, function, operator, *args_t)[source]

core.utils module

Different general-purpose functions used in the implementation of the core library.

core.utils.camel_case_to_snake_case(name: str) → str[source]

Converts a camel-case string into its snake-case representation

core.utils.print_if_verbose(*args, sep=' ', end='\n', file=None)[source]

Prints a message if the verbose option has been passed to the application

core.utils.print_to_std_error(*args, sep=' ', end='\n')[source]

This function wraps the print Python function to show something in the standard output error