Exemple #1
0
        private static string IrisTypeToCilTypeName(IrisType type)
        {
            if (type.IsPrimitive)
            {
                if (type == IrisType.Integer)
                {
                    return("int32");
                }
                else if (type == IrisType.String)
                {
                    return("string");
                }
                else if (type == IrisType.Boolean)
                {
                    return("bool");
                }
            }
            else if (type.IsArray)
            {
                return(IrisTypeToCilTypeName(type.GetElementType()) + "[]");
            }
            else if (type.IsByRef)
            {
                return(IrisTypeToCilTypeName(type.GetElementType()) + "&");
            }
            else if (type == IrisType.Void)
            {
                return("void");
            }

            return("_Unknown");
        }
        private void InitializeVariableIfNeeded(FilePosition fp, Variable variable)
        {
            IrisType varType = variable.Type;

            if (varType.IsArray || varType == IrisType.String)
            {
                // Variable needs to be initialized.
                Symbol varSymbol = _symbolTable.Lookup(variable.Name);
                if (varType.IsArray)
                {
                    MethodGenerator.InitArray(varSymbol, variable.SubRange);
                    if (varType.GetElementType() == IrisType.String)
                    {
                        // String arary - initialize all elements
                        EmitLoadSymbol(varSymbol, SymbolLoadMode.Raw);
                        Symbol initProc = LookupSymbol(fp, "$.initstrarray");
                        MethodGenerator.Call(initProc);
                    }
                }
                else
                {
                    // String
                    Symbol emptyStr = LookupSymbol(fp, "$.emptystr");
                    MethodGenerator.PushGlobal(emptyStr);
                    EmitStoreSymbol(varSymbol);
                }

                MethodGenerator.EmitDeferredInstructions();
            }
        }
Exemple #3
0
        private string TryFormatValue(DkmClrValue value, DkmInspectionContext inspectionContext)
        {
            if (value.ValueFlags.HasFlag(DkmClrValueFlags.Error))
            {
                // Error message.  Just show the error.
                return(value.HostObjectValue as string);
            }
            else if (value.IsNull)
            {
                return("<uninitialized>");
            }

            Type     lmrType  = value.Type.GetLmrType();
            IrisType irisType = Utility.GetIrisTypeForLmrType(lmrType);

            if (irisType == IrisType.Invalid)
            {
                // We don't know how to format this value
                return(null);
            }

            uint radix = inspectionContext.Radix;

            if (irisType.IsArray)
            {
                SubRange subrange = new SubRange(value.ArrayLowerBounds.First(), value.ArrayDimensions.First() - 1);
                return(string.Format(
                           "array[{0}..{1}] of {2}",
                           FormatInteger(subrange.From, radix),
                           FormatInteger(subrange.To, radix),
                           irisType.GetElementType()));
            }

            object hostObjectValue = value.HostObjectValue;

            if (hostObjectValue != null)
            {
                // If the value can be marshalled into the debugger process, HostObjectValue is the
                // equivalent value in the debugger process.
                switch (System.Type.GetTypeCode(hostObjectValue.GetType()))
                {
                case TypeCode.Int32:
                    return(FormatInteger((int)hostObjectValue, radix));

                case TypeCode.Boolean:
                    return((bool)hostObjectValue ? "true" : "false");

                case TypeCode.String:
                    return(FormatString(hostObjectValue.ToString(), inspectionContext.EvaluationFlags));
                }
            }

            return(null);
        }
        private static string TryGetFrameNameHelper(DkmInspectionContext inspectionContext, DkmStackWalkFrame frame, DkmVariableInfoFlags argumentFlags)
        {
            ImportedMethod currentMethod = TryGetCurrentMethod(inspectionContext, frame);

            if (currentMethod == null)
            {
                return(null);
            }

            string name = currentMethod.Name;

            if (string.Equals(name, "$.main", StringComparison.Ordinal))
            {
                return("<Main Block>");
            }

            if (argumentFlags == DkmVariableInfoFlags.None)
            {
                return(name);
            }

            Variable[] args = currentMethod.GetParameters();
            if (args.Length == 0)
            {
                return(name);
            }

            StringBuilder nameBuilder = new StringBuilder();

            nameBuilder.Append(name);
            nameBuilder.Append('(');

            bool first     = true;
            bool showTypes = argumentFlags.HasFlag(DkmVariableInfoFlags.Types);
            bool showNames = argumentFlags.HasFlag(DkmVariableInfoFlags.Names);

            foreach (Variable arg in args)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    nameBuilder.Append("; ");
                }

                IrisType argType = arg.Type;
                if (argType.IsByRef)
                {
                    nameBuilder.Append("var ");
                    argType = argType.GetElementType();
                }

                if (showNames)
                {
                    nameBuilder.Append(arg.Name);
                }

                if (showNames && showTypes)
                {
                    nameBuilder.Append(" : ");
                }

                if (showTypes)
                {
                    nameBuilder.Append(argType);
                }
            }

            nameBuilder.Append(')');
            return(nameBuilder.ToString());
        }
        private static string IrisTypeToCilTypeName(IrisType type)
        {
            if (type.IsPrimitive)
            {
                if (type == IrisType.Integer)
                    return "int32";
                else if (type == IrisType.String)
                    return "string";
                else if (type == IrisType.Boolean)
                    return "bool";
            }
            else if (type.IsArray)
            {
                return IrisTypeToCilTypeName(type.GetElementType()) + "[]";
            }
            else if (type.IsByRef)
            {
                return IrisTypeToCilTypeName(type.GetElementType()) + "&";
            }
            else if (type == IrisType.Void)
            {
                return "void";
            }

            return "_Unknown";
        }
