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.
Array
(_type, size=0, pointer_literal=False, pointer_declaration=False)[source]¶ Bases:
core.ast.Pointer
-
property
name
¶
-
property
-
class
core.ast.
ArrayAccessExpression
(left, right, c_type)[source]¶ Bases:
core.ast.BinaryASTNode
-
class
core.ast.
Assignment
(left, op, right, c_type)[source]¶ Bases:
core.ast.BinaryASTNode
-
class
core.ast.
BinaryASTNode
[source]¶ Bases:
core.ast.ASTNode
-
property
left
¶
-
property
right
¶
-
property
-
class
core.ast.
BinaryExpression
(left, op, right, c_type)[source]¶ Bases:
core.ast.BinaryASTNode
-
class
core.ast.
Block
(statements: list)[source]¶ Bases:
core.ast.ASTNode
-
class
core.ast.
Bool
(*args, **kwargs)[source]¶ Bases:
core.ast.Type
,core.ast.Singleton
-
name
= 'bool'¶
-
values
= ('true', 'false')¶
-
-
class
core.ast.
Break
[source]¶ Bases:
core.ast.ASTNode
-
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
-
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
-
class
core.ast.
Function
(name, return_type, param_types)[source]¶ Bases:
core.ast.ASTNode
-
property
stmts
¶
-
property
-
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
-
class
core.ast.
Invocation
(func_name, arguments, return_type, _is_stmt)[source]¶ Bases:
core.ast.ASTNode
-
class
core.ast.
Label
(label)[source]¶ Bases:
core.ast.ASTNode
-
class
core.ast.
Literal
(value, type)[source]¶ Bases:
core.ast.ASTNode
-
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
-
class
core.ast.
Pointer
(type)[source]¶ Bases:
core.ast.Type
-
property
name
¶
-
property
-
class
core.ast.
Program
[source]¶ Bases:
core.ast.ASTNode
-
property
functions
¶
-
property
-
class
core.ast.
Return
(exp=None, c_type=None)[source]¶ Bases:
core.ast.UnaryASTNode
-
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.
Struct
(name, *fields)[source]¶ Bases:
core.ast.Type
-
property
name
¶
-
property
struct_references
¶
-
property
-
class
core.ast.
StructAccessExpression
(op, exp, field, c_type)[source]¶ Bases:
core.ast.UnaryASTNode
-
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
-
class
core.ast.
TernaryASTNode
[source]¶ Bases:
core.ast.ASTNode
-
property
exp_1
¶
-
property
exp_2
¶
-
property
exp_3
¶
-
property
-
class
core.ast.
TernaryExpression
(exp_1, exp_2, exp_3, op, c_type)[source]¶ Bases:
core.ast.TernaryASTNode
-
class
core.ast.
UnaryASTNode
[source]¶ Bases:
core.ast.ASTNode
-
property
exp
¶
-
property
-
class
core.ast.
UnaryExpression
(op, exp, c_type, post_op=False)[source]¶ Bases:
core.ast.UnaryASTNode
-
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
-
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
-
core.ast.
expression_to_str
(expression: object) → str[source]¶ Converts one expression (it could be an AST node) to a string
-
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.
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.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_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_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_invocation
(program, function, return_type, exp_depth_prob)[source]¶
-
core.generators.
generate_program_with_function_distribution
(distribution, args, remove_unwanted_functions=True)[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_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.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_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.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.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