コード例 #1
0
        ////*******************************************************************************************
        ////***  FUNCTION ParseExpresionFile
        ////*** ***************************************************************************************
        ////***  DESCRIPTION  :  Parses the Expresion File
        ////***  INPUT ARGS   :  SymbolTable symbolTable, LiteralTable literalTable, string filePath
        ////***  OUTPUT ARGS :  N/A
        ////***  IN/OUT ARGS   :  N/A
        ////***  RETURN :  N/A
        ////*******************************************************************************************
        //public static void ParseExpresionFile(SymbolTable symbolTable, LiteralTable literalTable, string filePath)
        //{
        //    try
        //    {
        //        using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan))
        //        {

        //            try
        //            {
        //                using (StreamReader streamReader = new StreamReader(fileStream))
        //                {
        //                    int LineNumber = 1;
        //                    Chronicler.WriteLine("EXPRESION\tVALUE\tRELOCATABLE\tN-Bit\tI-Bit\tX-Bit");
        //                    while (!streamReader.EndOfStream)
        //                    {
        //                        //get the line and trim whitespace
        //                        string currentLine = streamReader.ReadLine().Trim();
        //                        if (ResolveF3F4Expresion(symbolTable, literalTable, currentLine, currentLine, out Globals.Symbol? symbol))
        //                        {
        //                            if (symbol != null)
        //                            {
        //                                StringBuilder sb = new StringBuilder("");
        //                                for (int x = 0; x < (16 - currentLine.Length); x++)
        //                                {
        //                                    sb.Append(" ");
        //                                }
        //                                Chronicler.Write(currentLine + sb.ToString());
        //                                Chronicler.Write(symbol.Value.value.ToString());
        //                                Chronicler.Write("\t" + ((symbol.Value.RFlag) ? "RELOCATABLE" : "ABSOLUTE"));
        //                                Chronicler.NewLine();
        //                            }
        //                        }
        //                        else
        //                        {
        //                            Chronicler.LogError("Line: \"" + LineNumber + "\" Skipping: \"" + currentLine + "\"", "Expresion File Parsing");
        //                        }
        //                        LineNumber++;
        //                    }
        //                    Chronicler.LogInfo("DONE loading symbols!");
        //                }
        //            }
        //            catch (IOException e)
        //            {
        //                Chronicler.WriteLine(e.Message);
        //                Chronicler.LogError("failed to open File: " + filePath);
        //            }
        //        }
        //    }
        //    catch (IOException e)
        //    {
        //        Chronicler.WriteLine(e.Message);
        //        Chronicler.LogError("failed to open File: " + filePath);
        //    }
        //}

        //*******************************************************************************************
        //***  FUNCTION ResolveF3F4Expresion
        //*** ***************************************************************************************
        //***  DESCRIPTION  :  Resolves an expresion and adds a literal or gets a symbol
        //***  INPUT ARGS   :  SymbolTable symbolTable, LiteralTable literalTable,
        //***                   string expresion, string currentLine
        //***  OUTPUT ARGS :  out Globals.Symbol? symbol
        //***  IN/OUT ARGS   :  N/A
        //***  RETURN :  bool rtnVal
        //*******************************************************************************************
        public static bool ResolveF3F4Expresion(Globals.DataStructures dataStructures, string expresionString, string currentLine, out Globals.ExpresionData expresionData, string callingModule = "ExpresionHandler")
        {
            expresionData = new Globals.ExpresionData();
            bool  rtnVal     = true;
            Regex testXValue = new Regex(@"(^[^=][^,;]*(?<x>,x|X){0,1}.*$)|(^[\t ]*=.*$)");
            bool  hasXValue  = testXValue.Match(expresionString).Groups["x"].Value != "";

            expresionString = expresionString.Trim();
            if (Parser.guessOperandType(expresionString) == Parser.OperandType.NEW_STYLE_LITERAL || Parser.guessOperandType(expresionString) == Parser.OperandType.OLD_STYLE_LITERAL)
            {
                Chronicler.LogInfo("Parsing as literal: " + expresionString);
                if (Parser.guessOperandType(expresionString) == Parser.OperandType.NEW_STYLE_LITERAL)
                {
                    LiteralTable.LiteralValue tmp = new LiteralTable.LiteralValue();
                    string comment = "";
                    Parser.ParseNewStyleLiteral(expresionString, out tmp, out comment, callingModule);
                    expresionData.literal = tmp;
                    dataStructures.literalTable.add(tmp);
                }
                else if (Parser.guessOperandType(expresionString) == Parser.OperandType.OLD_STYLE_LITERAL)
                {
                    LiteralTable.LiteralValue tmp = new LiteralTable.LiteralValue();
                    string comment = "";
                    Parser.ParseOldStyleLiteral(expresionString, out tmp, out comment, callingModule);
                    expresionData.literal = tmp;
                }
            }
            else if (expresionString.Length > 0 && expresionString[0] == '@')
            {
                expresionString = expresionString.Substring(1, expresionString.Length - 1);
                hasXValue       = testXValue.Match(expresionString).Groups["x"].Value != "";
                if (hasXValue != true)
                {
                    if (ParseTerms(dataStructures, expresionString, currentLine, out expresionData) != true)
                    {
                        rtnVal = false;
                    }
                    expresionData.N = true;
                    expresionData.I = false;
                }
                else
                {
                    rtnVal = false;
                    Chronicler.LogError("Can not apply both indirect adressing \n\tand x register indexing, skipping: \"" + currentLine + "\"", "Resovling Expresion");
                }
            }
            else if (expresionString.Length > 0 && expresionString[0] == '#')
            {
                expresionString = expresionString.Substring(1, expresionString.Length - 1);
                hasXValue       = testXValue.Match(expresionString).Groups["x"].Value != "";
                if (hasXValue != true)
                {
                    if (ParseTerms(dataStructures, expresionString, currentLine, out expresionData) != true)
                    {
                        rtnVal = false;
                    }
                    expresionData.N = false;
                    expresionData.I = false;
                }
                else
                {
                    rtnVal = false;
                    Chronicler.LogError("Can not apply both immediate adressing \n\tand x register indexing, skipping:\"" + currentLine + "\"", "Resovling Expresion");
                }
            }
            else if (hasXValue == true)
            {
                expresionString = testXValue.Replace(expresionString, "$2$4$5");
                if (ParseTerms(dataStructures, expresionString, currentLine, out expresionData) != true)
                {
                    rtnVal = false;
                }
                expresionData.N = true;
                expresionData.I = true;
                expresionData.X = true;
            }
            else
            {
                rtnVal = ParseTerms(dataStructures, expresionString, currentLine, out expresionData);
            }
            return(rtnVal);
        }
