Skip to content

roblox-csharp/roblox-cs

Repository files navigation

roblox-cs

A C# to Luau transpiler for Roblox


Discord server CI Status CD Status Coverage Status

Introduction

roblox-cs is a C# to Luau transpiler, which means we effectively translate C# code into Luau. This is done by taking the C# AST and converting it into a Luau AST (that is functionally the same) and then finally rendering the Luau AST into Luau source code.

Examples

Hello, world!

Note: In the future this example will automatically call Main().

void Main() => print("Hello, roblox-cs!");
-- Compiled with roblox-cs v2.0.0

local function Main(): ()
  print("Hello, roblox-cs!")
end
return nil

Classes

Note: In the future this example will import the CS library. It will also probably abandon typeof().

var myClass = new MyClass(69);
myClass.DoSomething();
print(myClass.MyProperty); // 69
print(myClass.MyField); // 0

class MyClass(int value)
{
  public readonly int MyField;
  public int MyProperty { get; } = value;

  public void DoSomething() =>
    print("doing something!");
}
-- Compiled with roblox-cs v2.0.0

local MyClass
do
  MyClass = setmetatable({}, {
    __tostring = function(): string
      return "MyClass"
    end
  })
  MyClass.__index = MyClass
  MyClass.__className = "MyClass"
  function MyClass.new(value: number): MyClass
    local self = (setmetatable({}, MyClass) :: any) :: MyClass
    return self:MyClass(value) or self
  end
  function MyClass:DoSomething(): ()
    return print("doing something!")
  end
  function MyClass:MyClass(value: number): MyClass?
    return nil
  end
end
CS.defineGlobal("MyClass", MyClass)
type MyClass = typeof(MyClass)

local myClass = MyClass.new(69)
myClass:DoSomething()
print(myClass.MyProperty)
print(myClass.MyField)
return nil

Type Reflection

Note: Object.GetType() is not supported.

var intType = typeof(int);
print(intType.Name); // Int32
print(intType.Namespace); // System
print(intType.BaseType.Name); // ValueType
local intType = { --[[ insert type info here ]] }; 
print(intType.Name);
print(intType.Namespace);
print(intType.BaseType.Name);

Join the Community!

Discord server