ls2csc.exe (alpha) is now available in the ls2module SDK!

The official C# Compiler for LavishScript 2.0

ls2csc.exe (alpha) is now available in the ls2module SDK!

Postby WhiteSlax » Thu Oct 04, 2012 4:21 pm

ls2csc.exe is currently distributed with the ls2module SDK, and is found in the installed "bin" folder.

In order to use it, you will need to install Microsoft Visual Studio 2013, and the latest version of Roslyn.

This compiler is still very young and does not fully implement all of the features of C#. In fact, if you need most of the features of C#, it is probably better that you don't even bother with ls2csc yet. However, it is coming along pretty nicely and I am happy enough with the progress to let some people try to play with it and start providing feedback.

So first of all a quick note on some major limitations in this alpha...
  • The compiler produces LS2 IL, and ls2il.exe does not currently produce bytecode (a near-future version will, but the bytecode is still fairly volatile)

Sooo... what DOES work...
  • C# 5.0 Syntax. ls2csc uses Roslyn to produce a SyntaxTree, semantic models, and diagnostic information (errors etc) from the C# code. Of course, supporting the syntax is not the same as implementing the features...
  • classes, enums
  • interfaces, is and as keywords
  • if, if..else
  • switch
  • for, foreach
  • using
  • while, do..while
  • break/continue
  • try..catch, try..catch..finally, try..finally
  • System.Console.WriteLine, String.Length, Array.Length, LavishScriptAPI, some LavishScript2, etc (you can browse ls2csc source on github for the current listing of Libraries)
  • Declaring and initializing various types of variables
  • calls to static and non-static methods
  • static and non-static property get/set
  • new keyword
  • ref/out keywords
  • indexers
  • Delegates
  • Events
  • Generic Methods (but not Generic CLASSES yet)
  • goto when used for switch case/default
  • Various math operators

Things that will definitely not work at this time but could likely be implemented in the Compiler without IL changes:
  • LINQ
  • lock keyword
  • goto for any use other than switch case/default
  • operator overloads

Things that will definitely not work at this time and require (or probably require) IL changes:
  • Structs
  • Generic classes

Once a few people are successfully testing their first C# code compiled to LS2 IL, one of the first things I'll be doing is setting up references, so that you can expect to interact with Inner Space with a ls2csc-built program...

And now for an example... the following C# Hello World! code compiles with ls2csc and will work fine with either ls2il.exe, or Inner Space build 5679 or later.
Code: Select all
// Hello1.cs
public class Hello1
{
   public static void Main()
   {
      System.Console.WriteLine("Hello, World!");
   }
}


To build this with ls2csc, save it and provide ls2csc the filename as the first parameter, like...
Code: Select all
ls2csc -i Hello1.cs


To have it write its output to file, use command-line features like this:
Code: Select all
ls2csc -i Hello1.cs -o Hello1.ls2il
WhiteSlax
Site Admin
 
Posts: 97
Joined: Mon May 07, 2012 7:24 pm

Re: ls2csc.exe (alpha) is now available in the ls2module SDK

Postby WhiteSlax » Sat Oct 06, 2012 5:44 pm

Changes in 0.6.20121006.1 include..
  • CALLSTATICMETHOD is now implemented (static methods with more than 2 parameters, or a return value and more than 1 parameter)
  • switches on strings should work
  • exceptions thrown by the compiler are now handled at the top level, and output goes to stderr
WhiteSlax
Site Admin
 
Posts: 97
Joined: Mon May 07, 2012 7:24 pm

Re: ls2csc.exe (alpha) is now available in the ls2module SDK

Postby WhiteSlax » Sat Oct 13, 2012 5:02 pm

Changes in 0.6.20121013.1 include..
  • fallback case for switches (e.g. branches of individual if statements per case) is now implemented. note that the LS2IL SWITCH instruction can handle Arrays as jump tables just like is currently done with Tables for the optimized String switch, so the next step is to optimize numeric cases that can be broken down into one or more Arrays...
  • "new" keyword for object construction implemented, though the environment does not currently have all of the allocators for the internal object types
  • Method overloads are now handled by adding parameter info to the fully-qualified name of the Method. This might break a couple of the previously-working internal methods, feel free to report those ;)
  • You can now set values in an array... (ARRAYSET implemented)
  • Managed type fields are implemented and should basically work

Today's random pile of C# code that should compile and work correctly. We have a new keyword to initialize an array, and a new keyword to initialize a custom object. hooray!
Code: Select all
namespace ls2csc_test
{
    public class TestClass
    {
        public TestClass(int val)
        {
            System.Console.WriteLine("Construction!");
            SomeValue = val;
            _someArray = new int[42];
        }
        public TestClass(string val)
        {
            SomeValue = 12;
            // _someArray is not initialized in this one, hope we get the right constructor or we might throw an exception!
            System.Console.WriteLine("String Construction!");
        }
        int[] _someArray;
        public int[] SomeArray
        {
            get { return _someArray; }
        }

        int _someValue;
        public int SomeValue
        {
            get { return _someValue; }
            set { _someValue = value; }
        }
    }

    public class Hello
    {
        public static void Main()
        {
            TestClass test = new TestClass(13);
            test.SomeArray[1] = 2012;
            int val = test.SomeValue;
            switch (val)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                    System.Console.WriteLine("wrong value! " + val);
                    break;
                case 12:
                    System.Console.WriteLine("really wrong value! " + val);
                    break;
                case 13:
                    System.Console.WriteLine("right value! " + val);
                    break;
                default:
                    System.Console.WriteLine("default wrong value! " + val);
                    break;
            }

