Esempio n. 1
0
 public bool Parse(WordEngine oWordEngine)
 {
     done  = false;
     error = null;
     name  = "";
     value = "";
     isSet = false;
     if (oWordEngine.Current == "set")
     {
         name = oWordEngine.NextNonWhitespace;
         IKOSppObject nameObject = KOSObjects.SingleOrDefault(x => x.Name == name);
         if (nameObject != null && nameObject.GetType() == typeof(PropertyObject))
         {
             isSet = true;
         }
         else
         {
             name = Phase2Compiler.changeVariable(name, KOSObjects);
         }
         if (name != null)
         {
             if (oWordEngine.NextNonWhitespace == "to")
             {
                 while (oWordEngine.NextNonWhitespace != ".")
                 {
                     if (oWordEngine.Current == null)
                     {
                         error = "Expecting  ., found end of file insted.";
                         return(false);
                     }
                     else
                     {
                         value += Phase2Compiler.changeVariable(oWordEngine.Current, KOSObjects);
                     }
                 }
                 done = true;
                 if (isSet)
                 {
                     name = string.Format(nameObject.CallString(false), value);
                 }
                 return(true);
             }
             else
             {
                 error = "Expecting to, found " + oWordEngine.Current + " insted.";
             }
         }
         else
         {
             error = "Expecting to, end of file insted.";
         }
     }
     else
     {
         error = "Expecting set, found " + oWordEngine.Current + " insted.";
     }
     return(false);
 }
Esempio 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);
            }
        }
Esempio 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.");
        }
Esempio n. 4
0
 public bool Parse(WordEngine oWordEngine)
 {
     //TODO: in the futuer I might want to check that this is a leagal init value for a parameter.
     while (oWordEngine.Current != null)
     {
         initValue += oWordEngine.Current;
         if (oWordEngine.NextNonWhitespace.Equals("."))
         {
             parseState = eParseShate.Done;
             return(false);
         }
     }
     parseError = "Expecting . found end of file.";
     return(true);
 }
Esempio n. 5
0
 public Phase2Compiler(string pCode, List <IKOSppObject> pKOSObjects)
 {
     KOSObjects  = pKOSObjects;
     oWordEngine = new WordEngine(pCode, SeperatorChars, strimgSep);
     errors      = new List <string>();
 }
Esempio n. 6
0
        public bool     Parse(WordEngine oWordEngine)
        {
            switch (parseState)
            {
            case eParseShate.Start:
                if (!oWordEngine.RegisterBlockTest(this))
                {
                    parseState = eParseShate.Error;
                    return(false);
                }
                parseState = eParseShate.MainLoop;
                break;

            case eParseShate.MainLoop:
                switch (oWordEngine.CurrentNonWhitespace)
                {
                case "private":
                    currentIsPublic = false;
                    parseState      = eParseShate.FuncVar;
                    break;

                case "public":
                    currentIsPublic = true;
                    parseState      = eParseShate.FuncVar;
                    break;

                case "property":
                    currentIsPublic = true;
                    parseState      = eParseShate.Property;
                    break;

                case WordEngine.BlockEndChar:
                    parseState = eParseShate.Done;
                    return(false);

                default:
                    if (oWordEngine.HasError)
                    {
                        parseError = oWordEngine.Error;
                    }
                    else
                    {
                        parseError = "Expecting private public or property, found " + oWordEngine.Current;
                    }
                    parseState = eParseShate.Error;
                    return(false);
                }
                break;

            case eParseShate.SendWordsEngineToObject:
                if (currentObject == null)
                {
                    parseError = "Compiler error! trying to send the word " + oWordEngine.Current + " to an null KOSObject.";
                    return(false);
                }
                if (!currentObject.Parse(oWordEngine))
                {
                    if (currentObject.HasParseError || !currentObject.IsParseComplete)
                    {
                        if (currentObject.HasParseError)
                        {
                            parseError = currentObject.ParseError;
                        }
                        else
                        {
                            parseError = "Compiler error! Parser " + currentObject.Name + " returned false, but is not complete and has no errors.";
                        }
                        return(false);
                    }
                    else
                    {
                        parseState = addObject(currentObject);
                    }
                }
                break;

            case eParseShate.Property:
                if (validateName(oWordEngine.NextNonWhitespace))
                {
                    currentObject = new PropertyObject(oWordEngine.Current, currentIsPublic);
                    parseState    = eParseShate.SendWordsEngineToObject;
                }
                else
                {
                    parseError = "The name " + oWordEngine.Current + " is not a valid name for a property.";
                    parseState = eParseShate.Error;
                    return(false);
                }
                break;

            case eParseShate.FuncVar:
                string funcVarName = oWordEngine.CurrentNonWhitespace;
                if (oWordEngine.NextNonWhitespace.Equals("("))     //function
                {
                    if (validateName(funcVarName))
                    {
                        currentObject   = new FunctionObject(funcVarName, currentIsPublic);
                        parseState      = eParseShate.GetFuncPrams;
                        canGetParameter = true;
                    }
                    else
                    {
                        parseError = "The name " + oWordEngine.Current + " is not a valid name for a function.";
                        parseState = eParseShate.Error;
                        return(false);
                    }
                }
                else if (oWordEngine.Current.Equals("=") || oWordEngine.Current.Equals("."))
                {
                    if (validateName(oWordEngine.Current))
                    {
                        currentObject = new VariableObject(funcVarName, currentIsPublic);
                        if (oWordEngine.Current.Equals("="))
                        {
                            parseState = eParseShate.SendWordsEngineToObject;
                        }
                        else
                        {
                            parseState = addObject(currentObject);
                        }
                    }
                    else
                    {
                        parseError = "The name " + funcVarName + " is not a valid name for a variable.";
                        parseState = eParseShate.Error;
                        return(false);
                    }
                }
                else
                {
                    parseState = eParseShate.Error;
                    parseError = "Expecting ( = or ., found " + oWordEngine.Current;
                    return(false);
                }
                break;

            case eParseShate.GetFuncPrams:
                if (oWordEngine.CurrentNonWhitespace.Equals(")"))
                {
                    parseState = eParseShate.SendWordsEngineToObject;
                }
                else if (oWordEngine.Current.Equals(","))
                {
                    if (canGetParameter)
                    {
                        parseError = "Expecting to find a paramter and found a , insted.";
                        parseState = eParseShate.Error;
                        return(false);
                    }
                    else
                    {
                        canGetParameter = true;
                    }
                }
                else if (canGetParameter)
                {
                    if (validateName(oWordEngine.Current))
                    {
                        FunctionObject func = currentObject as FunctionObject;
                        if (func != null)
                        {
                            if (!func.AddParameter(oWordEngine.Current))
                            {
                                parseError = "The function " + func.Name + " has two parameters withe the same name (" + oWordEngine.Current + ").";
                                parseState = eParseShate.Error;
                                return(false);
                            }
                            else
                            {
                                canGetParameter = false;
                            }
                        }
                        else
                        {
                            parseError = "Compiler error! Expecting a type of function object in currentObject(IKOSppObject).";
                            parseState = eParseShate.Error;
                            return(false);
                        }
                    }
                    else
                    {
                        parseError = "The name " + oWordEngine.Current + " is not a valid name for a function parameter.";
                        parseState = eParseShate.Error;
                        return(false);
                    }
                }
                else
                {
                    parseError = "Expecting to find a , or ) but found " + oWordEngine.Current + " insted";
                    parseState = eParseShate.Error;
                    return(false);
                }
                break;

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

            case eParseShate.Done:
                return(false);

            default:
                parseError = "Compiner error! Unhandled state in ClassObject: " + name + "" + parseState.ToString();
                parseState = eParseShate.Error;
                return(false);
            }
            return(true);
        }
