Skip to content

Latest commit

 

History

History
95 lines (71 loc) · 2.21 KB

File metadata and controls

95 lines (71 loc) · 2.21 KB

Getting Started

Prerequisites

Installation

dotnet add package ObjectIR.Core

Or add to your .csproj manually:

<PackageReference Include="ObjectIR.Core" Version="0.1.0" />

Your first module

The quickest way to build a module is with the fluent IRBuilder:

using ObjectIR.Core.Builder;
using ObjectIR.Core.IR;

// 1. Create a builder scoped to a module name
var builder = new IRBuilder("HelloWorld");

// 2. Define a class with a field and a method
builder
    .Class("Greeter")
        .Field("message", TypeReference.String)
            .Access(AccessModifier.Private)
            .EndField()
        .Method("Greet", TypeReference.Void)
            .Access(AccessModifier.Public)
            .Body()
                .Ldarg(0)                         // load 'this'
                .Ldfld(new FieldReference(
                    TypeReference.FromName("Greeter"),
                    "message",
                    TypeReference.String))        // load field
                .Ret()
            .EndBody()
        .EndMethod()
    .EndClass();

// 3. Get the finished module
Module module = builder.Build();

Console.WriteLine(module.Name);             // HelloWorld
Console.WriteLine(module.Types[0].Name);   // Greeter

Loading from text

If you prefer a text-based format over the fluent API, ModuleLoader can parse it for you:

using ObjectIR.Core.Serialization;

var text = """
    module HelloWorld
    class Greeter {
        field message: string
        method Greet() -> void {
            ldarg 0
            ldfld Greeter.message
            ret
        }
    }
    """;

var loader = new ModuleLoader();
Module module = loader.LoadFromText(text);

See Serialization for the full text format reference.


What to read next

Goal Page
Understand how everything fits together Architecture
Learn all the builder methods Builder API
Explore the data model IR Model
Save/load modules to disk Serialization
Merge multiple modules Composition