Boo fiddle, based on OnGUI

Hi!
This is a demonstration of how you can create your own fiddle environment for boo gui code.

import UnityEngine
import System.Reflection
import Boo.Lang.Compiler
import Boo.Lang.Compiler.IO
import Boo.Lang.Compiler.Pipelines

class BooFiddle (MonoBehaviour):

  private coreCode = "// Demo code\nBooFiddle.Demo()"
  private errors = ""

  private entryMInfo as MethodInfo

  static def Demo():
    GUILayout.Button("Demo!")

  def OnGUI():
    leftHalf = Rect(0, 0, Screen.width / 2, Screen.height * 0.5f)
    coreCode = GUI.TextArea(leftHalf, coreCode)

    buttonRect = Rect(0, Screen.height * 0.5f, Screen.width / 2, 16)
    if GUI.Button(buttonRect, "Compile"):

      compiler = BooCompiler()
      compiler.Parameters.References.Add(Assembly.GetExecutingAssembly());
      compiler.Parameters.Input.Add(StringInput("<script>", coreCode))
      compiler.Parameters.Pipeline = CompileToMemory()
      context = compiler.Run()

      if context.GeneratedAssembly is null:
        errors = join(e for e in context.Errors, "\n")
      else:
        entryMInfo = context.GeneratedAssembly.EntryPoint;
        errors = "Success!\nRunning..."

    if errors.Length:
      errorRect = Rect(0, Screen.height * 0.5f + 16, Screen.width / 2, Screen.height * 0.5f - 16)
      GUI.TextArea(errorRect, errors)

    rightHalf = Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height)
    GUILayout.BeginArea(rightHalf)

    entryMInfo.Invoke(null, (array(string, 0),)) if entryMInfo

    GUILayout.EndArea()
2 Likes

Here’s a slightly less intimidating screenshot :slight_smile:

I updated the code so that it demonstrates how to call into your own functions from the code. Also it compiles as a full script instead of some portion of another function, which it used to.

Boo will put the available code into a main-method, which is located by the “EntryPoint” property.