/// <summary> /// Initializes a new instance by associative array /// </summary> /// <param name="dict"></param> public Memory(VAL dict) { for (int i = 0; i < dict.Size; i++) { Add(dict[i][0].Str, dict[i][1]); } }
internal VAL Invoke(string func, VAL parameters, Memory DS) { VAL R0 = null; foreach (object function in functions) { if (function is IUserDefinedFunction) { R0 = ((IUserDefinedFunction)function).Function(func, parameters, DS); } else if (function is Functionn) { R0 = ((Functionn)function)(func, parameters, DS); } else if (function is KeyValuePair <string, Function1> ) { if (((KeyValuePair <string, Function1>)function).Key == func) { R0 = ((KeyValuePair <string, Function1>)function).Value(parameters, DS); } } if ((object)R0 != null) { return(R0); } } return(null); }
/// <summary> /// Add memory variable /// </summary> /// <param name="spacename"></param> /// <param name="source"></param> /// <returns></returns> public VAL Add(string spacename, Memory source) { VAL v = Assemble(source); this.Add(spacename, v); return(v); }
private static VAL HostTypeOffsetBoxing(object value, object host, object offset) { VAL v = VAL.Boxing1(value); v.temp = new HostOffset(host, offset); return(v); }
protected override void CreateClass() { var clss = new Class(ClassName) { Modifier = Modifier.Public | Modifier.Partial, Sorted = true }; builder.AddClass(clss); string code = ReadAllText(cmd.arg1); Memory DS = new Memory(); try { Script.Execute(code, DS); } catch (Exception ex) { cerr.WriteLine(ex.Message); return; } foreach (VAR var in DS.Names) { VAL val = DS[var]; create(clss, string.Empty, (string)var, val); } }
public static bool HostTypeAssign(VAL R0, VAL R1) { /*** * * CASE 2: * textbox1.Text ="Hello"; * * */ if (R0.temp != null && R0.temp is HostOffset) { HostOffset hosts = (HostOffset)R0.temp; object host = hosts.host; object offset = hosts.offset; if (offset is MethodInfo) { return(false); } return(HostTypeAssign(host, offset, R1.HostValue, R1.hty == HandlerActionType.Add)); } return(false); }
public static string SearchXmlConnectionString(VAL val) { if (val.Size != 3) { cerr.WriteLine("required 2 parameters on function config(file,path,value), 1: app.config/web.config name; 2: path to reach connection string; 3:connection string attribute"); return(null); } if (val[0].VALTYPE != VALTYPE.stringcon || val[1].VALTYPE != VALTYPE.stringcon || val[2].VALTYPE != VALTYPE.stringcon) { cerr.WriteLine("error on function config(file,path,value) argument type, 1: string, 2: string, 3:string"); return(null); } string xmlFile = (string)val[0]; string path = (string)val[1]; string value = (string)val[2]; try { return(SearchConnectionString(xmlFile, path, value)); } catch (Exception) { cerr.WriteLine($"cannot find connection string on {xmlFile}, path={path}"); return(null); } }
public static VAL Assign(VAL R0, VAL R1) { bool r = false; if (R0.ty != VALTYPE.funccon) { r = HostOperation.HostTypeAssign(R0, R1); } if (!r) { R0.ty = R1.ty; R0.Class = R1.Class; R0.hty = R1.hty; R0.value = R1.value; //R0.name = R1.name; if (R1.ty == VALTYPE.funccon || (R1.ty == VALTYPE.hostcon && (R1.value is MethodInfo || R1.value is MethodInfo[])) ) { R0.temp = R1.temp; //instance of CPU } } return(R0); }
public VAL InvokeFunction(string func, VAL parameters, Position position) { VAL ret = SystemFunction.Function(func, parameters, DS2, position); if ((object)ret == null) { if (userFunc != null) { ret = userFunc.Function(func, parameters, DS2); } } if ((object)ret == null) { ret = FunctionChain.Chain.Invoke(func, parameters, DS2); } if ((object)ret == null) { #if !EASYWAY throw new FunctionNotFoundException(position, string.Format("function {0}({1}) is not defined, or arguments are not matched.", func, parameters.List.ToString2())); #else VAL ret = new VAL(func, L); return(ret); #endif } return(ret); }
/// <summary> /// load cfg file from ftp site or web site /// </summary> /// <param name="val"></param> /// <param name="DS"></param> private static void include(VAL val, Memory DS) { if (val.Size != 1 || val[0].VALTYPE != VALTYPE.stringcon) { cerr.WriteLine("required 1 parameters on function include(file), file can be local disk file, hyperlink, and ftp link"); return; } string url = (string)val[0]; if (string.IsNullOrEmpty(url)) { return; } var link = FileLink.CreateLink(url); bool exists = false; try { exists = link.Exists; } catch (Exception ex) { cerr.WriteLine($"configuration file {link} doesn't exist, {ex.Message}"); return; } if (!exists) { cerr.WriteLine($"configuration file {link} doesn't exist"); return; } string code = null; try { code = link.ReadAllText(); } catch (Exception ex) { cerr.WriteLine($"failed to load configuration file {link}, {ex.Message}"); return; } if (string.IsNullOrEmpty(code)) { return; } try { Script.Execute(code, DS); } catch (Exception ex) { cerr.WriteLine($"configuration file format error in {link}, {ex.Message}"); } }
public object Evaluate(string code) { string json = Json.Serialize(new RemoteInputBlock { method = "Evaluate", code = code, mem = DS.Serialize() }); string result = RemoteAccess(uri, json); RemoteOutputBlock output = Json.Deserialize <RemoteOutputBlock>(result); if (!string.IsNullOrEmpty(output.err)) { throw new InvalidExpressionException($"failed to evaluate code: {code}, {output.err}"); } try { if (!string.IsNullOrEmpty(output.mem)) { DS = output.mem.Deserialize(); } VAL val = Script.Evaluate(output.ret); return(val.HostValue); } catch (Exception) { throw new Exception($"failed to evaluate code: {code}, {result}"); } }
public static object NewInstance(VAL val, object[] args) { object instance = HostType.NewInstance(val.Class, args); Val2Host(val, instance); return(instance); }
/*** * * 调用TIE的FunctionChain中函数 * * */ /// <summary> /// Invoke function defined in the function chains /// </summary> /// <param name="memory"></param> /// <param name="funcName">function name</param> /// <param name="parameters">function signatrue</param> /// <returns></returns> public static VAL InvokeChainedFunction(Memory memory, string funcName, object[] parameters) { Context context = new Context(memory); VAL arguments = new VAL(parameters); return(context.InvokeFunction(funcName, arguments, Position.UNKNOWN)); }
public static DataTable ToDataTable(this VAL val) { DataTable dataTable = new DataTable(); for (int i = 0; i < val.Size; i++) { VAL field = val[i]; VAL key = field[0]; VAL value = field[1]; Type ty; if (value.Value != null) { ty = value.Value.GetType(); } else { ty = typeof(string); } DataColumn dataColumn = new DataColumn(key.Str, ty); dataTable.Columns.Add(dataColumn); } DataRow dataRow = dataTable.NewRow(); ToDataRow(val, dataRow); dataTable.Rows.Add(dataRow); return(dataTable); }
/// <summary> /// Register a generic class /// </summary> /// <param name="typeName">type name in script</param> /// <param name="type">generic type</param> /// <returns></returns> public static bool Register(string typeName, Type type) { Computer.DS1.Add("$1", VAL.NewHostType(type)); Script.Execute(typeName + "=$1;", Computer.DS1); Computer.DS1.Remove("$1"); return(true); }
private void UserFuncCall(VAL func, VAL instance, bool arg0) { int count1 = -CS[IP + 1].operand.Addr - 1; if (moduleName == func.Class) { if (arg0) { VAL ip = SS.Pop(); SS.Push(instance); SS.Push(ip); count1++; } IP = (int)func.value; paramsCheck(IP, count1); } else { VALL L = SysFuncBeginParameter(); if (arg0) { L.Insert(instance); } VAL ret = InternalUserFuncCall(func, instance, new VAL(L)); SysFuncEnd(ret); } }
public static string Encode(object host, bool persistent) { Type type; if (host is MethodInfo) { MethodInfo methodInfo = (MethodInfo)host; if (methodInfo.IsStatic) { return(methodInfo.ReflectedType.FullName + "." + methodInfo.Name); } else { return(methodInfo.Name); } } else if (host is Type) { type = (Type)host; return(string.Format("typeof({0})", type.FullName)); } else { type = host.GetType(); } if (type.IsEnum) //处理enum常量 { return(HostOperation.EnumBitFlags(host)); } if (host is DateTime) { return(string.Format("new {0}({1})", typeof(DateTime).FullName, ((DateTime)host).Ticks)); } VAL val = HostValization.Host2Valor(host); if (persistent) { return(val.Valor); } else { //default contructor if (HostCoding.HasContructor(type, new Type[] {})) { return(string.Format("new {0}()", type.FullName)); //有缺省的constructor } if (type.FullName == host.ToString()) { return(string.Format("new {0}(...)", type.FullName)); } else { return(string.Format("new {0}({1})", type.FullName, host)); } } }
public static VAL Class2VAL(object instance, VAL val) { val["ClassName"] = new VAL(instance.GetType().FullName); FieldInfo[] fields = instance.GetType().GetFields(); foreach (FieldInfo fieldInfo in fields) { if (fieldInfo.IsStatic) continue; Attribute[] attributes = CustomAttributeProvider.GetAttributes<NonValizedAttribute>(fieldInfo); if (attributes.Length != 0) continue; attributes = CustomAttributeProvider.GetAttributes<AssociationAttribute>(fieldInfo); if (attributes.Length != 0) continue; val[fieldInfo.Name] = VAL.Boxing(fieldInfo.GetValue(instance)); } PropertyInfo[] properties = instance.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in properties) { ValizableAttribute[] attributes = CustomAttributeProvider.GetAttributes<ValizableAttribute>(propertyInfo); if (attributes.Length !=0 && propertyInfo.CanRead) val[propertyInfo.Name] = VAL.Boxing(propertyInfo.GetValue(instance, null)); else continue; } return val; }
private static string ToXML(VAL val, string tag, int tab) { StringWriter o = new StringWriter(); if (val.IsAssociativeArray()) { o.Write(Indent(tab)); o.WriteLine("<" + tag + ">"); for (int i = 0; i < val.Size; i++) { VAL v = val[i]; o.Write(ToXML(v[1], v[0].Str, tab + 1)); } o.Write(Indent(tab)); o.WriteLine("</" + tag + ">"); } else if (val.ty == VALTYPE.listcon) { for (int j = 0; j < val.Size; j++) { VAL v = val[j]; o.Write(ToXML(v, tag, tab + 1)); } } else { o.Write(Indent(tab)); o.Write("<" + tag + ">"); o.Write(XmlString(val.ToString2())); o.WriteLine("</" + tag + ">"); } return(o.ToString()); }
public static string ToJson(DataTable dt) { //array if (dt.Columns.Count == 1) { //string name = dt.Columns[0].ColumnName; string json = VAL.Boxing(dt.ToArray(row => row[0])).ToJson(); //string.Format("{0}={1}", name, json); return json; } string[] columns = dt.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray(); VAL L = new VAL(); foreach (DataRow row in dt.Rows) { VAL V = new VAL(); for (int i = 0; i < columns.Length; i++) { V.AddMember(columns[i], row[i]); } L.Add(V); } return L.ToJson(); }
public static VAL Slice(VALL arr) { VAL V = arr[0]; int start = 0; int stop = -1; int step = 1; switch (arr.Size) { case 1: break; case 2: start = arr[1].Intcon; break; case 3: start = arr[1].Intcon; stop = arr[2].Intcon; break; case 4: start = arr[1].Intcon; stop = arr[2].Intcon; step = arr[3].Intcon; break; } return(new VAL(V.List.Slice(start, stop, step))); }
private object PrepareDelegate(Type parameterType, object val) { Type gty1 = parameterType.GetGenericTypeDefinition(); Type[] gpty1 = parameterType.GetGenericArguments(); MethodInfo method1 = parameterType.GetMethod("Invoke"); if (val is MethodInfo) { MethodInfo method2 = (MethodInfo)val; if (gas.MatchGenericMethod(method1, method2)) { Type[] gpty2 = gas.ConstructGenericArguments(gpty1); parameterType = gty1.MakeGenericType(gpty2); val = Delegate.CreateDelegate(parameterType, null, method2); return(val); } else { return(null); } } else if (val is MulticastDelegate) { MethodInfo method2 = ((MulticastDelegate)val).GetType().GetMethod("Invoke"); if (gas.MatchGenericMethod(method1, method2)) { return(val); } else { return(null); } } else if (val is VAL) { VAL func = (VAL)val; if (func.ty == VALTYPE.funccon) { int argc = DynamicDelegate.FuncArgc(func); Type[] pTypes = DynamicDelegate.GetDelegateParameterTypes(parameterType); if (argc == pTypes.Length) { Type[] gpty2 = gas.ConstructGenericArguments(gpty1); if (gpty2 == null) { throw new HostTypeException("Generic Type is not matched on {0}", parameterType); } parameterType = gty1.MakeGenericType(gpty2); return(DynamicDelegate.ToDelegate(parameterType, val)); } return(null); } } return(null); }
private void paramsArray(int argc1, int argc2) { int diff = argc1 - argc2; VAL retAddr = SS.Pop(); VALL L1 = new VALL(); for (int i = 0; i < argc2 - 1; i++) { L1.Insert(SS.Pop()); } VALL L2 = new VALL(); for (int i = 0; i <= diff; i++) { L2.Add(SS.Pop()); } SS.Push(new VAL(L2)); for (int i = 0; i < argc2 - 1; i++) { SS.Push(L1[i]); } SS.Push(retAddr); CS[retAddr.Intcon].operand.Addr += diff; }
public static VAL ToVal(this DataSet ds) { string xml = ds.ToXml(); string code = new VAL(xml).ToString(); code = string.Format("devalize({0}, new System.Data.DataSet())", code); return new VAL(code); }
private VAL GetScopeVAL(string scope) { VAL THIS = new VAL(); if (scope == "") { return(THIS); } string[] L = Module.ParseScope(scope); THIS = GetVAL(L[0], false); int i = 1; while (i < L.Length) { string id = L[i]; if (!THIS[id].Defined) { THIS[id] = new VAL(); } THIS = THIS[id]; i++; } THIS.name = scope; return(THIS); }
public string GenerateTieScript(bool flat) { List <string> statements = new List <string>(); if (flat) { foreach (VAR var in DS.Names) { VAL val = DS[var]; createConfigFile(statements, string.Empty, (string)var, val); } } else { foreach (VAR var in DS.Names) { VAL val = DS[var]; string text = $"{var} = {val.ToExJson()};"; statements.Add(text); statements.Add(string.Empty); } } return(string.Join(Environment.NewLine, statements)); }
public CPU(Module module, Context context) { this.context = context; this.scope = ""; this.moduleName = module.moduleName; position = new Position(module.moduleName, null); SS = new StackSegment <VAL>(Constant.MAX_STACK); for (int i = 0; i < SS.Size; i++) { SS[i] = new VAL(); } ES = new StackSegment <VAL>(Constant.MAX_EXTRA); for (int i = 0; i < ES.Size; i++) { ES[i] = new VAL(); } EX = new StackSegment <int>(Constant.MAX_EXCEPTION); REG = new Register(SS); IP = module.IP1; BP = 0; SI = 0; CS = module.CS; }
public static VAL InvokeMethod(MethodInfo methodInfo, object host, object[] arguments) { try { object obj = methodInfo.Invoke(host, arguments); return(VAL.Boxing1(obj)); } catch (Exception e) { foreach (object arg in arguments) { if (arg is Delegate) { Delegate d = (Delegate)arg; if (d.Method.Name == Constant.FUNC_CON_INSTANCE_INVOKE) { throw new HostTypeException("Call delegate {0} failed in {1} of {2}", d, methodInfo, host); } } } throw new HostTypeException("Call failed({0}) in {1} of {2}", e, methodInfo, host); } }
private void setter(VAL arr, VAL value) { switch (arr.ty) { case VALTYPE.intcon: int pos = arr.Intcon; if (pos < list.Count && pos >= 0) { list[pos] = value; } else if (pos >= 0) { //subscript number > size of array, increase size of array automatically while (pos > list.Count) { list.Add(new VAL()); } list.Add(value); } else //pos < 0 { pos = Pos(pos); list[pos] = value; } return; case VALTYPE.stringcon: string key = arr.Str; foreach (VAL v in list) { if (v.ty != VALTYPE.listcon) { continue; } string prop = v[0].Str; if (v.Size == 2 && v[0].ty == VALTYPE.stringcon && key == prop) { v.List.Remove(v[1]); v.List.Add(value); return; } else if (prop == Expression.BASE_INSTANCE) { VAL BS = v[1][key]; if (BS.Defined) { HostOperation.Assign(BS, value); return; } } } Add(key, value); return; } }
private static VAL WriteVAL(DataTable dt, JsonStyle style) { string[] columns = dt.Columns.Cast <DataColumn>().Select(col => col.ColumnName).ToArray(); VAL L = new VAL(); foreach (DataRow row in dt.Rows) { VAL V = new VAL(); for (int i = 0; i < columns.Length; i++) { object obj; switch (row[i]) { case Guid x: obj = "{" + x.ToString() + "}"; break; case DBNull NULL: obj = null; break; default: obj = row[i]; break; } V.AddMember(columns[i], obj); } L.Add(V); } return(L); }
public VALL(ValizationInfo info) { VAL val = info.ToVAL(); ty = HostType.GetType((string)val[0].value); list = ((VALL)val[1].value).list; }
public static string ToPrimitive(object value) { //make double value likes integer, e.g. ToPrimitive(25.0) returns "25, ToPrimitive(25.3) returns "25.3" if (value is double) { return(value.ToString()); } else if (value is Guid) { return($"new Guid(\"{value}\")"); } else if (value is CodeString) { return(value.ToString()); } else if (value is byte[]) { var hex = (value as byte[]) .Select(b => $"0x{b:X}") .Aggregate((b1, b2) => $"{b1},{b2}"); return("new byte[] {" + hex + "}"); //return "new byte[] {0x" + BitConverter.ToString((byte[])value).Replace("-", ",0x") + "}"; } return(VAL.Boxing(value).ToString()); }
public static VAL EnumOperation(VAL R0, VAL R1, object value) { Type type0 = R0.value.GetType(); Type type1 = R1.value.GetType(); Type type = null; if (type1.IsEnum) { type = type1; } else if (type0.IsEnum) { type = type0; } if (type != null) { return(VAL.Boxing1(Enum.ToObject(type, value))); } else { return(VAL.Boxing1(value)); } }
/// <summary> /// return attribute description /// </summary> /// <returns></returns> public override string ToString() { string attribute = typeof(FieldAttribute).Name.Replace("Attribute", ""); //handling escape letter in the Caption string field = new VAL(Caption).ToString(); return string.Format("[{0}({1})]", attribute, field); }
public static VAL functions(string func, VAL parameters, Memory DS) { if (func != "devalize" && parameters.Size != 2) return null; var L0 = parameters[0]; var L1 = parameters[1]; Valizer.Devalize(L0, L1.Value); return L1; }
public static VAL ToVAL(this DataRow dataRow, VAL val) { foreach (DataColumn dataColumn in dataRow.Table.Columns) { val[dataColumn.ColumnName] = VAL.Boxing(dataRow[dataColumn.ColumnName]); } return val; }
public static VAL Class2VAL(object instance) { VAL val = new VAL(); return Class2VAL(instance,val); }
public static object VAL2Class(VAL val, object instance) { FieldInfo[] fields = instance.GetType().GetFields(); foreach (FieldInfo fieldInfo in fields) { if (fieldInfo.IsStatic) continue; try { VAL p = val[fieldInfo.Name]; if (p.Defined) { if(fieldInfo.FieldType == typeof(VAL)) fieldInfo.SetValue(instance, p); else fieldInfo.SetValue(instance, p.HostValue); } } catch (ArgumentException) { } } PropertyInfo[] properties = instance.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in properties) { try { VAL p = val[propertyInfo.Name]; if (p.Defined && propertyInfo.CanWrite) { if(propertyInfo.PropertyType == typeof(VAL)) propertyInfo.SetValue(instance, p, null); else propertyInfo.SetValue(instance, p.HostValue, null); } } catch (ArgumentException) { } } return instance; }
public static DataSet ToDataSet(DataSet host, Type type, VAL val) { string code = (string)val; return code.ToDataSet(host); }
public static DataTable ToDataTable(DataTable host, Type type, VAL val) { string code = (string)val; return code.ToDataTable(host); }
public void SetVAL(VAL val) { Conversion.VAL2Class(val, this); }
public void SetField(string fieldName, VAL value) { VAL BASE = Script.Evaluate(scope, memory); BASE[name][fieldName] = value; }
protected PersistentValue(VAL val) { Conversion.VAL2Class(val, this); }