Skip to content

Commit

Permalink
Consolidate error returns
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Sep 17, 2024
1 parent e14ff8f commit 91acd39
Showing 1 changed file with 27 additions and 55 deletions.
82 changes: 27 additions & 55 deletions mshell/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
var currToken = tokens[index];
if (index >= tokens.Count || tokens[index].TokenType == TokenType.EOF)
{
Console.Error.Write($"{currToken.Line}:{currToken.Column}: Found unbalanced bracket.\n");
return FailResult();
return FailWithMessage($"{currToken.Line}:{currToken.Column}: Found unbalanced bracket.\n");
}

if (tokens[index].TokenType == TokenType.LEFT_SQUARE_BRACKET)
Expand All @@ -336,8 +335,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
if (!result.Success) return result;
if (result.BreakNum > 0)
{
Console.Error.Write("Encountered break within list.\n");
return FailResult();
return FailWithMessage("Encountered break within list.\n");
}

MShellList l = new(listStack.Reverse());
Expand All @@ -350,8 +348,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write($"{currToken.Line}:{currToken.Column}: Found unbalanced square bracket.\n");
return FailResult();
return FailWithMessage($"{currToken.Line}:{currToken.Column}: Found unbalanced square bracket.\n");
}

}
Expand All @@ -366,8 +363,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else if (t.TokenType == TokenType.RIGHT_SQUARE_BRACKET)
{
Console.Error.Write($"{t.Line}:{t.Column}: Found unbalanced list.\n");
return FailResult();
return FailWithMessage($"{t.Line}:{t.Column}: Found unbalanced list.\n");
}
else if (t.TokenType == TokenType.LEFT_PAREN)
{
Expand All @@ -378,8 +374,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
{
if (index >= tokens.Count || tokens[index].TokenType == TokenType.EOF)
{
Console.Error.Write("Found unbalanced bracket.\n");
return FailResult();
return FailWithMessage("Found unbalanced bracket.\n");
}

if (tokens[index].TokenType == TokenType.LEFT_PAREN)
Expand All @@ -404,8 +399,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write("Found unbalanced quotation.\n");
return FailResult();
return FailWithMessage("Found unbalanced quotation.\n");
}
}
else
Expand All @@ -418,8 +412,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else if (t.TokenType == TokenType.RIGHT_PAREN)
{
Console.Error.Write("Unbalanced parenthesis found.\n");
return FailResult();
return FailWithMessage("Unbalanced parenthesis found.\n");
}
else if (t.TokenType == TokenType.IF)
{
Expand All @@ -429,8 +422,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
{
if (qList.Items.Count < 2)
{
Console.Error.Write("Quotation list for if should have a minimum of 2 elements.\n");
return FailResult();
return FailWithMessage("Quotation list for if should have a minimum of 2 elements.\n");
}

if (qList.Items.Any(i => !i.IsQuotation))
Expand All @@ -441,7 +433,6 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
Console.Error.Write(i.TypeName());
Console.Error.Write('\n');
}

return FailResult();
}

Expand All @@ -454,8 +445,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
if (!result.Success) return FailResult();
if (result.BreakNum > 0)
{
Console.Error.Write("Found break during evaluation of if condition.\n");
return FailResult();
return FailWithMessage("Found break during evaluation of if condition.\n");
}

if (stack.TryPop(out var condition))
Expand Down Expand Up @@ -484,8 +474,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write("Evaluation of condition quotation removed all stacks.");
return FailResult();
return FailWithMessage("Evaluation of condition quotation removed all stacks.");
}
}

Expand All @@ -494,8 +483,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
// Run the quotation on the index after the true index
if (!qList.Items[trueIndex + 1].IsQuotation)
{
Console.Error.Write($"True branch of if statement must be quotation. Received a {qList.Items[trueIndex + 1].TypeName()}");
return FailResult();
return FailWithMessage($"True branch of if statement must be quotation. Received a {qList.Items[trueIndex + 1].TypeName()}.\n");
}

MShellQuotation q = qList.Items[trueIndex + 1].AsQuotation;
Expand All @@ -509,8 +497,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
// Run the last quotation if there was no true condition.
if (!qList.Items[^1].IsQuotation)
{
Console.Error.Write($"Else branch of if statement must be quotation. Received a {qList.Items[^1].TypeName()}");
return FailResult();
return FailWithMessage($"Else branch of if statement must be quotation. Received a {qList.Items[^1].TypeName()}.\n");
}

