Skip to content

Commit 03c4eed

Browse files
committed
Add every ... member.
1 parent e0dfb05 commit 03c4eed

File tree

16 files changed

+215
-28
lines changed

16 files changed

+215
-28
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.byteskript</groupId>
88
<artifactId>byteskript</artifactId>
9-
<version>1.0.14</version>
9+
<version>1.0.15</version>
1010
<name>ByteSkript</name>
1111
<description>A compiled JVM implementation of the Skript language.</description>
1212

src/main/java/org/byteskript/skript/api/syntax/Literal.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public void compile(Context context, Pattern.Match match) throws Throwable {
4040
@Override
4141
public boolean allowedIn(State state, Context context) {
4242
return
43-
(state == CompileState.STATEMENT || state == CompileState.ENTRY_VALUE)
44-
&& context.hasCurrentUnit();
43+
(state == CompileState.STATEMENT || state == CompileState.ENTRY_VALUE); // remove unit check
4544
}
4645

4746
public abstract Type parse(String input);

src/main/java/org/byteskript/skript/api/syntax/SimpleExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void compile(Context context, Pattern.Match match) throws Throwable {
5454

5555
@Override
5656
public boolean allowedIn(State state, Context context) {
57-
return state == CompileState.STATEMENT && context.hasCurrentUnit();
57+
return state == CompileState.STATEMENT; // removed unit check
5858
}
5959

6060
}

src/main/java/org/byteskript/skript/compiler/Context.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public Collection<Library> getLibraries() {
238238
}
239239

240240
public Collection<SyntaxElement> getHandlers() {
241-
return getHandlers(getState(), getExpected());
241+
return this.getHandlers(this.getState(), this.getExpected());
242242
}
243243

244244
public State getState() {
@@ -250,8 +250,8 @@ public void setState(State state) {
250250
}
251251

252252
public void registerType(Type type) {
253-
if (type.isArray()) registerType(type.componentType().getSimpleName() + "s", type);
254-
else registerType(type.getSimpleName(), type);
253+
if (type.isArray()) this.registerType(type.componentType().getSimpleName() + "s", type);
254+
else this.registerType(type.getSimpleName(), type);
255255
}
256256

257257
public abstract void registerType(String name, Type type);

src/main/java/org/byteskript/skript/compiler/SkriptLangSpec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ private SkriptLangSpec() {
127127
new FunctionMember(),
128128
new NoArgsFunctionMember(),
129129
new Template(),
130-
new DictionaryMember()
130+
new DictionaryMember(),
131+
new EveryMember()
131132
);
132133
registerSyntax(CompileState.MEMBER_BODY,
133134
new Verify(),

src/main/java/org/byteskript/skript/lang/syntax/literal/IntegerLiteral.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import mx.kenzie.foundation.MethodBuilder;
1010
import mx.kenzie.foundation.Type;
1111
import mx.kenzie.foundation.WriteInstruction;
12+
import mx.kenzie.foundation.compiler.State;
1213
import org.byteskript.skript.api.note.Documentation;
1314
import org.byteskript.skript.api.syntax.Literal;
1415
import org.byteskript.skript.compiler.CommonTypes;
@@ -55,6 +56,11 @@ public void compile(Context context, Pattern.Match match) {
5556
}
5657
}
5758

59+
@Override
60+
public boolean allowedIn(State state, Context context) {
61+
return super.allowedIn(state, context);
62+
}
63+
5864
@Override
5965
public Integer parse(String input) {
6066
return Integer.valueOf(input);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2022 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.lang.syntax.timing;
8+
9+
import mx.kenzie.foundation.MethodBuilder;
10+
import mx.kenzie.foundation.Type;
11+
import mx.kenzie.foundation.WriteInstruction;
12+
import org.byteskript.skript.api.note.Documentation;
13+
import org.byteskript.skript.api.syntax.TriggerHolder;
14+
import org.byteskript.skript.compiler.*;
15+
import org.byteskript.skript.lang.element.StandardElements;
16+
import org.byteskript.skript.runtime.data.PeriodicalData;
17+
import org.byteskript.skript.runtime.data.SourceData;
18+
19+
import java.lang.reflect.Modifier;
20+
import java.time.Instant;
21+
22+
@Documentation(
23+
name = "Every",
24+
description = """
25+
Schedules a trigger to be run whenever a duration elapses.
26+
This will start after the script is loaded and the duration has elapsed for the first time.
27+
""",
28+
examples = {
29+
"""
30+
every 5 seconds:
31+
trigger:
32+
print "hello"
33+
"""
34+
}
35+
)
36+
public class EveryMember extends TriggerHolder {
37+
public EveryMember() {
38+
super(SkriptLangSpec.LIBRARY, StandardElements.MEMBER, "every %Duration%");
39+
}
40+
41+
@Override
42+
public Pattern.Match match(String thing, Context context) {
43+
if (!thing.startsWith("every ")) return null;
44+
return super.match(thing, context);
45+
}
46+
47+
@Override
48+
public CompileState getSubState() {
49+
return CompileState.STATEMENT; // looking for expressions here
50+
}
51+
52+
@Override
53+
public void preCompile(Context context, Pattern.Match match) throws Throwable {
54+
final int index = context.lineNumber(); // we're going to fake the method here to get the duration at runtime
55+
final MethodBuilder method = context.getBuilder().addMethod("every$" + index + "_control")
56+
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
57+
.setReturnType(CommonTypes.DURATION);
58+
context.setMethod(method);
59+
context.addFlag(AreaFlag.IN_TRIGGER);
60+
context.setState(CompileState.STATEMENT);
61+
super.preCompile(context, match);
62+
}
63+
64+
@Override
65+
public void compile(Context context, Pattern.Match match) {
66+
context.removeFlag(AreaFlag.IN_TRIGGER);
67+
final int index = context.lineNumber();
68+
final MethodBuilder control = context.getMethod();
69+
control.writeCode(WriteInstruction.cast(CommonTypes.DURATION));
70+
control.writeCode(WriteInstruction.returnObject());
71+
final MethodBuilder method = context.getBuilder()
72+
.addMethod("every$" + index)
73+
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
74+
.setReturnType(CommonTypes.VOID);
75+
context.setMethod(method);
76+
context.setState(CompileState.MEMBER_BODY);
77+
method
78+
.addAnnotation(SourceData.class).setVisible(true)
79+
.addValue("name", name())
80+
.addValue("type", "every")
81+
.addValue("line", context.lineNumber())
82+
.addValue("compiled", Instant.now().getEpochSecond());
83+
method
84+
.addAnnotation(PeriodicalData.class).setVisible(true)
85+
.addValue("control", "every$" + index + "_control")
86+
.addValue("fixed", true);
87+
}
88+
89+
@Override
90+
public String callSiteName(Context context, Pattern.Match match) {
91+
return "every$" + context.lineNumber();
92+
}
93+
94+
@Override
95+
public Type returnType(Context context, Pattern.Match match) {
96+
return CommonTypes.VOID;
97+
}
98+
99+
@Override
100+
public Type[] parameters(Context context, Pattern.Match match) {
101+
return new Type[0];
102+
}
103+
}

src/main/java/org/byteskript/skript/lang/syntax/timing/HoursExpression.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public HoursExpression() {
4444

4545
@ForceExtract
4646
public static Object find(Object object) {
47-
if (!(object instanceof Number)) {
47+
if (!(object instanceof final Number number)) {
4848
throw new ScriptRuntimeError("Timespan expression requires number.");
4949
} else {
50-
final Number number = (Number) object;
5150
return Duration.ofHours(number.longValue());
5251
}
5352
}

src/main/java/org/byteskript/skript/lang/syntax/timing/MillisecondsExpression.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public MillisecondsExpression() {
4444

4545
@ForceExtract
4646
public static Object find(Object object) {
47-
if (!(object instanceof Number)) {
47+
if (!(object instanceof final Number number)) {
4848
throw new ScriptRuntimeError("Timespan expression requires number.");
4949
} else {
50-
final Number number = (Number) object;
5150
return Duration.ofMillis(number.longValue());
5251
}
5352
}

src/main/java/org/byteskript/skript/lang/syntax/timing/MinutesExpression.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public MinutesExpression() {
4444

4545
@ForceExtract
4646
public static Object find(Object object) {
47-
if (!(object instanceof Number)) {
47+
if (!(object instanceof final Number number)) {
4848
throw new ScriptRuntimeError("Timespan expression requires number.");
4949
} else {
50-
final Number number = (Number) object;
5150
return Duration.ofMinutes(number.longValue());
5251
}
5352
}

0 commit comments

Comments
 (0)