Exemple #6
0
        public override void TranslateInput()
        {
            _context.Emitter.BeginProgram(_context.ClassName, _context.Importer.ImportedAssemblies);
            MethodGenerator.BeginMethod(_context.MethodName, IrisType.Void, _context.ParameterVariables, _context.LocalVariables, false, string.Empty);

            // Parse the L-Value.
            // We use a seperate lexer for this because it's part of a different string.
            Symbol lvalue;

            byte[]       buffer = Encoding.Default.GetBytes(_context.AssignmentLValue);
            MemoryStream input  = new MemoryStream(buffer);

            using (StreamReader reader = new StreamReader(input))
            {
                Lexer lvalLexer = Lexer.Create(reader, _context.CompileErrors);
                lvalue = ParseLValue(lvalLexer);
                if (lvalue == null)
                {
                    return; // Parsing the L-Value failed.  (Error message already generated)
                }
            }

            IrisType lhs = lvalue.Type;

            // Parse the R-Value.
            IrisType rhs = ParseExpression();

            if (!Accept(Token.Eof))
            {
                AddErrorAtTokenStart("Unexpected text after expression.");
            }

            // Now finish emitting the code to do the assignment.
            if (rhs != IrisType.Invalid)
            {
                bool hasElementType = false;
                if (lhs.IsByRef || lhs.IsArray)
                {
                    hasElementType = true;
                    lhs            = lhs.GetElementType();
                }

                if (lhs != rhs)
                {
                    AddErrorAtLastParsedPosition("Cannot assign value.  Expression type doesn't match value type");
                }
                else if (hasElementType)
                {
                    if (lvalue.Type.IsArray)
                    {
                        MethodGenerator.StoreElement(lhs);
                    }
                    else
                    {
                        MethodGenerator.Store(lhs);
                    }
                }
                else
                {
                    EmitStoreSymbol(lvalue);
                }
            }

            if (_context.ErrorCount == 0)
            {
                MethodGenerator.EndMethod();
                _context.Emitter.EndProgram();
            }
        }
 protected IrisType DerefType(IrisType type)
 {
     return type.IsByRef ? type.GetElementType() : type;
 }
        protected void ParseStatement(bool allowEmpty = false)
        {
            FilePosition statementStart = _lexer.TokenStartPosition;

            MethodGenerator.BeginSourceLine(statementStart);
            if (Accept(Token.KwFor))
            {
                Symbol       iterator;
                FilePosition fp = _lexer.TokenStartPosition;
                if (!Accept(Token.Identifier))
                {
                    AddError(fp, "Expecting integer identifier.");
                    SkipStatement();
                    return;
                }

                // Initial assignment
                iterator = LookupSymbol(fp, _lexeme);
                VerifyExpressionType(fp, DerefType(iterator.Type), IrisType.Integer);
                bool byRef = iterator.Type.IsByRef;
                if (byRef)
                {
                    EmitLoadSymbol(iterator, SymbolLoadMode.Raw);
                }

                Expect(Token.ChrAssign);
                fp = _lexer.TokenStartPosition;
                IrisType rhs = ParseExpression();
                VerifyExpressionType(fp, rhs, IrisType.Integer);

                if (byRef)
                {
                    MethodGenerator.Store(rhs);
                }
                else
                {
                    EmitStoreSymbol(iterator);
                }

                Expect(Token.KwTo);

                // Loop start and condition
                int loopLabel = GetNextLabel();
                MethodGenerator.Label(loopLabel);
                EmitLoadSymbol(iterator, SymbolLoadMode.Dereference);
                fp  = _lexer.TokenStartPosition;
                rhs = ParseExpression();
                VerifyExpressionType(fp, rhs, IrisType.Integer);
                int exitLabel = GetNextLabel();
                MethodGenerator.BranchCondition(Operator.GreaterThan, exitLabel);

                // Loop body
                Expect(Token.KwDo);
                FilePosition forEndPosition = _lastParsedPosition;
                MethodGenerator.EndSourceLine(forEndPosition);
                ParseStatement();

                // Loop end
                MethodGenerator.BeginSourceLine(statementStart); // Source position is the same as the loop start.
                Increment(iterator);
                MethodGenerator.Goto(loopLabel);
                MethodGenerator.Label(exitLabel);
                MethodGenerator.EndSourceLine(forEndPosition);
            }
            else if (Accept(Token.KwWhile))
            {
                int loopLabel = GetNextLabel();
                MethodGenerator.Label(loopLabel);

                FilePosition fp   = _lexer.TokenStartPosition;
                IrisType     type = ParseExpression();
                VerifyExpressionType(fp, type, IrisType.Boolean);
                int exitLabel = GetNextLabel();
                MethodGenerator.BranchFalse(exitLabel);

                Expect(Token.KwDo);

                MethodGenerator.EndSourceLine(_lastParsedPosition);

                ParseStatement();
                MethodGenerator.Goto(loopLabel);
                MethodGenerator.Label(exitLabel);
            }
            else if (Accept(Token.KwRepeat))
            {
                int loopLabel = GetNextLabel();
                MethodGenerator.Label(loopLabel);
                MethodGenerator.EmitNonCodeLineInfo(new SourceRange(statementStart, _lastParsedPosition));

                ParseStatements(Token.KwUntil);

                FilePosition fp   = _lexer.TokenStartPosition;
                IrisType     type = ParseExpression();
                VerifyExpressionType(fp, type, IrisType.Boolean);
                MethodGenerator.BranchFalse(loopLabel);

                MethodGenerator.EndSourceLine(_lastParsedPosition);
            }
            else if (Accept(Token.KwIf))
            {
                ParseIf();
            }
            else if (Accept(Token.KwBegin))
            {
                MethodGenerator.EmitNonCodeLineInfo(new SourceRange(statementStart, _lastParsedPosition));
                ParseStatements(Token.KwEnd);
            }
            else if (Accept(Token.Identifier))
            {
                FilePosition fp         = _lexer.TokenStartPosition;
                string       symbolName = _lexeme;
                Symbol       symbol     = LookupSymbol(fp, symbolName);
                IrisType     lhs        = symbol.Type;
                bool         assign     = false;
                bool         isArray    = false;
                if (Accept(Token.ChrOpenBracket))
                {
                    // Assignment to an array element.
                    isArray = true;
                    lhs     = ProcessArrayAccess(fp, symbol, SymbolLoadMode.Raw);
                }
                if (Accept(Token.ChrAssign))
                {
                    assign = true;
                    bool indirectAssign = false;

                    if (lhs.IsByRef)
                    {
                        lhs = lhs.GetElementType();
                        EmitLoadSymbol(symbol, SymbolLoadMode.Raw);
                        indirectAssign = true;
                    }

                    FilePosition exprPosition = _lexer.TokenStartPosition;
                    IrisType     rhs          = ParseExpression();

                    if (lhs.IsMethod)
                    {
                        AddError(fp, "Cannot assign to result of function or procedure call.");
                    }
                    else if (lhs != IrisType.Invalid)
                    {
                        if (rhs == IrisType.Void)
                        {
                            AddError(fp, "Cannot use procedure in assignment statement.");
                        }
                        else if (rhs != IrisType.Invalid && rhs != lhs)
                        {
                            AddError(exprPosition, string.Format("Cannot assign to '{0}' (type mismatch error).", symbolName));
                        }

                        if (isArray)
                        {
                            MethodGenerator.StoreElement(lhs);
                        }
                        else if (indirectAssign)
                        {
                            MethodGenerator.Store(lhs);
                        }
                        else
                        {
                            EmitStoreSymbol(symbol);
                        }
                    }
                }
                else if (isArray)
                {
                    // This is an array subscript.  Assignment is the only kind of statement that
                    // starts with an array subscript.
                    AddErrorAtTokenStart("Expecting ':='.");
                    SkipStatement();
                }

                if (!assign && !isArray)
                {
                    bool skipArgList = !Accept(Token.ChrOpenParen);
                    ProcessCall(fp, symbol, skipArgList);

                    if (symbol.Type.IsFunction)
                    {
                        MethodGenerator.Pop();
                    }
                }

                MethodGenerator.EndSourceLine(_lastParsedPosition);
            }
            else if (Accept(Token.KwElse))
            {
                AddErrorAtTokenStart("Cannot start statement with 'else' or unexpected ';' after if statement.");
                SkipStatement();
            }
            else if (!allowEmpty && !Accept(Token.ChrSemicolon))
            {
                AddErrorAtLastParsedPosition("Expecting statement.");
                SkipStatement();
            }
        }
 protected IrisType DerefType(IrisType type)
 {
     return(type.IsByRef ? type.GetElementType() : type);
 }