CDTk 7.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CDTk --version 7.1.0
                    
NuGet\Install-Package CDTk -Version 7.1.0
                    
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="CDTk" Version="7.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CDTk" Version="7.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CDTk" />
                    
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 CDTk --version 7.1.0
                    
#r "nuget: CDTk, 7.1.0"
                    
#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.
#:package CDTk@7.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CDTk&version=7.1.0
                    
Install as a Cake Addin
#tool nuget:?package=CDTk&version=7.1.0
                    
Install as a Cake Tool

Welcome to CDTk!

Start Here: The Basics

CDTk simplifies compiler design with its unified and fully-typed pipeline:

  • TokenSet: Identify and categorize input patterns (e.g., numbers, keywords, operators).
  • RuleSet: Combine tokens into meaningful structures using grammar rules.
  • MapSet: Convert parsed structures into your desired output (e.g., code, JSON, config).
class Number { }
class Identifier { }
class AssignmentNode { }

var tokens = new TokenSet
{
    new Token<Number>(@"\d+"),
    new Token<Identifier>(@"[A-Za-z_][A-Za-z0-9_]*")
};

var rules = new RuleSet
{
    new Rule<AssignmentNode>("variable:@Identifier '=' value:@Number")
        .Returns<AssignmentNode>("variable", "value")
};

var mappings = new MapSet
{
    new Map<AssignmentNode>("int {variable} = {value};")
};

var compiler = new Compiler()
    .WithTokens(tokens)
    .WithRules(rules)
    .WithTarget(mappings)
    .Build();

var result = compiler.Compile("x = 42;");
Console.WriteLine(result.Output);  // Outputs: int x = 42;

The Pipeline

When you call compiler.Compile(input), CDTk processes the input systematically:

  1. Lexing (TokenSet): Converts input text into tokens (e.g., Identifier("x"), Equals("="), Number("42")).
  2. Parsing (RuleSet): Organizes tokens into an AST (Abstract Syntax Tree).
  3. Code Generation (MapSet): Transforms the AST into output (e.g., int x = 42;).

Learn More: Check out the Pipeline Overview.


Defining Your Compiler

TokenSet

Tokens define basic patterns in your language. For example:

var tokens = new TokenSet
{
    new Token<Identifier>(@"[A-Za-z_][A-Za-z0-9_]*"),  // Variables
    new Token<Number>(@"\d+"),                        // Numbers
    new Token<Equals>("="),                           // Assignment operator
    new Token<Whitespace>(@"\s+").Ignore()            // Ignored whitespace
};

Learn More: See Tokens.

RuleSet

Rules describe your grammar in a declarative syntax:

var rules = new RuleSet
{
    new Rule<AssignmentNode>("variable:@Identifier '=' value:@Number")
        .Returns<AssignmentNode>("variable", "value")
};

Learn More: See Rules.

MapSet

Define templates for code generation or output transformation:

var mappings = new MapSet
{
    new Map<AssignmentNode>("int {variable} = {value};")  // Outputs C syntax
};

Learn More: See Mapping.


Compiling Your Input

Combine the TokenSet, RuleSet, and MapSet into Compiler:

var compiler = new Compiler()
    .WithTokens(tokens)
    .WithRules(rules)
    .WithTarget(mappings)
    .Build();

var result = compiler.Compile("x = 42;");
Console.WriteLine(result.Output);  // Outputs: int x = 42;

Debugging and Next Steps

Debugging

If you encounter issues:

  1. Unexpected Tokens: Adjust your TokenSet definitions.
  2. Grammar Errors: Refine your RuleSet for clarity.
  3. Missing Outputs: Confirm MapSet covers all node types.

Learn More: See the Debugging Guide.


Explore Further

CDTk offers advanced features and infinite customization:

  • Tokens: Dive deeper into token definitions.
  • Rules: Learn about recursive structures and alternations.
  • Mapping: Explore multi-target generation.
  • Examples: Study real-world compiler setups.
Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.