            System.Console.WriteLine("After switch: " + test.SomeArray[1]);

            int total = add(1,3,5);
            System.Console.WriteLine(total.ToString());
        }

        public static int add(int i1, int i2, int i3)
        {
            System.Console.WriteLine("i1: " + i1 + " i2: " + i2 + " i3: " + i3);
            return i1 + i2 + i3;
        }
    }
}
WhiteSlax
Site Admin
 
Posts: 97
Joined: Mon May 07, 2012 7:24 pm

Re: ls2csc.exe (alpha) is now available in the ls2module SDK

Postby WhiteSlax » Wed Oct 17, 2012 3:08 pm

Changes in 0.6.20121017.1 include..
  • Fixed a bug where binary expressions were not properly resolving into an existing register (e.g. local variable)
  • Fixed a bug where the non-void return Reference was not getting passed to a method with more than a few parameters
  • Exposed additional LavishScriptAPI functionality (only available through Inner Space, not through ls2il.exe... but it will compile as if Referenced)

Here is the current LavishScriptAPI, as currently built into ls2csc:
This is roughly the same as in the .NET API Lavish.InnerSpace.dll, minus some stuff the compiler doesn't support yet (generics). It's also likely to be broken into a separate file for the compiler soon... this code might be useful if you want syntax highlighting..
Code: Select all
namespace LavishScriptAPI
{
    class LavishScriptObject
    {
        public void Invalidate();
        public void Dispose();
        public bool IsValid { get; }

        public LavishScriptObject GetMember(string Member);
        public LavishScriptObject GetMember(string Member, params string[] indices);
        public LavishScriptObject GetIndex(params string[] indices);
        public bool ExecuteMethod(string Method);
        public bool ExecuteMethod(string Method, params string[] indices);
        public LavishScriptObject GetLSType();
        public string LSType { get; }

        //public override string ToString(); // this is implied...
    }

    class LavishScript
    {
        static public void ExecuteCommand(string Command);
        static public string DataParse(string DataSequence);

        static public class Objects
        {
            static public LavishScriptObject NewObject([In] string Type);
            static public LavishScriptObject NewObject([In] string Type, [In] params string[] indices);
            static public LavishScriptObject GetObject(string Name);
            static public LavishScriptObject GetObject(string Name, params string[] indices);
        }
    }
}


And a simple example of usage, roughly equivalent to "echo InnerSpace.Build=${InnerSpace.Build}"
Code: Select all
namespace ls2csc.Samples.LavishScript1
{
    public class GetObjectMember
    {
        public static void Main()
        {
            LavishScriptAPI.LavishScriptObject obj = LavishScriptAPI.LavishScript.Objects.GetObject("InnerSpace");
            LavishScriptAPI.LavishScriptObject build = obj.GetMember("Build");
            System.Console.WriteLine("InnerSpace.Build="+build.ToString());
        }
    }
}
WhiteSlax
Site Admin
 
Posts: 97
Joined: Mon May 07, 2012 7:24 pm

Re: ls2csc.exe (alpha) is now available in the ls2module SDK

Postby WhiteSlax » Wed Oct 17, 2012 5:04 pm

Changes in 0.6.20121017.2 include..
  • the LS2-managed object Allocator now uses default<T> of null instead of an instantiated object...
WhiteSlax
Site Admin
 
Posts: 97
Joined: Mon May 07, 2012 7:24 pm

Re: ls2csc.exe (alpha) is now available in the ls2module SDK

Postby WhiteSlax » Fri May 30, 2014 9:15 am

Changes in 0.10.20140529.1 include...
- new bootstrap compiler mode switch -b, used to generate bootstrap.ls2il (used to bootstrap internal LS2 objects via LS2IL)
- Fixed bugs with inherited object fields
- methods now generate input declarations for the metatable
- static fields should be fully working
- implemented static construction in the FieldInitializerRewriter
- added Object.GetType() as an LS2Intrinsic. Note that this currently returns a LavishScript2.Type and not a System.Type
- sealed many of the library classes, since these objects are implemented in C++ and not LS2-managed
- System.Exception is now bootstrapped instead of a C++ object
- FlattenToInstructions() can now shorten instructions that are too long by converting the long operand(s) into function value(s). This is needed because the bytecode uses an 8-bit size for the instruction + operands, and would throw an exception when using a string that pushes the instruction length past 255 bytes.
- implemented array initialization syntax e.g. string[] xyz = {"x", "y", "z"}
- implemented object initialiation syntax e.g. new Exception() { Message = "xyz" }
- implemented indexer properties, as methods via IndexerRewriter
- implemented ref/out keywords
- fixed generated fields from static auto-properties
- fixed postfix unary expression (++ / --) on properties
- implemented postfix unary expression on parameters, including ref/out
- Fixed a null reference exception in ControlFlowGraph.StripUnusedBlock
- implemented goto keyword for switches (goto case and goto default)
- switched a lot of NotImplementedException to NotSupportedException and other exception types
- GetType(), GetString() and GetMetaTable() intrinsic methods now work correctly on implicit "this"
- method parameters no longer fail if all parameters are literal values, but name:value syntax is used
- events and delegates implemented
- bootstrapped System.Delegate, System.MulticastDelegate, System.EventHandler, System.EventHandler<TEventArgs>
- defined System.IntPtr
- generic Methods should now work. (generic Types still not yet supported.)
- delegate declarations now generate Chunk metadata
- added NEWDELEGATE instruction
- added event field support
WhiteSlax
Site Admin
 
Posts: 97
Joined: Mon May 07, 2012 7:24 pm


Return to C#: ls2csc

Who is online

Users browsing this forum: No registered users and 1 guest

cron