/// <inheritdoc /> protected override object AssignToken(IScriptToken token, ScriptContext context) { object host = hosttoken.Execute(context); if (parameters.Length == 1) { if (host is Array array) { object value = token.Execute(context); array.SetValue(value, Converter.Convert <int>(parameters[0].Execute(context))); return(value); } } PropertyInfo[] indexer = host.GetType().GetProperties().Where(p => p.GetIndexParameters().Length == parameters.Length).ToArray(); object[] parametervalues = parameters.Select(p => p.Execute(context)).Concat(new[] { token.Execute(context) }).ToArray(); Tuple <MethodInfo, int>[] evaluated = indexer.Select(i => MethodOperations.GetMethodMatchValue(i.SetMethod, parametervalues)).Where(e => e.Item2 >= 0).OrderBy(m => m.Item2).ToArray(); if (evaluated.Length == 0) { throw new ScriptRuntimeException($"No index setter found on '{host.GetType().Name}' which matched the specified parameters '{string.Join(", ", parametervalues)}'", this); } return(MethodOperations.CallMethod(this, host, evaluated[0].Item1, parametervalues, context)); }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth) { DictionaryToken dictionary = (DictionaryToken)token; resulttext.Append("{"); if (dictionary.Entries.Any()) { foreach (KeyValuePair <IScriptToken, IScriptToken> entry in dictionary.Entries) { resulttext.AppendLine(); formatters[entry.Key].FormatToken(entry.Key, resulttext, formatters, depth + 1, true); resulttext.Append(" : "); formatters[entry.Value].FormatToken(entry.Value, resulttext, formatters, depth + 1); resulttext.Append(','); } --resulttext.Length; resulttext.AppendLine(); AppendIntendation(resulttext, depth); resulttext.Append('}'); } else { resulttext.Append('}'); } }
/// <inheritdoc /> public void FormatToken(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0, bool mustindent = false) { StatementBlock block = (StatementBlock)token; if (depth > -1) { resulttext.AppendLine(" {"); } foreach (IScriptToken child in block.Children) { formatters[child].FormatToken(child, resulttext, formatters, depth >= 0 ? depth : 0, true); resulttext.AppendLine(); } if (depth > -1) { for (int i = 0; i < depth - 1; ++i) { resulttext.Append('\t'); } resulttext.Append('}'); } if (depth < 0) { while (resulttext[resulttext.Length - 1] == '\r' || resulttext[resulttext.Length - 1] == '\n') { --resulttext.Length; } } }
/// <summary> /// creates an <see cref="ExpliciteTypeCast"/> /// </summary> /// <param name="typeprovider">type provider used to get type</param> /// <param name="value">value to cast</param> /// <param name="targetType">target type to cast to</param> /// <param name="isdynamic">determines whether to execute a dynamic cast</param> public ExpliciteTypeCast(ITypeProvider typeprovider, IScriptToken value, ScriptValue targetType, IScriptToken isdynamic) { Value = value; TargetType = targetType; IsDynamic = isdynamic; TypeProvider = typeprovider; }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { ScriptMember member = (ScriptMember)token; formatters[member.Host].FormatToken(member.Host, resulttext, formatters, depth); resulttext.Append('.').Append(member.Member); }
/// <inheritdoc /> protected override object AssignToken(IScriptToken token, ScriptContext context) { object host = hosttoken.Execute(context); if (host is Dictionary <object, object> dictionary) { return(dictionary[Member] = token.Execute(context)); } string member = membername.ToLower(); PropertyInfo property = host.GetType().GetProperties().FirstOrDefault(p => p.Name.ToLower() == member); if (property != null) { return(SetProperty(host, property, token, context)); } FieldInfo fieldinfo = host.GetType().GetFields().FirstOrDefault(f => f.Name.ToLower() == member); if (fieldinfo == null) { throw new ScriptRuntimeException($"A member with the name of {membername} was not found in type {host.GetType().Name}", this); } return(SetField(host, fieldinfo, token, context)); }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { resulttext.Append(token.Literal); if (token is IParameterContainer parametertoken && parametertoken.Parameters != null) { resulttext.Append('('); if (parametertoken.Parameters.Any()) { foreach (IScriptToken parameter in parametertoken.Parameters) { formatters[parameter].FormatToken(parameter, resulttext, formatters, depth); resulttext.Append(", "); } resulttext.Length -= 2; } resulttext.Append(')'); } if (token is IStatementContainer controltoken) { FormatBody(controltoken.Body, resulttext, formatters, depth); } }
/// <summary> /// determines the specified type from a string /// </summary> /// <param name="provider">provider for script named types</param> /// <param name="token">token from which type should be determined</param> /// <param name="typename">name of type</param> /// <returns>Type determined from string</returns> public static Type DetermineType(this ITypeProvider provider, IScriptToken token, string typename) { ITypeInstanceProvider instanceprovider = provider.GetType(typename); if (instanceprovider != null) { Type type = instanceprovider.ProvidedType; if (type == null) { throw new ScriptRuntimeException($"type '{typename}' does not provide type information", token); } return(type); } else { // try to load type dynamically Type type = Type.GetType(typename); if (type == null) { throw new ScriptRuntimeException($"Unknown type '{typename}'", token); } return(type); } }
/// <summary> /// creates a new <see cref="ScriptMethod"/> /// </summary> /// <param name="extensions">pool containing known hosts</param> /// <param name="hosttoken">host of method to be called</param> /// <param name="methodname">name of method to call</param> /// <param name="parameters">parameters for method call</param> public ScriptMethod(IExtensionProvider extensions, IScriptToken hosttoken, string methodname, IScriptToken[] parameters) { this.extensions = extensions; this.hosttoken = hosttoken; this.methodname = methodname.ToLower(); this.parameters = parameters; }
/// <summary> /// creates a new <see cref="ScriptParameter"/> /// </summary> /// <param name="typeProvider">provider used to resolve type name</param> /// <param name="variable">parameter variable</param> /// <param name="type">value which expresses the parameter type</param> /// <param name="defaultvalue">default value to use when parameter value is not provided</param> public ScriptParameter(ITypeProvider typeProvider, ScriptVariable variable, ScriptValue type, IScriptToken defaultvalue) { Variable = variable; Type = type; TypeProvider = typeProvider; DefaultValue = defaultvalue; }
/// <inheritdoc /> public void FormatToken(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0, bool mustident = false) { if (mustident) { AppendIntendation(resulttext, depth); } Format(token, resulttext, formatters, depth); }
/// <summary> /// formats a body for a control token /// </summary> /// <param name="token">body token</param> /// <param name="resulttext">formatted result target</param> /// <param name="formatters">collection of token formatters</param> /// <param name="depth">indentation depth (should be depth of control token)</param> protected void FormatBody(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth) { if (!(token is StatementBlock)) { resulttext.AppendLine(); } formatters[token].FormatToken(token, resulttext, formatters, depth + 1, true); }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { ImpliciteTypeCast cast = (ImpliciteTypeCast)token; resulttext.Append(cast.Keyword).Append('('); formatters[cast.Argument].FormatToken(cast.Argument, resulttext, formatters, depth); resulttext.Append(')'); }
/// <inheritdoc /> protected override object AssignToken(IScriptToken token, ScriptContext context) { if (Rhs is IAssignableToken assignable) { return(assignable.Assign(token, context)); } throw new ScriptRuntimeException("Can't assign value to non assignable token", token); }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { ArithmeticBlock block = (ArithmeticBlock)token; resulttext.Append('('); formatters[block.InnerBlock].FormatToken(block.InnerBlock, resulttext, formatters, depth); resulttext.Append(')'); }
/// <summary> /// creates a new <see cref="Break"/> /// </summary> /// <param name="depth">depth to break</param> internal Break(IScriptToken depth = null) { if (depth == null) { depth = new ScriptValue(1); } Depth = depth; }
/// <summary> /// creates a new <see cref="Break"/> /// </summary> /// <param name="depth">loop depth to continue</param> internal Continue(IScriptToken depth = null) { if (depth == null) { depth = new ScriptValue(1); } Depth = depth; }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { IBinaryToken binary = (IBinaryToken)token; formatters[binary.Lhs].FormatToken(binary.Lhs, resulttext, formatters); resulttext.Append(' ').Append(token.Literal).Append(' '); formatters[binary.Rhs].FormatToken(binary.Rhs, resulttext, formatters); }
/// <inheritdoc /> public override void VisitToken(IScriptToken token) { if (filter == null || filter(token)) { CheckToken(token); } base.VisitToken(token); }
void CheckToken(IScriptToken token) { if (token is ICodePositionToken positioninfo && positioninfo.TextIndex <= position && positioninfo.TextIndex >= 0) { if (Token != null && positioninfo.TextIndex <= Token.TextIndex) { return; } Token = positioninfo; } }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { StringInterpolation interpolation = (StringInterpolation)token; resulttext.Append("$\""); foreach (IScriptToken child in interpolation.Children) { if (child is ScriptValue value && value.Value is string stringvalue) { FormatString(stringvalue, resulttext); }
/// <inheritdoc /> public object Assign(IScriptToken token, ScriptContext context) { try { return(AssignToken(token, context)); } catch (ScriptException) { throw; } catch (Exception e) { throw new ScriptRuntimeException($"Unable to execute assignment '{this}'\n{e.Message}", token, e); } }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { Throw throwtoken = (Throw)token; resulttext.Append("throw("); formatters[throwtoken.Message].FormatToken(throwtoken.Message, resulttext, formatters, depth); if (throwtoken.Context != null) { resulttext.Append(", "); formatters[throwtoken.Context].FormatToken(throwtoken.Context, resulttext, formatters, depth); } resulttext.Append(")"); }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { IUnaryToken unarytoken = (IUnaryToken)token; if (unarytoken.IsPostToken) { formatters[unarytoken.Operand].FormatToken(unarytoken.Operand, resulttext, formatters, depth); resulttext.Append(unarytoken.Literal); } else { resulttext.Append(unarytoken.Literal); formatters[unarytoken.Operand].FormatToken(unarytoken.Operand, resulttext, formatters, depth); } }
object SetField(object host, FieldInfo fieldinfo, IScriptToken valuetoken, ScriptContext context) { object targetvalue = Converter.Convert(valuetoken.Execute(context), fieldinfo.FieldType); try { fieldinfo.SetValue(host, targetvalue); } catch (Exception e) { throw new ScriptRuntimeException("Unable to set field", null, e); } return(targetvalue); }
object SetProperty(object host, PropertyInfo property, IScriptToken valuetoken, ScriptContext context) { object targetvalue = Converter.Convert(valuetoken.Execute(context), property.PropertyType); try { property.SetValue(host, targetvalue, null); } catch (Exception e) { throw new ScriptRuntimeException("Unable to set property", null, e); } return(targetvalue); }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { ScriptIndexer indexer = (ScriptIndexer)token; formatters[indexer.Host].FormatToken(indexer.Host, resulttext, formatters, depth); resulttext.Append('['); foreach (IScriptToken parameter in indexer.Parameters) { formatters[parameter].FormatToken(parameter, resulttext, formatters, depth); resulttext.Append(", "); } resulttext.Length -= 2; resulttext.Append(']'); }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { Switch switchtoken = (Switch)token; resulttext.Append("switch("); foreach (IScriptToken parameter in switchtoken.Parameters) { formatters[parameter].FormatToken(parameter, resulttext, formatters, depth); } resulttext.AppendLine(")"); foreach (IScriptToken child in ((ListStatementBlock)switchtoken.Body).Children) { AppendIntendation(resulttext, depth); if (child is Case @case) { if ([email protected]) { resulttext.Append("case("); foreach (IScriptToken parameter in @case.Parameters) { formatters[parameter].FormatToken(parameter, resulttext, formatters, depth); resulttext.Append(", "); } resulttext.Length -= 2; resulttext.Append(")"); } else { resulttext.Append("default"); } if (!(@case.Body is StatementBlock)) { resulttext.AppendLine(); } formatters[@case.Body].FormatToken(@case.Body, resulttext, formatters, depth + 1, true); } else { formatters[child].FormatToken(child, resulttext, formatters, depth); } resulttext.AppendLine(); } resulttext.Length -= 2; }
/// <inheritdoc /> protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0) { NewInstance instance = (NewInstance)token; resulttext.Append("new ").Append(instance.TypeName).Append('('); if (instance.Parameters.Any()) { foreach (IScriptToken parameter in instance.Parameters) { formatters[parameter].FormatToken(parameter, resulttext, formatters, depth); resulttext.Append(", "); } resulttext.Length -= 2; } resulttext.Append(')'); }
/// <inheritdoc /> public ITokenFormatter this[IScriptToken token] { get { if (token == null) { throw new ArgumentNullException(nameof(token)); } Type tokentype = token.GetType(); if (formatters.TryGetValue(tokentype, out ITokenFormatter result)) { return(result); } PrepareHandler(tokentype); return(formatters[tokentype]); } }