Beispiel #1
0
 private void VisitChildren(AstNodeBase <TInstruction> node)
 {
     foreach (var child in node.GetChildren())
     {
         ((AstNodeBase <TInstruction>)child).Accept(this, null);
     }
 }
Beispiel #2
0
 public override void BeforeVisitCatchAll(AstNodeBase node)
 {
     base.BeforeVisitCatchAll(node);
     var expressionNode = node as ExpressionNodeBase;
     if (expressionNode != null)
         _result = ExpressionAnalyzer.Analyze(_analysisContext, expressionNode);
 }
Beispiel #3
0
        private static object GetIntegerRef(AstNodeBase statement, AstNodeBase astNode, Dictionary <string, Label> labels)
        {
            IIntegerRef integerRef = astNode[typeof(IIntegerRef)] as IIntegerRef;

            if (integerRef is IntegerAstNode)
            {
                return(((IntegerAstNode)integerRef).Value);
            }
            else if (integerRef is LabelAstNode)
            {
                LabelAstNode label = integerRef as LabelAstNode;

                if (label != null && labels.ContainsKey(label.Value))
                {
                    return(labels[label.Value].Integer);
                }
                else
                {
                    throw new AssembleException("label must be decared before use", statement.SourceSpan);
                }
            }
            else
            {
                throw new AssembleException("bad integer ref", statement.SourceSpan);
            }
        }
Beispiel #4
0
 public void Evaluate(int start, int end, FSA <TValue> fsa, AstNodeBase node, ORegexOptions options)
 {
     // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
     if (node is AstAtomNode <TValue> )
     {
         EvaluateAtom(start, end, fsa, (AstAtomNode <TValue>)node);
     }
     else if (node is AstConcatNode)
     {
         EvaluateConcat(start, end, fsa, (AstConcatNode)node, options);
     }
     else if (node is AstOrNode)
     {
         EvaluateOr(start, end, fsa, (AstOrNode)node, options);
     }
     else if (node is AstRepeatNode)
     {
         EvaluateRepeat(start, end, fsa, (AstRepeatNode)node, options);
     }
     else if (node is AstRootNode)
     {
         EvaluateRoot(start, end, fsa, (AstRootNode)node, options);
     }
     else
     {
         throw new NotImplementedException(node.GetType().Name);
     }
 }
Beispiel #5
0
 public AstRootNode(AstNodeBase innerExpression, bool matchBegin, bool matchEnd, Range range, IEnumerable<string> captureGroupNames)
     : base(range)
 {
     MatchBegin = matchBegin;
     MatchEnd = matchEnd;
     Regex = innerExpression;
     CaptureGroupNames = captureGroupNames.ToArray();
 }
Beispiel #6
0
        public override void AfterVisitCatchAll(AstNodeBase node)
        {
            base.AfterVisitCatchAll(node);
            var expressionNode = node as ExpressionNodeBase;
            if (expressionNode == null)
                return;

            base.PopMode();
        }
        public override void AfterVisitCatchAll(AstNodeBase node)
        {
            base.AfterVisitCatchAll(node);
            var statementNode = node as StatementNodeBase;
            if (statementNode == null)
                return;

            base.PopMode();
        }
        public override void BeforeVisitCatchAll(AstNodeBase node)
        {
            base.BeforeVisitCatchAll(node);
            var statementNode = node as StatementNodeBase;
            if (statementNode == null)
                return;

            base.PushMode(VisitorMode.VisitNodeOnly);
            _statements.Add(StatementAnalyzer.Analyze(_analysisContext, statementNode));
        }
Beispiel #9
0
 protected void AssembleValueOrLable(AstNodeBase node)
 {
     if (node is LabelAstNode)
     {
         WriteLine($"get_local ${((LabelAstNode)node).Value}");
     }
     else
     {
         WriteLine($"i32.const {((IntegerAstNode)node).Value}");
     }
 }
