Esempio n. 1
0
 public ParserState(Parser parser)
 {
     Parser = parser;
     Branches = new Dictionary<ushort, string>();
     Labels = new Dictionary<ushort, string>();
     DataFields = new Dictionary<ushort, string>();
     Scopes = new Scopes();
     Code = new List<byte>();
 }
Esempio n. 2
0
        private bool TryAssemble(string pathToAsmFile, out List<byte> machineCode, out string errorMessage)
        {
            machineCode = null;
            errorMessage = null;

            string asmFileContents = getFileContents(pathToAsmFile);
            if (asmFileContents == null)
            {
                errorMessage = c_ErrEmptyInput;
                return false;
            }

            Parser parser = new Parser();
            machineCode = parser.Parse(asmFileContents, Path.GetDirectoryName(pathToAsmFile));

            if (machineCode == null)
            {
                errorMessage = parser.ErrorMsg;
                return false;
            }

            return true;
        }
Esempio n. 3
0
        private static void Test(string asm, params ushort[] expected)
        {
            Parser p = new Parser();
            try
            {
                List<byte> assembled = p.Parse(asm, string.Empty);
                if (assembled == null)
                    throw new Exception(p.ErrorMsg);
                if (assembled.Count != expected.Length * 2)
                    throw new Exception("Failure to match: instruction {0} did not match expected bit length");
                for (int i = 0; i < expected.Length; i++)
                {
                    if (((expected[i] & 0x00ff) != assembled[i * 2]) || ((expected[i] >> 8) != assembled[i * 2 + 1]))
                    {
                        // format the expected and actual code values:
                        StringBuilder sbCode = new StringBuilder(), sbAssembled = new StringBuilder();
                        for (int j = 0; j < expected.Length; j++)
                            sbCode.Append($"{expected[j]:X4} ");
                        for (int j = 0; j < assembled.Count; j++)
                        {
                            int k = ((j % 2) == 0) ? 1 : -1;
                            sbAssembled.Append($"{assembled[j + k]:X2}");
                            if (j % 2 == 1)
                                sbAssembled.Append(" ");
                        }
                        throw new Exception(string.Format("Failure to match expected bit pattern\n" +
                            "Expected: {1}\n" + "Actual:   {2}",
                            asm, sbCode, sbAssembled));
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception($"{asm}\n{e.Message}", e);
            }

            m_TestCount++;
        }