internal void buildSecondTernaryOperator() { int type; string name = pop(out type); StatementData statement = getCurStatement(); FunctionData newFunc = new FunctionData(); CallData call = new CallData(); ValueData nname = new ValueData(); call.Name = nname; newFunc.Call = call; statement.Functions.Add(newFunc); FunctionData func = getLastFunction(); if (!func.IsValid()) { func.Call.SetParamClass((int)CallData.ParamClassEnum.PARAM_CLASS_TERNARY_OPERATOR); func.SetExtentClass((int)FunctionData.ExtentClassEnum.EXTENT_CLASS_STATEMENT); func.Call.Name.SetId(name); func.Call.Name.SetType(type); func.Call.Name.SetLine(getLastLineNumber()); } }
private void simplifyCallData(CallData data) { int ct = data.GetParamNum(); for (int i = 0; i < ct; ++i) { ISyntaxComponent param = data.GetParam(i); StatementData statement = param as StatementData; if (null != statement) { data.SetParam(i, simplifyStatement(statement)); } else { FunctionData func = param as FunctionData; if (null != func) { data.SetParam(i, simplifyStatement(func)); } else { CallData call = param as CallData; if (null != call) { data.SetParam(i, simplifyStatement(call)); } } } } }
internal void buildFirstTernaryOperator() { int type; string name = pop(out type); StatementData arg = popStatement(); StatementData _statement = newStatement(); FunctionData first = _statement.First; first.Call.Name.SetLine(getLastLineNumber()); _statement.CopyFirstComments(arg); arg.FirstComments.Clear(); mStatementSemanticStack.Push(_statement); StatementData statement = getCurStatement(); FunctionData func = getLastFunction(); if (!func.IsValid()) { func.Call.SetParamClass((int)CallData.ParamClassEnum.PARAM_CLASS_TERNARY_OPERATOR); func.SetExtentClass((int)FunctionData.ExtentClassEnum.EXTENT_CLASS_STATEMENT); func.Call.Name.SetId(name); func.Call.Name.SetType(type); if (arg.IsValid()) { func.Call.AddParams(arg); } } }
internal void buildOperator() { int type; string name = pop(out type); StatementData arg = popStatement(); StatementData _statement = newStatement(); FunctionData first = _statement.First; first.Call.Name.SetLine(getLastLineNumber()); mStatementSemanticStack.Push(_statement); StatementData statement = getCurStatement(); FunctionData func = getLastFunction(); if (!func.IsValid()) { func.Call.SetParamClass((int)CallData.ParamClassEnum.PARAM_CLASS_OPERATOR); func.Call.Name.SetId(name); func.Call.Name.SetType(type); if (arg.IsValid()) { func.Call.AddParams(arg); } } }
private void simplifyStatementData(StatementData data) { for (int i = 0; i < data.Functions.Count; ++i) { FunctionData func = data.Functions[i]; simplifyFunctionData(func); } }
internal void beginStatement() { StatementData statement = newStatement(); FunctionData first = statement.First; first.Call.Name.SetLine(getLastLineNumber()); mStatementSemanticStack.Push(statement); }
private FunctionData newFunctionOfStatement(StatementData data) { FunctionData func = new FunctionData(); ValueData name = new ValueData(); func.Name = name; data.Functions.Add(func); return(func); }
public static void ClassifyByRules(List <StatementData> statementDatas, List <SyncData> syncs) { HashSet <string> usedFileMasks = new HashSet <string>(); bool[] fileMaskPassed = new bool[syncs.Count]; StatementData forLeftStatementData = statementDatas[statementDatas.Count - 1]; statementDatas.RemoveAt(statementDatas.Count - 1); for (int i = 0; i < syncs.Count; i++) { string[] splittedFilePath = syncs[i].FD.RelativePath.Split(new string[] { "//", "\\\\" }, StringSplitOptions.RemoveEmptyEntries); string fileName = splittedFilePath[splittedFilePath.Length - 1]; for (int j = 0; j < statementDatas.Count; j++) { if (syncs[i].WasTriggeredBySyncRule) { break; } if (FileFitsMask(fileName, statementDatas[j].FileMask)) { fileMaskPassed[i] = true; usedFileMasks.Add(statementDatas[j].FileMask); for (int k = 0; k < statementDatas[j].SyncRules.Count; k++) { if (syncs[i].WasTriggeredBySyncRule) { break; } RunRule(statementDatas[j].SyncRules[k], syncs[i]); } } } } for (int i = 0; i < syncs.Count; i++) { if (syncs[i].WasTriggeredBySyncRule /*<= Might be reworked =>*/ || fileMaskPassed[i]) { continue; } for (int k = 0; k < forLeftStatementData.SyncRules.Count; k++) { if (syncs[i].WasTriggeredBySyncRule) { break; } RunRule(forLeftStatementData.SyncRules[k], syncs[i]); } } }
private StatementData newStatementWithOneFunction() { StatementData data = new StatementData(); FunctionData func = new FunctionData(); ValueData name = new ValueData(); func.Name = name; data.Functions.Add(func); return(data); }
private StatementData getCurStatement() { if (mStatementSemanticStack.Count == 0) { return(null); } StatementData topData = mStatementSemanticStack.Peek(); return(topData); }
private StatementData popStatement() { if (mStatementSemanticStack.Count == 0) { return(newStatement()); } StatementData cdata = mStatementSemanticStack.Pop(); return(cdata); }
private string RenderPlainText(StatementData data) { var result = new StringBuilder().AppendLine($"Statement for {data.Customer}"); foreach (var perf in data.Performances) { result.AppendLine($" {perf.Play.Name}: {Usd(perf.Amount)} ({perf.Audience} seats)"); } result.AppendLine($"Amount owed is {Usd(data.TotalAmount)}"); result.Append($"You earned {data.TotalVolumeCredits} credits"); return(result.ToString()); }
private StatementData newStatement() { StatementData data = new StatementData(); FunctionData func = new FunctionData(); CallData call = new CallData(); ValueData name = new ValueData(); call.Name = name; func.Call = call; data.Functions.Add(func); return(data); }
private AbstractSyntaxComponent simplifyStatement(StatementData data) { //对语句进行化简(语法分析过程中为了方便,全部按完整StatementData来构造,这里化简为原来的类型:ValueData/CallData/FunctionData等,主要涉及参数与语句部分) if (data.Functions.Count == 1) { //只有一个函数的语句退化为函数(再按函数进一步退化)。 FunctionData func = data.Functions[0]; func.CopyComments(data); return(simplifyStatement(func)); } return(data); }
private void cppOnFunctionEnd() { var statement = getCurStatement(); var first = getFirstFunction(); var second = getSecondFunction(); var last = getLastFunction(); var firstId = first.GetId(); var secondId = second.GetId(); var lastId = last.GetId(); if (firstId == "if" && first.HaveStatement() && lastId != "if" && lastId != "else" || firstId == "do" && lastId != "while" || firstId == "try" && lastId != "catch" && lastId != "finally") { statement.Functions.Remove(last); endStatement(); beginStatement(); var newStatement = getCurStatement(); newStatement.Functions.Add(last); return; } //构造函数初始化列表转换为:的参数列表 if (IsCppConstructor(statement, true)) { markParenthesisParam(); beginStatement(); return; } //构造函数初始化列表结束需要将函数体与最后一个初始化变量拆分 if (last.HaveStatement() && mStatementSemanticStack.Count > 1) { StatementData parentSt = getCurParentStatement(); if (IsCppConstructor(parentSt, false)) { var ps = new List <ISyntaxComponent>(last.Params); if (last.IsHighOrder) { var curStatement = getCurStatement(); curStatement.Functions[curStatement.Functions.Count - 1] = last.LowerOrderFunction; } else { last.SetParamClass((int)FunctionData.ParamClassEnum.PARAM_CLASS_NOTHING); last.Params.Clear(); } endStatement(); buildHighOrderFunction(); markStatement(); last = getLastFunction(); last.Params.AddRange(ps); } } }
internal void endStatement() { StatementData statement = popStatement(); if (mStatementSemanticStack.Count == 0) { simplifyStatementData(statement); if (!statement.IsValid()) { //_epsilon_表达式无语句语义 return; } //顶层元素结束 ScriptableDataInfo scriptData = new ScriptableDataInfo(); scriptData.CopyFrom(statement); scriptData.SetLoaded(true); mScriptDatas.Add(scriptData); } else { ISyntaxComponent statementSyntax = simplifyStatement(statement); FunctionData func = getLastFunction(); switch (func.GetExtentClass()) { case (int)FunctionData.ExtentClassEnum.EXTENT_CLASS_NOTHING: { /*这段先暂时注掉,忘记原来为啥要允许空语句作函数参数了(现在对于明确的空串参数不会判断为无效语句了) * if(func.Call.getParamClass()==(int)CallData.ParamClassEnum.PARAM_CLASS_OPERATOR && !statement.IsValid()) * return;//操作符就不支持空语句作参数了 * //函数参数,允许空语句,用于表达默认状态(副作用是a()与a[]将总是会有一个空语句参数)。 */ if (statement.IsValid()) { func.Call.AddParams(statementSyntax); } } break; case (int)FunctionData.ExtentClassEnum.EXTENT_CLASS_STATEMENT: { if (!statement.IsValid()) { //_epsilon_表达式无语句语义 return; } //函数扩展语句部分 func.AddStatement(statementSyntax); } break; } } }
protected IList <T> ReadSection <T>(StatementData dataContainer) where T : new() { var classMapping = classMappingFactory.GetInstance <IClassMapping <T>, T>(); if (classMapping == null) { return(null); } var result = ReadSection <T>(dataContainer, classMapping.Section, classMapping as IClassMapping); return(result); }
protected override void Load(StatementData statementData) { if (statementData.Functions.Count == 2) { FunctionData first = statementData.First; FunctionData second = statementData.Second; if (null != first && null != first.Call && null != second && null != second.Call) { Load(first); LoadVarName(second.Call); } } }
internal StatementData CreateStatementData( Invoice invoice, IReadOnlyDictionary <string, Play> plays) { var result = new StatementData(invoice.Performances.ConvertAll(EnrichPerformance), invoice.Customer); result.TotalAmount = TotalAmount(result); result.TotalVolumeCredits = TotalVolumeCredits(result); return(result); EnrichedPerformance EnrichPerformance(Performance aPerformance) { var calculator = CreatePerformanceCalculator(aPerformance, PlayFor(aPerformance)); return(new EnrichedPerformance(aPerformance, calculator.Play) { Amount = calculator.Amount, VolumeCredits = calculator.VolumeCredits }); } Play PlayFor(Performance aPerformance) { return(plays[aPerformance.PlayId]); } int TotalAmount(StatementData data) { return(data.Performances.Sum(element => element.Amount)); } int TotalVolumeCredits(StatementData data) { return(data.Performances.Sum(element => element.VolumeCredits)); } PerformanceCalculator CreatePerformanceCalculator(Performance aPerformance, Play aPlay) { switch (aPlay.PayType) { case PayType.Tragedy: return(new TragedyCalculator(aPerformance, aPlay)); case PayType.Comedy: return(new ComedyCalculator(aPerformance, aPlay)); default: throw new Exception($"unknown type: {aPlay.PayType}"); } } }
private string RenderHtml(StatementData data) { var result = new StringBuilder().AppendLine($"<h1>Statement for {data.Customer}</h1>"); result.AppendLine("<table>"); result.Append("<tr><th>play</th><th>seats</th><th>cost</th></tr>"); foreach (var perf in data.Performances) { result.Append($" <tr><td>{perf.Play.Name}</td><td>{perf.Audience}</td>"); result.AppendLine($"<td>{Usd(perf.Amount)}</td></tr>"); } result.AppendLine("</table>"); result.AppendLine($"<p>Amount owed is <em>{Usd(data.TotalAmount)}</em></p>"); result.AppendLine($"<p>You earned <em>{data.TotalVolumeCredits}</em> credits</p>"); return(result.ToString()); }
internal void markOperator() { StatementData statement = getCurStatement(); bool commentOnNewLine; IList <string> cmts = GetComments(out commentOnNewLine); if (cmts.Count > 0) { if (statement.LastComments.Count <= 0) { statement.LastCommentOnNewLine = commentOnNewLine; } statement.LastComments.AddRange(cmts); } }
private ISyntaxComponent simplifyStatement(StatementData data) { //对语句进行化简(语法分析过程中为了方便,全部按完整StatementData来构造,这里化简为原来的类型:ValueData/CallData/FunctionData等,主要涉及参数与语句部分) if (data.Functions.Count == 1) { //只有一个函数的语句退化为函数(再按函数进一步退化)。 FunctionData func = data.Functions[0]; return(simplifyStatement(func)); } else { //多个函数构成的语句不会退化,只对各个函数的参数与语句部分进行化简。 simplifyStatementData(data); return(data); } }
internal void beginFunction() { StatementData statement = getCurStatement(); FunctionData func = getLastFunction(); if (func.IsValid()) { //语句的多元函数的其它元函数名 FunctionData newFunc = new FunctionData(); CallData call = new CallData(); ValueData name = new ValueData(); call.Name = name; newFunc.Call = call; statement.Functions.Add(newFunc); } }
/// --------------------------------------------------------------- /// 特定语言工具函数 /// --------------------------------------------------------------- private bool IsCppConstructor(StatementData statement, bool curIsColon) { bool ret = false; var first = statement.First; var second = statement.Second; var third = statement.Third; var last = statement.Last; var firstId = first.GetId(); var secondId = second.GetId(); var thirdId = third.GetId(); if (firstId == "explicit" && second.HaveId() && second.HaveParam() && thirdId == ":" && (!curIsColon || third == last) || first.HaveId() && first.HaveParam() && secondId == ":" && (!curIsColon || second == last)) { ret = true; } return(ret); }
internal void beginStatement() { StatementData statement = newStatement(); bool commentOnNewLine; IList <string> cmts = GetComments(out commentOnNewLine); if (cmts.Count > 0) { if (statement.FirstComments.Count <= 0) { statement.FirstCommentOnNewLine = commentOnNewLine; } statement.FirstComments.AddRange(cmts); } mStatementSemanticStack.Push(statement); }
protected IList <object> ReadSection(Type type, StatementData dataContainer) { var classMappingTypeGeneric = typeof(IClassMapping <>); Type[] typeArgs = { type }; Type classMappingType = classMappingTypeGeneric.MakeGenericType(typeArgs); var classMapping = classMappingFactory.GetInstance(classMappingType) as IClassMapping; if (classMapping == null) { return(null); } var result = ReadSection(type, dataContainer, classMapping.Section, classMapping); return(result); }
private void buildOperator() { int type; string name = pop(out type); StatementData arg = popStatement(); ISyntaxComponent argComp = simplifyStatement(arg); StatementData _statement = newStatementWithOneFunction(); FunctionData first = _statement.First; first.Name.SetLine(getLastLineNumber()); _statement.CopyFirstComments(argComp); argComp.FirstComments.Clear(); mStatementSemanticStack.Push(_statement); StatementData statement = getCurStatement(); FunctionData func = getLastFunction(); if (!func.IsValid()) { if (name.Length > 0 && name[0] == '`') { func.SetParamClass((int)(FunctionData.ParamClassEnum.PARAM_CLASS_WRAP_INFIX_CALL_MASK | FunctionData.ParamClassEnum.PARAM_CLASS_OPERATOR)); func.Name.SetId(name.Substring(1)); func.Name.SetType(type); } else { func.SetParamClass((int)FunctionData.ParamClassEnum.PARAM_CLASS_OPERATOR); func.Name.SetId(name); func.Name.SetType(type); } if (argComp.IsValid()) { func.AddParam(argComp); } } }
private void buildSecondTernaryOperator() { int type; string name = pop(out type); StatementData statement = getCurStatement(); FunctionData newFunc = new FunctionData(); ValueData nname = new ValueData(); newFunc.Name = nname; statement.Functions.Add(newFunc); FunctionData func = getLastFunction(); if (!func.IsValid()) { func.SetParamClass((int)FunctionData.ParamClassEnum.PARAM_CLASS_TERNARY_OPERATOR); func.Name.SetId(name); func.Name.SetType(type); func.Name.SetLine(getLastLineNumber()); } }
private void buildFirstTernaryOperator() { int type; string name = pop(out type); StatementData arg = popStatement(); ISyntaxComponent argComp = simplifyStatement(arg); StatementData _statement = newStatementWithOneFunction(); FunctionData first = _statement.First; //三元运算符表示成op1(cond)(true_val)op2(false_val) first.LowerOrderFunction = new FunctionData(); first.LowerOrderFunction.Name = new ValueData(); first.LowerOrderFunction.Name.SetLine(getLastLineNumber()); _statement.CopyFirstComments(argComp); argComp.FirstComments.Clear(); mStatementSemanticStack.Push(_statement); StatementData statement = getCurStatement(); FunctionData func = getLastFunction(); if (!func.IsValid()) { func.LowerOrderFunction.SetParamClass((int)FunctionData.ParamClassEnum.PARAM_CLASS_PARENTHESIS); func.LowerOrderFunction.Name.SetId(name); func.LowerOrderFunction.Name.SetType(type); if (argComp.IsValid()) { func.LowerOrderFunction.AddParam(argComp); } func.SetParamClass((int)FunctionData.ParamClassEnum.PARAM_CLASS_TERNARY_OPERATOR); } }
public StatementData ParseStatement(Stream stream) { var res = new StatementData(); using (TextFieldParser parser = new TextFieldParser(stream)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); Section currentSection = null; while (!parser.EndOfData) { //Processing row string[] fields = parser.ReadFields(); if (fields.Length < 2) { continue; } if (currentSection == null || currentSection.Name != fields[0] || fields[1] == LineType.Header.ToString()) { currentSection = new Section() { Name = fields[0], Headers = fields[1] == LineType.Header.ToString() ? fields.Skip(2).ToList() : null }; res.Sections.Add(currentSection); } else { var line = new Line(fields[1], fields.Skip(2).ToArray()); currentSection.Lines.Add(line); } } } return(res); }
private Statement.Statement CreateStatement(StatementData statementValues) { Statement.Statement statement; var signal = _avalibleSignals.First(x => x.Name.Equals(statementValues.Signal)); if (_avalibleSignals.Any(x => x.Name.Equals(statementValues.SignalOrValue))) { var signal2 = _avalibleSignals.First(x => x.Name.Equals(statementValues.SignalOrValue)); switch (statementValues.ExpressionValue) { case "Equals": statement = new Statement.Equals(signal, signal2); break; case "Lagrer Then": statement = new Statement.LargerThen(signal, signal2); break; case "Less Then": statement = new Statement.LessThen(signal, signal2); break; case "Rising Edge": statement = new Statement.RisingEdge(signal, signal2); break; case "Falling Edge": statement = new Statement.FallingEdge(signal, signal2); break; default: throw new Exception(); } } else { double value; if (double.TryParse(statementValues.SignalOrValue, out value)) { switch (statementValues.ExpressionValue) { case "Equals": statement = new Statement.Equals(signal, value); break; case "Larger Then": statement = new Statement.LargerThen(signal, value); break; case "Less Then": statement = new Statement.LessThen(signal, value); break; case "Rising Edge": statement = new Statement.RisingEdge(signal, value); break; case "Falling Edge": statement = new Statement.FallingEdge(signal, value); break; default: throw new Exception(); } } else { throw new Exception(); } } return statement; }
private StatementData newStatement() { StatementData data = new StatementData(); FunctionData func = new FunctionData(); CallData call = new CallData(); ValueData name = new ValueData(); call.Name = name; func.Call = call; data.Functions.Add(func); return data; }
private AbstractSyntaxComponent simplifyStatement(StatementData data) { //对语句进行化简(语法分析过程中为了方便,全部按完整StatementData来构造,这里化简为原来的类型:ValueData/CallData/FunctionData等,主要涉及参数与语句部分) if (data.Functions.Count == 1) { //只有一个函数的语句退化为函数(再按函数进一步退化)。 FunctionData func = data.Functions[0]; func.CopyComments(data); return simplifyStatement(func); } else { //多个函数构成的语句不会退化,只对各个函数的参数与语句部分进行化简。 simplifyStatementData(data); return data; } }