Beispiel #10
0
 private void RepeatZeroOrOne(int start, int end, FSA <TValue> fsa, AstNodeBase node, bool isLasy, ORegexOptions options)
 {
     if (isLasy)
     {
         fsa.AddEpsilonTransition(start, end);
         Evaluate(start, end, fsa, node, options);
     }
     else
     {
         Evaluate(start, end, fsa, node, options);
         fsa.AddEpsilonTransition(start, end);
     }
 }
Beispiel #11
0
        private static ushort GetIntegerRef16(AstNodeBase statement, AstNodeBase astNode, Dictionary <string, Label> labels)
        {
            object value = GetIntegerRef(statement, astNode, labels);

            if ((value is byte) || (value is ushort))
            {
                return(Convert.ToUInt16(value));
            }
            else
            {
                throw new AssembleException("integer reference must be 16 bits", statement.SourceSpan);
            }
        }
Beispiel #12
0
        public AstRepeatNode(AstNodeBase arg, int minCount, int maxCount, bool isLazy, Range range)
            : base(range)
        {
            if (minCount > maxCount || minCount == 0 && maxCount == 0)
            {
                throw new ORegexException("Invalid expression repeat interval.");
            }

            MinCount = minCount;
            MaxCount = maxCount;
            IsLazy = isLazy;
            Argument = arg.ThrowIfNull();
        }
Beispiel #13
0
        private static byte GetIntegerRef8(AstNodeBase statement, AstNodeBase astNode, Dictionary <string, Label> labels)
        {
            object value = GetIntegerRef(statement, astNode, labels);

            if (value is byte)
            {
                return((byte)value);
            }
            else
            {
                throw new AssembleException("integer reference must be 8 bits", statement.SourceSpan);
            }
        }
Beispiel #14
0
        public AstRepeatNode(AstNodeBase arg, int minCount, int maxCount, bool isLazy, Range range)
            : base(range)
        {
            if (minCount > maxCount || minCount == 0 && maxCount == 0)
            {
                throw new ORegexException("Invalid expression repeat interval.");
            }

            MinCount = minCount;
            MaxCount = maxCount;
            IsLazy   = isLazy;
            Argument = arg.ThrowIfNull();
        }
Beispiel #15
0
        public AstRootNode Parse(string input, PredicateTable <TValue> predicateTable)
        {
            var lexer       = new RegexGrammarLexer(new AntlrInputStream(input));
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new RegexGrammarParser(tokenStream);

            parser.AddErrorListener(new ORegexErrorListener());

            var context = parser.expr();

            var args   = new ORegexAstFactoryArgs <TValue>(predicateTable, parser);
            var result = ORegexAstFactory <TValue> .CreateAstTree(context, args);

#if DEBUG
            AstNodeBase.Print(result);
#endif
            return(result);
        }
Beispiel #16
0
        private void RepeatZeroOrInfinite(int start, int end, FSA <TValue> fsa, AstNodeBase predicate, bool isLasy, ORegexOptions options)
        {
            var tmp = CreateNewState(fsa);

            if (isLasy)
            {
                fsa.AddEpsilonTransition(start, end);
                fsa.AddEpsilonTransition(tmp, end);
                Evaluate(tmp, tmp, fsa, predicate, options);
                fsa.AddEpsilonTransition(start, tmp);
            }
            else
            {
                Evaluate(tmp, tmp, fsa, predicate, options);
                fsa.AddEpsilonTransition(tmp, end);

                fsa.AddEpsilonTransition(start, tmp);
                fsa.AddEpsilonTransition(start, end);
            }
        }
Beispiel #17
0
        public override void BeforeVisitCatchAll(AstNodeBase node)
        {
            base.AfterVisitCatchAll(node);
            var expressionNode = node as ExpressionNodeBase;
            if (expressionNode == null)
                return;

            base.PushMode(VisitorMode.VisitNodeOnly);
            _result = ExpressionAnalyzer.Analyze(_analysisContext, (ExpressionNodeBase)node);
        }
