View Javadoc
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  /**
26   * A pointer to a tuple within the list of tuples.
27   * Addresses are used for jumps, especially in reaction to
28   * conditional checks (i.e., if false, jump to else block, etc.).
29   * <p>
30   * Addresses have the following properties:
31   * <ul>
32   * <li>A name (label).
33   * <li>An index into the tuple queue.
34   * </ul>
35   * An address may not necessarily have an index assigned upon creation.
36   * However, upon tuple traversal, all address indexes must
37   * point to a valid tuple.
38   * <p>
39   * All addresses should have a meaningful label.
40   *
41   * @author Danny Daglas
42   */
43  public class Address implements java.io.Serializable {
44  
45  	private static final long serialVersionUID = 109610985341478678L;
46  
47  	/** Human-readable label used when rendering or debugging this address. */
48  	private final String lbl;
49  
50  	/** Tuple index currently bound to this address, or {@code -1} when unresolved. */
51  	private int idx = -1;
52  
53  	/**
54  	 * Creates an unresolved address with the supplied label.
55  	 *
56  	 * @param lbl Label to associate with this jump target
57  	 */
58  	public Address(String lbl) {
59  		this.lbl = lbl;
60  	}
61  
62  	/**
63  	 * The label of the address.
64  	 * It is particularly useful when dumping tuples to an output stream.
65  	 *
66  	 * @return The label of the tuple.
67  	 */
68  	public String label() {
69  		return lbl;
70  	}
71  
72  	/**
73  	 * Set the tuple index of this address.
74  	 * This can be deferred anytime after creation of the address,
75  	 * but the index must be assigned prior to traversing the tuples.
76  	 *
77  	 * @param index The tuple location within the tuple list (queue)
78  	 *        for this address.
79  	 */
80  	public void assignIndex(int index) {
81  		this.idx = index;
82  	}
83  
84  	/**
85  	 * <p>
86  	 * index.
87  	 * </p>
88  	 *
89  	 * @return The index into the tuple queue/array.
90  	 */
91  	public int index() {
92  		return idx;
93  	}
94  
95  	@Override
96  	public String toString() {
97  		return label();
98  	}
99  }