Exemple #1
0
        // IsConstOrVar - Checkes whether a specified word is a constant or a variable
        // Input:  Atom - the name of the atom
        // Output: Returns true if the specified word is a constant or a variable
        private bool IsConstOrVar(string Atom)
        {
            bool bConstOrVar = false;

            // Search constant name space
            for (int i = 0; i < GlobalConstants.Count; i++)
            {
                ForthConstant fc = (ForthConstant)GlobalConstants[i];
                if (fc.Name.ToUpper() == Atom.ToUpper())
                {
                    bConstOrVar = true;
                }
            }
            // Search variable name space
            for (int i = 0; i < GlobalVariables.Count; i++)
            {
                ForthVariable fv = (ForthVariable)GlobalVariables[i];
                if (fv.Name.ToUpper() == Atom.ToUpper())
                {
                    bConstOrVar = true;
                }
            }

            return(bConstOrVar);
        }
Exemple #2
0
        private void DisplayMapInformation()
        {
            int i;

            Console.WriteLine("--- Map Information -------------------------------------\n\r");
            Console.WriteLine("* Global constants:");
            // Display global constants
            for (i = 0; i < GlobalConstants.Count; i++)
            {
                ForthConstant fc = (ForthConstant)GlobalConstants[i];
                if (fc.Value.GetType() != typeof(string))
                {
                    Console.WriteLine("{0} = {1}", fc.Name, fc.Value);
                }
                else
                {
                    Console.WriteLine("{0} = \"{1}\"", fc.Name, fc.Value);
                }
            }
            // Display global variables
            Console.WriteLine("\n* Global variables:");
            for (i = 0; i < GlobalVariables.Count; i++)
            {
                ForthVariable fv = (ForthVariable)GlobalVariables[i];
                Console.WriteLine("{0} = (Addr:{1}, Size:{2})", fv.Name, fv.Address, fv.Size);
            }
            // Display local variables
            Console.WriteLine("\n* Local variables:");
            for (i = 0; i < LocalVariables.Count; i++)
            {
                ForthLocalVariable flv = (ForthLocalVariable)LocalVariables[i];
                Console.WriteLine("{0} = (Addr:{1}, Word:{2})", flv.Name, flv.Address, flv.WordName);
            }
            // Display external words
            Console.WriteLine("\n* External words:");
            for (i = 0; i < ExternalWords.Count; i++)
            {
                ExternalWord ew = (ExternalWord)ExternalWords[i];
                Console.WriteLine("{0} = (Library:{1}, Class:{2}, Method:{3})", ew.Name, ew.Library, ew.Class, ew.Method);
            }

            Console.WriteLine();
        }
