Click here to Skip to main content
13,736,200 members
Click here to Skip to main content


248 bookmarked
Posted 22 Oct 2011
Licenced MIT

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;

namespace Diggins.Jigsaw
    public class List
        public static List Empty = new List(null, null);

        public dynamic Head { get; private set; }
        public dynamic Tail { get; private set; }

        public bool IsEmpty { get { return this == Empty; } }

        private List(dynamic head, dynamic tail) 
            Head = head;
            Tail = tail;

        public IEnumerable<dynamic> Elements
                for (var iter = this; !iter.IsEmpty; iter = iter.Tail)
                    yield return iter.Head;

        public static object Parse(Node node)
            switch (node.Label)
                case "Integer": return Int32.Parse(node.Text);
                case "String":  return node.Text;
                case "Atom":    return Parse(node[0]);
                case "Term":    return Parse(node[0]);
                case "SExpr":   return Build(node["Terms"].Nodes.Select(Parse));
                case "Symbol":  return node.Text;
                default: throw new Exception("Unrecognized node type " + node.Label);

        public static List Build(IEnumerable<Object> xs)
            var result = Empty;
            foreach (var x in xs)
                result = Cons(x, result);
            return result;

        public static List Unit(Object hd)
            return Cons(hd, Empty);
        public static List Cons(Object hd, Object tl)
            if (tl == null) throw new ArgumentNullException();
            return new List(hd, tl);
        public static List Parse(string s)
            var nodes = SExpressionGrammar.SExpr.Parse(s);
            if (nodes == null) 
                throw new Exception("Invalid string format");
            var node = nodes[0];
            if (node.Label != "SExpr") 
                throw new Exception("Expected a s-expression node");
            var result = Parse(nodes[0]);
            return (List)result;

        public override string ToString()
            return String.Format("({0})", String.Join(" ", Elements));

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 | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 12 Jul 2012
Article Copyright 2011 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid