org.snapscript.tree.define.AnyDefinition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap Show documentation
Show all versions of snap Show documentation
Dynamic scripting for the JVM
package org.snapscript.tree.define;
import static org.snapscript.core.Category.CLASS;
import static org.snapscript.core.ModifierType.CONSTANT;
import static org.snapscript.core.ModifierType.PUBLIC;
import static org.snapscript.core.Phase.COMPILED;
import static org.snapscript.core.Phase.DEFINED;
import static org.snapscript.core.Reserved.ANY_TYPE;
import static org.snapscript.core.Reserved.DEFAULT_PACKAGE;
import static org.snapscript.core.Reserved.METHOD_EQUALS;
import static org.snapscript.core.Reserved.METHOD_HASH_CODE;
import static org.snapscript.core.Reserved.METHOD_NOTIFY;
import static org.snapscript.core.Reserved.METHOD_NOTIFY_ALL;
import static org.snapscript.core.Reserved.METHOD_TO_STRING;
import static org.snapscript.core.Reserved.METHOD_WAIT;
import static org.snapscript.core.Reserved.TYPE_CONSTRUCTOR;
import static org.snapscript.core.Reserved.TYPE_THIS;
import java.util.List;
import org.snapscript.common.Progress;
import org.snapscript.core.Context;
import org.snapscript.core.Module;
import org.snapscript.core.Phase;
import org.snapscript.core.Result;
import org.snapscript.core.Scope;
import org.snapscript.core.State;
import org.snapscript.core.Type;
import org.snapscript.core.TypeLoader;
import org.snapscript.core.Value;
import org.snapscript.core.define.Instance;
import org.snapscript.core.function.Function;
import org.snapscript.core.function.Invocation;
public class AnyDefinition{
private final AnyFunctionBuilder builder;
public AnyDefinition(){
this.builder = new AnyFunctionBuilder();
}
public Type create(Scope scope) throws Exception {
Module module = scope.getModule();
Context context = module.getContext();
TypeLoader loader = context.getLoader();
Type result = loader.defineType(DEFAULT_PACKAGE, ANY_TYPE, CLASS);
Progress progress = result.getProgress();
List functions = result.getFunctions();
if(progress.done(DEFINED)) {
Function constructor = builder.create(result, TYPE_CONSTRUCTOR, NewInvocation.class, Object.class);
Function hashCode = builder.create(result, METHOD_HASH_CODE, HashCodeInvocation.class);
Function toString = builder.create(result, METHOD_TO_STRING, ToStringInvocation.class);
Function equals = builder.create(result, METHOD_EQUALS, EqualsInvocation.class, Object.class);
Function wait = builder.create(result, METHOD_WAIT, WaitInvocation.class);
Function waitFor = builder.create(result, METHOD_WAIT, WaitForInvocation.class, Long.class);
Function notify = builder.create(result, METHOD_NOTIFY, NotifyInvocation.class);
Function notifyAll = builder.create(result, METHOD_NOTIFY_ALL, NotifyAllInvocation.class);
functions.add(constructor);
functions.add(wait);
functions.add(waitFor);
functions.add(notify);
functions.add(notifyAll);
functions.add(hashCode);
functions.add(equals);
functions.add(toString);
progress.done(COMPILED);
}
progress.wait(COMPILED);
return result;
}
private static class NewInvocation implements Invocation