summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc/utils/ioutils/format/directives/GeneralNumberDirective.java
blob: f2fca71a9e9e2f07ccf4adb436886de237ef585c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package bjc.utils.ioutils.format.directives;

import bjc.utils.ioutils.format.*;

/**
 * Implementation skeleton for number directives.
 *
 * @author student
 *
 */
public abstract class GeneralNumberDirective implements Directive {
	/**
	 * Parameters for doing number formatting.
	 *
	 * @author bjculkin
	 *
	 */
	public static class NumberParams {
		/**
		 * Minimum # of printed columns
		 */
		public CLValue mincol = CLValue.nil();
		/**
		 * Character to use for padding if needed.
		 */
		public CLValue padchar = CLValue.nil();

		/**
		 * Should the sign always be printed?
		 */
		public boolean signed;

		/**
		 * Should there be commas inserted into the numbers?
		 */
		public boolean commaMode;

		/**
		 * Number of places to go before inserting a comma.
		 */
		public CLValue commaInterval = CLValue.nil();
		/**
		 * Character to use as a comma.
		 */
		public CLValue commaChar = CLValue.nil();
	}

	/**
	 * Get the parameters for a general number-handling directive.
	 * 
	 * @param compCTX
	 *                The compilation context at this point.
	 * @param argidx
	 *                The argument index to start looking at.
	 * 
	 * @return The parsed number parameters.
	 */
	protected NumberParams getParams(CompileContext compCTX, int argidx) {
		CLParameters params = compCTX.decr.parameters;
		CLModifiers  mods   = compCTX.decr.modifiers;

		NumberParams np = new NumberParams();

		if (params.length() >= (argidx + 2)) {
			params.mapIndex("mincol", argidx + 1);
			np.mincol = params.resolveKey("mincol");
		}

		if (params.length() >= (argidx + 3)) {
			params.mapIndex("padchar", argidx + 2);
			np.padchar = params.resolveKey("padchar");
		}

		if (mods.colonMod) {
			np.commaMode = true;

			if (params.length() >= (argidx + 4)) {
				params.mapIndex("cchar", argidx + 3);
				np.commaChar = params.resolveKey("cchar");
			}

			if (params.length() >= (argidx + 5)) {
				params.mapIndex("cinterval", argidx + 4);
				np.commaInterval = params.resolveKey("cinterval");
			}
		}

		np.signed = mods.atMod;

		return np;
	}
}