Skip to main content

crashtest.hxsmith.gen

The generator core: random choices, scopes, emission, and the typed expression/statement drivers.

Gen knows nothing about particular language constructs; features plug in through Feature (see feature.py). Everything random flows through Gen.rng, so a seed plus a feature set reproduces a program exactly.

class Config​

max_expr_depth: int​

max_expr_depth = 2

max_stmt_depth: int​

max_stmt_depth = 2

block_size: Tuple[int, int]​

block_size = (1, 4)

main_statements: Tuple[int, int]​

main_statements = (6, 14)

helper_functions: Tuple[int, int]​

helper_functions = (2, 5)

init​

def __init__(max_expr_depth: int = 2, max_stmt_depth: int = 2, block_size: Tuple[int, int] = (1, 4), main_statements: Tuple[int, int] = (6, 14), helper_functions: Tuple[int, int] = (2, 5)) -> None

class Fn​

Something callable. With a receiver it is a method: <receiver expr>.name(args).

name: str​

params: Tuple[Ty, ...]​

ret: Ty​

receiver: Optional[Ty]​

receiver = None

init​

def __init__(name: str, params: Tuple[Ty, ...], ret: Ty, receiver: Optional[Ty] = None) -> None

class Gen​

seed​

seed = seed

rng​

rng = random.Random(seed)

features​

features = list(features)

cfg​

cfg = config or Config()

class_name​

class_name = f'Fuzz{seed}'

lines: List[str]​

lines = []

level​

level = 0

scopes: List[List[Var]]​

scopes = [[]]

expr_depth​

expr_depth = 0

effects​

effects = 0

stmt_depth​

stmt_depth = 0

loop_depth​

loop_depth = 0

ret_ty: Optional[Ty]​

ret_ty = None

decls: List[str]​

decls = []

members: List[str]​

members = []

fns: List[Fn]​

fns = []

subtypes: Dict[Ty, List[Ty]]​

subtypes = {}

state: Dict[str, Any]​

state = {}

used: Set[str]​

used = set()

ready: Set[str]​

ready = {(f.name) for f in features}

active: List[Feature]​

init​

def __init__(seed: int, features: Sequence[Feature], config: Optional[Config] = None) -> None

chance​

def chance(p: float) -> bool

between​

def between(lo: int, hi: int) -> int

pick​

def pick(items: Sequence[Any]) -> Any

weighted​

def weighted(items: Sequence[Any], weights: Sequence[float]) -> Any

fresh​

def fresh(prefix: str) -> str

emit​

def emit(line: str = '') -> None

capture​

@contextmanager
def capture(level: int = 0) -> Iterator[List[str]]

Redirect emit into a fresh buffer; the buffer outlives the context.

block​

@contextmanager
def block(header: str, footer: str = '}') -> Iterator[None]

header, an indented scope, footer. Emits nothing for the footer if it is empty.

function​

@contextmanager
def function(params: Sequence[Tuple[str, Ty]], ret: Optional[Ty], inherit: bool = False) -> Iterator[None]

The context of a function body: its parameters in scope, return typed, loops reset. A closure passes inherit to keep seeing the enclosing function's variables.

declare_var​

def declare_var(name: str, ty: Ty, mutable: bool = True) -> Var

visible​

def visible(ty: Optional[Ty] = None, mutable: bool = False) -> List[Var]

subtype_choices​

def subtype_choices(ty: Ty) -> List[Ty]

pick_type​

def pick_type(depth: int = 0) -> Ty

show​

def show(expr: str, ty: Ty) -> str

A String expression printing expr deterministically.

expr​

def expr(ty: Ty) -> str

An expression of exactly type ty (or a subtype).

exprs​

def exprs(*tys: Ty) -> List[str]

stmt​

def stmt() -> bool

Emit one statement. Returns False if nothing could be built (nothing is left behind).

statements​

def statements(count: Optional[int] = None) -> None

observe​

def observe() -> None

Print every mutable scalar in scope: the function's final state becomes observable.

body​

def body(ret: Optional[Ty], count: Optional[int] = None) -> None

A function body: some statements, then the return value if there is one.

class GenError(Exception)​

No way to build what was asked in the current context; the caller backs out.

class Var​

name: str​

ty: Ty​

mutable: bool​

mutable = True

init​

def __init__(name: str, ty: Ty, mutable: bool = True) -> None