All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.tree.define.EnumConstructorBinder Maven / Gradle / Ivy

package org.snapscript.tree.define;

import static org.snapscript.core.Reserved.TYPE_CONSTRUCTOR;
import static org.snapscript.core.error.Reason.CONSTRUCTION;

import org.snapscript.core.Context;
import org.snapscript.core.error.ErrorHandler;
import org.snapscript.core.function.resolve.FunctionCall;
import org.snapscript.core.function.resolve.FunctionResolver;
import org.snapscript.core.module.Module;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.scope.instance.Instance;
import org.snapscript.core.type.Type;
import org.snapscript.tree.ArgumentList;
import org.snapscript.tree.construct.ConstructArgumentList;

public class EnumConstructorBinder {

   private final ConstructArgumentList arguments;
   
   public EnumConstructorBinder(ArgumentList arguments) {
      this.arguments = new ConstructArgumentList(null, arguments);
   }
   
   public Instance bind(Scope scope, Type type) throws Exception {
      Module module = scope.getModule();
      Context context = module.getContext();
      ErrorHandler handler = context.getHandler();
      FunctionResolver resolver = context.getResolver();
      Object[] array = arguments.create(scope, type);
      FunctionCall call = resolver.resolveStatic(scope, type, TYPE_CONSTRUCTOR, array);
      
      if(call == null) {
         handler.handleRuntimeError(CONSTRUCTION, scope, type, TYPE_CONSTRUCTOR, array);
      }
      return (Instance)call.invoke(scope, null, array);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy