/// <summary> /// Traduit le jeton en code lisible. /// </summary> /// <returns></returns> public string ToXML() { Func <string, string> i = delegate(string str) { return(Tools.StringUtils.Indent(str, 1)); }; Func <string, string, string> inlineencapsulate = delegate(string str, string delim) { return("<" + delim + ">" + str + "</" + delim + ">\n"); }; Func <string, string, string> encapsulate = delegate(string str, string delim) { return("<" + delim + ">\n" + i(str) + "\n</" + delim + ">\n"); }; Func <string, string> getChildrenCode = delegate(string name) { StringBuilder b = new StringBuilder(); b.Append("<" + name + ">\n"); for (int j = 0; j < SubTokens.Count; j++) { b.AppendLine(i(SubTokens[j].ToXML())); } b.Append("</" + name + ">\n"); return(b.ToString()); }; switch (TkType) { case ExpressionTokenType.BracketList: return(getChildrenCode("BracketList")); case ExpressionTokenType.FunctionCall: return("<FunctionCall>\n" + i("<Identifier>\n" + i(FunctionCallIdentifier.ToXML()) + "</Identifier>\n") + i(FunctionCallArgs.ToXML()) + "</FunctionCall>"); case ExpressionTokenType.GenericType: return("<GenericType>\n" + i("<Identifier>\n" + i(GenericTypeIdentifier.ToXML()) + "</Identifier>\n") + i(GenericTypeArgs.ToXML()) + "</GenericType>"); case ExpressionTokenType.ArrayType: return("<ArrayType>\n" + i("<Identifier>\n" + i(ArrayTypeIdentifier.ToXML()) + "</Identifier>\n") + i(ArrayTypeArgs.ToXML()) + "</ArrayType>"); case ExpressionTokenType.NamedCodeBlock: return("<NamedCodeBlock>\n" + i("<Identifier>\n" + i(SubTokens[0].ToXML()) + "</Identifier>\n") + i(SubTokens[1].ToXML()) + "</NamedCodeBlock>"); case ExpressionTokenType.NamedGenericCodeBlock: return("<NamedGenericCodeBlock>\n" + i("<Identifier>\n" + i(NamedGenericCodeBlockIdentifier.ToXML()) + "</Identifier>\n") + i(NamedGenericCodeBlockInstructions.ToXML()) + "</NamedGenericCodeBlock>"); case ExpressionTokenType.FunctionDeclaration: return("<FunctionDeclaration>\n" + i("<FuncName>\n" + i(SubTokens[0].ToXML()) + "</FuncName>\n") + i(SubTokens[1].ToXML()) + "</FunctionDeclaration>"); case ExpressionTokenType.List: return(getChildrenCode("List")); case ExpressionTokenType.ArgList: return(getChildrenCode("ArgList")); case ExpressionTokenType.GenericParametersList: return(getChildrenCode("GenList")); case ExpressionTokenType.Name: return(inlineencapsulate(this.Content, "Name")); case ExpressionTokenType.CodeBlock: return(getChildrenCode("CodeBlock")); case ExpressionTokenType.EndOfInstruction: return(inlineencapsulate(this.Content, "EndOfInstruction")); case ExpressionTokenType.InstructionList: return(getChildrenCode("InstructionList")); case ExpressionTokenType.ExpressionGroup: if (Operator.IsBinaryOperator) { return(encapsulate( encapsulate(Operands1.ToXML(), "Operand1") + Operator.ToXML() + encapsulate(Operands2.ToXML(), "Operand2"), "ExpressionGroup")); } else { return(encapsulate( encapsulate(Operands1.ToXML(), "Operand1") + Operator.ToXML(), "ExpressionGroup")); } case ExpressionTokenType.NumberLiteral: return(inlineencapsulate(this.Content, "NumberLiteral")); case ExpressionTokenType.Operator: return(inlineencapsulate(this.Content, "Operator")); case ExpressionTokenType.Separator: return(inlineencapsulate(this.Content, "Separator")); case ExpressionTokenType.StringLiteral: return(inlineencapsulate(this.Content, "StringLiteral")); case ExpressionTokenType.BoolLiteral: return(inlineencapsulate(this.Content, "BoolLiteral")); case ExpressionTokenType.Modifier: return(inlineencapsulate(this.Content, "Modifier")); default: return("??"); } }
/// <summary> /// Traduit le jeton en code lisible. /// </summary> /// <returns></returns> public string ToPython() { Func <string, string> getChildrenCode = delegate(string separator) { StringBuilder b = new StringBuilder(); for (int i = 0; i < SubTokens.Count; i++) { b.Append(SubTokens[i].ToPython()); if (i != SubTokens.Count - 1) { b.Append(separator); } } return(b.ToString()); }; Func <string, string> getChildrenCode2 = delegate(string ending) { StringBuilder b = new StringBuilder(); for (int i = 0; i < SubTokens.Count; i++) { b.Append(SubTokens[i].ToPython()); b.Append(ending); } return(b.ToString()); }; switch (TkType) { case ExpressionTokenType.BracketList: return("[" + getChildrenCode(",") + "]"); case ExpressionTokenType.FunctionCall: return(FunctionCallIdentifier.ToPython() + "(" + FunctionCallArgs.ToPython() + ")"); case ExpressionTokenType.GenericType: return(GenericTypeIdentifier.ToPython()); case ExpressionTokenType.ArrayType: return(ArrayTypeIdentifier.ToPython() + "[" + ArrayTypeArgs.ToPython() + "]"); case ExpressionTokenType.List: return(getChildrenCode(" ")); case ExpressionTokenType.ArgList: return(getChildrenCode(",")); case ExpressionTokenType.GenericParametersList: return(""); case ExpressionTokenType.Name: return(this.Content); case ExpressionTokenType.CodeBlock: return("{ " + getChildrenCode(",") + " }"); case ExpressionTokenType.NamedCodeBlock: return(SubTokens[0].ToPython() + ":\n" + Tools.StringUtils.Indent(SubTokens[1].ToPython(), 1) + "\n"); case ExpressionTokenType.FunctionDeclaration: return(SubTokens[0].ToPython() + ":\n" + Tools.StringUtils.Indent(SubTokens[1].ToPython(), 1) + "\n"); case ExpressionTokenType.NamedGenericCodeBlock: return(NamedGenericCodeBlockIdentifier.ToPython() + ":\n" + Tools.StringUtils.Indent(NamedGenericCodeBlockInstructions.ToPython(), 1) + "\n"); case ExpressionTokenType.EndOfInstruction: return(this.Content + "\n"); case ExpressionTokenType.InstructionList: if (this.ListTokens.Count == 0) { return("pass;"); } return(getChildrenCode2("\n")); case ExpressionTokenType.ExpressionGroup: if (Operator.IsBinaryOperator) { if (Operator.Content == "=" || Operator.Content == ".") { return(Operands1.ToPython() + Operator.ToPython() + Operands2.ToPython()); } else { return("(" + Operands1.ToPython() + Operator.ToPython() + Operands2.ToPython() + ")"); } } else { return(Operator.ToPython() + Operands1.ToPython()); } case ExpressionTokenType.NumberLiteral: return(this.Content); case ExpressionTokenType.Operator: return(this.Content); case ExpressionTokenType.Separator: return(this.Content); case ExpressionTokenType.StringLiteral: return("\"" + this.Content + "\""); case ExpressionTokenType.BoolLiteral: return(this.Content); case ExpressionTokenType.Modifier: return(this.Content); default: return("??"); } }
/// <summary> /// Traduit le jeton en "code" lisible. /// </summary> /// <returns></returns> public string ToDebugCode() { Func <string, string> getChildrenCode = delegate(string separator) { StringBuilder b = new StringBuilder(); for (int i = 0; i < SubTokens.Count; i++) { b.Append(SubTokens[i].ToDebugCode()); if (i != SubTokens.Count - 1) { b.Append(separator); } } return(b.ToString()); }; switch (TkType) { case ExpressionTokenType.BracketList: return("l[" + getChildrenCode(",") + "]"); case ExpressionTokenType.FunctionCall: return(FunctionCallIdentifier.ToDebugCode() + ".call(" + FunctionCallArgs.ToDebugCode() + ")"); case ExpressionTokenType.GenericType: return(GenericTypeIdentifier.ToDebugCode() + ".gen<" + GenericTypeArgs.ToDebugCode() + ">"); case ExpressionTokenType.ArrayType: return(ArrayTypeIdentifier.ToDebugCode() + ".arr[" + ArrayTypeArgs.ToDebugCode() + "]"); case ExpressionTokenType.List: return("l:(" + getChildrenCode(" _ ") + ")"); case ExpressionTokenType.ArgList: return("args:(" + getChildrenCode(",") + ")"); case ExpressionTokenType.GenericParametersList: return("l<" + getChildrenCode(",") + ">"); case ExpressionTokenType.Name: return(this.Content); case ExpressionTokenType.CodeBlock: return("{ " + getChildrenCode(",") + "}"); case ExpressionTokenType.FunctionDeclaration: return("decl:" + SubTokens[0].ToDebugCode() + "{\n" + Tools.StringUtils.Indent(SubTokens[1].ToDebugCode(), 1) + "\n}"); case ExpressionTokenType.EndOfInstruction: return("/" + this.Content); case ExpressionTokenType.InstructionList: return("\ninstruction_list:(\n" + getChildrenCode("?;\n") + ")\n"); case ExpressionTokenType.ExpressionGroup: if (Operator.IsBinaryOperator) { return("(" + Operands1.ToDebugCode() + Operator.ToDebugCode() + Operands2.ToDebugCode() + ")"); } else { return(Operator.ToDebugCode() + Operands1.ToDebugCode()); } case ExpressionTokenType.NamedCodeBlock: return("block:" + SubTokens[0].ToDebugCode() + "{\n" + Tools.StringUtils.Indent(SubTokens[1].ToDebugCode(), 1) + "}\n"); case ExpressionTokenType.NamedGenericCodeBlock: return("gen_block:" + NamedGenericCodeBlockIdentifier.ToDebugCode() + "{\n" + Tools.StringUtils.Indent(NamedGenericCodeBlockInstructions.ToDebugCode(), 1) + "}\n"); case ExpressionTokenType.NumberLiteral: return(this.Content); case ExpressionTokenType.Operator: return(this.Content); case ExpressionTokenType.Separator: return("/" + this.Content); case ExpressionTokenType.StringLiteral: return("\"" + this.Content + "\""); case ExpressionTokenType.Modifier: return(this.Content); case ExpressionTokenType.ConditionalStatement: return(this.Content); default: return("??"); } }