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 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 UnifiedSet<UnifiedParameter> CreateArgs(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "args"); Contract.Requires( node.Elements().All( e => e.Name() == "Symbol" || e.Name() == "block")); var args = node.Elements("Symbol") .Select(e => e.Value.ToVariableIdentifier().ToParameter()) .ToSet(); if (args.Count > 0 && node.LastElement().Name() == "block") { // デフォルト引数付き var asgnNodes = node.LastElement().Elements(); foreach (var asgnNode in asgnNodes) { var name = asgnNode.FirstElement().Value; args.First(arg => arg.Names[0].Name == name) .DefaultValue = CreateExpresion(asgnNode.NthElement(1)); } } return args; }
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 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)) ); }
public static UnifiedCast CreateCastExpression(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "castExpression"); /* * castExpression * : '(' primitiveType ')' unaryExpression * | '(' type ')' unaryExpressionNotPlusMinus */ if (node.LastElement().Name() == "unaryExpression") { return UnifiedCast.Create( CreatePrimitiveType(node.NthElement(1)), CreateUnaryExpression(node.NthElement(3)) ); } return UnifiedCast.Create( CreateType(node.NthElement(1)), CreateUnaryExpressionNotPlusMinus(node.NthElement(3)) ); }
public static IEnumerable<UnifiedExpression> CreateStatement( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "statement"); /* * statement * : block * | ('assert') expression (':' expression)? ';' * | 'assert' expression (':' expression)? ';' * | 'if' parExpression statement ('else' statement)? * | forstatement * | 'while' parExpression statement * | 'do' statement 'while' parExpression ';' * | trystatement * | 'switch' parExpression '{' switchBlockStatementGroups '}' * | 'synchronized' parExpression block * | 'return' (expression )? ';' * | 'throw' expression ';' * | 'break' (IDENTIFIER)? ';' * | 'continue' (IDENTIFIER)? ';' * | expression ';' * | IDENTIFIER ':' statement * | ';' */ var first = node.FirstElement(); switch (first.Name()) { case "block": yield return CreateBlock(first); yield break; case "forstatement": yield return CreateForstatement(first); yield break; case "trystatement": yield return CreateTrystatement(first); yield break; case "expression": yield return CreateExpression(first); yield break; case "IDENTIFIER": yield return UnifiedLabel.Create(first.Value); foreach (var stmt in CreateStatement(node.LastElement())) { yield return stmt; } yield break; } switch (first.Value) { case "assert": var list = node.Elements("expression") .Select(CreateExpression) .ToList(); if (list.Count == 1) { yield return UnifiedAssert.Create(list[0]); } else { yield return UnifiedAssert.Create(list[0], list[1]); } break; case "if": yield return CreateIf(node); break; case "while": yield return CreateWhile(node); break; case "do": yield return CreateDoWhile(node); break; case "switch": yield return CreateSwitch(node); break; case "synchronized": yield return CreateSynchronized(node); break; case "return": yield return CreateReturn(node); break; case "throw": yield return CreateThrow(node); break; case "break": yield return CreateBreak(node); break; case "continue": yield return CreateContinue(node); break; case ";": break; default: throw new IndexOutOfRangeException(); } }
public static UnifiedArgument CreateElementValuePair(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "elementValuePair"); /* * elementValuePair * : IDENTIFIER '=' elementValue */ return UnifiedArgument.Create( CreateElementValue(node.LastElement()), UnifiedVariableIdentifier.Create(node.FirstElement().Value), null); }
public static UnifiedExpression CreateFor_stmt(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "for_stmt"); /* * for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ var exprlist = CreateExprlist(node.Element("exprlist")); var testlist = CreateTestlist(node.Element("testlist")); var suite = CreateSuite(node.Element("suite")); var elseSuite = node.HasElementByContent("else") ? CreateSuite(node.LastElement()) : null; return UnifiedForeach.Create( exprlist.SelectMany( e => e.DescendantsAndSelf<UnifiedIdentifier>()) .Select( e => UnifiedVariableDefinition.Create( name: e.DeepCopy())) .ToVariableDefinitionList(), testlist.ToSmartTupleLiteral(), suite, elseSuite); }
public static UnifiedClassDefinition CreateClassdef(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "classdef"); /* * classdef: 'class' NAME ['(' [testlist] ')'] ':' suite */ var testlistNodes = node.Element("testlist"); var testlist = testlistNodes != null ? CreateTestlist(testlistNodes) .Select(UnifiedType.Create) .Select( UnifiedExtendConstrain. Create) .ToSet<UnifiedTypeConstrain>() : null; return UnifiedClassDefinition.Create( null, UnifiedSet<UnifiedModifier>.Create(), UnifiedVariableIdentifier.Create(node.NthElement(1).Value), null, testlist, CreateSuite(node.LastElement())); }
public static UnifiedExpression CreateYield_expr(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "yield_expr"); /* * yield_expr: 'yield' [testlist] */ if (node.Elements().Count() == 1) { return UnifiedYieldReturn.Create(); } return UnifiedYieldReturn.Create( CreateTestlist(node.LastElement()). ToSmartTupleLiteral()); }
public static UnifiedExpression CreateWhile_stmt(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "while_stmt"); /* * while_stmt: 'while' test ':' suite ['else' ':' suite] */ var test = CreateTest(node.Element("test")); var suite = CreateSuite(node.Element("suite")); var elseSuite = node.HasElementByContent("else") ? CreateSuite(node.LastElement()) : null; return UnifiedWhile.Create(test, suite, elseSuite); }
public static UnifiedExpression CreateTrailer( UnifiedExpression prefix, XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "trailer"); /* * trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */ switch (node.FirstElement().Value) { case "(": var arglistNode = node.Element("arglist"); return arglistNode != null ? UnifiedCall.Create( prefix, CreateArglist(arglistNode)) : UnifiedCall.Create( prefix, UnifiedSet<UnifiedArgument>.Create()); case "[": var second = node.NthElement(1); return UnifiedIndexer.Create( prefix, CreateSubscriptlist(second)); case ".": return UnifiedProperty.Create( ".", prefix, UnifiedVariableIdentifier.Create( node.LastElement().Value)); default: throw new IndexOutOfRangeException(); } }
public static UnifiedParameter CreateTfpdef(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "tfpdef"); /* * tfpdef: NAME [':' test] */ var ret = UnifiedParameter.Create( null, null, null, UnifiedVariableIdentifier.Create(node.FirstElement().Value).ToSet<UnifiedIdentifier>()); if (node.Elements().Count() > 1) { ret.AnnotationExpression = CreateTest(node.LastElement()); } return ret; }
public static UnifiedExpression CreateTest(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "test"); /* * test: or_test ['if' or_test 'else' test] | lambdef */ var first = node.FirstElement(); if (first.Name() == "lambdef") { return CreateLambdef(first); } var last = node.LastElement(); if (last.Name() != "test") { return CreateOr_test(first); } return UnifiedTernaryExpression.Create( CreateOr_test(node.NthElement(2)), CreateOr_test(first), CreateTest(last)); }
private static UnifiedIntegerLiteral CreateIntegerLiteral(XElement node, BigInteger integer) { var type = node.LastElement(); if (type.Name() == "IntegerTypeSuffix") { switch (type.Value.ToLower()) { case "u": return UnifiedIntegerLiteral.CreateUInt32(integer); case "ul": return UnifiedIntegerLiteral.CreateUInt32(integer); case "ull": return UnifiedIntegerLiteral.CreateUInt64(integer); case "l": return UnifiedIntegerLiteral.CreateInt32(integer); case "lu": return UnifiedIntegerLiteral.CreateUInt32(integer); case "ll": return UnifiedIntegerLiteral.CreateInt64(integer); case "llu": return UnifiedIntegerLiteral.CreateUInt64(integer); default: throw new InvalidOperationException(); } } return UnifiedIntegerLiteral.CreateInt32(integer); }
public static XElement CaseElse(XElement element) { return element.LastElement(); }
public static UnifiedFunctionDefinition CreateFuncdef(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "funcdef"); /* * funcdef: 'def' NAME parameters ['->' test] ':' suite */ var annotationSignNode = node.Elements().First(e => e.Value == "->"); var annotationExpression = annotationSignNode != null ? CreateTest(annotationSignNode) : null; return UnifiedFunctionDefinition.Create( null, UnifiedSet<UnifiedModifier>.Create(), null, null, UnifiedVariableIdentifier.Create(node.NthElement(1).Value), CreateParameters(node.NthElement(2)), null, CreateSuite(node.LastElement()), annotationExpression); }
public static UnifiedClassLikeDefinition CreateAnnotationTypeDeclaration ( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "annotationTypeDeclaration"); /* * annotationTypeDeclaration * : modifiers '@' 'interface' IDENTIFIER annotationTypeBody */ var annotationsAndModifiers = CreateModifiers(node.FirstElement()); return UnifiedAnnotationDefinition.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, UnifiedVariableIdentifier.Create(node.NthElement(3).Value), null, null, CreateAnnotationTypeBody(node.LastElement())); }
public static UnifiedIf CreateIf_stmt(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "if_stmt"); /* * if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ var conditionAndBodies = node.Elements("test") .Select( n => Tuple.Create( CreateTest(n), CreateSuite(((n)).NextElement(1)))); var elseSuiteNode = node.LastElement(); var elseSuite = elseSuiteNode.PreviousElement(1).Value == "else" ? CreateSuite(elseSuiteNode) : null; return UnifiedIf.Create(conditionAndBodies, elseSuite); }
public static UnifiedExpression CreateUnaryExpressionNotPlusMinus( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "unaryExpressionNotPlusMinus"); /* * unaryExpressionNotPlusMinus * : '~' unaryExpression * | '!' unaryExpression * | castExpression * | primary (selector)* ( '++' | '--' )? */ var firstElement = node.FirstElement(); switch (firstElement.Name()) { case "castExpression": return CreateCastExpression(firstElement); case "primary": var result = CreatePrimary(firstElement); result = node.Elements("selector") .Aggregate(result, CreateSelector); var lastNode = node.LastElement(); if (!lastNode.HasElement()) { var ope = lastNode.Value; result = UnifiedUnaryExpression.Create( result, UnifiedUnaryOperator.Create( ope, ope == "++" ? UnifiedUnaryOperatorKind. PostIncrementAssign : UnifiedUnaryOperatorKind. PostDecrementAssign)); } return result; } return UnifiedProgramGeneratorHelper.CreatePrefixUnaryExpression( node, CreateUnaryExpression, Sign2PrefixUnaryOperator); }
public static IEnumerable<UnifiedImport> CreateImport_from( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "import_from"); /* * import_from: ('from' ('.'* dotted_name | '.'+) * 'import' ('*' | '(' import_as_names ')' | import_as_names)) */ var dotted_nameNode = node.Element("dotted_name"); // support to relative import var dotted_name = dotted_nameNode != null ? CreateDotted_name(dotted_nameNode) : Enumerable.Empty<UnifiedIdentifier>(); var dotCount = node.ElementsByContent(".").Count(); var name = Enumerable.Repeat((UnifiedIdentifier)null, dotCount) .Concat(dotted_name) .ToProperty("."); if (node.LastElement().Value == "*") { yield return UnifiedImport.Create( UnifiedVariableIdentifier.Create("*"), null, name); yield break; } var results = CreateImport_as_names(node.Element("import_as_names")) .Select( t => UnifiedImport.Create( name.DeepCopy(), t.Item2, t.Item1)); foreach (var result in results) { yield return result; } }
public static UnifiedExpression CreateIdentifierSuffix( UnifiedExpression prefixProp, XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "identifierSuffix"); /* * identifierSuffix * : ('[' ']')+ '.' 'class' // java.lang.String[].class * | ('[' expression ']' )+ // strs[10] * | arguments // func(1, 2) * | '.' 'class' // java.lang.String.class * // this.<Integer>m(1), super.<Integer>m(1) * | '.' nonWildcardTypeArguments IDENTIFIER arguments * | '.' 'this' // Outer.this * | '.' 'super' arguments // new Outer().super(); * | innerCreator // new Outer().new <Integer> Inner<String>(1); */ var second = node.NthElementOrDefault(1); if (second == null) { // arguments // func(1, 2) if (node.FirstElement().Name() == "arguments") { return UnifiedCall.Create( prefixProp, CreateArguments(node.FirstElement())); } // innerCreator // new Outer().new <Integer> Inner<String>(1); return CreateInnerCreator( prefixProp, node.Element("innerCreator")); } // ('[' expression ']' )+ // strs[10] if (second.Name() == "expression") { return node.Elements("expression") .Select(CreateExpression) .Aggregate( prefixProp, (current, exp) => UnifiedIndexer.Create( current, UnifiedSet<UnifiedArgument>. Create( UnifiedArgument . Create ( exp, null, null))) ); } // '.' 'class' // java.lang.String.class if (second.Value == "class") { return UnifiedTypeof.Create(UnifiedType.Create(prefixProp)); } // ('[' ']')+ '.' 'class' // java.lang.String[].class if (node.LastElement().Value == "class") { var d = node.ElementsByContent("[").Count(); return UnifiedTypeof.Create( (UnifiedType.Create(prefixProp)). WrapArrayRepeatedly(d)); } // '.' nonWildcardTypeArguments IDENTIFIER arguments if (second.Name() == "nonWildcardTypeArguments") { var prop = UnifiedProperty.Create( ".", prefixProp, UnifiedVariableIdentifier.Create( node.NthElement(2).Value)); return UnifiedCall.Create( prop, CreateArguments(node.Element("arguments")), CreateNonWildcardTypeArguments( node.Element("nonWildcardTypeArguments"))); } // '.' 'this' // Outer.this if (second.Value == "this") { return UnifiedProperty.Create( ".", prefixProp, UnifiedThisIdentifier.Create("this")); } // '.' 'super' arguments // new Outer().super(); if (second.Value == "super") { var prop = UnifiedProperty.Create( ".", prefixProp, UnifiedSuperIdentifier.Create("super")); return UnifiedCall.Create( prop, CreateArguments(node.Element("arguments"))); } throw new InvalidOperationException(); }
public static IEnumerable<UnifiedExpression> CreateList_for( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "list_for"); /* * list_for: 'for' exprlist 'in' testlist_safe [list_iter] */ yield return UnifiedForeach.Create( CreateExprlist(node.NthElement(1)). ToSmartTupleLiteral(), CreateTestlist_safe(node.NthElement(3)). ToSmartTupleLiteral()); var last = node.LastElement(); if (last.Name() != "list_iter") { yield break; } foreach (var result in CreateList_iter(last)) { yield return result; } }
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())); } }
public static IEnumerable<UnifiedExpression> CreateList_if( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "list_if"); /* * list_if: 'if' old_test [list_iter] */ yield return UnifiedIf.Create(CreateOld_test(node.NthElement(1))); var last = node.LastElement(); if (last.Name() != "list_iter") { yield break; } foreach (var result in CreateList_iter(last)) { yield return result; } }
private static UnifiedExpression CreateDot3(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "dot3"); return UnifiedRange.Create( CreateExpresion(node.FirstElement()), CreateExpresion(node.LastElement())); }
public static UnifiedExpression CreateOld_lambdef(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "old_lambdef"); /* * old_lambdef: 'lambda' [varargslist] ':' old_test */ var varargslistNode = node.Element("varargslist"); return UnifiedLambda.Create( null, varargslistNode != null ? CreateVarargslist(varargslistNode) : null, CreateOld_test(node.LastElement()).ToBlock()); }
public static UnifiedSet<UnifiedParameter> CreateParameterTypeList( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "parameter_type_list"); /* parameter_type_list : parameter_list (',' '...')? */ var parameters = CreateParameterList(node.Element("parameter_list")); // TODO 可変長引数はmodifierなのか確認 if (node.LastElement().Value == "...") { parameters.Add( UnifiedParameter.Create( modifiers: UnifiedSet<UnifiedModifier>.Create( UnifiedModifier.Create("...")))); } return parameters; }
private static UnifiedFractionLiteral CreateFloatLiteral(XElement node, double number) { var type = node.LastElement(); if (type.Name() == "FloatTypeSuffix") { switch (type.Value.ToLower()) { case "f": return UnifiedFractionLiteral.Create(number, UnifiedFractionLiteralKind.Single); case "l": return UnifiedFractionLiteral.Create(number, UnifiedFractionLiteralKind.Double80); default: throw new InvalidOperationException(); } } return UnifiedFractionLiteral.Create(number, UnifiedFractionLiteralKind.Double); }