Beispiel #1
0
        public override void Emit(IlBuilder builder)
        {
            base.Emit(builder);
            _symbolConversion?.Emit(builder);

            if (_method.IsInstance)
            {
                if (((Owner == null || Owner.IsStatic) && _thisPtr == null) || _staticCall)
                {
                    ErrorManager.ExitWithError(new Exception("Can't call instance function from static context"));
                }


                if (!_valueType)
                {
                    builder.PushValue(_thisPtr ?? Owner.ThisPointer);
                }
                else
                {
                    builder.PushAddress(_thisPtr as CodeSymbol);
                }
            }

            foreach (var p in _parameters)
            {
                builder.PushValue(p);
            }

            if (_method.IsVirtual && !_baseAccess)
            {
                if (_valueType)
                {
                    //NOTE: This is sometimes emitted while not needed, like with System.Int32.ToString(), as these methods appear
                    //as virtual but actually aren't. I don't know why, but it still works so whatever.
                    builder.EmitLine("constrained. " + _thisPtr.Type.Name);
                }

                builder.EmitOpCode(OpCodes.Callvirt, _method.FullSignature);
            }
            else
            {
                builder.EmitOpCode(OpCodes.Call, _method.FullSignature);
            }

            builder.ShrinkStack(_parameters.Length);

            if (_return != CodeType.Void)
            {
                if (!_expression)
                {
                    builder.EmitOpCode(OpCodes.Pop);
                }
                else
                {
                    builder.ExpandStack(1);
                }
            }
        }
Beispiel #2
0
        public CodeSymbol AddSymbol(CodeSymbol symbol)
        {
            if (_table.ContainsKey(symbol.ID))
            {
                ErrorManager.ExitWithError(new DuplicateVariableException(symbol.ID));
            }

            _table.Add(symbol.ID, symbol);

            return(symbol);
        }
Beispiel #3
0
        public void CompileVarDec(TypeName type, SyntaxTreeNode node)
        {
            string name = node[0].ValueString;

            if (CurrentTable.Contains(name))
            {
                ErrorManager.ExitWithError(new Exception("Duplicate ID: " + name));
            }

            CodeSymbol symbol = builder.AddLocal(name, type);//CurrentTable.AddSymbol(new CodeSymbol(name, node[2].ValueString));

            if (node.Children.Length > 1)
            {
                builder.AddInstruction(new InterCopy(symbol, ToIntermediateExpression(node[1])));
            }
        }
Beispiel #4
0
        public Token GetNextToken()
        {
            if (_input.AtEnd)
            {
                return(Token.EndOfFile);
            }


            Token t = Token.Unknown;

            List <RegexDFA> test   = _dfas;
            List <RegexDFA> living = new List <RegexDFA>();


            //DFACompiler.PrintDFA(_dfas[0]);
            char c1 = _input.CurrentChar(0);

            if (c1 == GrammarConstants.EndChar)
            {
                Console.WriteLine();
            }
            int i = 0;

            while (true)
            {
                List <RegexDFA> newLiving = new List <RegexDFA>();

                //if (_input[_index + i] == '\n') { _lineIndex++; _lastLine = _index + i+1; }

                string c = _input.GetSubString(i + 1);

                foreach (var dfa in test)
                {
                    if (dfa.Progress(c))
                    {
                        newLiving.Add(dfa);
                    }
                }

                if (newLiving.Count <= 0)
                {
                    break;
                }
                living = newLiving;
                if (living.Count == 1)
                {
                    break;
                }
                test = living;
                i++;
            }

            string   cc    = _input.GetSubString(i);
            RegexDFA final = null;

            if (living.Count == 1)
            {
                final = living[0];
            }
            else if (living.Count > 0)
            {
                foreach (var dfa in living)
                {
                    if (dfa.Accepted(cc))
                    {
                        final = dfa;
                        break;
                    }
                }
                i--;
                if (final == null)
                {
                    throw new Exception("Final was null");
                }
            }


            bool accepted = false;

            cc = _input.GetSubString(i + 1);


            if (final != null)
            {
                accepted = final.Accepted(cc);

                i++;
                while (!_input.AtEnd &&
                       final.Progress(_input.GetSubString(i)))
                {
                    if (final.Accepted(_input.GetSubString(i)))
                    {
                        accepted = true;
                    }
                    i++;
                }

                //if (final.LastJumpAhead != -1) i = final.LastJumpAhead;
            }
            if (!_input.AtEnd)
            {
                i--;
            }


            //Discard White Spaces
            if (final?.Name == "Whitespace")
            {
                _input.Advance(i);
                return(GetNextToken());
            }

            if (accepted)
            {
                t = new Token(_input.GetSubString(i), final.Name, _input.GetInfo());
            }
            /*{ Line = _lines[_lineIndex], LineIndex = _index - _lastLine, LineNumber = _lineIndex + 1 }*/
            else
            {
                ErrorManager.ExitWithError(new Exception("No Token found in " + _input.GetInfo()));
                // ErrorManager.ExitWithError(new FailedToParseTokenException(_lines[_lineIndex], _index - _lastLine - 1, i + 1));
            }


            if (final.Action != null)
            {
                final.Action.Invoke(t);
            }
            else
            {
                t.Values["val"] = t.Text;
            }

            _input.Advance(i);


            return(t);
        }
Beispiel #5
0
        public void Parse(TokenStream input)
        {
            Stack <ParserStackEntry> stateStack = new Stack <ParserStackEntry>();


            stateStack.Push(new ParserStackEntry(_start, Token.Unknown));

            ParserAction nextAction()
            {
                var t = input.NextToken;

                try
                {
                    if (stateStack.Peek().State.Action.ContainsKey(new Terminal(t.Text, false).ID))
                    {
                        return(stateStack.Peek().State.Action[new Terminal(t.Text, false).ID]);
                    }
                    else
                    {
                        return(stateStack.Peek().State.Action[new Terminal(t.Type, true).ID]);
                    }
                }
                catch
                {
                    ErrorManager.ExitWithError(new ParserActionNotFoundException(stateStack.Peek().State, t));
                }
                return(new AcceptAction());
            }

            var action = nextAction();

            while (!(action is AcceptAction))
            {
                //if (input.NextToken.Text == "Console")
                //    Console.WriteLine("aaaa");

                switch (action)
                {
                case ShiftAction shift:
                    stateStack.Push(new ParserStackEntry(_states[shift.StateIndex], input.EatToken()));
                    break;

                case ReduceAction reduce:
                    Stack <ParserStackEntry> tempStack = new Stack <ParserStackEntry>(new Stack <ParserStackEntry>(stateStack));

                    for (int i = 0; i < reduce.PopCount; i++)
                    {
                        stateStack.Pop();
                    }

                    stateStack.Push(new ParserStackEntry(_states[stateStack.Peek().State.Goto[reduce.GotoID]], Token.Unknown));
                    tempStack.Push(stateStack.Peek());

                    if (reduce.Action != null)
                    {
                        reduce.Action.Invoke(tempStack);
                    }

                    break;
                }

                action = nextAction();
            }
        }