MrKWatkins.Ast
0.9.63
Prefix Reserved
See the version list below for details.
dotnet add package MrKWatkins.Ast --version 0.9.63
NuGet\Install-Package MrKWatkins.Ast -Version 0.9.63
<PackageReference Include="MrKWatkins.Ast" Version="0.9.63" />
paket add MrKWatkins.Ast --version 0.9.63
#r "nuget: MrKWatkins.Ast, 0.9.63"
// Install MrKWatkins.Ast as a Cake Addin #addin nuget:?package=MrKWatkins.Ast&version=0.9.63 // Install MrKWatkins.Ast as a Cake Tool #tool nuget:?package=MrKWatkins.Ast&version=0.9.63
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.127 | 47 | 12/17/2024 |
0.9.126 | 37 | 12/17/2024 |
0.9.125 | 40 | 12/17/2024 |
0.9.124 | 125 | 12/15/2024 |
0.9.123 | 205 | 12/8/2024 |
0.9.122 | 212 | 11/17/2024 |
0.9.121 | 389 | 11/1/2024 |
0.9.120 | 726 | 10/24/2024 |
0.9.119 | 658 | 10/21/2024 |
0.9.118 | 1,418 | 7/8/2024 |
0.9.117 | 675 | 3/27/2024 |
0.9.116 | 129 | 3/23/2024 |
0.9.115 | 119 | 3/22/2024 |
0.9.114-alpha | 100 | 3/7/2024 |
0.9.113 | 157 | 1/21/2024 |
0.9.112 | 114 | 1/18/2024 |
0.9.111 | 513 | 12/30/2023 |
0.9.110 | 257 | 11/30/2023 |
0.9.109 | 177 | 10/5/2023 |
0.9.108 | 508 | 8/22/2023 |
0.9.107 | 139 | 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 | 470 | 9/26/2022 |
0.9.25 | 448 | 9/26/2022 |
0.9.24 | 448 | 9/26/2022 |
0.9.23 | 489 | 9/23/2022 |
0.9.21 | 431 | 9/23/2022 |
0.9.10 | 457 | 9/18/2022 |
0.9.9 | 469 | 9/18/2022 |
0.9.8 | 472 | 9/17/2022 |
0.9.7 | 511 | 9/17/2022 |
0.9.6 | 484 | 9/17/2022 |
0.9.5 | 481 | 9/4/2022 |
0.9.4 | 486 | 9/3/2022 |
0.9.3 | 481 | 9/3/2022 |
0.9.2 | 482 | 9/3/2022 |
0.9.1 | 503 | 8/29/2022 |