Beispiel #18
0
        protected void Assemble(AstNodeBase statement)
        {
            var instruction = statement.ChildNodes[0];

            if (instruction is CuckAstNode)
            {
                if (instruction.ChildNodes.Count == 2)
                {
                    WriteLine();
                }
            }
            else
            {
                WriteLine();
            }

            if (instruction is CuckAstNode || instruction is AssignmentAstNode)
            {
                if (instruction.ChildNodes.Count == 2)
                {
                    if (instruction is CuckAstNode)
                    {
                        WriteLine(";; cuck assignment");
                    }
                    else
                    {
                        WriteLine(";; assignment");
                    }

                    Assemble(((LValueAstNode)instruction.ChildNodes[1]));
                    WriteLine($"set_local ${((LabelAstNode)instruction.ChildNodes[0]).Value}");
                }
            }
            else if (instruction is BingAstNode || instruction is BongAstNode)
            {
                WriteLine(";; bing/bong");
                WriteLine($"get_local ${((LabelAstNode)instruction.ChildNodes[0]).Value}");
                WriteLine($"i32.const 1");

                if (instruction is BingAstNode)
                {
                    WriteLine($"i32.add");
                }
                else
                {
                    WriteLine($"i32.sub");
                }

                WriteLine($"set_local ${((LabelAstNode)instruction.ChildNodes[0]).Value}");
            }
            else if (instruction is ShitPostAstNode)
            {
                WriteLine(";; shitpost");
                Assemble(((LValueAstNode)instruction.ChildNodes[0]));
                WriteLine($"call $print");
            }
            else if (instruction is BtfoAstNode)
            {
                WriteLine(";; btfo");
                WriteLine($"drop");
                Assemble(((LValueAstNode)instruction.ChildNodes[0]));
                WriteLine($"return");
            }
            else if (instruction is GrabAstNode)
            {
                WriteLine(";; grab");
                WriteLine($"set_local ${((LabelAstNode)instruction.ChildNodes[0]).Value}");
            }
            else if (instruction is CallAstNode)
            {
                WriteLine(";; call");

                var arguments = instruction[typeof(ArgumentsAstNode)] as ArgumentsAstNode;

                foreach (var argugment in arguments.ChildNodes)
                {
                    Assemble(((LValueAstNode)argugment));
                }

                WriteLine($"call ${((LabelAstNode)instruction.ChildNodes[0]).Value}");
            }
            else if (instruction is CaterpillarAstNode)
            {
                WriteLine(";; caterpillar");

                WriteLine($"block $block{_blockLevel}");
                Indent();

                WriteLine($"loop $loop{_blockLevel}");
                Indent();

                _blockLevel++;
                var statements = instruction.Find(typeof(StatementAstNode));

                foreach (var nestedStatement in statements)
                {
                    Assemble(nestedStatement);
                }
                _blockLevel--;

                WriteLine("br 0");

                Dedent();
                WriteLine("end");

                Dedent();
                WriteLine("end");
            }
            else if (instruction is YerFiredAstNode)
            {
                WriteLine(";; yerfired");
                WriteLine($"br $block{_blockLevel-1}");
            }
            else if (instruction is IfAstNode)
            {
                WriteLine(";; if");
                Assemble((LValueAstNode)instruction.ChildNodes[0]);
                Assemble((LValueAstNode)instruction.ChildNodes[2]);

                //equals is the only test
                WriteLine("i32.eq");
                WriteLine("if");
                Indent();

                var statements = instruction.Find(typeof(StatementAstNode));

                foreach (var nestedStatement in statements)
                {
                    Assemble(nestedStatement);
                }

                Dedent();
                WriteLine("end");
            }
        }
Beispiel #19
0
        private static uint GetIntegerRef32(AstNodeBase statement, AstNodeBase astNode, Dictionary <string, Label> labels)
        {
            object value = GetIntegerRef(statement, astNode, labels);

            return(Convert.ToUInt32(value));
        }