Esempio n. 7
0
        public bool     Parse(WordEngine oWordEngine)
        {
            switch (parseState)
            {
            case eParseShate.Start:
                if (!oWordEngine.RegisterBlockTest(this))
                {
                    parseState = eParseShate.Error;
                    return(false);
                }
                parseState = eParseShate.MainLoop;
                break;

            case eParseShate.MainLoop:
                switch (oWordEngine.CurrentNonWhitespace)
                {
                case "get":
                    if (getFunction == null)
                    {
                        getFunction   = new FunctionObject("get", false, "_get" + name);
                        currentObject = getFunction;
                    }
                    else
                    {
                        parseError = "The can only be one get in a propety.";
                        parseState = eParseShate.Error;
                        return(false);
                    }
                    break;

                case "set":
                    if (setFunction == null)
                    {
                        setFunction = new FunctionObject("set", false, "_set" + name);
                        setFunction.AddParameter("value");
                        currentObject = setFunction;
                    }
                    else
                    {
                        parseError = "The can only be one set in a propety.";
                        parseState = eParseShate.Error;
                        return(false);
                    }
                    break;

                case WordEngine.BlockEndChar:
                    parseState = eParseShate.Done;
                    return(false);

                default:
                    if (oWordEngine.HasError)
                    {
                        parseError = oWordEngine.Error;
                    }
                    else
                    {
                        parseError = "Expecting get or set, found " + oWordEngine.Current;
                    }
                    parseState = eParseShate.Error;
                    return(false);
                }
                parseState = eParseShate.SendWordsEngineToObject;
                break;

            case eParseShate.SendWordsEngineToObject:
                if (currentObject == null)
                {
                    parseError = "Compiler error! trying to send the word " + oWordEngine.Current + " to an null KOSObject.";
                    return(false);
                }
                if (!currentObject.Parse(oWordEngine))
                {
                    if (currentObject.HasParseError || !currentObject.IsParseComplete)
                    {
                        if (currentObject.HasParseError)
                        {
                            parseError = currentObject.ParseError;
                        }
                        else
                        {
                            parseError = "Compiler error! Parser " + currentObject.Name + " returned false, but is not complete and has no errors.";
                        }
                        return(false);
                    }
                    else
                    {
                        parseState = eParseShate.MainLoop;
                    }
                }
                break;

            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 PropertyObject: " + name + "" + parseState.ToString();
                parseState = eParseShate.Error;
                return(false);
            }
            return(true);
        }