MrKWatkins.Ast 0.9.94

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.94                
NuGet\Install-Package MrKWatkins.Ast -Version 0.9.94                
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.94" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MrKWatkins.Ast --version 0.9.94                
#r "nuget: MrKWatkins.Ast, 0.9.94"                
#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.94

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

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. 
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 53 12/22/2024
0.9.127 119 12/17/2024
0.9.126 68 12/17/2024
0.9.125 72 12/17/2024
0.9.124 195 12/15/2024
0.9.123 225 12/8/2024
0.9.122 214 11/17/2024
0.9.121 394 11/1/2024
0.9.120 728 10/24/2024
0.9.119 660 10/21/2024
0.9.118 1,420 7/8/2024
0.9.117 677 3/27/2024
0.9.116 131 3/23/2024
0.9.115 121 3/22/2024
0.9.114-alpha 102 3/7/2024
0.9.113 159 1/21/2024
0.9.112 116 1/18/2024
0.9.111 515 12/30/2023
0.9.110 259 11/30/2023
0.9.109 179 10/5/2023
0.9.108 510 8/22/2023
0.9.107 141 8/21/2023
0.9.106 180 8/16/2023
0.9.105 368 7/6/2023
0.9.104 183 7/4/2023
0.9.103 182 6/20/2023
0.9.102 169 6/19/2023
0.9.101 185 6/6/2023
0.9.100 162 6/6/2023
0.9.99 158 5/23/2023
0.9.98 170 5/15/2023
0.9.97 174 5/15/2023
0.9.96 164 5/11/2023
0.9.95 163 5/3/2023
0.9.94 161 5/3/2023
0.9.93 151 5/3/2023
0.9.92 168 5/3/2023
0.9.91 182 5/3/2023
0.9.90 184 5/3/2023
0.9.89 201 4/28/2023
0.9.87 213 4/27/2023
0.9.85 224 4/25/2023
0.9.81 202 4/24/2023
0.9.78 211 4/24/2023
0.9.76 195 4/24/2023
0.9.74 182 4/24/2023
0.9.73 199 4/24/2023
0.9.71 198 4/24/2023
0.9.69 189 4/24/2023
0.9.67 216 4/20/2023
0.9.63 217 4/20/2023
0.9.58 275 3/7/2023
0.9.56 259 3/7/2023
0.9.55 261 3/7/2023
0.9.49 262 3/6/2023
0.9.47 310 2/19/2023
0.9.46 291 2/19/2023
0.9.43 275 2/19/2023
0.9.42 307 1/27/2023
0.9.41 586 1/4/2023
0.9.34 342 12/10/2022
0.9.27 472 9/26/2022
0.9.26 472 9/26/2022
0.9.25 450 9/26/2022
0.9.24 450 9/26/2022
0.9.23 492 9/23/2022
0.9.21 433 9/23/2022
0.9.10 459 9/18/2022
0.9.9 481 9/18/2022
0.9.8 476 9/17/2022
0.9.7 514 9/17/2022
0.9.6 486 9/17/2022
0.9.5 483 9/4/2022
0.9.4 488 9/3/2022
0.9.3 483 9/3/2022
0.9.2 484 9/3/2022
0.9.1 505 8/29/2022