MrKWatkins.Ast
0.9.76
Prefix Reserved
See the version list below for details.
dotnet add package MrKWatkins.Ast --version 0.9.76
NuGet\Install-Package MrKWatkins.Ast -Version 0.9.76
<PackageReference Include="MrKWatkins.Ast" Version="0.9.76" />
paket add MrKWatkins.Ast --version 0.9.76
#r "nuget: MrKWatkins.Ast, 0.9.76"
// Install MrKWatkins.Ast as a Cake Addin #addin nuget:?package=MrKWatkins.Ast&version=0.9.76 // Install MrKWatkins.Ast as a Cake Tool #tool nuget:?package=MrKWatkins.Ast&version=0.9.76
MrKWatkins.Ast
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 | Versions 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. |
-
net7.0
- JetBrains.Annotations (>= 2022.3.1)
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 | 98 | 12/22/2024 |
0.9.127 | 129 | 12/17/2024 |
0.9.126 | 69 | 12/17/2024 |
0.9.125 | 73 | 12/17/2024 |
0.9.124 | 196 | 12/15/2024 |
0.9.123 | 227 | 12/8/2024 |
0.9.122 | 215 | 11/17/2024 |
0.9.121 | 395 | 11/1/2024 |
0.9.120 | 729 | 10/24/2024 |
0.9.119 | 661 | 10/21/2024 |
0.9.118 | 1,421 | 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 | 474 | 9/26/2022 |
0.9.26 | 476 | 9/26/2022 |
0.9.25 | 452 | 9/26/2022 |
0.9.24 | 452 | 9/26/2022 |
0.9.23 | 494 | 9/23/2022 |
0.9.21 | 435 | 9/23/2022 |
0.9.10 | 460 | 9/18/2022 |
0.9.9 | 482 | 9/18/2022 |
0.9.8 | 478 | 9/17/2022 |
0.9.7 | 516 | 9/17/2022 |
0.9.6 | 488 | 9/17/2022 |
0.9.5 | 485 | 9/4/2022 |
0.9.4 | 490 | 9/3/2022 |
0.9.3 | 485 | 9/3/2022 |
0.9.2 | 486 | 9/3/2022 |
0.9.1 | 507 | 8/29/2022 |