Ejemplo n.º 1
0
        private bool processCode()
        {
            currentObject = null;
            while (oWordEngine.NextLine())
            {
                while (oWordEngine.NextWord != null)
                {
                    #region Main Processor
                    switch (oWordEngine.Current.ToLower())
                    {
                    case "class":
                        if (validateName(oWordEngine.NextNonWhitespace))
                        {
                            currentObject = new ClassObject(oWordEngine.Current);
                        }
                        else
                        {
                            errors.Add(oWordEngine.FormatedPosition + "The name " + oWordEngine.Current + " is not a valid name for a class.\r\n" + oWordEngine.GetLineWithPositionMarker);
                            return(false);
                        }
                        break;

                    default:
                        if (currentObject == null)
                        {
                            errors.Add(oWordEngine.FormatedPosition + "Expecting class definition, found " + oWordEngine.Current + "." + oWordEngine.GetLineWithPositionMarker);
                            return(false);
                        }
                        if (!currentObject.Parse(oWordEngine))
                        {
                            if (currentObject.HasParseError || !currentObject.IsParseComplete)
                            {
                                if (currentObject.HasParseError)
                                {
                                    errors.Add(oWordEngine.FormatedPosition + currentObject.ParseError + "\r\n" + oWordEngine.GetLineWithPositionMarker);
                                }
                                else
                                {
                                    errors.Add(oWordEngine.FormatedPosition + "Compiler error! ClassObject parser returned false, but is not complete." + oWordEngine.GetLineWithPositionMarker);
                                }
                                return(false);
                            }
                            else
                            {
                                KOSObjects.Add(currentObject);
                            }
                        }
                        break;
                    }
                    #endregion
                }
            }
            if (oWordEngine.HasError)
            {
                errors.Add(oWordEngine.FormatedPosition + oWordEngine.Error + "\r\n" + oWordEngine.GetLineWithPositionMarker);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        public bool     Parse(WordEngine oWordEngine)
        {
            switch (parseState)
            {
            case eParseShate.MainLoop:
                String word = oWordEngine.CurrentNonWhitespace;
                if (oWordEngine.IsBlockStart)
                {
                    oWordEngine.RegisterBlockTest(this);
                }
                else
                {
                    parseError = "Function " + name + " expecting a block.";
                    parseState = eParseShate.Error;
                    return(false);
                }
                string StartOfFunctionMarker = oWordEngine.GetLineWithPositionMarker;
                do
                {
                    while (oWordEngine.NextWord != null && parseState == eParseShate.MainLoop)
                    {
                        if (oWordEngine.Current.Equals("."))
                        {
                            code = code.TrimEnd(' ') + oWordEngine.Current + "\r\n";
                        }
                        else
                        {
                            code += oWordEngine.Current;
                        }
                    }
                } while (parseState == eParseShate.MainLoop && oWordEngine.NextLine());
                if (oWordEngine.Current == null)
                {
                    parseState = eParseShate.Error;
                    parseError = "Function " + name + " reached the end of file before finding the clock end.\r\n" + StartOfFunctionMarker;
                }
                return(false);

            case eParseShate.Done:
                return(false);

            case eParseShate.Error:
                if (!HasParseError)
                {
                    parseError = "Compiler error! The parser " + name + " was put in Error state, but had no error.";
                }
                return(false);

            default:
                parseError = "Compiner error! Unhandled state in FunctionObject " + name + " : " + parseState.ToString();
                parseState = eParseShate.Error;
                return(false);
            }
        }
Ejemplo n.º 3
0
        public Compiler(String ScriptDir, String KOSppFileName)
        {
            StreamReader KOSPPFile = new StreamReader(ScriptDir + "\\" + KOSppFileName);

            oWordEngine = new WordEngine(KOSPPFile, allSpecChars, strimgSep);
            errors      = new List <string>();
            warinings   = new List <string>();
            Console.WriteLine("Compiling....");
            KOSObjects = new List <IKOSppObject>();
            if (!processCode())
            {
                foreach (string e in errors)
                {
                    Console.WriteLine(e);
                }
            }
            else
            {
                foreach (IKOSppObject KOSObj in KOSObjects)
                {
                    string filename = ScriptDir + "\\" + KOSObj.Name + ".ks";
                    Console.WriteLine("Writing class " + KOSObj.Name + " to " + filename);
                    StreamWriter KOSFile   = new StreamWriter(filename);
                    string       result    = "";
                    string       Text      = "";
                    WordEngine   oButifier = new WordEngine(KOSObj.GetKOSCode(),
                                                            new char[] { '{', '}', },
                                                            new char[] { '"' });
                    bool firstWord = true;
                    while (oButifier.NextLine())
                    {
                        while (oButifier.NextWord != null)
                        {
                            if (firstWord)
                            {
                                for (int i = 0; i < (oButifier.Current.Equals("{") ? oButifier.BlockNumber - 1 : oButifier.BlockNumber); i++)
                                {
                                    Text += "\t";
                                }
                                firstWord = false;
                            }
                            Text += oButifier.Current;
                        }
                        Text     += "\r\n";
                        firstWord = true;
                    }
                    KOSFile.Write(Text);
                    KOSFile.Close();
                    Console.WriteLine("Write Complete.");
                }
            }
            KOSPPFile.Close();
            Console.WriteLine("Done.");
        }
Ejemplo n.º 4
0
        public string parse()
        {
            SetToObject oSetToObject = null;
            string      result       = "";
            bool        doneSkip     = false;

            //skip to start of user code.
            while (oWordEngine.NextLine())
            {
                while (oWordEngine.NextWord != null)
                {
                    result += oWordEngine.Current;
                    if (oWordEngine.Current == "code@!$")
                    {
                        doneSkip = true;
                        break;
                    }
                }
                result += "\r\n";
                if (doneSkip)
                {
                    break;
                }
            }
            if (oWordEngine.Current == null)
            {
                errors.Add("User code marker not found.");
                return(null);
            }

            while (oWordEngine.NextLine())
            {
                //  for (int i = 0; i < oWordEngine.BlockNumber; i++ )
                //     result+="\t";
                while (oWordEngine.NextWord != null)
                {
                    #region Main Processor

                    if (oWordEngine.Current != null)
                    {
                        if (!oWordEngine.IsWhitespace)
                        {
                            if (oWordEngine.Current.Equals("set"))
                            {
                                oSetToObject = new SetToObject(KOSObjects);
                                oSetToObject.Parse(oWordEngine);
                                result += oSetToObject.KOSCode;
                            }
                            else
                            {
                                result += changeVariable(oWordEngine.Current, KOSObjects);
                            }
                        }
                        else
                        {
                            result += oWordEngine.Current;
                        }
                    }
                    #endregion
                }
                result += "\r\n";
            }
            if (oWordEngine.HasError)
            {
                errors.Add(oWordEngine.FormatedPosition + oWordEngine.Error + "\r\n" + oWordEngine.GetLineWithPositionMarker);
                return(null);
            }
            return(result);
        }