Click here to Skip to main content
13,352,428 members (58,187 online)
Click here to Skip to main content


248 bookmarked
Posted 22 Oct 2011

Implementing Programming Languages Using C# 4.0

, 12 Jul 2012
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.Linq.Expressions;

namespace Diggins.Jigsaw
    public class ExpressionCompiler
        private readonly string ReturnTargetName = "$return";

        VarBindings env = new VarBindings();

        public static Expression Noop
            get { return Expression.Call(null, typeof(Primitives).GetMethod("noop")); }

        public Expression AddBinding(ParameterExpression param)
            return AddBinding(param.Name, param);

        public Expression AddBinding(string name, Expression exp)
            env = env.AddBinding(name, exp);
            return exp;

        public Expression Lookup(String s)
            return (Expression)env[s];

        public Expression ScopedExpr(Func<Expression> x)
            VarBindings old = env;
            var result = x();
            env = old;
            return result;

        public LabelTarget GetReturnTarget()
            var label = (LabelExpression)env.Find(ReturnTargetName);
            return label.Target;

        public LabelExpression CreateReturnTarget()
            var target = Expression.Label(typeof(Object), ReturnTargetName);
            var label = Expression.Label(target, Expression.Constant(null));
            env = env.AddBinding(ReturnTargetName, label);
            return label;

        public static Expression CompileIndexExpr(Expression self, Expression index)
            return Expression.Call(null, Primitives.GetMethod("index"), new Expression[] { self, index });

        public static Expression CompileCallExpr(Expression func, IEnumerable<Expression> args)
            return Expression.Call(null, Primitives.GetMethod("dynamic_invoke"), args);

        public Expression CompileForLoop(Expression init, Expression cond, Expression step, Expression body)
            throw new NotImplementedException();

        public Expression CreateBinaryExpression(string op, Expression left, Expression right)
            switch (op)
                case "+":
                    return Expression.Add(left, right);
                case "-":
                    return Expression.Subtract(left, right);
                case "*":
                    return Expression.Multiply(left, right);
                case "/":
                    return Expression.Divide(left, right);
                case "%":
                    return Expression.Modulo(left, right);
                case ">>":
                    return Expression.RightShift(left, right);
                case "<<":
                    return Expression.LeftShift(left, right);
                case ">":
                    return Expression.GreaterThan(left, right);
                case ">=":
                    return Expression.GreaterThanOrEqual(left, right);
                case "<":
                    return Expression.LessThan(left, right);
                case "<=":
                    return Expression.LessThanOrEqual(left, right);
                case "==":
                    return Expression.Equal(left, right);
                case "!=":
                    return Expression.NotEqual(left, right);
                case "??":
                    return Expression.Coalesce(left, right);
                case "=":
                    return Expression.Assign(left, right);
                case "+=":
                    return Expression.AddAssign(left, right);
                case "-=":
                    return Expression.SubtractAssign(left, right);
                case "*=":
                    return Expression.MultiplyAssign(left, right);
                case "/=":
                    return Expression.DivideAssign(left, right);
                case "%=":
                    return Expression.ModuloAssign(left, right);
                case ">>=":
                    return Expression.RightShiftAssign(left, right);
                case "<<=": 
                    return Expression.LeftShiftAssign(left, right);
                    return null;

        public LambdaExpression CreateStatementLambda(IEnumerable<ParameterExpression> paramlist, Func<Expression> bodyfunc)
            return (LambdaExpression)ScopedExpr(() =>
                var ps = paramlist.ToArray();
                foreach (var p in ps) AddBinding(p);
                var returnTarget = CreateReturnTarget();
                var body = Expression.Block(typeof(Object), bodyfunc(), returnTarget);
                return Expression.Lambda(body, ps);

        public LambdaExpression CreateExpressionLambda(IEnumerable<ParameterExpression> paramlist, Func<Expression> bodyfunc)
            return (LambdaExpression)ScopedExpr(() =>
                var ps = paramlist.ToArray();
                foreach (var p in ps) AddBinding(p);
                return Expression.Lambda(bodyfunc(), ps);

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.


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


About the Author

Christopher Diggins
Software Developer Autodesk
Canada Canada
This article was written by Christopher Diggins, a computer science nerd who currently works at Autodesk as an SDK specialist.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 12 Jul 2012
Article Copyright 2011 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid