1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package hat.codebuilders;
26 
27 import hat.Config;
28 
29 public class C99HATConfigBuilder extends HATCodeBuilder<C99HATConfigBuilder> {
30 
31     C99HATConfigBuilder staticConstInt(String name, int padWidth, int value) {
32         staticKeyword().space().constexprKeyword().space().intType().space().identifier(name, padWidth).space().equals().space().intHexValue(value).semicolon().nl();
33         return this;
34     }
35 
36     public C99HATConfigBuilder staticConstIntShiftedOne(String name, int padWidth, int shift) {
37         staticKeyword().space().constexprKeyword().space().intType().space().identifier(name, padWidth).space().equals().space().intValue(1).leftShift().intHexValue(shift).semicolon().nl();
38         return this;
39     }
40 
41     public C99HATConfigBuilder className() {
42         return identifier("BasicConfig");
43     }
44 
45     public C99HATConfigBuilder bitNamesVar() {
46         return identifier("bitNames");
47     }
48 
49     public C99HATConfigBuilder bitDescriptionsVar() {
50         return identifier("bitDescriptions");
51     }
52 
53     public C99HATConfigBuilder configBitsVar() {
54         return identifier("configBits");
55     }
56 
57     public C99HATConfigBuilder configBitsAnd() {
58         return configBitsVar().space().ampersand().space();
59     }
60 
61     public C99HATConfigBuilder configBitsAndBitName(String bitName) {
62         return configBitsAnd().identifier(bitName + "_BIT");
63     }
64 
65     public static String toCamelExceptFirst(String s) {
66         String[] parts = s.split("_");
67         StringBuilder camelCaseString = new StringBuilder("");
68         for (String part : parts) {
69             camelCaseString.append(camelCaseString.isEmpty()
70                     ? part.toLowerCase()
71                     : part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase());
72         }
73         return camelCaseString.toString();
74     }
75 
76     public C99HATConfigBuilder camelExceptFirst(String s) {
77         return identifier(toCamelExceptFirst(s));
78     }
79 
80     C99HATConfigBuilder std(String s) {
81         return identifier("std").colon().colon().identifier(s);
82     }
83 
84     public C99HATConfigBuilder stdEndl() {
85         return std("endl");
86     }
87 
88     public C99HATConfigBuilder stdCout(String s) {
89         return std("cout").space().leftShift().space().dquote().literal(s).dquote();
90     }
91 
92     static public void main(){
93         var c = Config.fromSpec("INFO,SHOW_CODE,HEADLESS,NO_BUFFER_TAGGING,SHOW_KERNEL_MODEL,SHOW_COMPUTE_MODEL,PLATFORM:0,DEVICE:0");
94         System.out.println(c);
95         System.exit(1);
96     }
97 }