public static UnifiedExpression CreateIterationStatement(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "iteration_statement"); /* iteration_statement : 'while' '(' expression ')' statement | 'do' statement 'while' '(' expression ')' ';' | 'for' '(' expression_statement expression_statement expression? ')' statement */ var first = node.FirstElement().Value; var body = UnifiedBlock.Create( CreateStatement(node.FirstElement("statement"))); switch (first) { case "while": return UnifiedWhile.Create(CreateExpression(node.NthElement(2)), body); case "do": return UnifiedDoWhile.Create(CreateExpression(node.NthElement(4)), body); case "for": var step = node.Element("expression"); var stepExp = step != null ? CreateExpression(step) : null; return UnifiedFor.Create( CreateExpressionStatement(node.NthElement(2)), CreateExpressionStatement(node.NthElement(3)), stepExp, body); default: throw new InvalidOperationException(); } }
public static UnifiedExpression CreateAlias(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "alias"); return UnifiedAlias.Create( CreateExpresion(node.FirstElement()), CreateExpresion(node.LastElement())); }
public static UnifiedExpression CreateLit(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "lit"); var child = node.FirstElement(); switch (child.Name()) { case "Symbol": return UnifiedSymbolLiteral.Create(child.Value); } return CreateExpresion(child); }
public static UnifiedBinaryExpression CreateAsgn(XElement node) { Contract.Requires(node != null); Contract.Requires( node.Name() == "lasgn" || node.Name() == "masgn" || node.Name() == "iasgn" || node.Name() == "gasgn" || node.Name() == "cvdecl" || node.Name() == "cdecl"); return UnifiedBinaryExpression.Create( CreateExpresion(node.FirstElement()), UnifiedBinaryOperator.Create( "=", UnifiedBinaryOperatorKind.Assign), CreateExpresion(node.LastElement())); }
public static UnifiedExpression CreateExpressionStatement( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "expression_statement"); /* expression_statement : ';' | expression ';' */ var first = node.FirstElement(); return first.Name == "expression" ? CreateExpression(first) : null; }
public static UnifiedProgram CreateCompilationUnit(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "compilationUnit"); /* * compilationUnit * : ( (annotations)? packageDeclaration )? (importDeclaration)* (typeDeclaration)* */ var program = UnifiedProgram.Create(UnifiedBlock.Create()); var expressions = program.Body; var first = node.FirstElementOrDefault(); if (first.SafeName() == "annotations") { var annotations = CreateAnnotations(node.FirstElement()); var packageDeclaration = CreatePackageDeclaration(node.NthElement(1)); packageDeclaration.Annotations = annotations; expressions.Add(packageDeclaration); expressions = packageDeclaration.Body; } else if (first.SafeName() == "packageDeclaration") { var packageDeclaration = CreatePackageDeclaration(first); expressions.Add(packageDeclaration); expressions = packageDeclaration.Body; } foreach (var e in node.Elements("importDeclaration")) { var importDeclaration = CreateImportDeclaration(e); expressions.Add(importDeclaration); } foreach (var e in node.Elements("typeDeclaration")) { var typeDeclaration = CreateTypeDeclaration(e); expressions.AddRange(typeDeclaration); } // Pick up comment nodes which are in last children of the root node program.Comments = node.Elements(Code2XmlConstants.CommentName) .Select(CreateComment) .ToSet(); return program; }
public static UnifiedExpression CreateExternalDeclaration( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "external_declaration"); /* * external_declaration * options {k=1;} * : ( declaration_specifiers? declarator declaration* '{' )=> function_definition * | declaration * ; */ // 前者が関数宣言のこと、後者がtypedefのこと var first = node.FirstElement(); if (first.Name() == "function_definition") { return CreateFunctionDefinition(first); } if (first.Name() == "declaration") { return CreateDeclaration(first); } throw new InvalidOperationException(); }
private static IEnumerable<UnifiedCase> CreateWhenAndDefault( XElement node) { Contract.Requires(node != null); if (node.Name() == "nil") { yield break; } if (node.Name() != "when") { yield return UnifiedCase.CreateDefault(CreateSmartBlock(node)); } else { var first = node.FirstElement(); var caseConds = first.Elements() .Select(CreateExpresion) .ToList(); int i; for (i = 0; i < caseConds.Count - 1; i++) { yield return UnifiedCase.Create(caseConds[i]); } yield return UnifiedCase.Create( caseConds[i], CreateSmartBlock(node.LastElement())); } }
private static UnifiedExpression CreateDot3(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "dot3"); return UnifiedRange.Create( CreateExpresion(node.FirstElement()), CreateExpresion(node.LastElement())); }
private static UnifiedIf CreateIf(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "statement"); Contract.Requires(node.FirstElement().Name() == "IF"); /* * 'if' parExpression statement ('else' statement)? */ var trueBody = CreateStatement(node.Element("statement")) .ToBlock(); UnifiedBlock falseBody = null; if (node.Elements("statement").Count() == 2) { var falseNode = node.Elements("statement").ElementAt(1); falseBody = CreateStatement(falseNode).ToBlock(); } return UnifiedIf.Create( CreateParExpression(node.Element("parExpression")), trueBody, falseBody); }
public static IEnumerable<UnifiedVariableIdentifier> CreateLasgnOrMasgnOrNil( XElement node) { Contract.Requires(node != null); Contract.Requires( node.Name() == "lasgn" || node.Name() == "masgn" || node.Name() == "nil"); Contract.Requires( node.Name() == "nil" || node.Elements().Count() == 1); Contract.Requires( node.Name() == "nil" || node.Name() != "masgn" || node.FirstElement().Name() == "array"); return node.Descendants("Symbol") .Select(CreateSymbol); }
public static UnifiedType CreateStructOrUnionSpecifier(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "struct_or_union_specifier"); /* struct_or_union_specifier * : struct_or_union IDENTIFIER? '{' struct_declaration_list '}' * | struct_or_union IDENTIFIER */ // 構造体の定義と宣言の両方をこのメソッドで作成 // 常に UnifiedType を返すが、 // 構造体定義をしている場合だけ関数の呼び出し元で UnifiedType の中身をとりだす // typedef "struct {}" data; -> クラス? // "struct data {}"; -> クラス // "struct data" a; -> 型 var isStruct = CreateStructOrUnion(node.FirstElement()) == "struct"; var identifier = node.Element("IDENTIFIER"); var typeName = identifier == null ? null : UnifiedVariableIdentifier.Create( identifier.Value); // 型の場合 if (node.Elements().Count() == 2) { var baseType = UnifiedType.Create(typeName); return isStruct ? baseType.WrapStruct() : baseType.WrapUnion(); } // struct or union の定義がある場合 var body = CreateStructDeclarationList( node.Element("struct_declaration_list")); var structOrUnion = isStruct ? (UnifiedClassLikeDefinition) UnifiedStructDefinition. Create( name: typeName, body: body) : UnifiedUnionDefinition.Create( name: typeName, body: body); // TODO struct or unionはあくまでもTypeとして返すののか? return UnifiedType.Create(structOrUnion); }
public static UnifiedExpression CreateTypeSpecifier(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "type_specifier"); /* type_specifier * : 'void' * | 'char' * | 'short' * | 'int' * | 'long' * | 'float' * | 'double' * | 'signed' * | 'unsigned' * | struct_or_union_specifier * | enum_specifier * | type_id */ var first = node.FirstElement(); switch (first.Name()) { case "struct_or_union_specifier": return CreateStructOrUnionSpecifier(first); case "enum_specifier": return CreateEnumSpecifier(first); case "type_id": return CreateTypeId(first); default: return UnifiedType.Create( UnifiedVariableIdentifier.Create(first.Value)); } }
public static UnifiedLiteral CreateLiteral(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "literal"); /* * literal * : intLiteral * | longLiteral * | floatLiteral * | doubleLiteral * | charLiteral * | stringLiteral * | trueLiteral * | falseLiteral * | nullLiteral */ var first = node.FirstElement(); switch (first.Name()) { case "intLiteral": return CreateIntLiteral(first); case "longLiteral": return CreateLongLiteral(first); case "floatLiteral": return CreateFloatLiteral(first); case "doubleLiteral": return CreateDoubleLiteral(first); case "charLiteral": return UnifiedCharLiteral.Create(first.Value); case "stringLiteral": return UnifiedStringLiteral.Create(first.Value); case "trueLiteral": return UnifiedBooleanLiteral.Create(true); case "falseLiteral": return UnifiedBooleanLiteral.Create(false); case "nullLiteral": return UnifiedNullLiteral.Create(); } throw new InvalidOperationException(); }
public static UnifiedParameter CreateParameterDeclaration(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "parameter_declaration"); /* parameter_declaration : declaration_specifiers (declarator|abstract_declarator)* */ UnifiedIdentifier name; UnifiedSet<UnifiedParameter> parameters; var modifiersAndType = CreateDeclarationSpecifiers( node.Element("declaration_specifiers")); var modifiers = modifiersAndType.Item1; var type = (UnifiedType)modifiersAndType.Item2; var declarators = node.Elements("declarator"); if (declarators.Count() == 0) { return UnifiedParameter.Create(null, modifiers, type); } var declarator = CreateDeclarator(node.FirstElement("declarator")); // abstract_declaratorはおそらく[]など=> よって、最初に現れることはないはず( // TODO 未検証) if (node.Element("abstract_declarator") != null) { // TODO (int y[])の場合は、[]はどこに付くのか? throw new NotImplementedException(); } // TODO 実際のところ、そこまで理解しきれていない else if (declarators.Count() == 1) { // 多分declarator自体は1つしか現れないはず( // TODO 未検証) return UnifiedParameter.Create( null, modifiers, type, declarator.Item1.ToSet()); } else if (node.Element("declarator") != null) { parameters = declarator.Item2; name = declarator.Item1; if (parameters != null && parameters.Count > 0) { // この場合はパラメータが関数ポインタ var returnType = type; type = UnifiedType.Create( UnifiedFunctionDefinition.Create( null, modifiers, returnType, null, null, parameters)); modifiers = null; } return UnifiedParameter.Create( null, modifiers, type, name.ToSet(), null); } throw new InvalidOperationException(); }
public static UnifiedSet<UnifiedParameter> CreateDeclaratorSuffix( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "declarator_suffix"); /* declarator_suffix * : '[' constant_expression ']' * | '[' ']' * | '(' parameter_type_list ')' * | '(' identifier_list ')' * | '(' ')' * ; */ // 空のパラメータリストを生成する var parameters = UnifiedSet<UnifiedParameter>.Create(); // ()の場合 if (node.FirstElement().Value.Equals("(") && node.LastElement().Value.Equals(")")) { if (node.Element("parameter_type_list") != null) { parameters = CreateParameterTypeList( node.Element("parameter_type_list")); } else if (node.Element("identifier_list") != null) { // TODO どう考えてもargumentCollectionだが、対応するプログラムをまずは検証する throw new NotImplementedException(); } // []の場合 } else if (node.FirstElement().Value.Equals("[") && node.LastElement().Value.Equals("]")) { // TODO []がくるケースがまだ未検証 throw new NotImplementedException(); } else { throw new InvalidOperationException(); } return parameters; }
public static UnifiedClassLikeDefinition CreateInterfaceDeclaration( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "interfaceDeclaration"); /* * interfaceDeclaration * : normalInterfaceDeclaration | annotationTypeDeclaration */ var first = node.FirstElement(); if (first.Name() == "normalInterfaceDeclaration") { return CreateNormalInterfaceDeclaration(first); } if (first.Name() == "annotationTypeDeclaration") { return CreateAnnotationTypeDeclaration(first); } throw new InvalidOperationException(); }
public static UnifiedClassLikeDefinition CreateEnumDeclaration( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "enumDeclaration"); /* * enumDeclaration * : modifiers ('enum') IDENTIFIER ('implements' typeList)? enumBody */ var annotationsAndModifiers = CreateModifiers(node.FirstElement()); var name = node.NthElement(2).Value; var typeListNode = node.Element("typeList"); var constrains = typeListNode != null ? CreateTypeList(typeListNode) .Select( UnifiedImplementsConstrain .Create) .ToSet<UnifiedTypeConstrain>() : null; var enumBody = CreateEnumBody(node.Element("enumBody")); return UnifiedEnumDefinition.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, UnifiedVariableIdentifier.Create(name), null, constrains, enumBody); }
public static UnifiedGenericParameter CreateTypeParameter(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "typeParameter"); /* * typeParameter * : IDENTIFIER ('extends' typeBound)? */ if (node.Elements().Count() == 1) { return UnifiedGenericParameter.Create( UnifiedType.Create(node.FirstElement().Value)); } return UnifiedGenericParameter.Create( UnifiedType.Create(node.FirstElement().Value), UnifiedSet<UnifiedTypeConstrain>.Create( CreateTypeBound(node.LastElement()) .Select(UnifiedExtendConstrain.Create)) ); }
private static UnifiedDoWhile CreateDoWhile(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "statement"); Contract.Requires(node.FirstElement().Name() == "DO"); /* * 'do' statement 'while' parExpression ';' */ return UnifiedDoWhile.Create( CreateParExpression(node.Element("parExpression")), UnifiedBlock.Create( CreateStatement(node.Element("statement")))); }
public static UnifiedExpression CreateEnumSpecifier(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "enum_specifier"); /* enum_specifier : 'enum' '{' enumerator_list '}' | 'enum' IDENTIFIER '{' enumerator_list '}' | 'enum' IDENTIFIER */ // enum { RED, BLUE, YELLOW } -> 宣言 // enum COLOR { RED, BLUE, YELLOW } -> 宣言 // enum COLOR -> 型 if (node.Elements("enumerator_list").Count() == 0) { // TODO 型名はどうなるのか? return UnifiedType.Create( "enum " + node.FirstElement("IDENTIFIER").Value); } var identifier = node.Element("IDENTIFIER"); UnifiedIdentifier name = identifier != null ? UnifiedIdentifier.CreateVariable( identifier.Value) : null; var body = CreateEnumeratorList(node.Element("enumerator_list")); return UnifiedEnumDefinition.Create( null, null, name, null, null, body); }
public static UnifiedExpression CreateMemberDecl(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "memberDecl"); /* * memberDecl * : fieldDeclaration * | methodDeclaration * | classDeclaration * | interfaceDeclaration */ var first = node.FirstElement(); switch (first.Name()) { case "fieldDeclaration": return CreateFieldDeclaration(first); case "methodDeclaration": return CreateMethodDeclaration(first); case "classDeclaration": return CreateClassDeclaration(first); case "interfaceDeclaration": return CreateInterfaceDeclaration(first); default: throw new InvalidOperationException(); } }
public static UnifiedFunctionDefinition CreateFunctionDefinition( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "function_definition"); /* function_definition * : declaration_specifiers? declarator * ( declaration+ compound_statement // K&R style * | compound_statement // ANSI style * ) */ UnifiedSet<UnifiedModifier> modifiers = null; UnifiedType type = null; UnifiedSet<UnifiedGenericParameter> genericParameters = null; UnifiedIdentifier name = null; UnifiedSet<UnifiedParameter> parameters = null; UnifiedBlock body = null; var first = node.FirstElement(); if (first.Name() == "declaration_specifiers") { var modifiersAndType = CreateDeclarationSpecifiers(first); modifiers = modifiersAndType.Item1; type = (UnifiedType)modifiersAndType.Item2; } var declarator = CreateDeclarator(node.Element("declarator")); name = declarator.Item1; parameters = declarator.Item2; if (node.Elements("declaration").Count() != 0) { // TODO declaration+ compound_statement に該当するケースが未検出 throw new NotImplementedException(); } body = CreateCompoundStatement(node.Element("compound_statement")); return UnifiedFunctionDefinition.Create( null, modifiers, type, genericParameters, name, parameters, null, body); }
public static UnifiedVariableDefinition CreateVariableDeclarator ( XElement node, UnifiedSet<UnifiedAnnotation> annotations, UnifiedSet<UnifiedModifier> modifiers, UnifiedType type) { Contract.Requires(node != null); Contract.Requires(node.Name() == "variableDeclarator"); /* * variableDeclarator * : IDENTIFIER ('[' ']')* ('=' variableInitializer)? */ var initializer = node.HasElement("variableInitializer") ? CreateVariableInitializer( node.Element( "variableInitializer")) : null; var dimension = node.ElementsByContent("[").Count(); type = type.WrapArrayRepeatedly(dimension); return UnifiedVariableDefinition.Create( annotations, modifiers, type, UnifiedVariableIdentifier.Create(node.FirstElement().Value), initializer ); }
public static UnifiedModifier CreateStorageClassSpecifier(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "storage_class_specifier"); /* * storage_class_specifier * : 'extern' * | 'static' * | 'auto' * | 'register' */ return UnifiedModifier.Create(node.FirstElement().Value); }
CreateInterfaceBodyDeclaration( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "interfaceBodyDeclaration"); /* * interfaceBodyDeclaration * : interfaceFieldDeclaration * | interfaceMethodDeclaration * | interfaceDeclaration * | classDeclaration * | ';' */ var first = node.FirstElement(); switch (first.Name()) { case "interfaceFieldDeclaration": yield return CreateInterfaceFieldDeclaration(first); break; case "interfaceMethodDeclaration": yield return CreateInterfaceMethodDeclaration(first); break; case "interfaceDeclaration": yield return CreateInterfaceDeclaration(first); break; case "classDeclaration": yield return CreateClassDeclaration(first); break; default: yield break; } }
public static UnifiedModifier CreateTypeQualifier(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "type_qualifier"); /* type_qualifier * : 'const' * | 'volatile' */ return UnifiedModifier.Create(node.FirstElement().Name()); }
public static UnifiedType CreateType(XElement node) { Contract.Requires(node == null || node.Name() == "type"); /* * type * : classOrInterfaceType ('[' ']')* * | primitiveType ('[' ']')* */ if (node == null) { return UnifiedType.Create("void"); } var first = node.FirstElement(); UnifiedType type; switch (first.Name()) { case "classOrInterfaceType": type = CreateClassOrInterfaceType(first); break; case "primitiveType": type = CreatePrimitiveType(first); break; default: throw new InvalidOperationException(); } var dimension = node.ElementsByContent("[").Count(); type = type.WrapArrayRepeatedly(dimension); return type; }
public static UnifiedExpression CreateDeclaration(XElement node) { // TODO なぜかここのcontractにひっかかる // Contract.Requires(node != null); // Contract.Requires(node.Name() == "declaration"); /* declaration : 'typedef' declaration_specifiers? {$declaration::isTypedef=true;} init_declarator_list ';' // special case, looking for typedef | declaration_specifiers init_declarator_list? ';' */ var firstNode = node.FirstElement(); switch (firstNode.Name()) { case "TOKEN": // TODO このケースになるプログラムが未検証 // typedef int S8; はいいけれど、これに初期化子も付けられるのが謎 throw new NotImplementedException(); break; // TODO プロトタイプ宣言はどう扱うか決める case "declaration_specifiers": // const int a = 0; // struct data { int x; }; => ここもこれになるのだけど、扱いはどうすれば var modifiersAndType = CreateDeclarationSpecifiers(firstNode); var modifiers = modifiersAndType.Item1; var type = modifiersAndType.Item2; // TODO 返り値をtypeじゃなくす var initDeclaratorList = node.Element("init_declarator_list"); if (initDeclaratorList == null) { return type.DeepCopy(); } else { UnifiedVariableDefinitionList variables = null; variables = UnifiedVariableDefinitionList.Create( CreateInitDeclaratorList(initDeclaratorList). Select( e => UnifiedVariableDefinition.Create( null, modifiers.DeepCopy(), (UnifiedType)type.DeepCopy(), e.Item1, e.Item2))); return variables; } default: throw new InvalidOperationException(); } }
public static UnifiedGenericArgument CreateTypeArgument(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "typeArgument"); /* * typeArgument * : type * | '?' (('extends' | 'super') type)? */ if (node.FirstElement().Name() == "type") { return UnifiedGenericArgument.Create( CreateType(node.FirstElement())); } var anyType = UnifiedType.Create(node.NthElement(0).Value); var typeNode = node.Element("type"); if (typeNode != null) { UnifiedTypeConstrain constrain; if (node.NthElement(1).Value == "extends") { constrain = UnifiedExtendConstrain.Create(CreateType(typeNode)); } else { constrain = UnifiedSuperConstrain.Create(CreateType(typeNode)); } return UnifiedGenericArgument.Create( anyType, null, constrain.ToSet()); } return UnifiedGenericArgument.Create(anyType); }