Exemple #3
0
        // DoAnalysis - Builds up the word, constant and variable lists
        // Input:  None
        // Output: None (Globally changes GlobalVariables, GlobalConstants, LocalVariables, Words)
        private void DoAnalysis()
        {
            ForthAtom Atom, NextAtom;
            ForthWord WordDef = null;           // Holds the definition of a word
            string    temp;
            int       noIFs    = 0;             // Number of IF statements
            int       noDOs    = 0;             // Number of DO statements
            int       noBEGINs = 0;             // Number of BEGIN statements
            int       noWHILEs = 0;             // Number of WHILE statements
            int       noCASEs  = 0;             // Number of CASE statements
            int       noOFs    = 0;             // Number of OF statements

            // Define enumerator for the source atoms list
            IEnumerator saEnum = SourceAtoms.GetEnumerator();

            while (saEnum.MoveNext())
            {
                Atom = (ForthAtom)saEnum.Current;
                // If the atom does not start with " or ." make the atom upper case
                if (!Atom.Name.StartsWith(".\"") && !Atom.Name.StartsWith("\""))
                {
                    Atom.Name = Atom.Name.ToUpper();
                }
                // Process atoms
                switch (Atom.Name)
                {
                case "EXTERN":                          // Store information after EXTERN for calling methods at runtime
                    if (InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareOutsideWords, Atom);
                    }
                    saEnum.MoveNext();                                  // Advance to next atom
                    NextAtom = ((ForthAtom)saEnum.Current);
                    string ForthWord = NextAtom.Name;                   // Forth word name
                    if (IsReserved(ForthWord))
                    {
                        RaiseException(ExceptionType._EReservedWord, NextAtom);
                    }
                    if (!IsIdentifier(ForthWord))
                    {
                        RaiseException(ExceptionType._EInvalidIdentifier, NextAtom);
                    }
                    saEnum.MoveNext();                                  // Advance to next atom
                    NextAtom = ((ForthAtom)saEnum.Current);
                    string FileName = NextAtom.Name;                    // Library name
                    saEnum.MoveNext();                                  // Advance to next atom
                    NextAtom = ((ForthAtom)saEnum.Current);
                    string Callee     = NextAtom.Name;
                    int    DotPos     = Callee.LastIndexOf('.');
                    string ClassName  = Callee.Substring(0, DotPos);
                    string MethodName = Callee.Substring(DotPos + 1);
                    ExternalWords.Add(new ExternalWord(ForthWord, FileName, ClassName, MethodName));
                    break;

                case "LIBRARY":                         // Store next atom as the name of the library
                    if (InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareOutsideWords, Atom);
                    }
                    saEnum.MoveNext();                                  // Advance to next atom
                    NextAtom = ((ForthAtom)saEnum.Current);
                    string libname = NextAtom.Name;
                    if (IsReserved(libname))
                    {
                        RaiseException(ExceptionType._EReservedWord, NextAtom);
                    }
                    if (!IsIdentifier(libname))
                    {
                        RaiseException(ExceptionType._EInvalidIdentifier, NextAtom);
                    }
                    LibraryName = libname;
                    break;

                case "CONSTANT":                         // Associate the value on the global stack to the next atom
                    if (InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareOutsideWords, Atom);
                    }
                    saEnum.MoveNext();                                  // Advance to next atom
                    NextAtom = ((ForthAtom)saEnum.Current);
                    if (IsReserved(NextAtom.Name))
                    {
                        RaiseException(ExceptionType._EReservedWord, NextAtom);
                    }
                    if (!IsIdentifier(NextAtom.Name))
                    {
                        RaiseException(ExceptionType._EInvalidIdentifier, NextAtom);
                    }
                    if (IsConstOrVar(NextAtom.Name))
                    {
                        RaiseException(ExceptionType._EDuplicateConst, NextAtom);
                    }
                    if (SourceStack.Count == 0)
                    {
                        RaiseException(ExceptionType._EUnableToDefineConst, NextAtom);
                    }
                    GlobalConstants.Add(new ForthConstant(NextAtom.Name, SourceStack.Pop()));                             // Add constant definiton to the list
                    break;

                case "VARIABLE":                         // The next atom is the variable name with the initial size of 1
                    saEnum.MoveNext();                   // Advance to next atom
                    NextAtom = ((ForthAtom)saEnum.Current);
                    if (IsReserved(NextAtom.Name))
                    {
                        RaiseException(ExceptionType._EReservedWord, NextAtom);
                    }
                    if (!IsIdentifier(NextAtom.Name))
                    {
                        RaiseException(ExceptionType._EInvalidIdentifier, NextAtom);
                    }
                    if (IsConstOrVar(NextAtom.Name))
                    {
                        RaiseException(ExceptionType._EDuplicateVar, NextAtom);
                    }
                    if (!InWordDefinition)
                    {
                        GlobalVariables.Add(new ForthVariable(NextAtom.Name, 1));
                    }
                    else
                    {
                        LocalVariables.Add(new ForthLocalVariable(NextAtom.Name, WordDef.Name));
                    }
                    break;

                case "ALLOT":                         // The last number on the stack is the additional size for the last defined variable
                    if (InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareOutsideWords, Atom);
                    }
                    if (SourceStack.Count == 0)
                    {
                        RaiseException(ExceptionType._EUnableToAllocVar, Atom);
                    }
                    if (SourceStack.Peek().GetType() != typeof(int))
                    {
                        RaiseException(ExceptionType._EWrongAllotConstType, Atom);
                    }
                    int AllotSize = (int)SourceStack.Pop();                             // Get the ALLOT size
                    // Change the size of the last defined variable
                    int           LastVarPos = GlobalVariables.Count - 1;               // Get the position of the last variable we defined
                    ForthVariable fv         = (ForthVariable)(GlobalVariables[LastVarPos]);
                    fv.Size += AllotSize;
                    GlobalVariables[LastVarPos] = fv;                                   // Update changes in list
                    break;

                case ":":                         // Begin word definition
                    if (InWordDefinition)
                    {
                        RaiseException(ExceptionType._ENestedWordsNotAllowed, Atom);
                    }
                    saEnum.MoveNext();                                  // Advance to next atom
                    NextAtom      = ((ForthAtom)saEnum.Current);
                    NextAtom.Name = NextAtom.Name.ToUpper();
                    if (IsReserved(NextAtom.Name))
                    {
                        RaiseException(ExceptionType._EReservedWord, NextAtom);
                    }
                    if (!IsIdentifier(NextAtom.Name))
                    {
                        RaiseException(ExceptionType._EInvalidIdentifier, NextAtom);
                    }
                    InWordDefinition = true;                                    // Signal beginning of word definition
                    WordDef          = new ForthWord(NextAtom.Name);            // Alloc here the structure, we fill it later
                    if (NextAtom.Name == "MAIN")
                    {
                        MainDefined = true;
                    }
                    break;

                case ";":                         // End word definition
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    // All control structures should be terminated by now
                    if ((noIFs > 0) || (noDOs > 0) || (noBEGINs > 0) || (noWHILEs > 0) || (noCASEs > 0))
                    {
                        RaiseException(ExceptionType._EUnfinishedControlStruct, Atom);
                    }
                    Words.Add(WordDef);                                 // Add word to the list
                    InWordDefinition = false;                           // Signal end of word definition
                    break;

                case "IF":                         // IF statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    noIFs++;
                    goto default;

                case "ELSE":                         // ELSE statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noIFs == 0)
                    {
                        RaiseException(ExceptionType._EMalformedIETStruct, Atom);
                    }
                    goto default;

                case "THEN":                         // THEN statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noIFs == 0)
                    {
                        RaiseException(ExceptionType._EMalformedIETStruct, Atom);
                    }
                    noIFs--;
                    goto default;

                case "DO":                         // DO statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    noDOs++;
                    goto default;

                case "LOOP":                         // LOOP statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noDOs == 0)
                    {
                        RaiseException(ExceptionType._EMalformedDLStruct, Atom);
                    }
                    noDOs--;
                    goto default;

                case "+LOOP":                         // +LOOP statement
                    goto case "LOOP";

                case "LEAVE":                         // LEAVE statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noDOs == 0)
                    {
                        RaiseException(ExceptionType._EMalformedDLStruct, Atom);
                    }
                    goto default;

                case "BEGIN":                         // BEGIN statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    noBEGINs++;
                    goto default;

                case "WHILE":                         // WHILE statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noBEGINs == 0)
                    {
                        RaiseException(ExceptionType._EMalformedBWRStruct, Atom);
                    }
                    noWHILEs++;
                    goto default;

                case "REPEAT":                         // REPEAT statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if ((noBEGINs == 0) || (noWHILEs == 0))
                    {
                        RaiseException(ExceptionType._EMalformedBWRStruct, Atom);
                    }
                    noBEGINs--;
                    noWHILEs--;
                    goto default;

                case "AGAIN":                         // AGAIN statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noBEGINs == 0)
                    {
                        RaiseException(ExceptionType._EMalformedBAStruct, Atom);
                    }
                    noBEGINs--;
                    goto default;

                case "UNTIL":                         // UNTIL statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noBEGINs == 0)
                    {
                        RaiseException(ExceptionType._EMalformedBUStruct, Atom);
                    }
                    noBEGINs--;
                    goto default;

                case "CASE":                         // CASE statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    noCASEs++;
                    goto default;

                case "OF":                         // OF statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noCASEs == 0)
                    {
                        RaiseException(ExceptionType._EMalformedCOEStruct, Atom);
                    }
                    noOFs++;
                    goto default;

                case "ENDOF":                         // ENDOF statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noOFs == 0)
                    {
                        RaiseException(ExceptionType._EMalformedCOEStruct, Atom);
                    }
                    noOFs--;
                    goto default;

                case "ENDCASE":                         // ENDCASE statement
                    if (!InWordDefinition)
                    {
                        RaiseException(ExceptionType._EDeclareInsideWords, Atom);
                    }
                    if (noOFs > 0)
                    {
                        RaiseException(ExceptionType._EMalformedCOEStruct, Atom);
                    }
                    noCASEs--;
                    goto default;

                default:
                    temp = Atom.Name;                                   // Get the atom name
                    if (InWordDefinition)
                    {
                        WordDef.Definition.Add(temp);                                   // Add atom to word definition
                    }
                    else
                    {
                        // The atom should now be a constant previously defined, a number or a string
                        int ConstValue;                                         // Here we hold the value of the constant, in case we find it
                        if (GetConstIntValue(temp, out ConstValue))
                        {
                            SourceStack.Push(ConstValue);
                        }
                        else if (IsNumber(temp))
                        {
                            SourceStack.Push(Convert.ToInt32(temp));
                        }
                        else if (IsString(temp))
                        {
                            SourceStack.Push(temp.Trim(new char[] { '"' }));
                        }
                        else
                        {
                            RaiseException(ExceptionType._EInvalidIdentifier, Atom);
                        }
                    }
                    break;
                }
            }
            // Check whether word MAIN is defined
            if (MainDefined == false)
            {
                RaiseException(ExceptionType._EMainNotDefined);
            }
        }