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

org.snapscript.tree.variable.ArrayPointer Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.tree.variable;

import static org.snapscript.core.Reserved.PROPERTY_LENGTH;

import java.lang.reflect.Array;

import org.snapscript.core.Scope;
import org.snapscript.core.Value;
import org.snapscript.core.ValueType;

public class ArrayPointer implements VariablePointer {
   
   private final ObjectPointer pointer;
   private final String name;
   
   public ArrayPointer(String name) {
      this.pointer = new ObjectPointer(name);
      this.name = name;
   }
   
   @Override
   public Value get(Scope scope, Object left) {
      if(name.equals(PROPERTY_LENGTH)) {
         int length = Array.getLength(left);
         return ValueType.getConstant(length);
      }
      return pointer.get(scope, left);
   }
}