예제 #1
0
        private string ParseStack(FileStream inS)
        {
            switch (WhitespaceAssembler.GetNextChar(inS))
            {
            case 0:
                return("Push " + ParseNum(inS).ToString());

            case 1:
                throw new Exception("Parse Stack Manipultion encountered an invalid operand! [Tab]");

            case 2:
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    return("Copy");

                case 1:
                    return("Swap");

                case 2:
                    return("Pop");
                }
                throw new Exception("Parse Stack Manipulation followed by [LF] encountered an unknown operand!");
            }
            throw new Exception("Parse Stack Manipulation encountered an unknown operand!");
        }
예제 #2
0
        private string ParseFlow(FileStream inS)
        {
            switch (WhitespaceAssembler.GetNextChar(inS))
            {
            case 0:
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    return("Label " + ParseNum(inS).ToString());

                case 1:
                    return("Call " + ParseNum(inS).ToString());

                case 2:
                    return("Jump " + ParseNum(inS).ToString());
                }
                throw new Exception("Parse Flow followed by [Space] encountered an unknown operand!");

            case 1:
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    return("Jz " + ParseNum(inS).ToString());

                case 1:
                    return("Jn " + ParseNum(inS).ToString());

                case 2:
                    return("Ret");
                }
                throw new Exception("Parse Flow followed by [Tab] encountered an unknown operand!");

            case 2:
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    throw new Exception("Parse Flow followed by [LF] encountered an invalid operand! [Space]");

                case 1:
                    throw new Exception("Parse Flow followed by [LF] encountered an invalid operand! [Tab]");

                case 2:
                    done = true;
                    return("End");
                }
                throw new Exception("Parse Flow followed by [LF] encountered an unknown operand!");
            }
            throw new Exception("Parse Flow encountered an unknown operand!");
        }
예제 #3
0
        private string ParseHeap(FileStream inS)
        {
            switch (WhitespaceAssembler.GetNextChar(inS))
            {
            case 0:
                return("St");

            case 1:
                return("Ld");

            case 2:
                throw new Exception("Parse Heap encountered an invalid operand! [LF]");
            }
            throw new Exception("Parse Heap encountered an unknown operand!");
        }
예제 #4
0
        public void ParseInstruction(int n, FileStream inS)
        {
            if (done)
            {
                throw new Exception("Instructions continue after end of program reached!");
            }
            switch (n)
            {
            // stack manip
            case 0:
                insts.Add(ParseStack(inS));
                break;

            // arithmetic, heap, or I/O
            case 1:
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    insts.Add(ParseArithmetic(inS));
                    break;

                case 1:
                    insts.Add(ParseHeap(inS));
                    break;

                case 2:
                    insts.Add(ParseIO(inS));
                    break;
                }
                break;

            // flow control
            case 2:
                insts.Add(ParseFlow(inS));
                break;
            }
            if (WhitespaceAssembler.debug)
            {
                Console.WriteLine("Finished Instruction: " + insts[insts.Count - 1]);
            }
        }
예제 #5
0
        private int ParseNum(FileStream inS)
        {
            bool done      = false;
            int  signCheck = WhitespaceAssembler.GetNextChar(inS);

            if (signCheck == 2)
            {
                throw new Exception("Parse Number encountered an invalid sign! [LF]");
            }
            bool positive = signCheck == 0;
            int  res      = 0;
            int  bitCount = 0;

            while (!done)
            {
                bitCount++;
                if (bitCount > 31)
                {
                    throw new Exception("Parse Number encountered a number larger than 31 bits!");
                }
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    res <<= 1;
                    break;

                case 1:
                    res <<= 1;
                    res  += 1;
                    break;

                case 2:
                    done = true;
                    break;
                }
            }
            return(positive ? res : -res);
        }
예제 #6
0
        private string ParseIO(FileStream inS)
        {
            switch (WhitespaceAssembler.GetNextChar(inS))
            {
            case 0:
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    return("OutC");

                case 1:
                    return("OutN");

                case 2:
                    throw new Exception("Parse IO followed by [Space] encountered an invalid operand! [LF]");
                }
                throw new Exception("Parse IO followed by [Space] encountered an unknown operand!");

            case 1:
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    return("ReadC");

                case 1:
                    return("ReadN");

                case 2:
                    throw new Exception("Parse IO followed by [Tab] encountered an invalid operand! [LF]");
                }
                throw new Exception("Parse IO followed by [Tab] encountered an unknown operand!");

            case 2:
                throw new Exception("Parse IO encountered an invalid operand! [LF]");
            }
            throw new Exception("Parse IO encountered an unknown operand!");
        }
예제 #7
0
        private string ParseArithmetic(FileStream inS)
        {
            switch (WhitespaceAssembler.GetNextChar(inS))
            {
            case 0:
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    return("Add");

                case 1:
                    return("Sub");

                case 2:
                    return("Mult");
                }
                throw new Exception("Parse Arithmetic followed by [Space] encountered an unknown operand!");

            case 1:
                switch (WhitespaceAssembler.GetNextChar(inS))
                {
                case 0:
                    return("Div");

                case 1:
                    return("Mod");

                case 2:
                    throw new Exception("Parse Arithmetic followed by [Tab] encountered an invalid operand! [LF]");
                }
                throw new Exception("Parse Arithmetic followed by [Tab] encountered an unknown operand!");

            case 2:
                throw new Exception("Parse Arithmetic encountered an invalid operand! [LF]");
            }
            throw new Exception("Parse Arithmetic encountered an unknown operand!");
        }