private static void BindingControl(ITagLink taglink, List <TagNodeHandle> valueChangedList, ExpressionEval eval) { var ctrl = taglink as UIElement; if (ctrl == null) { return; } var complex = taglink as ITagReader; if (complex != null) { string txt = complex.TagReadText; if (!string.IsNullOrEmpty(txt)) { foreach (var v in txt.GetListFromText()) { ITagLink tagConn = complex; string[] strs = v.Key.Split('.'); if (strs.Length > 1) { for (int i = 0; i < strs.Length - 1; i++) { var c = tagConn as ITagReader; if (c == null || c.Children == null) { break; } foreach (var item in c.Children) { if (item.Node == strs[i]) { tagConn = item; break; } } } } var r = tagConn as ITagReader; var key = strs[strs.Length - 1]; try { var action = r.SetTagReader(key, eval.Eval(v.Value)); if (action != null) { action(); ValueChangedEventHandler handle = (s1, e1) => { ctrl.InvokeAsynchronously(action); }; foreach (ITag tag in eval.TagList) { valueChangedList.Add(new TagNodeHandle(tag.ID, key, tagConn, handle)); tag.ValueChanged += handle; } } } catch (Exception e) { App.AddErrorLog(e); MessageBox.Show(string.Format("设备'{0}'属性'{1}'的值'{2}'转化出错!", string.IsNullOrEmpty(r.Node) ? r.GetType().ToString() : r.Node, key, v.Value)); } if (Attribute.IsDefined(tagConn.GetType(), typeof(StartableAttribute), false)) { FrameworkElement element = tagConn as FrameworkElement; element.Cursor = Cursors.UpArrow; element.AddHandler(UIElement.MouseEnterEvent, new MouseEventHandler(element_MouseEnter)); element.AddHandler(UIElement.MouseLeaveEvent, new MouseEventHandler(element_MouseLeave)); element.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(element_MouseLeftButtonDown)); } var hmi = tagConn as HMIControlBase; if (hmi != null && hmi.ShowCaption && !string.IsNullOrEmpty(hmi.Caption)) { AdornerLayer lay = AdornerLayer.GetAdornerLayer(hmi); if (lay != null) { TextAdorner frame = new TextAdorner(hmi); frame.Text = hmi.Caption; lay.Add(frame); } } } } } var writer = taglink as ITagWriter; if (writer != null && !string.IsNullOrEmpty(writer.TagWriteText)) { var delgts = new List <Delegate>(); foreach (var item in writer.TagWriteText.GetListFromText()) { try { if (!string.IsNullOrEmpty(item.Value)) { delgts.Add(eval.WriteEval(item.Key, item.Value)); } else { delgts.Add(eval.WriteEval(item.Key)); } } catch (Exception e) { App.AddErrorLog(e); MessageBox.Show(string.Format("设备{0}变量{1}写入PLC公式转换失败", taglink.Node, item.Key) + "\n" + e.Message); } writer.SetTagWriter(delgts); } } }
void InitServerByDatabase() { try { using (var dataReader = DataHelper.Instance.ExecuteProcedureReader("InitServer", DataHelper.CreateParam("@TYPE", System.Data.SqlDbType.Int, 1))) { if (dataReader == null) { Environment.Exit(0); } //dataReader.Read(); dataReader.Read(); int count = dataReader.GetInt32(0); _list = new List <TagMetaData>(count); _mapping = new Dictionary <string, ITag>(count); dataReader.NextResult(); while (dataReader.Read()) { _list.Add(new TagMetaData(dataReader.GetInt16(0), dataReader.GetInt16(1), dataReader.GetString(2), dataReader.GetString(3), (DataType)dataReader.GetByte(4), (ushort)dataReader.GetInt16(5), dataReader.GetBoolean(6), dataReader.GetFloat(7), dataReader.GetFloat(8), dataReader.GetInt32(9))); //_list[i].Description = dataReader.GetSqlString(6).Value; } _list.Sort(); if (reader != null && group == null) { group = reader.AddGroup("Group1", 1, 0, 0, true) as ClientGroup; group.AddItems(_list); } dataReader.NextResult(); _conditions = new List <ICondition>(); _conditionList = new ObservableCollection <ICondition>(); while (dataReader.Read()) { int id = dataReader.GetInt32(0); AlarmType type = (AlarmType)dataReader.GetInt32(2); ICondition cond; string source = dataReader.GetString(1); if (_conditions.Count > 0) { cond = _conditions[_conditions.Count - 1]; if (cond.ID == id) { cond.AddSubCondition(new SubCondition((SubAlarmType)dataReader.GetInt32(9), dataReader.GetFloat(10), (Severity)dataReader.GetByte(11), dataReader.GetString(12), dataReader.GetBoolean(13))); continue; } } switch (type) { case AlarmType.Complex: cond = new ComplexCondition(id, source, dataReader.GetString(6), dataReader.GetFloat(7), dataReader.GetInt32(8)); break; case AlarmType.Level: cond = new LevelAlarm(id, source, dataReader.GetString(6), dataReader.GetFloat(7), dataReader.GetInt32(8)); break; case AlarmType.Dev: cond = new DevAlarm(id, (ConditionType)dataReader.GetByte(4), source, dataReader.GetString(6), dataReader.GetFloat(5), dataReader.GetFloat(7), dataReader.GetInt32(8)); break; case AlarmType.ROC: cond = new ROCAlarm(id, source, dataReader.GetString(6), dataReader.GetFloat(7), dataReader.GetInt32(8)); break; case AlarmType.Quality: cond = new QualitiesAlarm(id, source, dataReader.GetString(6)); break; default: cond = new DigitAlarm(id, source, dataReader.GetString(6), dataReader.GetInt32(8)); break; } cond.AddSubCondition(new SubCondition((SubAlarmType)dataReader.GetInt32(9), dataReader.GetFloat(10), (Severity)dataReader.GetByte(11), dataReader.GetString(12), dataReader.GetBoolean(13))); cond.IsEnabled = dataReader.GetBoolean(3); var simpcond = cond as SimpleCondition; if (simpcond != null) { simpcond.Tag = this[source]; } else { var complexcond = cond as ComplexCondition; if (complexcond != null) { var action = complexcond.SetFunction(reval.Eval(source)); if (action != null) { ValueChangedEventHandler handle = (s1, e1) => { action(); }; foreach (ITag tag in reval.TagList) { tag.ValueChanged += handle;// tag.Refresh(); } } } } cond.AlarmActive += new AlarmEventHandler(cond_SendAlarm); cond.AlarmAck += new EventHandler(cond_AckAlarm); //_conditions.Add(cond);// UpdateCondition(cond); _conditions.Add(cond); } dataReader.NextResult(); while (dataReader.Read()) { _scales.Add(new Scaling(dataReader.GetInt16(0), (ScaleType)dataReader.GetByte(1), dataReader.GetFloat(2), dataReader.GetFloat(3), dataReader.GetFloat(4), dataReader.GetFloat(5))); } } reval.Clear(); _scales.Sort(); _compare = new CompareCondBySource(); _conditions.Sort(_compare); } catch (Exception e) { App.AddErrorLog(e); Environment.Exit(0); } }
public void buildAS3Throw(CompileEnv env, ASTool.AS3.AS3Throw as3throw, Builder builder) { if (as3throw.Exception != null) { var testV = ExpressionEval.Eval(as3throw.Exception); if (testV != null && as3throw.Exception.Value.IsReg) { OpStep op = new OpStep(OpCode.raise_error, new SourceToken(as3throw.Token.line, as3throw.Token.ptr, as3throw.Token.sourceFile)); op.arg1 = new ASBinCode.rtData.RightValue(testV); op.arg1Type = testV.rtType; op.arg2 = null; op.arg2Type = RunTimeDataType.unknown; op.reg = null; op.regType = RunTimeDataType.unknown; env.block.opSteps.Add(op); } else { builder.buildExpression(env, as3throw.Exception); OpStep op = new OpStep(OpCode.raise_error, new SourceToken(as3throw.Token.line, as3throw.Token.ptr, as3throw.Token.sourceFile)); RightValueBase lv = builds.ExpressionBuilder.getRightValue(env, as3throw.Exception.Value, as3throw.Token, builder); op.arg1 = lv; op.arg1Type = lv.valueType; op.arg2 = null; op.arg2Type = RunTimeDataType.unknown; op.reg = null; op.regType = RunTimeDataType.unknown; env.block.opSteps.Add(op); } } else { //***只有在catch块中才能throw***; int l = 0; for (int i = 0; i < env.block.opSteps.Count; i++) { if (env.block.opSteps[i].opCode == OpCode.enter_catch) { l++; } else if (env.block.opSteps[i].opCode == OpCode.quit_catch) { l--; } } if (l < 1) { throw new BuildException(as3throw.Token.line, as3throw.Token.ptr, as3throw.Token.sourceFile, "此处不能有throw;" ); } throw new BuildException(as3throw.Token.line, as3throw.Token.ptr, as3throw.Token.sourceFile, "throw必须有抛出的对象;" ); //OpStep op = new OpStep(OpCode.raise_error, new SourceToken(as3throw.Token.line, as3throw.Token.ptr, as3throw.Token.sourceFile)); //op.arg1 = null; //op.arg1Type = RunTimeDataType.unknown; //op.arg2 = null; //op.arg2Type = RunTimeDataType.unknown; //op.reg = null; //op.regType = RunTimeDataType.unknown; //env.block.opSteps.Add(op); } }
public static void Main(string[] args) { if (args.Length < 1 || !File.Exists(args[0])) { Console.WriteLine("tail.exe file.tail"); System.Environment.Exit(0); } Dictionary <string, TYPEDEF> typedefTable = new Dictionary <string, TYPEDEF> (); Dictionary <string, VARDECL> symbolTable = new Dictionary <string, VARDECL> (); string[] lines = File.ReadAllLines(args[0]); // Organize the TAIL contructs into lists. // Then process the lists. // I assume that if correct syntax is used and that // only TAIL expressions, typedefs, vardecls or empty lines // can exist in a tail file. int lineCount = 0; foreach (string line in lines) { try { if (line.ToUpper().StartsWith("TYPEDEF")) { TYPEDEF.Parse(line, typedefTable); } else if (line.ToUpper().StartsWith("VARDECL")) { new VARDECL(line, typedefTable, symbolTable); } else if (!String.IsNullOrEmpty(line)) { ExpressionEval.Eval(line, typedefTable, symbolTable); } } catch (SemanticException semex) { Console.WriteLine("Semantic Exception on line " + lineCount + " : " + semex.Message); } catch (SyntacticException synex) { Console.WriteLine("Syntax Exceptionon line " + lineCount + " : " + synex.Message); } catch (Exception ex) { Console.WriteLine("Internal Exception: " + ex.Message); } lineCount++; } /* * foreach (string str in typedefTable.Keys) * { * Console.WriteLine("Type: " + str); * } * * Console.WriteLine(""); * * foreach (string str in symbolTable.Keys) * { * Console.WriteLine("Var: " + str); * }*/ }
public void buildAS3Switch(CompileEnv env, ASTool.AS3.AS3Switch as3switch, Builder builder) { if (!string.IsNullOrEmpty(as3switch.label)) { //**包装一个block** ASTool.AS3.AS3Block tempblock = new ASTool.AS3.AS3Block(as3switch.Token); tempblock.CodeList = new List <ASTool.AS3.IAS3Stmt>(); tempblock.CodeList.Add(as3switch); tempblock.label = as3switch.label; as3switch.label = null; builder.buildStmt(env, tempblock); } else { //***先编译条件*** ASTool.AS3.AS3StmtExpressions expressions = new ASTool.AS3.AS3StmtExpressions(as3switch.Token); expressions.as3exprlist = new List <ASTool.AS3.AS3Expression>(); expressions.as3exprlist.Add(as3switch.Expr); builder.buildStmt(env, expressions); //**取判断条件*** ASTool.AS3.AS3Expression expr = as3switch.Expr; int defaults = 0; for (int i = 0; i < as3switch.CaseList.Count; i++) { ASTool.AS3.AS3SwitchCase c = as3switch.CaseList[i]; if (c.IsDefault) { defaults++; } if (defaults > 1) { throw new BuildException(c.token.line, c.token.ptr, c.token.sourceFile, "switch只能有1个default:"); } } List <string> switchlabels = new List <string>(); for (int i = 0; i < as3switch.CaseList.Count; i++) { ASTool.AS3.AS3SwitchCase c = as3switch.CaseList[i]; if (!c.IsDefault) { switchlabels.Add(env.makeLabel("SWITCH_CASE")); } else { switchlabels.Add(env.makeLabel("SWITCH_DEFAULT")); } } for (int i = 0; i < as3switch.CaseList.Count; i++) { ASTool.AS3.AS3SwitchCase c = as3switch.CaseList[i]; if (!c.IsDefault) { RunTimeValueBase v = ExpressionEval.Eval(c.Condition); if (v != null && c.Condition.Value.IsReg) { OpStep op = new OpStep(OpCode.assigning, new SourceToken(c.token.line, c.token.ptr, c.token.sourceFile)); var eax = env.createASTRegister(c.Condition.Value.Reg); eax.setEAXTypeWhenCompile(v.rtType); op.reg = eax; op.regType = eax.valueType; op.arg1 = new ASBinCode.rtData.RightValue(v); op.arg1Type = v.rtType; env.block.opSteps.Add(op); } else { ASTool.AS3.AS3StmtExpressions caseExpr = new ASTool.AS3.AS3StmtExpressions(c.token); caseExpr.as3exprlist = new List <ASTool.AS3.AS3Expression>(); caseExpr.as3exprlist.Add(c.Condition); builder.buildStmt(env, caseExpr); } { ASTool.AS3.Expr.AS3ExprStep step = new ASTool.AS3.Expr.AS3ExprStep(c.token); step.Type = ASTool.AS3.Expr.OpType.LogicEQ; step.OpCode = "=="; step.Arg1 = c.holdreg; step.Arg2 = expr.Value; step.Arg3 = c.Condition.Value; ASTool.AS3.AS3StmtExpressions compare = new ASTool.AS3.AS3StmtExpressions(c.token); ASTool.AS3.AS3Expression expression = new ASTool.AS3.AS3Expression(c.token); expression.exprStepList = new ASTool.AS3.Expr.AS3ExprStepList(); expression.exprStepList.Add(step); expression.Value = c.holdreg; compare.as3exprlist = new List <ASTool.AS3.AS3Expression>(); compare.as3exprlist.Add(expression); builder.buildStmt(env, compare); } { ASTool.AS3.AS3StmtExpressions conditionsJump = new ASTool.AS3.AS3StmtExpressions(c.token); ASTool.AS3.AS3Expression expression = new ASTool.AS3.AS3Expression(c.token); expression.exprStepList = new ASTool.AS3.Expr.AS3ExprStepList(); ASTool.AS3.Expr.AS3ExprStep step = new ASTool.AS3.Expr.AS3ExprStep(c.token); step.Type = ASTool.AS3.Expr.OpType.IF_GotoFlag; step.OpCode = switchlabels[i]; step.Arg1 = c.holdreg; expression.exprStepList.Add(step); conditionsJump.as3exprlist = new List <ASTool.AS3.AS3Expression>(); conditionsJump.as3exprlist.Add(expression); builder.buildStmt(env, conditionsJump); } } else { ASTool.AS3.AS3StmtExpressions jump = new ASTool.AS3.AS3StmtExpressions(c.token); ASTool.AS3.AS3Expression expression = new ASTool.AS3.AS3Expression(c.token); expression.exprStepList = new ASTool.AS3.Expr.AS3ExprStepList(); ASTool.AS3.Expr.AS3ExprStep step = new ASTool.AS3.Expr.AS3ExprStep(c.token); step.Type = ASTool.AS3.Expr.OpType.GotoFlag; step.OpCode = switchlabels[i]; expression.exprStepList.Add(step); jump.as3exprlist = new List <ASTool.AS3.AS3Expression>(); jump.as3exprlist.Add(expression); builder.buildStmt(env, jump); } } int lblid = env.getLabelId(); OpStep lbl_case_start = new OpStep(OpCode.flag, new SourceToken(as3switch.Token.line, as3switch.Token.ptr, as3switch.Token.sourceFile)); lbl_case_start.flag = "SWITCH_START_" + lblid; env.block.opSteps.Add(lbl_case_start); for (int i = 0; i < as3switch.CaseList.Count; i++) { ASTool.AS3.AS3SwitchCase c = as3switch.CaseList[i]; //if (!c.IsDefault) { OpStep lbl_case = new OpStep(OpCode.flag, new SourceToken(c.token.line, c.token.ptr, c.token.sourceFile)); lbl_case.flag = switchlabels[i]; env.block.opSteps.Add(lbl_case); } if (c.Body != null) { for (int j = 0; j < c.Body.Count; j++) { builder.buildStmt(env, c.Body[j]); } } } OpStep lbl_case_end = new OpStep(OpCode.flag, new SourceToken(as3switch.Token.line, as3switch.Token.ptr, as3switch.Token.sourceFile)); lbl_case_end.flag = "SWITCH_END_" + lblid; env.block.opSteps.Add(lbl_case_end); } }