Example #1
0
        public override List <CodePart> Compile(string filePath, int startLineNum, string scriptText, string contextId, CompilerOptions options)
        {
            List <CodePart> parts = null;

            // make the code lowercase
            scriptText = MakeLowerCase(scriptText);
            scriptText = ReplaceIdentifiers(scriptText);

            //if (contextId != "interpreter") parts = _cache.GetFromCache(scriptText);

            // if parts is null means the code doesn't exists in the cache
            if (parts == null)
            {
                parts = new List <CodePart>();
                ParseTree parseTree = parser.Parse(scriptText);
                if (parseTree.Errors.Count == 0)
                {
                    var compiler = new Compiler();
                    LoadContext(contextId);

                    // TODO: handle compile errors (e.g. wrong run parameter count)
                    CodePart mainPart = compiler.Compile(startLineNum, parseTree, currentContext, options);

                    // add locks and triggers
                    parts.AddRange(currentContext.Locks.GetNewParts());
                    parts.AddRange(currentContext.Triggers.GetNewParts());
                    parts.AddRange(currentContext.Subprograms.GetNewParts());

                    parts.Add(mainPart);

                    AssignSourceId(parts, filePath);

                    //if (contextId != "interpreter") _cache.AddToCache(scriptText, parts);
                }
                else
                {
                    // TODO: Come back here and check on the possiblity of reporting more
                    // errors than just the first one.  It appears that TinyPG builds a
                    // whole array of error messages so people could see multiple syntax
                    // errors in one go if we supported the reporting of it.  It may be that
                    // it was deliberately not done because it might be too verbose that way
                    // for the small text terminal.

                    ParseError error = parseTree.Errors[0];
                    throw new KOSParseException(error, scriptText);
                }
            }

            return(parts);
        }
Example #2
0
        public override List <CodePart> Compile(string filePath, int startLineNum, string scriptText, string contextId, CompilerOptions options)
        {
            var       parts     = new List <CodePart>();
            ParseTree parseTree = parser.Parse(scriptText);

            if (parseTree.Errors.Count == 0)
            {
                var compiler = new Compiler();
                LoadContext(contextId);

                CodePart mainPart;
                try
                {
                    mainPart = compiler.Compile(startLineNum, parseTree, currentContext, options);
                }
                catch (KOSCompileException e)
                {
                    e.AddSourceText((short)startLineNum, scriptText);
                    throw;
                }

                // add locks and triggers
                parts.AddRange(currentContext.UserFunctions.GetNewParts());
                parts.AddRange(currentContext.Triggers.GetNewParts());
                parts.AddRange(currentContext.Subprograms.GetNewParts());

                parts.Add(mainPart);

                AssignSourceId(parts, filePath);

                //if (contextId != "interpreter") _cache.AddToCache(scriptText, parts);
            }
            else
            {
                // TODO: Come back here and check on the possibility of reporting more
                // errors than just the first one.  It appears that TinyPG builds a
                // whole array of error messages so people could see multiple syntax
                // errors in one go if we supported the reporting of it.  It may be that
                // it was deliberately not done because it might be too verbose that way
                // for the small text terminal.

                ParseError error = parseTree.Errors[0];
                throw new KOSParseException(error, scriptText);
            }

            return(parts);
        }