public void add(string key, FuncClass value)
 {
     if (Functions.ContainsKey(key))
         Functions[key] = value;
     else
         Functions.Add(key, value);
 }
Beispiel #2
0
        private FuncClass createConstructor(FuncClass function)
        {
            function.Name = "constructor";

            foreach (string var in Properties.Keys)
                function.Code.Insert(1, var + "=" + Properties[var]);

            if(!function.hasReturn )
                function.Code.Insert(function.Code.Count -1, "return " + Name + "_");
            else
                Global.Errors .Add (new ErrorClass ("Class " + Name, "Class's construtor already has a return", ""));
            function.hasReturn = true;

            return createFunction(function);
        }
Beispiel #3
0
        private FuncClass createFunction(FuncClass function)
        {
            function.Name = Name + "_" + function.Name;

            List<string> new_code = new List<string>();
            foreach (string raw_line in function.Code )
            {
                LineClass line = new LineClass(raw_line);
                string newline = line.rawline;

                //Loop through each token to rename localization
                line.Tokens = Global.Tokenize(newline, Global.stdTokenizeString);
                List<string> new_tokens = new List<string>();

                bool isComment = false;
                foreach (string token in line.Tokens)
                {
                    string new_token = token;
                    if (!isComment)
                    {
                        if (token == "#") isComment = true;
                        if (Properties .ContainsKey (new_token)) new_token = Name + "_." + new_token;

                    }
                    new_tokens.Add(new_token);
                }
                newline = string.Join("", new_tokens.ToArray<string>()); //re-build each new token into a string

                //add the coments back in
                newline += line.Comments;
                new_code.Add(newline);
            }

            function.Code = new_code;

            return function;
        }
