1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.millscript.functions;
22
23 import org.millscript.commons.alert.Alert;
24 import org.millscript.millscript.alert.Phases;
25 import org.millscript.millscript.vm.Machine;
26
27 /**
28 * This class implements an Alert to indicate a problem executing a function.
29 */
30 public class FunctionAlert extends Alert {
31
32 /**
33 * This is the ID for all future releases.
34 */
35 private static final long serialVersionUID = 3689070642611761971L;
36
37 /**
38 * Constructs a new function alert with the specified complaint and
39 * explanation.
40 *
41 * @param comp the complaint
42 * @param exp the complaints explanation
43 */
44 public FunctionAlert( final String comp, final String exp ) {
45 super( comp, exp );
46 }
47
48 /**
49 * Adds a culprit for an argument to a function, as specified by the
50 * supplied argument position and value.
51 *
52 * @param argNumber the arguments position
53 * @param arg the arguments value
54 * @return this function alert
55 */
56 public FunctionAlert culpritArg( final int argNumber, final Object arg ) {
57 return (FunctionAlert) this.culprit( "arg (" + argNumber + ")", arg );
58 }
59
60 /**
61 * Gathers the specified function and number of arguments as culprits of
62 * this alert.
63 *
64 * @param f the culprit function
65 * @param mc the machine to gather culprit arguments from
66 * @param nargs the number of culprit arguments
67 * @return this alert with function and argument culprits
68 */
69 public FunctionAlert culpritArgs( final Function f, final Machine mc, final int nargs ) {
70 this.culprit( "function", f );
71 for ( int i = nargs; i >= 1; i-- ) {
72 this.culpritArg( i, mc.popObject() );
73 }
74 return this;
75 }
76
77 /**
78 * Gathers the specified function, number of arguments and update arguments
79 * as culprits of this alert.
80 *
81 * @param f the culprit function
82 * @param mc the machine to gather culprit arguments from
83 * @param nargs the number of culprit arguments
84 * @param unargs the number of culprit update arguments
85 * @return this alert with function and argument culprits
86 */
87 public FunctionAlert culpritUArgs( final Function f, final Machine mc, final int nargs, final int unargs ) {
88 this.culprit( "updater", f );
89 for ( int i = nargs + unargs; i >= 1; i-- ) {
90 this.culpritArg( i, mc.popObject() );
91 }
92 return this;
93 }
94
95 /**
96 * Convenience method for constructing a new alert with the specified
97 * complaint and explanation in the evaluation phase.
98 *
99 * @param comp the complaint
100 * @param exp the explanation for the complaint
101 * @return a new alert with the specified complaint and explanation
102 */
103 public static FunctionAlert eval( final String comp, final String exp ) {
104 return (FunctionAlert) new FunctionAlert( comp, exp ).setPhase( Phases.EVAL );
105 }
106
107 }