Click here to Skip to main content
12,247,928 members (55,699 online)
Click here to Skip to main content

Stats

121.9K views
3K downloads
244 bookmarked
Posted

Implementing Programming Languages Using C# 4.0

, 12 Jul 2012 MIT
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
{
    /// <summary>
    /// The SECD machine was invented by David Landin and is an abstract machine useful for evaluating 
    /// functional languages. Its basic architecture closely resembles that of modern virtual machines like 
    /// the .NET VM and the Java VM. 
    /// 
    /// This implementation uses the Lisp list structure. 
    /// </summary>
    class SECDMachine
    {
        #region registers
        List Stack;
        List Env;
        List Code;
        List Dump;
        #endregion

        #region stack operations
        public void Push(object o, ref List list) 
        { 
            list = List.Cons(o, list); 
        }

        public dynamic Nth(int n, List list)
        {
            return n == 0 
                ? list.Head 
                : Nth(n - 1, list.Tail);
        }

        public void Push(object o)
        {
            Push(o, ref Stack);
        }

        public dynamic Pop(ref List list) 
        { 
            var top = list.Head; 
            list = list.Tail; 
            return top; 
        }

        public dynamic Pop()
        {
            return Pop(ref Stack);
        }
        #endregion 

        #region primitives
        public void LoadConstant(dynamic c) 
        { 
            Push(c); 
        }
        
        public void LoadVariable(List v) 
        {
            int level = v.Head;
            int pos = v.Tail;
            List frame = Nth(level, Env);
            dynamic val = Nth(pos, frame);
            Push(val);
        }

        public void Select(List a, List b) 
        { 
            Push(Code, ref Dump); 
            Code = Pop() == List.Empty ? a : b; 
        }
        
        public void Join() 
        { 
            Code = Pop(ref Dump); 
        }

        public void LoadFunction(List f) 
        { 
            Push(List.Cons(f, Env)); 
        }
        
        public void Apply() 
        { 
            Push(Stack, ref Dump); 
            Push(Env, ref Dump); 
            Push(Code, ref Dump); 
            List closure = Pop();
            List paramlist = Pop();
            Env = closure.Tail;
            Push(paramlist, ref Env);
            Code = closure.Head;
            Stack = List.Empty;
        }

        public void Return()
        {
            var r = Pop();
            Code = Pop(ref Dump);
            Env = Pop(ref Dump);
            Stack = Pop(ref Dump);
            Push(r);
        }

        public void Dummy()
        {
            Push(List.Empty, ref Env);
        }

        public void TailApply()
        {
            List closure = Pop();
            List paramlist = Pop();
            Env = List.Cons(closure.Tail, Env.Tail);
            Push(paramlist, ref Env);
            Code = closure.Head;
            Stack = List.Empty;
        }

        public void RecApply()
        {
            Push(Stack, ref Dump);
            Push(Env, ref Dump);
            Push(Code, ref Dump);
            List closure = Pop();
            List paramlist = Pop();
            Env = List.Cons(closure.Tail, Env.Tail);
            Push(paramlist, ref Env);
            Code = closure.Head;
            Stack = List.Empty;
        }
        #endregion 
    }
}

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

Share

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...

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