Beispiel #4
0
        /*
        //Converts all R++ commenting ("//", "/*", and "/") to RSL style
        private List<string> convertComments(List<string> code)
        {
            List<string> result = new List<string>();
            bool blockcommentFlag = false;

            foreach (string line in code)
            {
                string newline = line;
                if (blockcommentFlag) newline = "#" + newline;
                if (newline.Contains("/*")) { newline = newline.Replace("/*", "#"); blockcommentFlag = true; }
                if (newline.Contains("/")) { newline = newline.Replace("/", ""); blockcommentFlag = false; }
                newline = newline.Replace("//", "#");
                result.Add(newline);
            }
            return result;
        }

        //Extracts all Extended and Inherited Functions from the prototypes
        private void getPrototypes(List<string> Code)
        {
            InheritedFunctions = new Dictionary<string, FuncClass>();
            ExtendedFunctions = new Dictionary<string, FuncClass>();

            foreach (string untrimmed_line in Code)
            {
                string line = untrimmed_line.Trim();

                if (line.ToLower().StartsWith("#inherit"))
                {
                    try
                    {
                        string temp_file_name = line.Replace("#inherit ", "");
                        FileClass temp_file = new FileClass(temp_file_name);
                        foreach (string key in temp_file.Functions.Keys)
                            if (!InheritedFunctions.ContainsKey(key)) InheritedFunctions.Add(key, temp_file.Functions[key]);
                    }
                    catch
                    {
                        Global.Errors.Add (new ErrorClass ("File", "Improper use of #inherit", line);
                    }
                }
                else if (line.ToLower().StartsWith("#extend"))
                {
                    try
                    {
                        string temp_file_name = line.Replace("#extend ", "");
                        FileClass temp_file = new FileClass(temp_file_name);
                        foreach (string key in temp_file.Functions.Keys)
                            if (!ExtendedFunctions.ContainsKey(key)) ExtendedFunctions.Add(key, temp_file.Functions[key]);
                    }
                    catch
                    {
                        Global.Errors.Add (new ErrorClass ("File", "Improper use of #extend", line);
                    }
                }
                else if (line != "") break;
            }
        }

        //Returns a list of all the non-converted functions within the code
        private Dictionary <string,FuncClass > getFunctions(List<string> Code)
        {
            Dictionary <string,FuncClass > result = new Dictionary<string, FuncClass>();
            FuncClass tempfunc = new FuncClass();

            bool funcFlag = false;

            foreach (string untrimmed_line in Code)
            {
                string line = untrimmed_line.Trim();

                //Collect Header
                if ((line.StartsWith("#") || line == "") && !funcFlag)
                {
                    tempfunc.Header.Add(line);
                }
                //Get Function declaration
                else if (!funcFlag)
                {
                    funcFlag = true;
                    tempfunc.getDefinition(line);
                }
                //Get code for each function
                else
                {
                    tempfunc.Code.Add(line);
                    //Function Closing
                    if (!(line.StartsWith("//") || line.StartsWith("#")) & line.EndsWith("}"))
                    {
                        if (!result.ContainsKey(tempfunc.Name))  //Duplicity Checking
                        {
                            tempfunc.Initialize();
                            result.Add(tempfunc.Name, tempfunc);
                        }
                        else Global.Errors.Add (new ErrorClass ("File", "Function name already declared", line);

                        funcFlag = false;
                        tempfunc = new FuncClass();
                    }
                }
            }
            //Adds all the declared functions to a global list used in other parts of the code
            global.addFunctions(result);

            return result;
        }*/
        //Extracts all usable code in one pass: Prototypes, block comments, and functions
        private void ExtractCode(List<string> code)
        {
            bool inFunction = false;
            bool inBlockComment = false;
            bool inClass = false;

            ClassClass temp_class = new ClassClass ();
            //Global.CurrentLine = 0;

            List<string> header = new List<string>();
            List<string> body = new List<string>();
            string defintion_line = "";

            foreach (string untrimmed_line in code)
            {
                string line = untrimmed_line.ToLower().Trim();
                //Global.CurrentLine++; //increment the line for error purposes

                //Handle Block Commenting right away
                if (inBlockComment) line = "#" + line;
                line = line.Replace("//", "#");
                if (line.Contains("/*"))
                {
                    line = line.Replace("/*", "#");
                    inBlockComment = true;
                }
                if (line.Contains("*/"))
                {
                    line = line.Replace("*/", "");
                    inBlockComment = false;
                }

                if (line.StartsWith("#inherit"))
                    InheritedFunctions = ProcessProtoype (line.Replace ("#inherit ",""), InheritedFunctions );
                else if (line.StartsWith("#extend"))
                    ExtendedFunctions = ProcessProtoype(line.Replace("#extend ", ""), ExtendedFunctions);
                else if ((line.StartsWith("//") || line.StartsWith("#") || line == "") && !inFunction)
                    header.Add(line);

                else if (!inClass & line.StartsWith("class"))
                {
                    inClass = true;
                    temp_class = new ClassClass (line.Replace ("class ",""));
                }
                else if (inClass & !inFunction & line.Contains('='))
                {
                    temp_class.Properties.Add(line.Split('=')[0].Trim (), line.Split('=')[1].Trim());
                }
                else if (inClass & !inFunction & line.Contains("end class"))
                {
                    Classes.Add(temp_class.Name, temp_class);
                    inClass = false;
                }
                else if (!inFunction) //Grabs any non-comment, non-empty line and starts addign it to functions
                {
                    inFunction = true;
                    defintion_line = line;
                }
                else
                {
                    body.Add(line);
                    if (line.Contains("}"))
                    {
                        FuncClass temp_function = new FuncClass(defintion_line, header, body);
                        if (inClass)
                            temp_class.Functions.Add(temp_function.Name, temp_function);
                        else if (!Functions.ContainsKey(temp_function.Name)) //Duplicity Checking
                        {
                            Functions.Add(temp_function.Name, temp_function);
                            FunctionList.add (temp_function.Name, temp_function);
                        }
                        else
                            Global.Errors.Add(new ErrorClass("File", "Function name already declared", line));

                        //Clean up to prep for grabbing the next function
                        inFunction = false;
                        header = new List<string>();
                        body = new List<string>();
                        defintion_line = "";
                    }
                }
            }
        }