Antarr Byrd
Antarr Byrd

Reputation: 26071

Unhandled exception, unable to debug

I'm trying to debug my c# application that check MIPS syntax. But its not allowing be to debug it. No matter where I enter my break point it gets ignored, including the first line of the Main() function. Its also throwing me this error. 'add a b c' works fine if i don't call HasValidParams() 'add a b' throws exception in the same situation neither works when calling HasValidParams()

enter image description here

program.cs

private static void Main(string[] args)
        {
            var validator = new MipsValidator();
            Console.Write("Please enter a MIPS statement: ");
            string input = Console.ReadLine();
            List<string> arguments = input.Split(new char[0]).ToList();
            Response status = validator.IsSyntaxValid(arguments);
            //Check syntax
            if (status.Success.Equals(true))
            {
                Response stat = validator.HasValidParams(arguments);
                //Check parameters
                if (stat.Success.Equals(true))
                {
                Console.WriteLine(string.Format("'{0}' is a valid mips instruction ", input));
                }
                else
                {
                    foreach (var reason in stat.Reasons)
                    {
                        Console.WriteLine(reason);
                    }
                }
            }
            else
            {
                foreach (string reason in status.Reasons)
                {
                    Console.WriteLine(reason);
                }
            }
        }

mips-validator.cs

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace mips_validator.utils
{
    public class MipsValidator : IMipsValidator
    {
        #region Implementation of IMipsValidator

        public Response IsSyntaxValid(List<string> args)
        {
            var response = new Response {Success = true};
            var op = (Operator) Enum.Parse(typeof (Operator), args[0]);
            switch (op)
            {
                case Operator.addi:
                case Operator.add:
                case Operator.beq:
                    if (args.Count != 4)
                    {
                        response.Reasons.Add(string.Format("4 operands required for {0}, {1} parameters provided.",
                                                           op, args.Count));
                        response.Success = false;
                    }
                    break;
                case Operator.j:
                    if (args.Count != 2)
                    {
                        response.Reasons.Add(string.Format("1 operands required for {1}, {0} parameters provided.",
                                                           args.Count, op));
                        response.Success = false;
                    }
                    break;
                default:
                    response.Reasons.Add(string.Format("{0} is an unknown mips operation", op));
                    response.Success = false;
                    break;
            }
            return response;
        }

        public Response HasValidParams(List<string> parameters)
        {
            string op1, op2, op3;
            var temporary = new Regex(@"/\$t\d+/");
            var store = new Regex(@"/\$s\d+/");
            var zero = new Regex(@"/\$zero/");
            var osReserved = new Regex(@"/\$k0|1/");
            var memory = new Regex(@"");
            var constant = new Regex(@"/-?\d*/");
            var label = new Regex(@"/.*\:/");
            Operator operation;
            var response = new Response {Success = true};
            string opString = parameters[0];
            Enum.TryParse(opString.Replace("$", string.Empty), true, out operation);
            switch (operation)
            {
                case Operator.add:
                    {
                        op1 = parameters[1];
                        op2 = parameters[2];
                        if (!temporary.IsMatch(op1) && !store.IsMatch(op1) && !zero.IsMatch(op1))
                        {
                            response.Reasons.Add(string.Format("{0}: error register expected", op1));
                            response.Success = false;
                        }
                        if (!temporary.IsMatch(op2) && !store.IsMatch(op2) && !zero.IsMatch(op2))
                        {
                            response.Reasons.Add(string.Format("{0}: error register expected", op2));
                            response.Success = false;
                        }
                    }
                    break;
                case Operator.addi:
                    {
                        op1 = parameters[1];
                        op2 = parameters[2];
                        if (!temporary.IsMatch(op1) && !store.IsMatch(op1) && !zero.IsMatch(op1))
                        {
                            response.Reasons.Add(string.Format("{0}: error register expected", op1));
                            response.Success = false;
                        }
                        if (!constant.IsMatch(op2) && !zero.IsMatch(op2))
                        {
                            response.Reasons.Add(string.Format("{0}: error constant expected", op2));
                            response.Success = false;
                        }
                    }
                    break;
                case Operator.beq:
                    {
                        op1 = parameters[1];
                        op2 = parameters[2];
                        op3 = parameters[3];
                        if (!temporary.IsMatch(op1) && !store.IsMatch(op1) && !zero.IsMatch(op1))
                        {
                            response.Reasons.Add(string.Format("{0}:  error register expected", op1));
                            response.Success = false;
                        }
                        if (!temporary.IsMatch(op2) && !store.IsMatch(op2) && !zero.IsMatch(op2))
                        {
                            response.Reasons.Add(string.Format("{0}: error register expected", op2));
                            response.Success = false;
                        }
                        if (!label.IsMatch(op3) && !constant.IsMatch(op3))
                        {
                            response.Reasons.Add(string.Format("{0}: error label or constant expected", op3));
                            response.Success = false;
                        }
                    }
                    break;
            }

            return response;
        }

        #endregion
    }
}

SOLUTION------- Response.cs(old)

public class Response
{
    public List<string> Reasons;
    public bool Success = true;

}

Response.cs(current)

 public class Response
    {
        public Response()
        {
            Reasons = new List<string>();
            Success = true;
        }
        public List<string> Reasons;
        public bool Success = true;

    }

Upvotes: 0

Views: 340

Answers (3)

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

You're not showing the Response class, so make sure Reasons is actually set to a collection you can add to and not left to the default, null.

Edit: The below possible cause for a crash was pointed put by @nodakai not to be one at all; turns out an empty char array is a special case to split on whitespace.

*You calculate arguments by doing List arguments = input.Split(new char[0]).ToList(); ...which as far as I can tell does absolutely nothing except put the original string inside a List. You probably want to split on new char[] {' '} instead to split on spaces.*

Upvotes: 2

M.Babcock
M.Babcock

Reputation: 18965

I can't tell if you're looking for a way to be able to debug your project or if you'd prefer to be told potential issues in your code.

For the latter:

Make sure Response.Reasons is initialized by the constructor of Response (or a field initializer).

Upvotes: 3

CodeZombie
CodeZombie

Reputation: 5377

Check if your breakpoint looks like this:

enter image description here

If it does, your source code differs from the code the assembly was actually compiled with. Make sure your project is built properly (right click on the solution and select "Rebuild") and check your current configuration:

enter image description here

Hope this helps...

Upvotes: 1

Related Questions