Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / Javascript

Implementing Programming Languages Using C# 4.0

Rate me:
Please Sign up or sign in to vote.
4.92/5 (115 votes)
12 Jul 2012MIT17 min read 231.5K   3.6K   249  
An introduction to creating programming language tools using C# 4.0.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;

namespace Diggins.Jigsaw
{
    public static class Utilities
    {
        public static Type GetType(string s)
        {
            s = s.Trim();
            switch (s)
            {
                case "int": return typeof(int);
                case "uint": return typeof(uint);
                case "char": return typeof(char);
                case "float": return typeof(float);
                case "double": return typeof(double);
                case "bool": return typeof(bool);
                case "byte": return typeof(byte);
                case "sbyte": return typeof(sbyte);
                case "object": return typeof(object);
                case "decimal": return typeof(decimal);
                case "string": return typeof(string);
                default:
                    var r = Type.GetType(s);
                    if (r != null) return r;
                    r = Type.GetType("System." + s);
                    if (r != null) return r;
                    throw new Exception("Could not find type " + s);
            }
        }

        public static void TimeExecution(Action a)
        {
            Console.WriteLine("Starting execution");
            var start = DateTime.Now;
            a();
            var elapsed = DateTime.Now - start;
            Console.WriteLine("Time of execution is {0} sec, {1} msec", elapsed.TotalSeconds, elapsed.Milliseconds);
        }

        public static void RunMain(Assembly asm, params string[] args)
        {
            MethodInfo main = null;
            foreach (var t in asm.GetTypes())
                foreach (var mi in t.GetMethods(BindingFlags.Public | BindingFlags.Static))
                    if (mi.Name == "Main")
                       main = mi;
            main.Invoke(null, new object[] { args });
        }

        public static string Unquote(string s)
        {
            return s.Substring(1, s.Length - 2);
        }

        public static void Test()
        {
            Func<object, object> f = x => x;
            Delegate d = f;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer Ara 3D
Canada Canada
I am the designer of the Plato programming language and I am the founder of Ara 3D. I can be reached via email at cdiggins@gmail.com

Comments and Discussions