Пример #1
0
 public UserFunction(ParseNode originalNode)
 {
     codePart     = new CodePart();
     functions    = new Dictionary <int, UserFunctionCodeFragment>();
     newFunctions = new List <UserFunctionCodeFragment>();
     OriginalNode = originalNode;
 }
Пример #2
0
        public void AddBlock(CodePart part)
        {
            if (LastRoutine)
            {
                AddRoutineBlock(part); return;
            }
            var       Caller = LastCaller;
            CodeBlock Block  = new CodeBlock()
            {
                Part           = part.CreateEmpty(),
                CodeTree       = this,
                IsRoutineBlock = Caller.IsRoutineBlock
            };
            var p = Caller.Location;

            Block.Location = Caller.IsRoutineBlock ? new Point(p.X + Caller.Width + 15, p.Y) : new Point(p.X, p.Y + Caller.Height + 15);
            TreeNode <CodePart> node;

            if (Tree.value == Caller.Part)
            {
                node = Tree.AddChild(Block.Part);
            }
            else
            {
                node = Tree.FindChild(Tree, Caller.Part).Parent.AddChild(Block.Part);
            }
            Block.Node = node;
            Controls.Add(Block);
            Block.Invalidate();
            ActiveControl = Block;
        }
Пример #3
0
        public CodePart Compile(ParseTree tree, Context context)
        {
            _part    = new CodePart();
            _context = context;

            try
            {
                if (tree.Nodes.Count > 0)
                {
                    PreProcess(tree);
                    CompileProgram(tree);
                }
            }
            catch (Exception e)
            {
                if (_lastNode != null)
                {
                    throw new Exception(string.Format("Error parsing {0}: {1}", ConcatenateNodes(_lastNode), e.Message));
                }
                else
                {
                    throw;
                }
            }

            return(_part);
        }
Пример #4
0
        public CodePart Compile(int startLineNum, ParseTree tree, Context context, CompilerOptions options)
        {
            InitCompileFlags();

            part = new CodePart();
            this.context = context;
            this.options = options;
            this.startLineNum = startLineNum;

            ++context.NumCompilesSoFar;

            try
            {
                if (tree.Nodes.Count > 0)
                {
                    PreProcess(tree);
                    CompileProgram(tree);
                }
            }
            catch (KOSException kosException)
            {
                if (lastNode != null)
                {
                    throw;  // TODO something more sophisticated will go here that will
                    // attach source/line information to the exception before throwing it upward.
                    // that's why this seemingly pointless "catch and then throw again" is here.
                }
                SafeHouse.Logger.Log("Exception in Compiler: " + kosException.Message);
                SafeHouse.Logger.Log(kosException.StackTrace);
                throw;  // throw it up in addition to logging the stack trace, so the kOS terminal will also give the user some message.
            }

            return part;
        }
Пример #5
0
 public UserFunction(ParseNode originalNode)
 {
     codePart = new CodePart();
     functions = new Dictionary<int, UserFunctionCodeFragment>();
     newFunctions = new List<UserFunctionCodeFragment>();
     OriginalNode = originalNode;
 }
Пример #6
0
        public CodePart Compile(ParseTree tree, Context context, CompilerOptions options)
        {
            _part = new CodePart();
            _context = context;
            _options = options;

            try
            {
                if (tree.Nodes.Count > 0)
                {
                    PreProcess(tree);
                    CompileProgram(tree);
                }
            }
            catch (Exception e)
            {
                if (_lastNode != null)
                {
                    throw new Exception(string.Format("Error parsing {0}: {1}", ConcatenateNodes(_lastNode), e.Message));
                }
                else
                {
                    throw;
                }
            }

            return _part;
        }
Пример #7
0
        public CodePart GetNewFunctionsCodePart()
        {
            var newFunctionsPart = new CodePart();

            foreach (LockFunction function in newFunctions)
            {
                newFunctionsPart.FunctionsCode.AddRange(function.Code);
            }

            ClearNewFunctions();
            return(newFunctionsPart);
        }
Пример #8
0
        public CodePart GetCodePart()
        {
            CodePart mergedPart = new CodePart();
            mergedPart.InitializationCode = _codePart.InitializationCode;
            mergedPart.MainCode = _codePart.MainCode;

            foreach (LockFunction function in _functions.Values)
            {
                mergedPart.FunctionsCode.AddRange(function.Code);
            }

            return mergedPart;
        }
Пример #9
0
        public CodePart GetCodePart()
        {
            CodePart mergedPart = new CodePart();

            mergedPart.InitializationCode = _codePart.InitializationCode;
            mergedPart.MainCode           = _codePart.MainCode;

            foreach (LockFunction function in _functions.Values)
            {
                mergedPart.FunctionsCode.AddRange(function.Code);
            }

            return(mergedPart);
        }
Пример #10
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);
        }
