Click here to Skip to main content
15,885,366 members
Articles / General Programming / Algorithms

A Simulator for Knuth's MIX Computer

Rate me:
Please Sign up or sign in to vote.
4.91/5 (26 votes)
2 Feb 2011CPOL7 min read 55.4K   668   34  
Assembler and Simulator for Don Knuth's MIX Computer from The Art of Computer Programming.
using System;
using System.Collections.Generic;
using System.Linq;

namespace MIXLib.Util
{
    public static class CommandLineHelper
    {
        public static Dictionary<string, string> SplitCommandLine(string commandLine, Dictionary<string, string> aliases)
        {
            return SplitCommandLine(commandLine, aliases, false);
        }

        public static Dictionary<string, string> SplitCommandLine(string commandLine, Dictionary<string, string> aliases, bool noCase)
        {
            bool inQuotes = false;
            Dictionary<string, string> result = new Dictionary<string, string>();

            var argList = commandLine.Split(c =>
            {
                if (c == '\"')
                    inQuotes = !inQuotes;

                return !inQuotes && c == ' ';
            })
            .Select(arg =>
            {
                if (noCase)
                    return arg.Trim().TrimMatchingQuotes('\"').ToLower();
                else
                    return arg.Trim().TrimMatchingQuotes('\"');
            }).Where(arg => !string.IsNullOrEmpty(arg)).Skip(1);

            foreach (var a in argList)
            {
                var parts = a.Split(':');

                if (parts.Length == 1)
                {
                    if (aliases.ContainsKey(parts.First()))
                        result.Add(aliases[parts.First()], null);
                    else
                        result.Add(parts.First(), null);
                }
                else
                {
                    string k = parts.First();
                    string v = string.Join(":", parts.Skip(1).ToArray());

                    if (aliases.ContainsKey(k))
                        k = aliases[k];

                    result.Add(k, v);
                }
            }
            
            return result;
        }

        public static IEnumerable<string> Split(this string str,
                                            Func<char, bool> controller)
        {
            int nextPiece = 0;

            for (int c = 0; c < str.Length; c++)
            {
                if (controller(str[c]))
                {
                    yield return str.Substring(nextPiece, c - nextPiece);
                    nextPiece = c + 1;
                }
            }

            yield return str.Substring(nextPiece);
        }

        public static string TrimMatchingQuotes(this string input, char quote)
        {
            if ((input.Length >= 2) &&
                (input[0] == quote) && (input[input.Length - 1] == quote))
                return input.Substring(1, input.Length - 2);

            return input;
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Engineer
Greece Greece
I am a software developer (mainly in C# and T-SQL) for a project management company in Athens, Greece. I have been working with computers since early 1987. I am adept at Pascal, C, C++, Java (my MSc was sponsored by Sun Microsystems), Lisp, Scheme, F#, C# VB.Net, Perl and some others that are too obscure to mention. When I want a quick and dirty solution to a programming problem I use a functional language, such as Haskell, Scheme or, more recently, F#.

I also play the keyboards and compose music.

---------------------------------------------------------

MSc Distributed Systems and Networks - University of Kent at Canterbury
BEng Computer Systems Engineering - University of Kent at Canterbury

Comments and Discussions