View Javadoc
1   package org.metricshub.jawk.intermediate;
2   
3   import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4   import org.metricshub.jawk.ext.ExtensionFunction;
5   
6   /*-
7    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
8    * Jawk
9    * ჻჻჻჻჻჻
10   * Copyright (C) 2006 - 2025 MetricsHub
11   * ჻჻჻჻჻჻
12   * This program is free software: you can redistribute it and/or modify
13   * it under the terms of the GNU Lesser General Public License as
14   * published by the Free Software Foundation, either version 3 of the
15   * License, or (at your option) any later version.
16   *
17   * This program is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   * GNU General Lesser Public License for more details.
21   *
22   * You should have received a copy of the GNU General Lesser Public
23   * License along with this program.  If not, see
24   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
25   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
26   */
27  
28  /**
29   * Marks a position within the tuple list (queue).
30   *
31   * @author Danny Daglas
32   */
33  public class PositionTracker {
34  
35  	private int idx = 0;
36  	private final java.util.List<Tuple> queue;
37  	private Tuple tuple;
38  
39  	@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "PositionTracker must iterate over the shared tuple list")
40  	public PositionTracker(java.util.List<Tuple> queue) {
41  		this.queue = queue;
42  		this.tuple = queue.isEmpty() ? null : queue.get(idx);
43  	}
44  
45  	public boolean isEOF() {
46  		return idx >= queue.size();
47  	}
48  
49  	public void next() {
50  		assert tuple != null;
51  		++idx;
52  		tuple = tuple.getNext();
53  		assert queue.size() == idx || queue.get(idx) == tuple;
54  	}
55  
56  	public void jump(Address address) {
57  		idx = address.index();
58  		tuple = queue.get(idx);
59  	}
60  
61  	@Override
62  	public String toString() {
63  		return "[" + idx + "]-->" + tuple.toString();
64  	}
65  
66  	public Opcode opcode() {
67  		return tuple.getOpcode();
68  	}
69  
70  	public long intArg(int argIdx) {
71  		Class<?> c = tuple.getTypes()[argIdx];
72  		if (c == Long.class) {
73  			return tuple.getInts()[argIdx];
74  		}
75  		throw new Error("Invalid arg type: " + c + ", arg_idx = " + argIdx + ", tuple = " + tuple);
76  	}
77  
78  	public boolean boolArg(int argIdx) {
79  		Class<?> c = tuple.getTypes()[argIdx];
80  		if (c == Boolean.class) {
81  			return tuple.getBools()[argIdx];
82  		}
83  		throw new Error("Invalid arg type: " + c + ", arg_idx = " + argIdx + ", tuple = " + tuple);
84  	}
85  
86  	public Object arg(int argIdx) {
87  		Class<?> c = tuple.getTypes()[argIdx];
88  		if (c == Long.class) {
89  			return tuple.getInts()[argIdx];
90  		}
91  		if (c == Double.class) {
92  			return tuple.getDoubles()[argIdx];
93  		}
94  		if (c == String.class) {
95  			return tuple.getStrings()[argIdx];
96  		}
97  		if (c == Address.class) {
98  			assert argIdx == 0;
99  			return tuple.getAddress();
100 		}
101 		if (c == ExtensionFunction.class) {
102 			assert argIdx == 0;
103 			return tuple.getExtensionFunction();
104 		}
105 		throw new Error("Invalid arg type: " + c + ", arg_idx = " + argIdx + ", tuple = " + tuple);
106 	}
107 
108 	public ExtensionFunction extensionFunctionArg() {
109 		if (tuple.getTypes()[0] != ExtensionFunction.class) {
110 			throw new Error("Tuple does not contain an extension function: " + tuple);
111 		}
112 		return tuple.getExtensionFunction();
113 	}
114 
115 	public Address addressArg() {
116 		assert tuple.getAddress() != null || tuple.getAddressSupplier() != null : "tuple.address = "
117 				+ tuple.getAddress() + ", tuple.address_supplier = " + tuple.getAddressSupplier();
118 		if (tuple.getAddress() == null) {
119 			tuple.setAddress(tuple.getAddressSupplier().get());
120 		}
121 		return tuple.getAddress();
122 	}
123 
124 	public Class<?> classArg() {
125 		assert tuple.getCls() != null;
126 		return tuple.getCls();
127 	}
128 
129 	public int lineNumber() {
130 		assert tuple.getLineno() != -1 : "The line number should have been set by queue.add(), but was not.";
131 		return tuple.getLineno();
132 	}
133 
134 	public int current() {
135 		return idx;
136 	}
137 
138 	public void jump(int index) {
139 		this.idx = index;
140 		tuple = queue.get(this.idx);
141 	}
142 }