MShellQuotation q = qList.Items[^1].AsQuotation;
Expand All @@ -522,14 +509,12 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write("Argument for if expected to be a list of quotations.\n");
return FailResult();
return FailWithMessage("Argument for if expected to be a list of quotations.\n");
}
}
else
{
Console.Error.Write("Nothing on stack for if.\n");
return FailResult();
return FailWithMessage("Nothing on stack for if.\n");
}

index++;
Expand Down Expand Up @@ -587,22 +572,19 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe

if (loopCount >= 15000)
{
Console.Error.Write("Looks like infinite loop.\n");
return FailResult();
return FailWithMessage("Looks like infinite loop.\n");
}

index++;
}
else
{
Console.Error.Write($"{t.Line}:{t.Column}: Expected quotation on top of stack for 'loop'.\n");
return FailResult();
return FailWithMessage($"{t.Line}:{t.Column}: Expected quotation on top of stack for 'loop'.\n");
}
}
else
{
Console.Error.Write($"{t.Line}:{t.Column}: Quotations expected on stack for 'loop'.\n");
return FailResult();
return FailWithMessage($"{t.Line}:{t.Column}: Quotations expected on stack for 'loop'.\n");
}
}
else if (t.TokenType == TokenType.BREAK)
Expand All @@ -614,8 +596,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
{
if (stack.Count < 2)
{
Console.Error.Write($"{stack} tokens on the stack are not enough for the '=' operator.\n");
return FailResult();
return FailWithMessage($"{stack} tokens on the stack are not enough for the '=' operator.\n");
}

var arg1 = stack.Pop();
Expand All @@ -627,8 +608,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write($"'=' is only currently implemented for integers. Received a {arg1.TypeName()} {arg1.DebugString()} {arg2.TypeName()} {arg2.DebugString()}\n");
return FailResult();
return FailWithMessage($"'=' is only currently implemented for integers. Received a {arg1.TypeName()} {arg1.DebugString()} {arg2.TypeName()} {arg2.DebugString()}\n");
}

index++;
Expand All @@ -637,8 +617,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
{
if (stack.Count < 1)
{
Console.Error.Write($"No tokens found on the stack for the 'not' operator.\n");
return FailResult();
return FailWithMessage($"No tokens found on the stack for the 'not' operator.\n");
}

var arg = stack.Pop();
Expand All @@ -648,17 +627,15 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write($"Not operator only implemented for boolean variables. Found a {arg.TypeName()} on top of the stack..\n");
return FailResult();
return FailWithMessage($"Not operator only implemented for boolean variables. Found a {arg.TypeName()} on top of the stack..\n");
}
index++;
}
else if (t.TokenType == TokenType.AND)
{
if (stack.Count < 2)
{
Console.Error.Write($"Not enough tokens on the stack for the 'and' operator.\n");
return FailResult();
return FailWithMessage($"Not enough tokens on the stack for the 'and' operator.\n");
}

var arg1 = stack.Pop();
Expand All @@ -670,17 +647,15 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write($"'and' operator only implemented for boolean variables. Found a {arg1.TypeName()} and a {arg2.TypeName()} on top of the stack.\n");
return FailResult();
return FailWithMessage($"'and' operator only implemented for boolean variables. Found a {arg1.TypeName()} and a {arg2.TypeName()} on top of the stack.\n");
}
index++;
}
else if (t.TokenType == TokenType.OR)
{
if (stack.Count < 2)
{
Console.Error.Write($"Not enough tokens on the stack for the 'or' operator.\n");
return FailResult();
return FailWithMessage($"Not enough tokens on the stack for the 'or' operator.\n");
}

var arg1 = stack.Pop();
Expand All @@ -692,8 +667,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write($"'or' operator only implemented for boolean variables. Found a {arg1.TypeName()} and a {arg2.TypeName()} on top of the stack.\n");
return FailResult();
return FailWithMessage($"'or' operator only implemented for boolean variables. Found a {arg1.TypeName()} and a {arg2.TypeName()} on top of the stack.\n");
}
index++;
}
Expand All @@ -720,8 +694,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write($"Nothing on stack to store into variable '{t.RawText.Substring(1, t.RawText.Length - 1)}'.\n");
return FailResult();
return FailWithMessage($"Nothing on stack to store into variable '{t.RawText.Substring(1, t.RawText.Length - 1)}'.\n");
}

index++;
Expand Down Expand Up @@ -884,8 +857,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write($"Token type '{t.TokenType}' (Raw Token: '{t.RawText}') not implemented yet.\n");
return FailResult();
return FailWithMessage($"Token type '{t.TokenType}' (Raw Token: '{t.RawText}') not implemented yet.\n");
// throw new NotImplementedException($"Token type {t.TokenType()} not implemented yet.");
}
}
Expand Down

0 comments on commit 91acd39

Please sign in to comment.