Skip to content

Commit

Permalink
Add '?' operator to be the one that leaves exit code on the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Sep 17, 2024
1 parent 3cdf0d4 commit e14ff8f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
40 changes: 33 additions & 7 deletions mshell/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ private TokenNew ScanToken()
return MakeToken(TokenType.EXECUTE);
case '|':
return MakeToken(TokenType.PIPE);
case '?':
return MakeToken(TokenType.QUESTION);
default:
return ParseLiteralOrNumber();
}
Expand Down Expand Up @@ -157,7 +159,7 @@ private TokenNew ParseLiteralOrNumber()
while (true)
{
char c = Peek();
if (!char.IsWhiteSpace(c) && c != ']' && c != ')' && c != '<' && c != '>' && c != ';') Advance();
if (!char.IsWhiteSpace(c) && c != ']' && c != ')' && c != '<' && c != '>' && c != ';' && c != '?') Advance();
else break;
}

Expand Down Expand Up @@ -279,6 +281,23 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else return FailWithMessage($"Nothing on stack to duplicate for 'dup'.\n");
}
else if (t.RawText == "drop")
{
if (!stack.TryPop(out MShellObject? _))
{
return FailWithMessage($"Nothing on stack to drop.\n");
}
}
else if (t.RawText == ".s")
{
Console.Error.Write("Current stack:");

foreach (var v in stack)
{
Console.Error.Write(v.DebugString());
Console.Error.Write('\n');
}
}
else
{
_push(new LiteralToken(t), stack);
Expand Down Expand Up @@ -460,7 +479,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
else
{
Console.Error.Write($"Can't evaluate condition for type.\n");
return FailWithMessage($"Can't evaluate condition for type {condition.TypeName()}.\n");
}
}
else
Expand Down Expand Up @@ -515,7 +534,7 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe

index++;
}
else if (t.TokenType == TokenType.EXECUTE)
else if (t.TokenType == TokenType.EXECUTE || t.TokenType == TokenType.QUESTION)
{
if (stack.TryPop(out var arg))
{
Expand All @@ -531,7 +550,10 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe

if (!result.Success) return result;

_push(new IntToken(new TokenNew(t.Line, t.Column, t.Start, exitCode.ToString(), TokenType.INTEGER)), stack);
if (t.TokenType == TokenType.QUESTION)
{
_push(new IntToken(new TokenNew(t.Line, t.Column, t.Start, exitCode.ToString(), TokenType.INTEGER)), stack);
}
}
else
{
Expand Down Expand Up @@ -918,6 +940,7 @@ private void ExecuteQuotation(MShellQuotation q)
StartInfo = info
};

int exitCode;
try
{
using (p)
Expand Down Expand Up @@ -947,17 +970,17 @@ private void ExecuteQuotation(MShellQuotation q)
}

p.WaitForExit();

// Console.Out.Write(stdout);
// Console.Error.Write(stderr);
exitCode = p.ExitCode;
}
}
catch (Exception e)
{
return (FailWithMessage($"There was an exception running process with args { string.Join(", ", arguments.Select(s => $"'{s}'")) }.\n{e.Message}\n"), 1);
}

return (new EvalResult(true, -1), p.ExitCode);
return (new EvalResult(true, -1), exitCode);
}

public (EvalResult, int) RunProcess(MShellPipe pipe, ExecuteContext context)
Expand Down Expand Up @@ -1064,8 +1087,10 @@ private void ExecuteQuotation(MShellQuotation q)
}

foreach (var p in processes) p.WaitForExit();
var exitCodes = processes.Select(process => process.ExitCode).ToList();
foreach (var p in processes) p.Dispose();

return (new EvalResult(true, -1), 1);
return (new EvalResult(true, -1), exitCodes.Last());
}

public byte[] ReadAllBytesFromStream(Stream stream)
Expand Down Expand Up @@ -1120,6 +1145,7 @@ public enum TokenType
PLUS,
PIPE,
INTERPRET,
QUESTION
}

public class TokenNew
Expand Down
7 changes: 7 additions & 0 deletions mshell/tests/if_on_process.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
# This is running the process command false. The '?' pushes the exit code on the stack when finished.
# 0 results in the true branch in mshell.
(["false"]?)
([echo "The command run had a successful exit code"];)
([echo "The command run had an unsuccessful code"];)
] if
1 change: 1 addition & 0 deletions mshell/tests/if_on_process.msh.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The command run had an unsuccessful code

0 comments on commit e14ff8f

Please sign in to comment.