ktsu.Coder.Core
1.0.9
Prefix Reserved
dotnet add package ktsu.Coder.Core --version 1.0.9
NuGet\Install-Package ktsu.Coder.Core -Version 1.0.9
<PackageReference Include="ktsu.Coder.Core" Version="1.0.9" />
<PackageVersion Include="ktsu.Coder.Core" Version="1.0.9" />
<PackageReference Include="ktsu.Coder.Core" />
paket add ktsu.Coder.Core --version 1.0.9
#r "nuget: ktsu.Coder.Core, 1.0.9"
#:package ktsu.Coder.Core@1.0.9
#addin nuget:?package=ktsu.Coder.Core&version=1.0.9
#tool nuget:?package=ktsu.Coder.Core&version=1.0.9
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.Ast;
using ktsu.Coder.Languages;
using ktsu.Coder.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 | Versions 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 is compatible. 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. |
-
net10.0
- ktsu.DeepClone (>= 2.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.7)
- YamlDotNet (>= 16.3.0)
-
net9.0
- ktsu.DeepClone (>= 2.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.7)
- YamlDotNet (>= 16.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
## v1.0.9 (patch)
Changes since v1.0.8:
- chore: store icon.png in LFS as .gitattributes declares ([@matt-edmondson](https://github.com/matt-edmondson))
- docs: scope build badge to the default branch ([@matt-edmondson](https://github.com/matt-edmondson))
- docs: correct README, DESCRIPTION and TAGS metadata ([@matt-edmondson](https://github.com/matt-edmondson))