public override object?Evaluate(CompilerState state, ArgumentSyntax node) { var value = (EngineInstruction.Evaluate(state, node.Content).Value as Argument[]) !; var result = value.Select(e => e.Value).ToArray(); return(result); }
/// <summary> /// /// </summary> /// <param name="state"></param> /// <param name="arguments"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"/> /// <exception cref="CompilerException"/> public override object?Evaluate(CompilerState state, CommandSyntax commandSyntax) { if (state is null) { throw new ArgumentNullException(nameof(state)); } if (commandSyntax is null) { throw new ArgumentNullException(nameof(commandSyntax)); } if (commandSyntax.Name == null) { throw new CompilerException(commandSyntax, "Received command without a name."); } var command = state.Commands.Find(commandSyntax.Name.Value, commandSyntax.Arguments); if (command == null) { throw new CompilerException(commandSyntax, $"Could not find command '{commandSyntax.Name.Value}'."); } var arguments = new List <Argument>(); foreach (var argSyntax in commandSyntax.Arguments) { var argResult = EngineInstruction.Evaluate(state, argSyntax); arguments.Add(argResult); } if (commandSyntax.Environment != null) { var block = EngineInstruction.Evaluate(state, commandSyntax.Environment); arguments.Add(block); } try { var obj = command.Execution(commandSyntax, state, arguments.ToArray()); state.Context.Logger.Debug($"Successfully executed command '{command.Name}'."); return(obj); } catch (CompilerException e) { e.Origin ??= commandSyntax; throw; } catch (Exception ex) { throw new CompilerException(commandSyntax, ex.Message, ex); } }
/// <summary> /// /// </summary> /// <param name="state"></param> /// <param name="arguments"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"/> public override object Evaluate(CompilerState state, ListSyntax list) { if (state is null) { throw new ArgumentNullException(nameof(state)); } if (list is null) { throw new ArgumentNullException(nameof(list)); } var results = new List <Argument>(); // Group continueous leafs into one paragraph Paragraph?currentParagraph = null; foreach (var element in list.SelectMany(e => EngineInstruction.Evaluate(state, e).Flatten())) { if (element.Value is Leaf leaf) { if (currentParagraph == null) { currentParagraph = new Paragraph(); results.Add(new Argument(element.Source, currentParagraph)); } currentParagraph.Leaves.Add(leaf); } else if (element.Value is string str) { if (currentParagraph == null) { currentParagraph = new Paragraph(); results.Add(new Argument(element.Source, currentParagraph)); } currentParagraph.Leaves.Add(new TextLeaf(str)); } else if (element.Value != null) { currentParagraph = null; results.Add(element); } } return(results.ToArray()); }
/// <summary> /// /// </summary> /// <param name="state"></param> /// <param name="arguments"></param> /// <returns></returns> /// <exception cref="ArgumentException"/> /// <exception cref="ArgumentNullException"/> public override object?Evaluate(CompilerState state, FieldSyntax field) { if (field is null) { throw new ArgumentNullException(nameof(field)); } if (field.Name is null) { throw new ArgumentException("Field name cannot be empty", nameof(field)); } var fieldArgument = EngineInstruction.Evaluate(state, field.Value); return(new ObjectField(field, field.Name.Value, fieldArgument)); }
/// <summary> /// /// </summary> /// <param name="state"></param> /// <param name="array"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"/> public override object?Evaluate(CompilerState state, ArraySyntax array) { if (state is null) { throw new ArgumentNullException(nameof(state)); } if (array is null) { throw new ArgumentNullException(nameof(array)); } var arguments = new List <Argument>(); foreach (var arrayElement in array) { arguments.Add(EngineInstruction.Evaluate(state, arrayElement)); } return(new ObjectArray(array, state, arguments.ToArray())); }
/// <summary> /// /// </summary> /// <param name="state"></param> /// <param name="arguments"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"/> public override object?Evaluate(CompilerState state, ObjectSyntax obj) { if (state is null) { throw new ArgumentNullException(nameof(state)); } if (obj is null) { throw new ArgumentNullException(nameof(obj)); } var creator = new ObjectCreator(obj, state); var fields = new List <ObjectField>(); foreach (var field in obj.Fields) { fields.Add((EngineInstruction.Evaluate(state, field).Value as ObjectField) !); } creator.Fields.AddRange(fields); return(creator); }