Пример #11
0
        public override List <CodePart> Compile(string scriptText, string contextId)
        {
            List <CodePart> parts = null;

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

            // TODO: I don't like that the compiler has to know about the interpreter
            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)
                {
                    Compiler compiler = new Compiler();
                    LoadContext(contextId);

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

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

                    parts.Add(mainPart);

                    AssignInstructionId(parts);

                    // TODO: I don't like that the compiler has to know about the interpreter
                    if (contextId != "interpreter")
                    {
                        _cache.AddToCache(scriptText, parts);
                    }
                }
                else
                {
                    ParseError error = parseTree.Errors[0];
                    RaiseParseException(scriptText, error.Line, error.Position);
                }
            }

            return(parts);
        }
Пример #12
0
        public CodePart GetCodePart()
        {
            var mergedPart = new CodePart
                {
                    InitializationCode = codePart.InitializationCode,
                    MainCode = codePart.MainCode
                };

            foreach (LockFunction function in functions.Values)
            {
                mergedPart.FunctionsCode.AddRange(function.Code);
            }

            return mergedPart;
        }
Пример #13
0
        public CodePart GetCodePart()
        {
            var mergedPart = new CodePart
            {
                InitializationCode = codePart.InitializationCode,
                MainCode           = codePart.MainCode
            };

            foreach (LockFunction function in functions.Values)
            {
                mergedPart.FunctionsCode.AddRange(function.Code);
            }

            return(mergedPart);
        }
Пример #14
0
        void AddRoutineBlock(CodePart part)
        {
            var       Caller = LastCaller;
            CodeBlock Block  = new CodeBlock()
            {
                Part           = part.CreateEmpty(),
                CodeTree       = this,
                IsRoutineBlock = true
            };
            var p = Caller.Location;

            Block.Location = new Point(p.X + Caller.Width + 15, p.Y);
            Block.Node     = Tree.FindChild(Tree, Caller.Part).AddChild(Block.Part);
            Controls.Add(Block);
            Block.Invalidate();
            ActiveControl = Block;
        }
Пример #15
0
        public CodePart Compile(int startLineNum, ParseTree tree, Context context, CompilerOptions options)
        {
            InitCompileFlags();

            part = new CodePart();
            this.context = context;
            this.options = options;
            this.startLineNum = startLineNum;

            ++context.NumCompilesSoFar;

            if (tree.Nodes.Count > 0)
            {
                PreProcess(tree);
                CompileProgram(tree);
            }
            return part;
        }
Пример #16
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
                {
                    ParseError error = parseTree.Errors[0];
                    RaiseParseException(scriptText, error.Line, error.Position);
                }
            }

            return(parts);
        }
Пример #17
0
 public Lock()
 {
     _codePart  = new CodePart();
     _functions = new Dictionary <int, LockFunction>();
 }
Пример #18
0
 public Trigger()
 {
     codePart = new CodePart();
 }
Пример #19
0
 public Trigger()
 {
     _codePart = new CodePart();
 }
Пример #20
0
 protected override void AddEndOfProgram(CodePart linkedObject, bool isMainProgram)
 {
     linkedObject.MainCode.Add(new OpcodeEOF());
 }
Пример #21
0
 public Subprogram(string subprogramName)
 {
     codePart          = new CodePart();
     SubprogramName    = subprogramName;
     PointerIdentifier = string.Format("$program-{0}*", subprogramName);
 }
Пример #22
0
 public ScriptFile(string Code)
 {
     code  = Code;
     asm   = BuildAssembly();
     Class = (CodePart)Activator.CreateInstance(FindDerivedTypes(asm, typeof(CodePart)).First());
 }
Пример #23
0
 public Subprogram(string subprogramName)
 {
     codePart = new CodePart();
     SubprogramName = subprogramName;
     PointerIdentifier = string.Format("$program-{0}*", subprogramName);
 }
Пример #24
0
 public Lock()
 {
     codePart = new CodePart();
     functions = new Dictionary<int, LockFunction>();
     newFunctions = new List<LockFunction>();
 }
Пример #25
0
        public CodePart GetNewFunctionsCodePart()
        {
            var newFunctionsPart = new CodePart();

            foreach (UserFunctionCodeFragment function in newFunctions)
            {
                newFunctionsPart.FunctionsCode.AddRange(function.Code);
            }

            ClearNewFunctions();
            return newFunctionsPart;
        }
Пример #26
0
 protected override void AddInitializationCode(CodePart linkedObject, CodePart part)
 {
     linkedObject.MainCode.AddRange(part.InitializationCode);
 }
Пример #27
0
 public Lock()
 {
     codePart     = new CodePart();
     functions    = new Dictionary <int, LockFunction>();
     newFunctions = new List <LockFunction>();
 }
Пример #28
0
 protected override void AddEndOfProgram(CodePart linkedObject, bool isMainProgram)
 {
     linkedObject.MainCode.Add(new OpcodeEOF());
 }
Пример #29
0
 protected override void AddInitializationCode(CodePart linkedObject, CodePart part)
 {
     linkedObject.MainCode.AddRange(part.InitializationCode);
 }
Пример #30
0
        public CodePart GetNewFunctionsCodePart()
        {
            var newFunctionsPart = new CodePart();

            foreach (LockFunction function in newFunctions)
            {
                newFunctionsPart.FunctionsCode.AddRange(function.Code);
            }

            ClearNewFunctions();
            return newFunctionsPart;
        }