Beispiel #1
0
        public bool Compile(string text, CompilerStage stage)
        {
            Debug.Assert(_document == null); //td:
            _document = createDocument();

            _document.applyChanges(stage);
            return(_document.hasErrors());
        }
Beispiel #2
0
        public bool applyChanges(CompilerStage stage)
        {
            if (stage < _stage)
            {
                return(true);
            }

            if (_stage < CompilerStage.Lexical && stage >= CompilerStage.Lexical)
            {
                _stage = CompilerStage.Lexical;
                applyLexical();
            }

            if (_stage < CompilerStage.Syntactical && stage >= CompilerStage.Syntactical)
            {
                _stage = CompilerStage.Syntactical;
                applySyntactical();

                _semanticalTries = 0;
            }

            if (_stage <= CompilerStage.Semantical && stage >= CompilerStage.Semantical)
            {
                _stage = CompilerStage.Semantical;
                bool result = applySemantical();
                if (!result)
                {
                    if (_semanticalTries > 5)
                    {
                        return(false);
                    }

                    if (stage == CompilerStage.Finished)
                    {
                        return(applyChanges(stage));
                    }
                }

                _semanticalTries++;
                return(result);
            }

            _stage = stage;
            return(true); //finished
        }
Beispiel #3
0
        public bool applyChanges(CompilerStage stage)
        {
            if (stage < _stage)
            {
                return(true);
            }

            var oldStage = _stage;

            _stage = stage;

            if (oldStage < CompilerStage.Lexical && stage >= CompilerStage.Lexical)
            {
                applyLexical();
            }

            if (oldStage < CompilerStage.Syntactical && stage >= CompilerStage.Syntactical)
            {
                applySyntactical();
                _semanticalTries = 0;
            }

            if (oldStage <= CompilerStage.Semantical && stage >= CompilerStage.Semantical)
            {
                bool result = applySemantical();
                if (result)
                {
                    _semanticalTries = 0;
                }
                else
                {
                    _semanticalTries++;
                }
                return(result);
            }

            return(true); //finished
        }
Beispiel #4
0
 public bool Advance(CompilerStage stage)
 {
     _document.applyChanges(stage);
     return(_document.hasErrors());
 }
Beispiel #5
0
        private TNode applyNodeChanges(TNode node, string kind, CompilerStage stage)
        {
            List <Change> changeList = null;

            switch (stage)
            {
            case CompilerStage.Lexical:     changeList = _lexicalChanges; break;

            case CompilerStage.Syntactical: changeList = _syntacticalChanges; break;

            case CompilerStage.Semantical:  changeList = _semanticalChanges; break;

            default: throw new NotImplementedException();
            }

            if (!changeList.Any())
            {
                return(node);
            }

            IEnumerable <Change> changes;

            if (kind != null)
            {
                changes = poll(changeList, kind);
            }
            else
            {
                changes = new List <Change>(changeList);
                changeList.Clear(); //take all for the stage
            }

            if (changes == null || !changes.Any())
            {
                return(node);
            }

            if (stage == CompilerStage.Semantical)
            {
                var transformers = new Dictionary <int, Func <TNode, TNode, TModel, Scope, TNode> >();
                foreach (var change in changes)
                {
                    addTransformer(transformers, change);
                    //if (change.SemanticalTransform != null)
                    //    transformers[change.ID] = change.SemanticalTransform; duplicates IDS
                    //else
                    //{
                    //    Debug.Assert(change.Transform != null);
                    //    transformers[change.ID] = (oldNode, newNode, model, scope) => change.Transform(oldNode, scope);
                    //}
                }

                return(transform(node, transformers));
            }
            else
            {
                var transformers = new Dictionary <int, Func <TNode, Scope, TNode> >();
                foreach (var change in changes)
                {
                    if (change.Transform != null)
                    {
                        addTransformer(transformers, change);
                    }

                    //transformers[change.ID] = change.Transform;
                }

                return(transform(node, transformers));
            }
        }
Beispiel #6
0
 private TNode applyNodeChanges(TNode root, CompilerStage stage)
 {
     return(applyNodeChanges(root, null, stage));
 }