Skip to content

Commit 07c24c2

Browse files
committed
Add extender.
1 parent d29e6bb commit 07c24c2

File tree

6 files changed

+95
-19
lines changed

6 files changed

+95
-19
lines changed

pom.xml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@
4747
</execution>
4848
</executions>
4949
</plugin>
50-
<plugin>
51-
<groupId>org.panteleyev</groupId>
52-
<artifactId>jpackage-maven-plugin</artifactId>
53-
<configuration>
54-
<name>ByteSkript</name>
55-
<appVersion>${project.version}</appVersion>
56-
<vendor>byteskript.org</vendor>
57-
<mainClass>org.byteskript.skript.app.ScriptLoader</mainClass>
58-
<mainJar>ByteSkript.jar</mainJar>
59-
<input>target/jar</input>
60-
<destination>target/dist</destination>
61-
</configuration>
62-
</plugin>
6350
</plugins>
6451
</build>
6552

@@ -81,7 +68,7 @@
8168
<dependency>
8269
<groupId>mx.kenzie</groupId>
8370
<artifactId>foundation</artifactId>
84-
<version>1.1.5</version>
71+
<version>1.1.6</version>
8572
<scope>compile</scope>
8673
</dependency>
8774
<dependency>

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@
3131
import org.byteskript.skript.lang.syntax.dictionary.DictionaryMember;
3232
import org.byteskript.skript.lang.syntax.dictionary.ImportFunctionEffect;
3333
import org.byteskript.skript.lang.syntax.dictionary.ImportTypeEffect;
34-
import org.byteskript.skript.lang.syntax.entry.ReturnType;
35-
import org.byteskript.skript.lang.syntax.entry.Template;
36-
import org.byteskript.skript.lang.syntax.entry.Trigger;
37-
import org.byteskript.skript.lang.syntax.entry.Verify;
34+
import org.byteskript.skript.lang.syntax.entry.*;
3835
import org.byteskript.skript.lang.syntax.entry.syntax.*;
3936
import org.byteskript.skript.lang.syntax.event.AnyLoadEvent;
4037
import org.byteskript.skript.lang.syntax.event.CurrentEventExpression;
@@ -131,6 +128,7 @@ private SkriptLangSpec() {
131128
new FunctionMember(),
132129
new NoArgsFunctionMember(),
133130
new Template(),
131+
new Extends(),
134132
new DictionaryMember(),
135133
new EveryMember()
136134
);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2021 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.entry;
8+
9+
import mx.kenzie.foundation.Type;
10+
import mx.kenzie.foundation.compiler.State;
11+
import org.byteskript.skript.api.note.Documentation;
12+
import org.byteskript.skript.api.syntax.SimpleEntry;
13+
import org.byteskript.skript.compiler.*;
14+
import org.byteskript.skript.lang.element.StandardElements;
15+
16+
@Documentation(
17+
name = "Extend Type",
18+
description = "Specify a type this type can extend.",
19+
examples = {
20+
"""
21+
type Square:
22+
extend: Shape
23+
"""
24+
}
25+
)
26+
public class Extends extends SimpleEntry {
27+
28+
public Extends() {
29+
super(SkriptLangSpec.LIBRARY, StandardElements.METADATA, "extend: %Type%");
30+
}
31+
32+
@Override
33+
public void compile(Context context, Pattern.Match match) {
34+
final String name = match.meta();
35+
final Type type = context.getType(name);
36+
if (type != null) context.getBuilder().setSuperclass(type);
37+
else context.getBuilder().setSuperclass(new Type(name.replace('.', '/')));
38+
context.setState(CompileState.ROOT);
39+
}
40+
41+
@Override
42+
public Pattern.Match match(String thing, Context context) {
43+
if (!thing.startsWith("extend: ")) return null;
44+
final Pattern.Match match = super.match(thing, context);
45+
if (match == null) return null;
46+
final String name = match.groups()[0].trim();
47+
if (name.isEmpty()) {
48+
context.getError().addHint(this, "A type should be specified after the 'extend:' entry.");
49+
return null;
50+
}
51+
if (name.contains("\"")) {
52+
context.getError().addHint(this, "Type names should not be written inside quotation marks.");
53+
return null;
54+
}
55+
return new Pattern.Match(Pattern.fakeMatcher(thing), name);
56+
}
57+
58+
@Override
59+
public boolean allowAsInputFor(Type type) {
60+
return false;
61+
}
62+
63+
@Override
64+
public boolean allowedIn(State state, Context context) {
65+
return context.getState() == CompileState.ROOT && context.hasFlag(AreaFlag.IN_TYPE);
66+
}
67+
68+
69+
}

src/main/java/org/byteskript/skript/lang/syntax/type/TypeMember.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ public void compile(Context context, Pattern.Match match) {
6969
.addValue("line", context.lineNumber())
7070
.addValue("compiled", Instant.now().getEpochSecond());
7171
builder.setModifiers(0x0001 | 0x0020);
72+
final WriteInstruction instruction = (writer, visitor) -> {
73+
final Type parent = builder.getSuperclass();
74+
if (parent == null)
75+
visitor.visitMethodInsn(183, "java/lang/Object", "<init>", "()V", false);
76+
else {
77+
final String internal = builder.getSuperclass().internalName();
78+
visitor.visitMethodInsn(183, internal, "<init>", "()V", false);
79+
}
80+
};
7281
builder.addMethod("<init>")
73-
.writeCode(WriteInstruction.loadThis(), WriteInstruction.superObject(), WriteInstruction.returnEmpty());
82+
.writeCode(WriteInstruction.loadThis(), instruction, WriteInstruction.returnEmpty());
7483
context.useSubBuilder(builder);
7584
context.addFlag(AreaFlag.IN_TYPE);
7685
context.setState(CompileState.ROOT); // members can go inside this!

src/test/resources/config.csk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
key 1: value
2+
3+
key 2: hello there!
4+
5+
another key: another value :o

src/test/resources/types.bsk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type Box:
2828
trigger:
2929
return "Hello?"
3030

31+
type Crate:
32+
extend: Box
33+
3134
function basic_use:
3235
trigger:
3336
set {thing} to a new Box
@@ -42,6 +45,11 @@ function basic_use:
4245
set {boolean} to bonk() from {thing}
4346
assert {boolean} is true: "Member function didn't return correctly."
4447
assert another_func() from {thing} is 10: "Template default function didn't return correctly."
48+
set {thing} to a new Crate
49+
assert {thing} exists: "Type creation failed."
50+
assert {thing} is a Crate: "Type self-comparison failed."
51+
assert {thing} is a Box: "Type super-comparison failed."
52+
assert {thing} is a Thing: "Type super template-comparison failed."
4553

4654
template type Shape:
4755
function get_sides:

0 commit comments

Comments
 (0)