public override ExecuteResult Execute(Space space) { if (!IsSuccess(_expression.Execute(space))) { ErrorLogger.LogRuntimeError(ErrorRuntimeCode.Foreach, "The expression execute failed!"); return ExecuteResult.Failed; } if (!ValueTool.IsArray(_expression.value)) { ErrorLogger.LogRuntimeError(ErrorRuntimeCode.Foreach, "The expression isn't a array!"); return ExecuteResult.Failed; } var arr = (_expression.value as ValueArray).value; if (arr.Count == 0) { return ExecuteResult.Successed; } var tempSpace = new Space(SpaceType.Loop, space); var itemVariable = new Variable(_name, VariableAttribute.None); if (!tempSpace.AddVariable(itemVariable)) { ErrorLogger.LogRuntimeError(_name); ErrorLogger.LogRuntimeError(ErrorRuntimeCode.Foreach, "The variable \"" + _name + "\" execute failed!"); return ExecuteResult.Failed; } var contentSpace = new Space(SpaceType.Loop, tempSpace); foreach (var item in arr) { itemVariable.SetValue(item.value); contentSpace.Clear(); var ret = _sentence.Execute(contentSpace); if (!IsSuccess(ret)) { ErrorLogger.LogRuntimeError(ErrorRuntimeCode.Foreach, "The sentence execute failed!"); return ExecuteResult.Failed; } if (ret == ExecuteResult.Break) { break; } if (ret == ExecuteResult.Return) { SetReturnValue((_sentence as SentenceReturn).returnValue); tempSpace.Clear(); return ExecuteResult.Return; } } tempSpace.Clear(); return ExecuteResult.Successed; }
public Variable Execute(Space space) { if (!Sentence.IsSuccess(_valueExpression.Execute(space))) { ErrorLogger.LogRuntimeError(ErrorRuntimeCode.VariableArrayItemAnalysis, "The expression execute failed!"); return null; } var retValue = _valueExpression.value; Variable retVariable = null; var expressionVecSize = _indexExpressionVec.Count; for (var i = 0; i < expressionVecSize; ++i) { if (!ValueTool.IsArray(retValue)) { ErrorLogger.LogRuntimeError(ErrorRuntimeCode.VariableArrayItemAnalysis, "The result of expression isn't a array!"); return null; } var arr = (retValue as ValueArray).value; var expression = _indexExpressionVec[i]; if (!Sentence.IsSuccess(expression.Execute(space))) { ErrorLogger.LogRuntimeError(ErrorRuntimeCode.VariableArrayItemAnalysis, "The expression execute failed!"); return null; } var indexValue = expression.value; if (!ValueTool.IsInteger(indexValue)) { ErrorLogger.LogRuntimeError(ErrorRuntimeCode.VariableArrayItemAnalysis, "The index isn't a integer value!"); return null; } var index = (int) ((indexValue as ValueNumber).value); if (index >= arr.Count) { ErrorLogger.LogRuntimeError(ErrorRuntimeCode.VariableArrayItemAnalysis, "The index out of range!"); return null; } retVariable = arr[index]; if (!retVariable) { ErrorLogger.LogRuntimeError(ErrorRuntimeCode.VariableArrayItemAnalysis, "The array value is invalid!"); return null; } retValue = retVariable.value; } return retVariable; }
private BuiltInFunction() { Action <string, int, Func <List <Value>, Space, Value> > _Emplace = (string name, int paramSize, Func <List <Value>, Space, Value> func) => { var value = new ValueFunction(paramSize, func); var variable = new Variable(name, VariableAttribute.Const, value); variables.Add(name, variable); }; // print _Emplace.Invoke("print", 1, (List <Value> args, Space s) => { string ret = ""; foreach (var arg in args) { ret += ValueTool.ToString(arg); } System0.Echo(ret); return(ValueNull.DEFAULT_VALUE); }); // type _Emplace("type", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } return(new ValueString(ValueTool.ToTypeString(args[0]))); }); // is_null _Emplace("is_null", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } bool ret = true; foreach (var arg in args) { ret &= ValueTool.IsNull(arg); } return(new ValueBool(ret)); }); // is_number _Emplace("is_number", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } bool ret = true; foreach (var arg in args) { ret &= ValueTool.IsNumber(arg); } return(new ValueBool(ret)); }); // is_bool _Emplace("is_bool", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } bool ret = true; foreach (var arg in args) { ret &= ValueTool.IsBool(arg); } return(new ValueBool(ret)); }); // is_string _Emplace("is_string", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } bool ret = true; foreach (var arg in args) { ret &= ValueTool.IsString(arg); } return(new ValueBool(ret)); }); // is_array _Emplace("is_array", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } bool ret = true; foreach (var arg in args) { ret &= ValueTool.IsArray(arg); } return(new ValueBool(ret)); }); // is_function _Emplace("is_function", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } bool ret = true; foreach (var arg in args) { ret &= ValueTool.IsFunction(arg); } return(new ValueBool(ret)); }); // is_object _Emplace("is_object", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } bool ret = true; foreach (var arg in args) { ret &= ValueTool.IsObject(arg); } return(new ValueBool(ret)); }); // len _Emplace("len", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } var value = args[0]; if (ValueTool.IsArray(value)) { return(new ValueNumber((double)(value as ValueArray).value.Count)); } else if (ValueTool.IsString(value)) { return(new ValueNumber((double)(value as ValueString).value.Length)); } return(ValueNull.DEFAULT_VALUE); }); // to_string _Emplace("to_string", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } var value = args[0]; return(new ValueString(ValueTool.ToString(value))); }); // to_number _Emplace("to_number", 1, (List <Value> args, Space s) => { if (args.Count == 0) { return(null); } var value = args[0]; if (!ValueTool.IsString(value)) { return(null); } double ret = 0; if (!double.TryParse((value as ValueString).value, out ret)) { ret = 0; } return(new ValueNumber(ret)); }); }