public override RuntimeValueWrapper Execute(DoExpression e, RuntimeContext context) { RuntimeValueWrapper result = null; RuntimeContext newContext = new RuntimeContext() { PreviousContext = context }; foreach (Expression expression in e.Expressions) { VarExpression var = expression as VarExpression; if (var == null) { expression.BuildContext(newContext); result = expression.Execute(newContext); } else { result = new RuntimeValueWrapper(new RuntimeUnevaluatedValue(var.Expression), context); if (!var.Pattern.Match(context, result)) { throw new Exception("模式匹配不成功。"); } } } if (result == null) { return(RuntimeValueWrapper.CreateValue(new object())); } else { return(result); } }
public static RuntimeValueWrapper ReturnStateMonadValue(RuntimeValueWrapper[] arguments) { return(RuntimeValueWrapper.CreateValue(new StatePackage() { result = arguments[0], state = arguments[1] })); }
public override bool Match(RuntimeContext context, RuntimeValueWrapper valueWrapper) { if (!valueWrapper.IsInvokable) { return(Value.Equals(valueWrapper.RuntimeObject)); } return(base.Match(context, valueWrapper)); }
private void EnsureValueExecuted() { while (!Value.IsReady) { RuntimeValueWrapper valueWrapper = Value.Execute(Context); Value = valueWrapper.Value; Context = valueWrapper.Context; } }
public ScriptingValue Invoke(ScriptingValue firstArgument, params ScriptingValue[] arguments) { RuntimeValueWrapper result = ValueWrapper.Invoke(firstArgument.ValueWrapper); foreach (ScriptingValue argument in arguments) { result = result.Invoke(argument.ValueWrapper); } return(new ScriptingValue(result)); }
public override RuntimeValueWrapper Execute(RuntimeContext context) { return(RuntimeValueWrapper.CreateValue( Elements .Take(Elements.Count - 1) .Select(e => new RuntimeValueWrapper(new RuntimeUnevaluatedValue(e), context)) .Union((RuntimeValueWrapper[])Elements.Last().Execute(context).RuntimeObject) .ToArray() )); }
public override bool Match(RuntimeContext context, RuntimeValueWrapper valueWrapper) { if (context.Values.ContainsKey(Name)) { throw new Exception(string.Format("{0}不可重复定义。", Name)); } else { context.Values.Add(Name, valueWrapper); return(true); } }
public override bool Match(RuntimeContext context, RuntimeValueWrapper valueWrapper) { if (context.Values.ContainsKey(Name)) { throw new Exception(string.Format("{0}不可重复定义。", Name)); } else if (!valueWrapper.IsInvokable) { return(new Flag(Name).Equals(valueWrapper.RuntimeObject)); } else { return(false); } }
public override RuntimeValueWrapper Execute(RuntimeContext context) { Monad monad = null; if (MonadProvider == null) { monad = new PureMonad(); } else { RuntimeValueWrapper monadWrapper = MonadProvider.Execute(context); monad = (Monad)monadWrapper.RuntimeObject; } return(monad.Execute(this, context)); }
public override RuntimeValueWrapper Execute(RuntimeContext context) { RuntimeValueWrapper valueToBeMatched = Source.Execute(context); foreach (CasePair pair in Pairs) { RuntimeContext newContext = new RuntimeContext() { PreviousContext = context }; if (pair.Pattern.Match(newContext, valueToBeMatched)) { return(new RuntimeValueWrapper(new RuntimeUnevaluatedValue(pair.Expression), newContext)); } } throw new Exception("模式匹配不成功。"); }
public override RuntimeValueWrapper Execute(DoExpression e, RuntimeContext context) { Func <RuntimeValueWrapper[], RuntimeValueWrapper> monadFunction = arguments => { StatePackage result = new StatePackage() { result = null, state = arguments[0] }; RuntimeContext newContext = new RuntimeContext() { PreviousContext = context }; newContext.Values.Add("return", RuntimeValueWrapper.CreateFunction(ReturnStateMonadValue, 2)); foreach (Expression expression in e.Expressions) { VarExpression var = expression as VarExpression; if (var == null) { expression.BuildContext(newContext); result = RunStateMonad(new RuntimeValueWrapper(new RuntimeUnevaluatedValue(expression), newContext), result.state); } else { result = RunStateMonad(new RuntimeValueWrapper(new RuntimeUnevaluatedValue(var.Expression), newContext), result.state); if (!var.Pattern.Match(newContext, result.result)) { throw new Exception("模式匹配不成功。"); } } if (!(bool)continueFunction.Invoke(result.state).RuntimeObject) { break; } } if (result.result == null) { result.result = RuntimeValueWrapper.CreateValue(new object()); } return(RuntimeValueWrapper.CreateValue(result)); }; return(RuntimeValueWrapper.CreateFunction(monadFunction, 1)); }
public override bool Match(RuntimeContext context, RuntimeValueWrapper valueWrapper) { if (!valueWrapper.IsInvokable) { RuntimeValueWrapper[] valueWrappers = valueWrapper.RuntimeObject as RuntimeValueWrapper[]; if (valueWrappers.Length == Elements.Count) { for (int i = 0; i < Elements.Count; i++) { if (!Elements[i].Match(context, valueWrappers[i])) { return(false); } } return(true); } } return(base.Match(context, valueWrapper)); }
public override RuntimeValueWrapper Execute(DoExpression e, RuntimeContext context) { RuntimeContext newContext = new RuntimeContext() { PreviousContext = context }; foreach (Expression expression in e.Expressions) { expression.BuildContext(newContext); } if (e.Expressions.Count > 0) { return(new RuntimeValueWrapper(new RuntimeUnevaluatedValue(e.Expressions.Last()), newContext)); } else { return(RuntimeValueWrapper.CreateValue(new object())); } }
public override bool Match(RuntimeContext context, RuntimeValueWrapper valueWrapper) { if (!valueWrapper.IsInvokable) { RuntimeValueWrapper[] valueWrappers = valueWrapper.RuntimeObject as RuntimeValueWrapper[]; if (valueWrappers.Length >= Elements.Count - 1) { for (int i = 0; i < Elements.Count - 1; i++) { if (!Elements[i].Match(context, valueWrappers[i])) { return(false); } } return(Elements[Elements.Count - 1].Match( context, RuntimeValueWrapper.CreateValue(valueWrappers.Skip(Elements.Count - 1).ToArray()) )); } } return(base.Match(context, valueWrapper)); }
public override RuntimeValueWrapper Invoke(RuntimeContext context, RuntimeValueWrapper argument) { List <IncompletedExpression> newIncompletedExpressions = new List <IncompletedExpression>(); foreach (IncompletedExpression incompletedExpression in IncompletedExpressions) { RuntimeContext newContext = new RuntimeContext(); newContext.PreviousContext = incompletedExpression.Context.PreviousContext; foreach (var pair in incompletedExpression.Context.Values) { newContext.Values.Add(pair.Key, pair.Value); } if (incompletedExpression.Patterns.First().Match(newContext, argument)) { if (incompletedExpression.Patterns.Count == 1) { return(incompletedExpression.Expression.Execute(newContext)); } else { IncompletedExpression newIncompletedExpression = new IncompletedExpression(); newIncompletedExpression.Context = newContext; newIncompletedExpression.Expression = incompletedExpression.Expression; newIncompletedExpression.Patterns = incompletedExpression.Patterns.Skip(1).ToList(); newIncompletedExpressions.Add(newIncompletedExpression); } } } if (newIncompletedExpressions.Count == 0) { throw new Exception("模式匹配不成功。"); } else { RuntimeInvokableValue value = new RuntimeInvokableValue(); value.IncompletedExpressions.AddRange(newIncompletedExpressions); return(new RuntimeValueWrapper(value, context)); } }
public override void BuildContext(RuntimeContext context) { RuntimeInvokableValue invokableValue = null; if (context.Values.ContainsKey(Name)) { RuntimeValueWrapper valueWrapper = context.Values[Name]; invokableValue = valueWrapper.Value as RuntimeInvokableValue; if (invokableValue == null || this.Patterns.Count == 0) { throw new Exception(string.Format("{0}不可重复定义。", Name)); } } if (this.Patterns.Count == 0) { context.Values.Add(Name, new RuntimeValueWrapper(new RuntimeUnevaluatedValue(Expression), context)); } else { if (invokableValue == null) { invokableValue = new RuntimeInvokableValue(); context.Values.Add(Name, new RuntimeValueWrapper(invokableValue, context)); } RuntimeInvokableValue.IncompletedExpression incompletedExpression = new RuntimeInvokableValue.IncompletedExpression() { Context = new RuntimeContext() { PreviousContext = context }, Expression = Expression, Patterns = new List <Expression>(Patterns), }; invokableValue.IncompletedExpressions.Add(incompletedExpression); } }
public override RuntimeValueWrapper Invoke(RuntimeContext context, RuntimeValueWrapper argument) { if (ParameterCount > 0) { RuntimeValueWrapper[] newArguments = Arguments.Concat(new RuntimeValueWrapper[] { argument }).ToArray(); if (ParameterCount == 1) { return(ExternalFunction(newArguments)); } else { return(new RuntimeValueWrapper(new RuntimeExternalValue() { ExternalFunction = ExternalFunction, Arguments = newArguments, ParameterCount = ParameterCount - 1 }, context)); } } else { throw new Exception("外界函数参数数量不能为0。"); } }
public virtual RuntimeValueWrapper Execute(RuntimeContext context) { return(RuntimeValueWrapper.CreateValue(new object())); }
public override RuntimeValueWrapper Invoke(RuntimeContext context, RuntimeValueWrapper argument) { throw new NotSupportedException(); }
public virtual bool Match(RuntimeContext context, RuntimeValueWrapper valueWrapper) { return(false); }
public static StatePackage RunStateMonad(RuntimeValueWrapper monad, RuntimeValueWrapper state) { return(monad.Invoke(state).RuntimeObject as StatePackage); }
public override RuntimeValueWrapper Execute(RuntimeContext context) { return(RuntimeValueWrapper.CreateValue(new Flag(Name))); }
public static ScriptingValue CreateFunction(Func <ScriptingValue[], ScriptingValue> externalFunction, int parameterCount) { return(new ScriptingValue(RuntimeValueWrapper.CreateFunction( (xs) => externalFunction(xs.Select(w => new ScriptingValue(w)).ToArray()).ValueWrapper , parameterCount))); }
public RuntimeValueWrapper Invoke(RuntimeValueWrapper argument) { EnsureValueExecuted(); return(Value.Invoke(Context, argument)); }
public override RuntimeValueWrapper Execute(RuntimeContext context) { return(RuntimeValueWrapper.CreateValue( Elements.Select(e => new RuntimeValueWrapper(new RuntimeUnevaluatedValue(e), context)).ToArray() )); }
public static ScriptingValue CreateValue(object o) { return(new ScriptingValue(RuntimeValueWrapper.CreateValue(o))); }
public static ScriptingValue CreateArray(params object[] o) { return(new ScriptingValue(RuntimeValueWrapper.CreateArray(o))); }
public StateMonad(RuntimeValueWrapper continueFunction) { this.continueFunction = continueFunction; }
public static void LoadLibrary(ScriptingEnvironment e) { e.DefineValue("pure", ScriptingValue.CreateValue(new PureMonad())); e.DefineValue("ordered", ScriptingValue.CreateValue(new OrderedMonad())); e.DefineValue("state", ScriptingValue.CreateFunction(State, 1)); e.DefineValue("continue", ScriptingValue.CreateFunction(Continue, 1)); e.DefineValue("io", ScriptingValue.CreateFunction(IOContinue, 1)); e.DefineValue("create_state", new ScriptingValue(RuntimeValueWrapper.CreateFunction(StateMonad.ReturnStateMonadValue, 2))); e.DefineValue("(+)", ScriptingValue.CreateFunction(PrimitiveAdd, 2)); e.DefineValue("(-)", ScriptingValue.CreateFunction(PrimitiveSub, 2)); e.DefineValue("(*)", ScriptingValue.CreateFunction(PrimitiveMul, 2)); e.DefineValue("(/)", ScriptingValue.CreateFunction(PrimitiveDiv, 2)); e.DefineValue("(%)", ScriptingValue.CreateFunction(PrimitiveMod, 2)); e.DefineValue("(++)", ScriptingValue.CreateFunction(PrimitiveConcat, 2)); e.DefineValue("(<)", ScriptingValue.CreateFunction(PrimitiveLt, 2)); e.DefineValue("(<=)", ScriptingValue.CreateFunction(PrimitiveLe, 2)); e.DefineValue("(>)", ScriptingValue.CreateFunction(PrimitiveGt, 2)); e.DefineValue("(>=)", ScriptingValue.CreateFunction(PrimitiveGe, 2)); e.DefineValue("(==)", ScriptingValue.CreateFunction(PrimitiveEq, 2)); e.DefineValue("(!=)", ScriptingValue.CreateFunction(PrimitiveNe, 2)); e.DefineValue("(&&)", ScriptingValue.CreateFunction(PrimitiveAnd, 2)); e.DefineValue("(||)", ScriptingValue.CreateFunction(PrimitiveOr, 2)); e.DefineValue("(^)", ScriptingValue.CreateFunction(PrimitiveXor, 2)); e.DefineValue("not", ScriptingValue.CreateFunction(PrimitiveNot, 1)); e.DefineValue("neg", ScriptingValue.CreateFunction(PrimitiveNeg, 1)); e.DefineValue("unit", ScriptingValue.CreateFunction(PrimitiveUnit, 1)); foreach (MethodInfo method in typeof(Math).GetMethods(BindingFlags.Public | BindingFlags.Static)) { if (method.ReturnType == typeof(double) && method.GetParameters().Length > 0 && method.GetParameters().All(p => p.ParameterType == typeof(double))) { string methodName = method.Name.ToLower(); if (!e.IsDefined(methodName)) { e.DefineValue(methodName, ScriptingValue.CreateFunction(MakeDoubleFunction(method), method.GetParameters().Length)); } } } e.DefineValue("pi", ScriptingValue.CreateValue(Math.PI)); e.DefineValue("e", ScriptingValue.CreateValue(Math.E)); e.DefineValue("aggregate", ScriptingValue.CreateFunction(Aggregate, 3)); e.DefineValue("distinct", ScriptingValue.CreateFunction(Distinct, 1)); e.DefineValue("except", ScriptingValue.CreateFunction(Except, 2)); e.DefineValue("first", ScriptingValue.CreateFunction(First, 2)); e.DefineValue("intersect", ScriptingValue.CreateFunction(Intersect, 2)); e.DefineValue("last", ScriptingValue.CreateFunction(Last, 2)); e.DefineValue("order_by", ScriptingValue.CreateFunction(OrderBy, 2)); e.DefineValue("reverse", ScriptingValue.CreateFunction(Reverse, 1)); e.DefineValue("select", ScriptingValue.CreateFunction(Select, 2)); e.DefineValue("select_many", ScriptingValue.CreateFunction(SelectMany, 2)); e.DefineValue("skip", ScriptingValue.CreateFunction(Skip, 2)); e.DefineValue("skip_while", ScriptingValue.CreateFunction(SkipWhile, 2)); e.DefineValue("take", ScriptingValue.CreateFunction(Take, 2)); e.DefineValue("take_while", ScriptingValue.CreateFunction(TakeWhile, 2)); e.DefineValue("union", ScriptingValue.CreateFunction(Union, 2)); e.DefineValue("where", ScriptingValue.CreateFunction(Where, 2)); e.DefineValue("zip", ScriptingValue.CreateFunction(Zip, 2)); e.DefineValue("to_lower", ScriptingValue.CreateFunction(ToLower, 1)); e.DefineValue("to_upper", ScriptingValue.CreateFunction(ToUpper, 1)); e.DefineValue("find", ScriptingValue.CreateFunction(Find, 2)); e.DefineValue("find_all", ScriptingValue.CreateFunction(FindAll, 2)); e.DefineValue("reg_find", ScriptingValue.CreateFunction(RegFind, 2)); e.DefineValue("reg_find_all", ScriptingValue.CreateFunction(RegFindAll, 2)); e.DefineValue("length", ScriptingValue.CreateFunction(Length, 1)); e.DefineValue("item", ScriptingValue.CreateFunction(Item, 2)); e.DefineValue("empty", ScriptingValue.CreateFunction(Empty, 1)); e.DefineValue("split", ScriptingValue.CreateFunction(Split, 2)); e.DefineValue("to_int", ScriptingValue.CreateFunction(ToInt, 1)); e.DefineValue("to_double", ScriptingValue.CreateFunction(ToDouble, 1)); e.DefineValue("to_string", ScriptingValue.CreateFunction(ToString, 1)); e.DefineValue("read_file", ScriptingValue.CreateFunction(ReadFile, 1)); e.DefineValue("write_file", ScriptingValue.CreateFunction(WriteFile, 2)); }
internal ScriptingValue(RuntimeValueWrapper valueWrapper) { this.ValueWrapper = valueWrapper; }