Skip to content

Commit

Permalink
Update literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
Moderocky committed Mar 30, 2022
1 parent bed19ac commit c219f77
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 29 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/byteskript/skript/compiler/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ public void setStoredVariableName(String storedVariableName) {

public abstract void createTree(ProgrammaticSplitTree tree);

public abstract <Tree extends ProgrammaticSplitTree> Tree findTree(Class<Tree> type);

public abstract void closeAllTrees();

public abstract void removeTree(ProgrammaticSplitTree tree);
Expand Down Expand Up @@ -154,6 +152,14 @@ public SectionMeta getSection(int index) {
return sections.get(index);
}

public SectionMeta getTriggerSection() {
final TriggerTree tree = this.findTree(TriggerTree.class);
if (tree == null) return null;
return tree.owner();
}

public abstract <Tree extends ProgrammaticSplitTree> Tree findTree(Class<Tree> type);

public Section getParent() {
if (sections.isEmpty()) return null;
return sections.get(0).handler();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/byteskript/skript/compiler/ElementTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ public void disableCompilation() {
}
}

public void disableChildren() {
for (final ElementTree tree : nested) {
tree.disableCompilation();
}
}

public <Type> Type getLiteralValue() {
if (current instanceof Literal<?> literal) {
final Object meta = match.meta();
if (meta instanceof String string)
return (Type) literal.parse(string);
return (Type) meta;
}
return null;
}

public SyntaxElement current() {
return current;
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/byteskript/skript/compiler/FileContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,6 @@ public void createTree(ProgrammaticSplitTree tree) {
this.trees.add(0, tree);
}

@Override
public <Tree extends ProgrammaticSplitTree> Tree findTree(Class<Tree> type) {
for (final ProgrammaticSplitTree tree : this.trees) {
if (type.isInstance(tree)) return (Tree) tree;
}
return null;
}

@Override
public synchronized void closeAllTrees() {
for (final ProgrammaticSplitTree tree : trees.toArray(new ProgrammaticSplitTree[0])) {
Expand Down Expand Up @@ -404,6 +396,14 @@ public ProgrammaticSplitTree getCurrentTree() {
return trees.isEmpty() ? null : trees.get(0);
}

@Override
public <Tree extends ProgrammaticSplitTree> Tree findTree(Class<Tree> type) {
for (final ProgrammaticSplitTree tree : this.trees) {
if (type.isInstance(tree)) return (Tree) tree;
}
return null;
}

@Override
public ProgrammaticSplitTree getTree(SectionMeta meta) {
for (final ProgrammaticSplitTree tree : trees) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/byteskript/skript/compiler/Pattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public java.util.regex.Pattern[] getCompiledPatterns() {
}

public Match match(final String thing, final Context context) {
return match(thing, context, null);
return match(thing, context, thing);
}

public Match match(final String thing, final Context context, final Object meta) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.byteskript.skript.compiler.*;
import org.byteskript.skript.compiler.structure.Function;
import org.byteskript.skript.lang.element.StandardElements;
import org.byteskript.skript.lang.syntax.literal.StringLiteral;

@Documentation(
name = "Import Function",
Expand Down Expand Up @@ -50,7 +51,7 @@ public Pattern.Match match(String thing, Context context) {
@Override
public void compile(Context context, Pattern.Match match) throws Throwable {
final ElementTree tree = context.getCompileCurrent();
final String name = tree.nested()[0].match().meta();
final String name = new StringLiteral().parse(tree.nested()[0].match().meta());
final Type type = tree.nested()[1].match().meta();
context.registerFunction(new Function(name, type));
context.setState(CompileState.CODE_BODY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Pattern.Match match(String thing, Context context) {
final char c = thing.charAt(0);
if (c != '-' && (c < LOW || c > HIGH)) return null;
final Matcher matcher = PATTERN.matcher(thing);
if (matcher.find()) return new Pattern.Match(matcher);
if (matcher.find()) return new Pattern.Match(matcher, thing);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Pattern.Match match(String thing, Context context) {
final char c = thing.charAt(0);
if (c != '-' && (c < LOW || c > HIGH)) return null;
final Matcher matcher = PATTERN.matcher(thing);
if (matcher.find()) return new Pattern.Match(matcher);
if (matcher.find()) return new Pattern.Match(matcher, thing);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Pattern.Match match(String thing, Context context) {
final char c = thing.charAt(0);
if (c != '-' && (c < LOW || c > HIGH)) return null;
final Matcher matcher = PATTERN.matcher(thing);
if (matcher.find()) return new Pattern.Match(matcher);
if (matcher.find()) return new Pattern.Match(matcher, thing);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Pattern.Match match(String thing, Context context) {
final char c = thing.charAt(0);
if (c != '-' && (c < LOW || c > HIGH)) return null;
final Matcher matcher = PATTERN.matcher(thing);
if (matcher.find()) return new Pattern.Match(matcher);
if (matcher.find()) return new Pattern.Match(matcher, thing);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Pattern.Match match(String thing, Context context) {
if (thing.charAt(0) != '/') return null;
if (thing.charAt(thing.length() - 1) != '/') return null;
final Matcher matcher = PATTERN.matcher(thing);
if (matcher.find()) return new Pattern.Match(matcher);
if (matcher.find()) return new Pattern.Match(matcher, thing);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Pattern.Match match(String thing, Context context) {
if (thing.charAt(thing.length() - 1) != '"') return null;
final String contents = thing.substring(1, thing.length() - 1);
if (!matches(contents)) return null;
return new Pattern.Match(Pattern.fakeMatcher(thing), contents);
return new Pattern.Match(Pattern.fakeMatcher(thing), thing);
}

@Override
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/byteskript/skript/runtime/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,6 @@ public <From, To> Converter<From, To> getConverter(Class<From> from, Class<To> t
return null;
}

public <From, To> void registerConverter(Class<From> from, Class<To> to, Converter<From, To> converter) {
final Converter.Data data = new Converter.Data(from, to);
this.converters.put(data, converter);
}

public <From, To> void unregisterConverter(Class<From> from, Class<To> to) {
final Converter.Data data = new Converter.Data(from, to);
this.converters.remove(data);
}

@Description("""
Gets the parent class-loader attached to this Skript runtime.
This is used to search available libraries and scripts for classes.
Expand Down Expand Up @@ -555,6 +545,11 @@ public boolean registerLibrary(Library library) {
return compiler.addLibrary(library);
}

public <From, To> void registerConverter(Class<From> from, Class<To> to, Converter<From, To> converter) {
final Converter.Data data = new Converter.Data(from, to);
this.converters.put(data, converter);
}

@Description("""
Removes a library instance from the current compiler.
""")
Expand All @@ -567,6 +562,11 @@ public boolean unregisterLibrary(Library library) {
return compiler.removeLibrary(library);
}

public <From, To> void unregisterConverter(Class<From> from, Class<To> to) {
final Converter.Data data = new Converter.Data(from, to);
this.converters.remove(data);
}

@Description("""
Returns the provided script compiler.
""")
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/byteskript/skript/test/SyntaxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class SyntaxTest extends SkriptTest {
= new Skript();
// = new Skript(new DebugSkriptCompiler(Stream.controller(System.out)));


@Test
public void all() throws Throwable {
final URI uri = SyntaxTest.class.getClassLoader().getResource("tests").toURI();
Expand Down

0 comments on commit c219f77

Please sign in to comment.