|
| 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.flow.execute; |
| 8 | + |
| 9 | +import mx.kenzie.foundation.compiler.State; |
| 10 | +import org.byteskript.skript.api.note.Documentation; |
| 11 | +import org.byteskript.skript.api.syntax.Section; |
| 12 | +import org.byteskript.skript.compiler.CompileState; |
| 13 | +import org.byteskript.skript.compiler.Context; |
| 14 | +import org.byteskript.skript.compiler.Pattern; |
| 15 | +import org.byteskript.skript.compiler.SkriptLangSpec; |
| 16 | +import org.byteskript.skript.compiler.structure.MonitorTree; |
| 17 | +import org.byteskript.skript.compiler.structure.ProgrammaticSplitTree; |
| 18 | +import org.byteskript.skript.compiler.structure.SectionMeta; |
| 19 | +import org.byteskript.skript.lang.element.StandardElements; |
| 20 | + |
| 21 | +@Documentation( |
| 22 | + name = "Monitor Section", |
| 23 | + description = """ |
| 24 | + Locks an object so only this process may use it during the block. |
| 25 | + Additionally waits for other locks on the same object to close before starting. |
| 26 | + """, |
| 27 | + examples = { |
| 28 | + """ |
| 29 | + monitor {list}: |
| 30 | + add 10 to {list} |
| 31 | + """ |
| 32 | + } |
| 33 | +) |
| 34 | +public class MonitorSection extends Section { |
| 35 | + |
| 36 | + public MonitorSection() { |
| 37 | + super(SkriptLangSpec.LIBRARY, StandardElements.SECTION, "monitor %Object%"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Pattern.Match match(String thing, Context context) { |
| 42 | + if (!thing.startsWith("monitor ")) return null; |
| 43 | + return super.match(thing, context); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void compile(Context context, Pattern.Match match) throws Throwable { |
| 48 | + final MonitorTree tree = new MonitorTree(context.getSection(1)); |
| 49 | + this.compileTogether(context, tree); |
| 50 | + } |
| 51 | + |
| 52 | + public void compileTogether(Context context, MonitorTree tree) throws Throwable { |
| 53 | + context.createTree(tree); |
| 54 | + tree.start(context); |
| 55 | + context.setState(CompileState.CODE_BODY); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public boolean allowedIn(State state, Context context) { |
| 60 | + return super.allowedIn(state, context) |
| 61 | + && context.getSection() != null |
| 62 | + && context.getMethod() != null; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean isDelay() { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onSectionExit(Context context, SectionMeta meta) { |
| 72 | + final ProgrammaticSplitTree current; |
| 73 | + if (context.getTree(context.getSection()) instanceof MonitorTree found) current = found; |
| 74 | + else current = context.getCurrentTree(); |
| 75 | + if (current instanceof MonitorTree tree) tree.close(context); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void compileInline(Context context, Pattern.Match match) throws Throwable { |
| 80 | + final MonitorTree tree = new MonitorTree(context.getSection()); |
| 81 | + this.compileTogether(context, tree); |
| 82 | + tree.close(context); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments