Skip to content

Commit

Permalink
Add positional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Sep 17, 2024
1 parent c7aba54 commit bf75d6d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
53 changes: 46 additions & 7 deletions mshell/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ static int Main(string[] args)
bool printLex = false;
string? input = null;

List<string> positionalArgs = new List<string>();
while (i < args.Length)
{
if (args[i] == "--lex")
var arg = args[i];
i++;
if (arg == "--lex")
{
printLex = true;
}
else if (input is not null)
{
positionalArgs.Add(arg);
}
else
{
input = File.ReadAllText(args[i]);
input = File.ReadAllText(arg);
}

i++;
}

input ??= Console.In.ReadToEnd();
Expand All @@ -47,7 +52,7 @@ static int Main(string[] args)
return 0;
}

Evaluator e = new(false);
Evaluator e = new(false, positionalArgs);
EvalResult result = e.Evaluate(tokens, new Stack<MShellObject>(), new ExecuteContext());

return result.Success ? 0 : 1;
Expand Down Expand Up @@ -118,11 +123,25 @@ private TokenNew ScanToken()
return MakeToken(TokenType.PIPE);
case '?':
return MakeToken(TokenType.QUESTION);
case '$':
return char.IsDigit(Peek()) ? ParsePositional() : ParseLiteralOrNumber();
default:
return ParseLiteralOrNumber();
}
}

private TokenNew ParsePositional()
{
while (true)
{
if (AtEnd()) break;
var c = Advance();
if (!char.IsDigit(c)) break;
}

return MakeToken(TokenType.POSITIONAL);
}

private TokenNew ParseString()
{
// When this is called, we've already consumed a single double quote.
Expand Down Expand Up @@ -233,15 +252,17 @@ private void EatWhitespace()

public class Evaluator
{
private readonly List<string> _positionalArgs;
private readonly Action<MShellObject, Stack<MShellObject>> _push;
private int _loopDepth = 0;

private Dictionary<string, MShellObject> _variables = new();

// private Stack<Stack<MShellObject>> _stack = new();

public Evaluator(bool debug)
public Evaluator(bool debug, List<string> positionalArgs)
{
_positionalArgs = positionalArgs;
_push = debug ? PushWithDebug : PushNoDebug;
// _tokens = tokens;
// Stack<Stack<MShellObject>> stack = new();
Expand Down Expand Up @@ -903,6 +924,23 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
}
}
}
else if (t.TokenType == TokenType.POSITIONAL)
{
int positionalNum = int.Parse(t.RawText[1..]);
if (positionalNum < 1)
{
return FailWithMessage($"Found positional argument less than 1 ({t.RawText}). Positional arguments start at 1.\n");
}

if (positionalNum > _positionalArgs.Count)
{
return FailWithMessage($"Found positional argument greater than total supplied ({t.RawText}, {_positionalArgs.Count} supplied).\n");
}

MShellString s = new MShellString(_positionalArgs[positionalNum - 1]);
_push(s, stack);
index++;
}
else
{
return FailWithMessage($"Token type '{t.TokenType}' (Raw Token: '{t.RawText}') not implemented yet.\n");
Expand Down Expand Up @@ -1179,7 +1217,8 @@ public enum TokenType
PLUS,
PIPE,
INTERPRET,
QUESTION
QUESTION,
POSITIONAL
}

public class TokenNew
Expand Down
2 changes: 2 additions & 0 deletions mshell/positional.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[echo "Number 1: " $1 +];
[echo "Number 2: " $2 +];

0 comments on commit bf75d6d

Please sign in to comment.