A flexible and extensible .NET library for representing code as Abstract Syntax Trees (AST), serializing to YAML, and generating code in multiple programming languages.
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.
- 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
- FunctionDeclaration: Represents function/method declarations with parameters and body
- Parameter: Function parameters with optional default values
- ReturnStatement: Return statements with optional expressions
- AstLeafNode: Generic leaf nodes for literals (strings, numbers, booleans)
- Python: Full support for function generation with type hints and proper indentation
Add the NuGet package:
dotnet add package ktsu.Coderusing 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")));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 + bvar 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"// Deserialize from YAML
var deserializer = new YamlDeserializer();
AstNode deserializedAst = deserializer.Deserialize(yamlContent);
// Generate code from deserialized AST
string regeneratedCode = pythonGenerator.Generate(deserializedAst);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
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
}
}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.AppContributions 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
MIT License. Copyright (c) ktsu.dev