/// <summary> /// Parses a for command /// </summary> private ICommandNode ParseForCommand() { Debugger.Write("Parsing For Command"); Position startPosition = CurrentToken.Position; Accept(TokenType.For); IdentifierNode identifier = ParseIdentifier(); // Declare I as a new variable of type integer with an undefined value VarDeclarationNode declaration = new VarDeclarationNode(identifier, new TypeDenoterNode( new IdentifierNode( new Token(TokenType.Identifier, "Integer", startPosition))), startPosition); // Evaluate E1 Accept(TokenType.Becomes); IExpressionNode becomesExpression = ParseExpression(); // Assign I to the value of E1 AssignCommandNode assign = new AssignCommandNode(identifier, becomesExpression); // Evaluate E2 Accept(TokenType.To); IExpressionNode toExpression = ParseExpression(); Accept(TokenType.Do); // Execute command ICommandNode command = ParseSingleCommand(); Accept(TokenType.Next); return(new ForCommandNode(declaration, assign, toExpression, command, startPosition)); }
/// <summary> /// Parses an if command /// </summary> /// <returns>An abstract syntax tree representing the if command</returns> private ICommandNode ParseIfCommand() { Debugger.Write("Parsing If Command"); Position startPosition = CurrentToken.Position; Accept(If); IExpressionNode expression = ParseBinaryExpression(); if (CurrentToken.Type == LeftBracket) { Accept(LeftBracket); expression = ParseBinaryExpression(); Accept(RightBracket); } Accept(Then); ICommandNode thenCommand = ParseSingleCommand(); if (CurrentToken.Type == Else) { Accept(Else); ICommandNode elseCommand = ParseSingleCommand(); Accept(EndIf); return(new IfElseCommandNode(expression, thenCommand, elseCommand, startPosition)); } Accept(EndIf); return(new IfCommandNode(expression, thenCommand, startPosition)); }
public ForCommandNode(ICommandNode assign, IExpressionNode expression, ICommandNode command, Position position) { Assign = assign; Expression = expression; Command = command; Position = position; }
/// <summary> /// Parses a program /// </summary> /// <returns>An abstract syntax tree representing the program</returns> private ProgramNode ParseProgram() { Debugger.Write("Parsing program"); ICommandNode command = ParseCommand(); return(new ProgramNode(command)); }
/// <summary> /// Parses an if command /// </summary> /// <returns>An abstract syntax tree representing the if command</returns> private ICommandNode ParseIfCommand() { Debugger.WriteDebuggingInfo("Parsing If Command"); Position startPosition = CurrentToken.Position; Accept(If); IExpressionNode expression = ParseExpression(); Accept(Then); ICommandNode thenCommand = ParseSingleCommand(); switch (CurrentToken.Type) { case Else: Accept(Else); ICommandNode elseCommand = ParseSingleCommand(); Accept(EndIf); return(new IfElseCommandNode(expression, thenCommand, elseCommand, startPosition)); } // ICommandNode thenCommand = ParseSingleCommand(); // Accept(EndIf); // ICommandNode endIfCommand = ParseSingleCommand(); // ICommandNode elseCommand = ParseSingleCommand(); Accept(EndIf); return(new IfCommandNode(expression, thenCommand, /*elseCommand,*/ startPosition)); }
private string[] GetCommandNames(ICommandNode node, string[] commandNames, string find) { var commandName = commandNames.FirstOrDefault() ?? string.Empty; if (commandName == string.Empty) { var query = from item in node.Childs where item.IsEnabled from name in new string[] { item.Name }.Concat(item.Aliases) where name.StartsWith(find) where name != this.Name orderby name select name; return(query.ToArray()); } else if (node.Childs.ContainsKey(commandName) == true) { return(this.GetCommandNames(node.Childs[commandName], commandNames.Skip(1).ToArray(), find)); } else if (node.ChildsByAlias.ContainsKey(commandName) == true) { return(this.GetCommandNames(node.ChildsByAlias[commandName], commandNames.Skip(1).ToArray(), find)); } return(null); }
/// <summary> /// Creates a new if command node /// </summary> /// <param name="expression">The condition expression</param> /// <param name="thenCommand">The then branch command</param> /// <param name="elseCommand">The else branch command</param> /// <param name="position">The position in the code where the content associated with the node begins</param> public IfElseCommandNode(IExpressionNode expression, ICommandNode thenCommand, ICommandNode elseCommand, Position position) { Expression = expression; ThenCommand = thenCommand; ElseCommand = elseCommand; Position = position; }
/// <summary> /// Parses an if command /// </summary> /// <returns>An abstract syntax tree representing the if command</returns> private ICommandNode ParseIfCommand() { Debugger.Write("Parsing If Command"); Position startPosition = CurrentToken.Position; Accept(If); IExpressionNode expression = ParseExpression(); Accept(Then); ICommandNode thenCommand = ParseSingleCommand(); if (CurrentToken.Type == Else) { Accept(Else); ICommandNode elseCommand = ParseSingleCommand(); return(new IfCommandNode(expression, thenCommand, elseCommand, startPosition)); } else if (CurrentToken.Type == Noelse) { Accept(Noelse); // made else command nullable return(new IfCommandNode(expression, thenCommand, null, startPosition)); } else { AddToErrorPositions(startPosition, "was expecting noelse or else, unknown node recieved"); return(new ErrorNode(startPosition)); } }
/// <summary> /// Creates a new if command node /// </summary> /// <param name="expression">The condition expression</param> /// <param name="thenCommand">The then branch command</param> /// <param name="elseCommand">The else branch command</param> /// <param name="position">The position in the code where the content associated with the node begins</param> public IfCommandNode(IExpressionNode expression, ICommandNode thenCommand, /*ICommandNode endIfCommand,/* ICommandNode elseCommand, */ Position position) { Expression = expression; ThenCommand = thenCommand; // ElseCommand = elseCommand; // EndIfCommand = endIfCommand; Position = position; }
/// <summary> /// Creates a new for node /// </summary> /// <param name="varDeclaration"></param> /// <param name="assignCommand"></param> /// <param name="toExpression"></param> /// <param name="command"></param> /// <param name="position"></param> public ForCommandNode(VarDeclarationNode varDeclaration, AssignCommandNode assignCommand, IExpressionNode toExpression, ICommandNode command, Position position) { VarDeclaration = varDeclaration; AssignCommand = assignCommand; ToExpression = toExpression; Command = command; Position = position; }
/// <summary> /// Parses a program /// </summary> /// <returns>An abstract syntax tree representing the program</returns> private ProgramNode ParseProgram() { Debugger.Write("Parsing program"); ICommandNode singleCommand = ParseSingleCommand(); ProgramNode program = new ProgramNode(singleCommand); return(program); }
/// <summary> /// Parses a program /// </summary> /// <returns>An abstract syntax tree representing the program</returns> private ProgramNode ParseProgram() { Debugger.WriteDebuggingInfo("Parsing program"); ICommandNode command = ParseCommand(); ProgramNode program = new ProgramNode(command); return(program); }
public CommandService(ILogger <CommandService> logger, Action <ICommandBuilder> setup) { _logger = logger; var rootBuilder = new RootCommandBuilder(); setup(rootBuilder); _root = rootBuilder.Build(); }
/// <summary> /// Parses a begin command /// </summary> private ICommandNode ParseBeginCommand() { Debugger.Write("Parsing Begin Command"); Accept(TokenType.Begin); ICommandNode command = ParseCommand(); Accept(TokenType.End); return(command); }
private void Count(ICommandNode node, ref int count) { foreach (var child in node.Children) { Count(child, ref count); } count += 1; }
public ForCommandNode(IdentifierNode identifier, IExpressionNode expression, IExpressionNode expressionTo, ICommandNode command, Position position) { Identifier = identifier; Expression = expression; ExpressionTo = expressionTo; Command = command; Position = position; }
public ForCommandNode(IdentifierNode identifier, IExpressionNode becomesExpression, IExpressionNode toExpression, ICommandNode command, Position position/*, IExpressionNode doExpression, IExpressionNode nextExpression*/) { Identifier = identifier; BecomesExpression = becomesExpression; ToExpression = toExpression; Command = command; //Position = position; }
private static void Build(CommandInterpreter cli, ICommandNode node, Command parent = null, bool check = true) { if (cli == null) { throw new ArgumentNullException(nameof(cli)); } if (node == null) { throw new ArgumentNullException(nameof(node)); } Type type = node.GetType(); Cmd rootInfo = type.GetAttribute <Cmd>(); if (rootInfo == null) { if (check) { throw new ArgumentException($"{node.GetType().Name} must have the attribute {typeof(Cmd).Name} defined!"); } return; } Command rootCommand = new Command(rootInfo.Name, rootInfo.Keyword, GetAliases(type), rootInfo.Description, parent); AddParams(rootCommand, type); rootCommand.OnExecuted += node.Execute; if (parent == null) { cli.Register(rootCommand); } BuildSubcommandNodes(cli, node, rootCommand); foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)) { Cmd cmd = method.GetAttribute <Cmd>(); if (cmd == null) { continue; } if (!HasValidParameters(method)) { throw new Exception($"Method has {typeof(Cmd).Name} attribute, but has invalid parameters!"); } Command subcommand = new Command(cmd.Name, cmd.Keyword, GetAliases(method), cmd.Description, rootCommand); AddParams(subcommand, method); subcommand.OnExecuted += (data) => { // TODO: Replace lambda with wrapper. method.Invoke(node, new object[] { data }); }; } }
private INode ParseClassNode() { string name = this.ParseName(); this.ParseToken(TokenType.Delimiter, "{"); ICommandNode body = this.ParseNodes(); this.ParseToken(TokenType.Delimiter, "}"); return(new ClassNode(name, body)); }
private static string GetCommandNames(ICommandNode node) { var sb = new StringBuilder(); sb.Append(node.Name); foreach (var item in node.Aliases) { sb.Append($", {item}"); } return(sb.ToString()); }
/// <summary> /// Parses a begin command /// </summary> /// <returns>An abstract syntax tree representing the begin command</returns> private ICommandNode ParseBeginCommand() { Debugger.WriteDebuggingInfo("Parsing Begin Command"); Position position = CurrentToken.Position; Accept(Begin); ICommandNode command = ParseCommand(); Accept(End); return(command); }
/// <summary> /// Parses a begin command /// </summary> private ICommandNode ParseBeginCommand() { Debugger.Write("Parsing Begin Command"); Position startPosition = CurrentToken.Position; Accept(Begin); ICommandNode command = ParseCommand(); Accept(End); //return new BeginCommandNode(command, startPosition); return(command); }
private ICommandNode ParseRepeatCommand() { Debugger.Write("Parsing Repeat Command"); Position position = CurrentToken.Position; Accept(Repeat); ICommandNode command = ParseSingleCommand(); Accept(Until); IExpressionNode expression = ParseExpression(); return(new RepeatCommandNode(command, expression, position)); }
/// <summary> /// Parses a let command /// </summary> private ICommandNode ParseLetCommand() { Debugger.Write("Parsing Let Command"); Position startPosition = CurrentToken.Position; Accept(TokenType.Let); IDeclarationNode declaration = ParseDeclaration(); Accept(TokenType.In); ICommandNode command = ParseSingleCommand(); return(new LetCommandNode(declaration, command, startPosition)); }
/// <summary> /// Parses a while command /// </summary> private WhileCommandNode ParseWhileCommand() { Debugger.Write("Parsing While Command"); Position startPosition = CurrentToken.Position; Accept(TokenType.While); IExpressionNode expression = ParseBracketExpression(); Accept(TokenType.Do); ICommandNode command = ParseSingleCommand(); return(new WhileCommandNode(expression, command, startPosition)); }
/// <summary> /// Parses a while command /// </summary> /// <returns>An abstract syntax tree representing the skip command</returns> private ICommandNode ParseWhileCommand() { Debugger.Write("Parsing While Command"); Position startPosition = CurrentToken.Position; //differs Accept(While); IExpressionNode expression = ParseExpression(); Accept(Do); ICommandNode command = ParseSingleCommand(); return(new WhileCommandNode(expression, command, startPosition)); }
/// <summary> /// Parses a skip command /// </summary> /// <returns>An abstract syntax tree representing the skip command</returns> /*private ICommandNode ParseEmptyCommand() * { * * Debugger.WriteDebuggingInfo("Parsing Skip Command"); * Position startPosition = CurrentToken.Position; * return new EmptyCommandNode(startPosition); * } */ /// <summary> /// Parses a while command /// </summary> /// <returns>An abstract syntax tree representing the while command</returns> private ICommandNode ParseWhileCommand() { Debugger.WriteDebuggingInfo("Parsing While Command"); Position startPosition = CurrentToken.Position; Accept(While); Accept(LeftBracket); IExpressionNode expression = ParseExpression(); Accept(RightBracket); Accept(Do); ICommandNode command = ParseSingleCommand(); return(new WhileCommandNode(expression, command, startPosition)); }
/// <summary> /// Parses an if command /// </summary> /// <returns>An abstract syntax tree representing the skip command</returns> private ICommandNode ParseIfCommand() { Debugger.Write("Parsing If Command"); Position startPosition = CurrentToken.Position; //differs Accept(If); IExpressionNode expression = ParseExpression(); Accept(Then); ICommandNode thenCommand = ParseSingleCommand(); Accept(Else); ICommandNode elseCommand = ParseSingleCommand(); return(new IfCommandNode(expression, thenCommand, elseCommand, startPosition)); }
public ICommandNode ParseFor() { Debugger.Write("Parsing for command"); Position startPosition = CurrentToken.Position; Accept(For); AssignCommandNode assign = AssignForCommand(); Accept(To); IExpressionNode expression = ParseExpression(); Accept(Do); ICommandNode command = ParseSingleCommand(); Accept(Next); return(new ForCommandNode(assign, expression, command, startPosition)); }
private int Build(ICommandNode node, ref int i, ref ICommandProvider.CommandInfo[] array) { var children = new int[node.Children.Count]; for (var index = 0; index < node.Children.Count; index++) { children[index] = Build(node.Children[index], ref i, ref array); } if (node.Redirect != null) { _logger.LogCritical($"Node {node.Name} has redirect. This is unsupported. "); } array[i] = new ICommandProvider.CommandInfo(node.Name, null, node.Type, children, node.IsExecutable, node.Parser); i++; return(i - 1); }