1 package io.jawk.intermediate;
2
3 /*-
4 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5 * Jawk
6 * ჻჻჻჻჻჻
7 * Copyright (C) 2006 - 2026 MetricsHub
8 * ჻჻჻჻჻჻
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU General Lesser Public
20 * License along with this program. If not, see
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23 */
24
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.LinkedHashSet;
28 import java.util.Map;
29 import java.util.Set;
30
31 /**
32 * The standard AWK built-in functions, the single source of truth shared by
33 * the parser (recognition and dispatch) and the runtime (the {@code FUNCTAB}
34 * listing). Each constant carries the function name as it appears in AWK
35 * source code, which may differ from the constant name itself (e.g.
36 * {@link #INT} for the <code>int</code> function). {@code print},
37 * {@code printf}, and {@code getline} are statements, not functions, so they
38 * are not listed, as in gawk.
39 */
40 public enum BuiltinFunction {
41
42 /** The {@code atan2} function. */
43 ATAN2("atan2"),
44 /** The {@code close} function. */
45 CLOSE("close"),
46 /** The {@code cos} function. */
47 COS("cos"),
48 /** The {@code exp} function. */
49 EXP("exp"),
50 /** The {@code gsub} function. */
51 GSUB("gsub"),
52 /** The {@code index} function. */
53 INDEX("index"),
54 /** The {@code int} function. */
55 INT("int"),
56 /** The {@code length} function. */
57 LENGTH("length"),
58 /** The {@code log} function. */
59 LOG("log"),
60 /** The {@code match} function. */
61 MATCH("match"),
62 /** The {@code rand} function. */
63 RAND("rand"),
64 /** The {@code sin} function. */
65 SIN("sin"),
66 /** The {@code split} function. */
67 SPLIT("split"),
68 /** The {@code sprintf} function. */
69 SPRINTF("sprintf"),
70 /** The {@code sqrt} function. */
71 SQRT("sqrt"),
72 /** The {@code srand} function. */
73 SRAND("srand"),
74 /** The {@code sub} function. */
75 SUB("sub"),
76 /** The {@code substr} function. */
77 SUBSTR("substr"),
78 /** The {@code system} function. */
79 SYSTEM("system"),
80 /** The {@code tolower} function. */
81 TOLOWER("tolower"),
82 /** The {@code toupper} function. */
83 TOUPPER("toupper");
84
85 /**
86 * A mapping of built-in function names to their enum constants, for
87 * name-based lookup.
88 */
89 private static final Map<String, BuiltinFunction> BY_NAME = new HashMap<String, BuiltinFunction>();
90
91 /** Names of all built-in functions, in declaration order. */
92 private static final Set<String> NAMES;
93
94 static {
95 Set<String> names = new LinkedHashSet<String>();
96 for (BuiltinFunction function : values()) {
97 BY_NAME.put(function.awkName, function);
98 names.add(function.awkName);
99 }
100 NAMES = Collections.unmodifiableSet(names);
101 }
102
103 /**
104 * The name of the function as it appears in AWK source code.
105 */
106 private final String awkName;
107
108 BuiltinFunction(String awkNameParam) {
109 this.awkName = awkNameParam;
110 }
111
112 /**
113 * Returns the name of the function as it appears in AWK source code.
114 *
115 * @return the AWK-visible function name
116 */
117 public String getAwkName() {
118 return awkName;
119 }
120
121 /**
122 * Resolves an AWK function name to its enum constant.
123 *
124 * @param name the function name as it appears in AWK source code
125 * @return the matching constant, or <code>null</code> if the name does not
126 * denote a built-in function
127 */
128 public static BuiltinFunction of(String name) {
129 return BY_NAME.get(name);
130 }
131
132 /**
133 * Returns the names of all standard built-in functions, as they appear in
134 * AWK source code and in gawk's {@code FUNCTAB}.
135 *
136 * @return unmodifiable set of function names, in declaration order
137 */
138 public static Set<String> names() {
139 return NAMES;
140 }
141 }