コード例 #2
0
        //*******************************************************************************************
        //***  FUNCTION ParseTerms
        //*** ***************************************************************************************
        //***  DESCRIPTION  :  parses an arithmatic operation str to a symbol, or add a literal
        //***  INPUT ARGS   :  SymbolTable symbolTable, LiteralTable literalTable,
        //***                   string expresion, string currentLine
        //***  OUTPUT ARGS :  out Globals.Symbol? result
        //***  IN/OUT ARGS   :  N/A
        //***  RETURN :  bool rtnVal
        //*******************************************************************************************
        static bool ParseTerms(Globals.DataStructures dataStructures, string expresionString, string currentLine, out Globals.ExpresionData expresionData)
        {
            bool rtnVal = true;

            expresionString = expresionString.Trim();
            expresionData   = new Globals.ExpresionData();

            Regex testForCommentOnlyLine = new Regex(@"^[\t ]*(;.*)$");
            Match commentOnlyLine        = testForCommentOnlyLine.Match(expresionString);

            if (commentOnlyLine.Success)
            {
                expresionData.comment = commentOnlyLine.Groups["$1"].Value;
                return(true);
            }

            string line = expresionString.Trim();

            if (rtnVal == true && (Regex.Match(line, @"^[\t ]{0,}(-{0,1}[\t ]{0,}[A-Za-z0-9]+).*$").Success != true))
            {
                rtnVal = false;
                Chronicler.LogError("Could not parse first(" + line + ") term in: " + currentLine, "parsing terms");
            }
            if (rtnVal == true && (Regex.Match(line, @"^[\t ]{0,}(-{0,1}[\t ]{0,}[A-Za-z0-9]+)[\t ]{0,}(([+-])[\t ]{0,}(-{0,1}[\t ]{0,}[A-Za-z0-9]+)){0,1}[\t ]{0,}(;.*){0,1}$").Success != true))
            {
                rtnVal = false;
                Chronicler.LogError("Couldn't parse second term in: " + currentLine, "parsing terms");
            }
            string first = "";
            string arithmaticOperator = "";
            string second             = "";

            if (rtnVal == true)
            {
                Match fullLine = (Regex.Match(line, @"^[\t ]{0,}(?<first>-{0,1}[\t ]{0,}[A-Za-z0-9]+)[\t ]{0,}(?<testTermCount>(?<operand>[+-])[\t ]{0,}(?<second>-{0,1}[\t ]{0,}[A-Za-z0-9]+)){0,1}[\t ]{0,}(;.*){0,1}$"));
                if (fullLine.Success != true)
                {
                    rtnVal = false;
                    Chronicler.WriteLine("Error parsing literal");
                }
                first = fullLine.Groups["first"].Value.Trim();
                arithmaticOperator = fullLine.Groups["operand"].Value.Trim();
                second             = fullLine.Groups["second"].Value.Trim();

                Regex stripWhiteSpace = new Regex(@"\s+");
                first = stripWhiteSpace.Replace(first, "");
                arithmaticOperator = stripWhiteSpace.Replace(arithmaticOperator, "");
                second             = stripWhiteSpace.Replace(second, "");
                if (second != "" && arithmaticOperator == "")
                {
                    rtnVal = false;
                }
            }
            if (rtnVal == true)
            {
                rtnVal = ParseTerm(dataStructures.symbolTable, first, out expresionData.first, currentLine);
                if (second != "")
                {
                    rtnVal = ParseTerm(dataStructures.symbolTable, second, out expresionData.second, currentLine);
                    if (expresionData.first.HasValue && expresionData.second.HasValue)
                    {
                        if (arithmaticOperator == "+")
                        {
                            expresionData.rflag         = Globals.Symbol.AddRFlags(expresionData.first.Value, expresionData.second.Value);
                            expresionData.operatorValue = Globals.ExpresionData.Arithmetic.ADD;
                        }
                        else if (arithmaticOperator == "-")
                        {
                            expresionData.rflag         = Globals.Symbol.SubtractRFlags(expresionData.first.Value, expresionData.second.Value);
                            expresionData.operatorValue = Globals.ExpresionData.Arithmetic.SUBTRACT;
                        }
                        else
                        {
                            rtnVal = false;
                            Chronicler.LogError("Invalid operator value for line: " + currentLine, "term arithmatic module");
                        }
                    }
                    else
                    {
                        Chronicler.LogError("Couldn't resolve symbols in artithmetic: " + currentLine, "term arithmatic module");
                    }
                    rtnVal = rtnVal == true?ParseTerm(dataStructures.symbolTable, second, out expresionData.second, currentLine) : false;
                }


                if (expresionData.first.HasValue && !expresionData.second.HasValue && second == "")
                {
                    expresionData.rflag = expresionData.first.Value.RFlag;
                }
            }
            return(rtnVal);
        }