EvolveBlock

Helpers & Utilities · AiDotNet.Evolution.Programs

Finds, extracts, and rewrites the marked editable region of a program source.

For Beginners

Suppose you want a language model to improve one function in a file without touching the rest. You put a start comment above it and an end comment below it, and this class does the rest: it finds those comments, hands you the code between them, and glues a new version back in place. If the markers are missing or broken it tells you so instead of quietly mangling the file. Use ForLanguage to get comment markers that are valid in your language.

How It Works

Program evolution keeps a file runnable by confining edits to a region fenced with comment markers, so imports, fixtures, and the entry point an evaluator calls survive every generation. This class implements that fence: Extract splits the source around each marker pair, TryReplaceFirst and TryReplaceAll substitute new bodies while preserving every character outside the blocks, and Wrap adds markers to a file that has none, matching the way the reference implementation prepares a bare code string.

Detection matches the reference implementation’s substring test, so an indented marker or a marker followed by a trailing comment is still recognized, and CRLF, CR, and LF sources are handled identically. Every method is deterministic, performs no I/O, and reports malformed input through Status or a false return value rather than by throwing; exceptions are reserved for null arguments.

Methods

Method Summary
EvolveBlockExtractionResult Extract(string source, EvolveBlockMarkers markers) Scans source using the comment markers that are valid in a language.
EvolveBlockExtractionResult Extract(string source, ProgramLanguage language) Scans source using the comment markers that are valid in a language.
bool ContainsStartMarker(string source, EvolveBlockMarkers markers) Reports whether source contains at least one start marker.
bool TryReplaceAll(string source, IReadOnlyList<string> replacements, EvolveBlockMarkers markers, string rewritten) Replaces the body of every well-formed block, preserving everything outside them.
bool TryReplaceFirst(string source, string replacement, EvolveBlockMarkers markers, string rewritten) Replaces the body of the first well-formed block, preserving everything outside it.
string Wrap(string source, EvolveBlockMarkers markers) Wraps a source that has no start marker in one evolve block.

Fields

Field Summary
string DefaultEndMarker The default hash-comment end marker, matching the reference OpenEvolve implementation.
string DefaultStartMarker The default hash-comment start marker, matching the reference OpenEvolve implementation.
string SlashEndMarker The double-slash end marker for C-like languages.
string SlashStartMarker The double-slash start marker for C-like languages.
string SqlEndMarker The double-dash end marker for SQL.
string SqlStartMarker The double-dash start marker for SQL.