summaryrefslogtreecommitdiff
path: root/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java
blob: e0f66b1fe8630b50b6ea7173d5b94b68a2227544 (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
package bjc.dicelang.neodice.commands;

import static bjc.dicelang.neodice.statements.StatementValue.Type.*;

import java.util.*;

import bjc.dicelang.neodice.*;
import bjc.dicelang.neodice.statements.*;

/**
 * A command that produces a polyhedral die.
 * 
 * @author Ben Culkin
 *
 */
public class PolyhedralDieCommand implements Command {
	@Override
	public StatementValue execute(Iterator<String> words, DieBoxCLI state) {
		if (!words.hasNext()) {
			throw new DieBoxException("Number of sides to polyhedral-die must be provided");
		} else {
			StatementValue sideValue = state.runStatement(words);
			
			if (sideValue.type == INTEGER) {
				int numSides = ((IntegerStatementValue)sideValue).value;
				
				if (numSides < 0) throw new DieBoxException("Number of sides to polyhedral-die was not valid (must be less than 0, was %d)", numSides);
				
				Die<StatementValue> die = Die
						.polyhedral(numSides)
						.transform(IntegerStatementValue::new);
				
				return new DieStatementValue(INTEGER, die);
			} else {
				throw new DieBoxException("Number of sides to polyhedral-die wasn't an integer (was %s, of type %s)",
						sideValue, sideValue.type);
			}
		}
	}
	
	@Override
	public String shortHelp() {
		return "create a single polyhedral die";
	}
	
	@Override
	public String longHelp() {
		return "Creates a single polyhedral die with a fixed number of sides";
	}
}