ktsu.Coder.Core 1.0.3

Prefix Reserved
dotnet add package ktsu.Coder.Core --version 1.0.3
                    
NuGet\Install-Package ktsu.Coder.Core -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ktsu.Coder.Core" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ktsu.Coder.Core" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="ktsu.Coder.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ktsu.Coder.Core --version 1.0.3
                    
#r "nuget: ktsu.Coder.Core, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=ktsu.Coder.Core&version=1.0.3
                    
Install ktsu.Coder.Core as a Cake Addin
#tool nuget:?package=ktsu.Coder.Core&version=1.0.3
                    
Install ktsu.Coder.Core as a Cake Tool

ktsu.Coder

A flexible and extensible .NET library for representing code as Abstract Syntax Trees (AST), serializing to YAML, and generating code in multiple programming languages.

Overview

ktsu.Coder provides a language-agnostic way to represent code structures using Abstract Syntax Trees. The library allows you to:

  • Build AST structures programmatically
  • Serialize AST to human-readable YAML format
  • Generate code in supported programming languages
  • Perform round-trip serialization/deserialization

This makes it ideal for code generation tools, transpilers, and any application that needs to work with code structures in a language-independent way.

Features

  • Language-Agnostic AST: Represent code structures without being tied to a specific programming language
  • YAML Serialization: Human-readable serialization format that's perfect for version control and diffs
  • Extensible Language Support: Plugin-based architecture for adding new target languages
  • Deep Cloning: Full support for cloning AST structures
  • Type Safety: Strongly-typed AST nodes with compile-time safety
  • Metadata Support: Attach custom metadata to any AST node

Supported AST Node Types

  • FunctionDeclaration: Represents function/method declarations with parameters and body
  • Parameter: Function parameters with optional default values
  • ReturnStatement: Return statements with optional expressions
  • AstLeafNode<T>: Generic leaf nodes for literals (strings, numbers, booleans)

Supported Target Languages

  • Python: Full support for function generation with type hints and proper indentation

Installation

Add the NuGet package:

dotnet add package ktsu.Coder

Quick Start

Creating an AST

using ktsu.Coder.Core.Ast;
using ktsu.Coder.Core.Languages;
using ktsu.Coder.Core.Serialization;

// Create a function declaration
var function = new FunctionDeclaration("calculate_sum")
{
    ReturnType = "int"
};

// Add parameters
function.Parameters.Add(new Parameter("a", "int"));
function.Parameters.Add(new Parameter("b", "int"));
function.Parameters.Add(new Parameter("debug", "bool")
{
    IsOptional = true,
    DefaultValue = "False"
});

// Add function body
function.Body.Add(new ReturnStatement(new AstLeafNode<string>("a + b")));

Serializing to YAML

var serializer = new YamlSerializer();
string yamlContent = serializer.Serialize(function);
Console.WriteLine(yamlContent);

Output:

functionDeclaration:
    name: calculate_sum
    returnType: int
    parameters:
        - name: a
          type: int
        - name: b
          type: int
        - name: debug
          type: bool
          isOptional: true
          defaultValue: False
    body:
        - returnStatement:
              expression:
                  Leaf<String>: a + b

Generating Code

var pythonGenerator = new PythonGenerator();
string pythonCode = pythonGenerator.Generate(function);
Console.WriteLine(pythonCode);

Output:

def calculate_sum(a: int, b: int, debug: bool = False) -> int:
    return "a + b"

Round-trip Serialization

// Deserialize from YAML
var deserializer = new YamlDeserializer();
AstNode deserializedAst = deserializer.Deserialize(yamlContent);

// Generate code from deserialized AST
string regeneratedCode = pythonGenerator.Generate(deserializedAst);

Architecture

The library follows SOLID principles with a clean separation of concerns:

  • AST Layer: Language-agnostic representation of code structures
  • Serialization Layer: YAML serialization/deserialization
  • Language Layer: Pluggable code generators for specific languages

Extending with New Languages

To add support for a new language, implement the ILanguageGenerator interface:

public class JavaScriptGenerator : LanguageGeneratorBase
{
    public override string LanguageId => "javascript";
    public override string DisplayName => "JavaScript";
    public override string FileExtension => "js";

    protected override void GenerateInternal(AstNode node, StringBuilder builder, int indentLevel)
    {
        // Implementation for JavaScript code generation
    }
}

Examples

The repository includes two example applications:

  • Coder.CLI: Command-line tool demonstrating basic functionality
  • Coder.App: Console application with more complex examples

Run them to see the library in action:

dotnet run --project Coder.CLI
dotnet run --project Coder.App

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues for:

  • New language generators
  • Additional AST node types
  • Bug fixes and improvements
  • Documentation enhancements

License

MIT License. Copyright (c) ktsu.dev

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 398 6/13/2025 1.0.3 is deprecated because it is no longer maintained.
1.0.2 233 6/13/2025

## v1.0.3 (patch)

Changes since v1.0.2:

- Enhance YAML serialization and deserialization for new node types ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance expression system integration and serialization support ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.2 (patch)

Changes since v1.0.1:

- Enhance expression handling in ExpressionDemo ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor project structure and enhance expression handling ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance YAML serialization and deserialization handling ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor expression handling and improve code consistency ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.1 (patch)

Changes since v1.0.0:

- Implement dependency injection and enhance AST expression handling ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor AST expression handling and enhance visual editor support ([@matt-edmondson](https://github.com/matt-edmondson))
- Update implementation and design documents to reflect current project status ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.0 (major)

- Design and implementation plan documentation ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor Coder library to address build errors and improve serialization logic ([@matt-edmondson](https://github.com/matt-edmondson))
- Add AST node classes and YAML serialization support ([@matt-edmondson](https://github.com/matt-edmondson))
- Update package references in Coder.Core.csproj ([@matt-edmondson](https://github.com/matt-edmondson))
- Update project configuration and enhance build scripts ([@matt-edmondson](https://github.com/matt-edmondson))
- Add initial project files and implementation plan for Coder library ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance Coder.App with type consistency and testing improvements ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor AST node classes to improve deep cloning and metadata handling ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance Coder.App with XML documentation and null handling improvements ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor AST node classes and improve YAML serialization ([@matt-edmondson](https://github.com/matt-edmondson))
- Initial commit for Coder ([@matt-edmondson](https://github.com/matt-edmondson))
- Transform Coder.App into a TUI using Spectre.Console ([@matt-edmondson](https://github.com/matt-edmondson))
- Remove Sample class and associated unit tests from the project ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance Coder library with comprehensive CLI and App examples ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor AST implementation and improve serialization ([@matt-edmondson](https://github.com/matt-edmondson))