org.snapscript.tree.define.ClassBuilder 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.constraint.Constraint.NONE;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.module.Module;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.scope.State;
import org.snapscript.core.type.Type;
import org.snapscript.core.type.TypeBody;
import org.snapscript.core.variable.Value;
import org.snapscript.tree.annotation.AnnotationList;
import org.snapscript.tree.constraint.ClassName;
public class ClassBuilder {
private final AtomicReference reference;
private final ClassPropertyGenerator generator;
private final ConstantPropertyBuilder builder;
private final AnnotationList annotations;
private final TypeHierarchy hierarchy;
private final ClassName name;
public ClassBuilder(AnnotationList annotations, ClassName name, TypeHierarchy hierarchy) {
this.reference = new AtomicReference();
this.generator = new ClassPropertyGenerator();
this.builder = new ConstantPropertyBuilder();
this.annotations = annotations;
this.hierarchy = hierarchy;
this.name = name;
}
public Type create(TypeBody body, Scope outer) throws Exception {
Module module = outer.getModule();
String alias = name.getName(outer);
int modifiers = name.getModifiers(outer);
Type enclosing = outer.getType();
Type type = module.addType(alias, modifiers);
if(enclosing != null) {
String name = type.getName();
String prefix = enclosing.getName();
String key = name.replace(prefix + '$', ""); // get the class name
Value value = Value.getConstant(type);
State state = outer.getState();
builder.createStaticProperty(body, key, enclosing, NONE);
state.addValue(key, value);
}
reference.set(type);
return type;
}
public Type define(TypeBody body, Scope outer) throws Exception {
Type type = reference.get();
Scope scope = type.getScope();
List generics = name.getGenerics(scope);
List constraints = type.getGenerics();
constraints.addAll(generics);
annotations.apply(scope, type);
generator.generate(body, scope, type);
hierarchy.define(scope, type);
return type;
}
public Type compile(TypeBody body, Scope outer) throws Exception {
Type type = reference.get();
hierarchy.compile(outer, type);
return type;
}
}