MrKWatkins.Ast 0.9.139

Prefix Reserved
dotnet add package MrKWatkins.Ast --version 0.9.139
                    
NuGet\Install-Package MrKWatkins.Ast -Version 0.9.139
                    
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="MrKWatkins.Ast" Version="0.9.139" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MrKWatkins.Ast" Version="0.9.139" />
                    
Directory.Packages.props
<PackageReference Include="MrKWatkins.Ast" />
                    
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 MrKWatkins.Ast --version 0.9.139
                    
#r "nuget: MrKWatkins.Ast, 0.9.139"
                    
#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 MrKWatkins.Ast@0.9.139
                    
#: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=MrKWatkins.Ast&version=0.9.139
                    
Install as a Cake Addin
#tool nuget:?package=MrKWatkins.Ast&version=0.9.139
                    
Install as a Cake Tool

MrKWatkins.Ast

Build Status NuGet Version NuGet Downloads

C# library to build and manipulate abstract syntax trees when writing compilers.

Background

As part of my Oakley project to create a compiler and it's associated OakAsm project to create an assembler (details coming soon) I needed to represent abstract syntax trees in C#. This library was created so I could share the code between those two projects.

Full documentation is available at mrkwatkins.github.io/Ast. Source code and issue tracking are on GitHub.

Usage

Create a base node type for your abstract syntax tree:

public abstract class Expression : PropertyNode<Expression>
{
}

Create more specific nodes:

public sealed class ConstantNumber : Expression
{
    public ConstantNumber(int value)
    {
        Value = value;
    }

    public int Value
    {
        get => Properties.GetOrThrow<int>(nameof(Value));
        init => Properties.Set(nameof(Value), value);
    }
}

public sealed class Addition : Expression
{
    public Addition(ConstantNumber x, ConstantNumber y)
    {
        Children.Add(x);
        Children.Add(y);
    }
}

Walk the tree:

var fifty = new ConstantNumber(50);
var sixty = new ConstantNumber(60);
var expression = new Addition(fifty, sixty);

var allNodes = expression.ThisAndDescendants;
var fiftyAndParent = fifty.ThisAndAncestors;
var fiftyAndSixty = fifty.ThisAndNextSiblings;
var justSixty = sixty.PreviousSibling;
var result = expression.Children.OfType<ConstantNumber>().Sum(n => n.Value);

Mark nodes with errors, warnings and info messages:

sixty.AddError("Value must be less than 55.");
var expressionHasErrors = expression.ThisAndDescendantsHaveErrors; // true.

Associate nodes with their position in source code during parsing:

var source = new TextFile(new FileInfo("MySource.code"));   // Contains "50 + 60".
expression.SourcePosition = source.CreatePosition(0, 7, 0, 0); // startIndex, length, startLineIndex, startColumnIndex.
fifty.SourcePosition = source.CreatePosition(0, 2, 0, 0);
sixty.SourcePosition = source.CreatePosition(5, 2, 0, 5);

Output errors with highlighted source information:

var errors = MessageFormatter.FormatErrors(expression, MessageFormatterOptions.PrefixAndHighlight);

// MySource.code (1, 6): Error: Value must be less than 55.
// 50 + 60
//      --

Manipulate and copy the tree:

sixty.ReplaceWith(new ConstantNumber(55));

var copy = expression.Copy();

Install

dotnet add package MrKWatkins.Ast

Pull Requests

I'm not accepting pull requests at the current time; this project is tailored for some other projects of mine and I want to get them in a suitable state first.

Feel free to raise issues for bugs or suggestions, but I make no guarantees they will get looked at I'm afraid!

License

MIT

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 (19)

Showing the top 5 NuGet packages that depend on MrKWatkins.Ast:

Package Downloads
MrKWatkins.OakAsm

Shared code for the OakAsm project.

MrKWatkins.OakAsm.Z80

Library containing assembly definitions for the Z80 CPU, part of the OakAsm project.

MrKWatkins.OakAsm.Formatting

Library for formatting assembly code, part of the OakAsm project.

MrKWatkins.OakAsm.Disassembly

CPU-agnostic disassembly of machine code to an assembly AST, part of the OakAsm project.

MrKWatkins.OakAsm.Parsing

Library for parsing assembly code, part of the OakAsm project.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.9.139 574 10/6/2026
0.9.138 119 10/6/2026
0.9.137 920 8/1/2026
0.9.136 156 7/29/2026
0.9.135 1,556 3/10/2026
0.9.134 1,466 11/15/2025
0.9.133 758 9/8/2025
0.9.132 481 8/12/2025
0.9.131 407 8/10/2025
0.9.130 475 8/6/2025
0.9.129 2,591 1/29/2025
0.9.128 2,536 12/22/2024
0.9.127 319 12/17/2024
0.9.126 254 12/17/2024
0.9.125 260 12/17/2024
0.9.124 456 12/15/2024
0.9.123 563 12/8/2024
0.9.122 442 11/17/2024
0.9.121 642 11/1/2024
0.9.120 1,176 10/24/2024
Loading failed