-
Notifications
You must be signed in to change notification settings - Fork 0
/
addCoreStdLib.pde
74 lines (65 loc) · 2.45 KB
/
addCoreStdLib.pde
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
void addCoreStdLib(StaticContext ctx) {
for (String ident : new String[] { "count", "i" }) {
for (int i = 0; i < 2; i++) {
ctx.addCallable(new CallableDefinition(
i == 0
? mkFunctionCallableIdent(ident, Arrays.asList(new String[] { FLOAT_TYPE_NAME }))
: mkVariableCallableIdent(ident),
FLOAT_TYPE_NAME
) {
@Override
public RuntimeValue eval(StaticContext ctx, DynamicContext dynCtx, List<RuntimeValue> params) {
float nf = params == null ? 0 : ((FloatValue)params.get(0)).value;
int n = (int)nf;
return new FloatValue(dynCtx.countStack.get(dynCtx.countStack.size() - 1 - n));
}
@Override
public void additionStaticCheck(StaticContext ctx, int blockDepth, List<Expr> params, int begin, int end) {
float nf;
if (params == null) {
nf = 0;
} else {
Expr paramExpr = params.get(0);
if (!(paramExpr instanceof FloatLiteralExpr)) {
throw new StaticCheckException("n must be a float literal.", begin, end);
}
nf = ((FloatLiteralExpr)paramExpr).value;
}
if (nf != (int)nf || nf < 0) {
throw new StaticCheckException("n must be a non-negative integer.", begin, end);
}
int n = (int)nf;
if (n >= blockDepth) {
throw new StaticCheckException("n must be less than nest depth.", begin, end);
}
}
}
);
}
}
ctx.addCmd(new CmdDefinition(new CmdIdent(false, "shot", Arrays.asList(new String[] {}))) {
@Override
public void eval(StaticContext ctx, DynamicContext dynCtx, List<RuntimeValue> params, List<Cmd> block, boolean createCtx) {
BallConfig ball = dynCtx.ballStack.get(dynCtx.ballStack.size() - 1);
dynCtx.balls.add(ball);
}
@Override
public void additionStaticCheck(StaticContext ctx, int blockDepth, List<Expr> params, List<Cmd> block, int begin, int end) {
if (blockDepth == 0) {
throw new StaticCheckException("shot cannot be used on the outermost side.", begin, end);
}
}
});
ctx.addCmd(new CmdDefinition(new CmdIdent(false, "sound", Arrays.asList(new String[] {}))) {
@Override
public void eval(StaticContext ctx, DynamicContext dynCtx, List<RuntimeValue> params, List<Cmd> block, boolean createCtx) {
dynCtx.playSound = true;
}
@Override
public void additionStaticCheck(StaticContext ctx, int blockDepth, List<Expr> params, List<Cmd> block, int begin, int end) {
if (blockDepth == 0) {
throw new StaticCheckException("sound cannot be used on the outermost side.", begin, end);
}
}
});
}