private CodeMemberMethod GenerateImperativeMethod(BinaryOperatorExpression node) { _scope.Clear(); if (node.LeftOperand.Type != AphidExpressionType.IdentifierExpression) { throw new NotImplementedException(); } var funcId = (IdentifierExpression)node.LeftOperand; var func = (FunctionExpression)node.RightOperand; CodeStatement[] body; if (func.Body.Count == 0) { return(null); //return new CodeStatementCollection(); #pragma warning disable CS0162 // Unreachable code detected body = Array.Empty <CodeStatement>(); #pragma warning restore CS0162 // Unreachable code detected } else { body = func.Body .SelectMany(x => GenerateImperativeStatement(x).OfType <CodeStatement>()) .ToArray(); } var methodAttrs = ParseMethodAttributes(funcId); var typeRef = CodeHelper.TypeRef(methodAttrs.Type); var method = new CodeMemberMethod { Name = funcId.Identifier, ReturnType = methodAttrs.IsList ? ParserCode.GetListTypeRef(typeRef) : typeRef }; if (methodAttrs.IsRoot) { method.Attributes = MemberAttributes.Public | MemberAttributes.Final; method.Statements.Add(GenerateContextInitialization()); } method.Statements.AddRange(body); return(method); //var str = CSharpHelper.GenerateCode(method); //return new CodeStatementCollection(body); }
private CodeStatementCollection GenerateImperativeStatement(IdentifierExpression node) { var parserId = ParserIdentifier.FromIdentifierExpression(node); if (parserId.Name != "Error") { var typeRef = CodeHelper.TypeRef(parserId.Type ?? _config.BaseClass); typeRef = parserId.IsList ? ParserCode.GetListTypeRef(typeRef) : typeRef; var init = new CodeDefaultValueExpression(typeRef); var varDecl = new CodeVariableDeclarationStatement(typeRef, node.Identifier, init); _scope.Add(node.Identifier, AphidObject.Complex()); return(new CodeStatementCollection(new[] { varDecl })); } return(new CodeStatementCollection(new[] { new CodeThrowExceptionStatement( new CodeObjectCreateExpression( _config.ExceptionClass, CodeHelper.VarRef("CurrentToken"))) })); }
private CodeExpression GenerateImperativeExpression(IdentifierExpression node, bool isCondition = false) { switch (node.Identifier) { case ParserGeneratorDirective.CurrentTokenType: return(GetCurrentTokenType()); case ParserGeneratorDirective.CurrentLexeme: return(GetCurrentLexeme()); default: if (IsTokenType(node.Identifier)) { var exp = (CodeExpression)GetTokenTypeRef(node.Identifier); return(!isCondition ? exp : CodeHelper.BinOpExp( GetCurrentTokenType(), CodeBinaryOperatorType.ValueEquality, exp)); } else if (node.Attributes.Count > 0) { var attr = node.Attributes.Single().Identifier; return(attr == "list" ? new CodeObjectCreateExpression( ParserCode.GetListTypeRef(CodeHelper.TypeRef(node.Identifier))) : throw new NotImplementedException()); } else { return(CodeHelper.VarRef(node.Identifier)); } } }
private static CodeMemberProperty[] CreateProperties(RuleStruct ruleType) { var isMutable = ParserGeneratorConfig.Current.IsAstMutable; var properties = ruleType.Properties .Select(x => new CodeMemberProperty { Name = x.Name, Attributes = MemberAttributes.Public | MemberAttributes.Final, Type = ParserCode.GetTypeRef(x), HasGet = true, HasSet = isMutable, CustomAttributes = new CodeAttributeDeclarationCollection( new[] { new CodeAttributeDeclaration( TypeRef <DataMemberAttribute>()) }) }) .ToArray(); foreach (var p in properties) { p.GetStatements.Add(Return(GetFieldName(p.Name))); if (isMutable) { p.SetStatements.Add( Assign( GetFieldName(p.Name), VarRef("value"))); } } return(properties); }