MrKWatkins.Ast 0.9.105

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package MrKWatkins.Ast --version 0.9.105                
NuGet\Install-Package MrKWatkins.Ast -Version 0.9.105                
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.105" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MrKWatkins.Ast --version 0.9.105                
#r "nuget: MrKWatkins.Ast, 0.9.105"                
#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.
// Install MrKWatkins.Ast as a Cake Addin
#addin nuget:?package=MrKWatkins.Ast&version=0.9.105

// Install MrKWatkins.Ast as a Cake Tool
#tool nuget:?package=MrKWatkins.Ast&version=0.9.105                

MrKWatkins.Ast

Build Status NuGet Version and Download Count

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.

Usage

Create a base node type for your abstract syntax tree:

public abstract class Expression : Node<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.ThisAndDescendents;
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.HasErrors; // 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);

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

Manipulate and copy the tree:

sixty.ReplaceWith(new ConstantNumber(55));

var copy = expression.Copy();

Full documentation will be available with version 1.0.x.

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 net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

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

Package Downloads
MrKWatkins.OakAsm

Shared code for the OakAsm project.

MrKWatkins.OakAsm.Formatting

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

MrKWatkins.OakAsm.Z80

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

MrKWatkins.OakAsm.Parsing

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

MrKWatkins.OakAsm.IO

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.9.128 733 12/22/2024
0.9.127 137 12/17/2024
0.9.126 77 12/17/2024
0.9.125 81 12/17/2024
0.9.124 212 12/15/2024
0.9.123 234 12/8/2024
0.9.122 219 11/17/2024
0.9.121 399 11/1/2024
0.9.120 733 10/24/2024
0.9.119 667 10/21/2024
0.9.118 1,422 7/8/2024
0.9.117 678 3/27/2024
0.9.116 132 3/23/2024
0.9.115 122 3/22/2024
0.9.114-alpha 103 3/7/2024
0.9.113 160 1/21/2024
0.9.112 117 1/18/2024
0.9.111 516 12/30/2023
0.9.110 260 11/30/2023
0.9.109 180 10/5/2023
0.9.108 511 8/22/2023
0.9.107 142 8/21/2023
0.9.106 181 8/16/2023
0.9.105 370 7/6/2023
0.9.104 185 7/4/2023
0.9.103 184 6/20/2023
0.9.102 171 6/19/2023
0.9.101 187 6/6/2023
0.9.100 164 6/6/2023
0.9.99 160 5/23/2023
0.9.98 173 5/15/2023
0.9.97 177 5/15/2023
0.9.96 166 5/11/2023
0.9.95 165 5/3/2023
0.9.94 163 5/3/2023
0.9.93 153 5/3/2023
0.9.92 170 5/3/2023
0.9.91 184 5/3/2023
0.9.90 186 5/3/2023
0.9.89 203 4/28/2023
0.9.87 215 4/27/2023
0.9.85 226 4/25/2023
0.9.81 212 4/24/2023
0.9.78 213 4/24/2023
0.9.76 197 4/24/2023
0.9.74 184 4/24/2023
0.9.73 201 4/24/2023
0.9.71 200 4/24/2023
0.9.69 191 4/24/2023
0.9.67 218 4/20/2023
0.9.63 219 4/20/2023
0.9.58 276 3/7/2023
0.9.56 260 3/7/2023
0.9.55 262 3/7/2023
0.9.49 263 3/6/2023
0.9.47 312 2/19/2023
0.9.46 293 2/19/2023
0.9.43 277 2/19/2023
0.9.42 309 1/27/2023
0.9.41 588 1/4/2023
0.9.34 344 12/10/2022
0.9.27 476 9/26/2022
0.9.26 478 9/26/2022
0.9.25 454 9/26/2022
0.9.24 454 9/26/2022
0.9.23 496 9/23/2022
0.9.21 437 9/23/2022
0.9.10 462 9/18/2022
0.9.9 485 9/18/2022
0.9.8 480 9/17/2022
0.9.7 518 9/17/2022
0.9.6 490 9/17/2022
0.9.5 487 9/4/2022
0.9.4 492 9/3/2022
0.9.3 487 9/3/2022
0.9.2 488 9/3/2022
0.9.1 509 8/29/2022