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

org.snapscript.core.StackIndex Maven / Gradle / Ivy

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

import java.util.Iterator;

import org.snapscript.common.ArrayStack;
import org.snapscript.common.Cache;
import org.snapscript.common.HashCache;
import org.snapscript.common.Stack;

public class StackIndex implements Index {
   
   private final Cache locals;
   private final Stack stack;

   public StackIndex() {
      this.locals = new HashCache();
      this.stack = new ArrayStack(0); // do not use default
   }
   
   @Override
   public Iterator iterator(){
      return locals.keySet().iterator();
   }
   
   @Override
   public int get(String name) {
      Integer index = locals.fetch(name);
      
      if(index != null){
         return index;
      }
      return -1;
   }
   
   @Override
   public int index(String name) {
      Integer index = locals.fetch(name);
      
      if(index != null) {
         throw new IllegalStateException("Duplicate variable '" + name + "' in scope");
      }
      int size = locals.size();
      
      locals.cache(name, size);
      stack.push(name);
      
      return size;
   }
   
   @Override
   public void reset(int index) {
      int size = locals.size();
      
      for(int i = size; i > index; i--) {
         String name = stack.pop();
         locals.take(name);
      }
   }

   @Override
   public int size(){
      return locals.size();
   }
   
   @Override
   public String toString() {
      return String.valueOf(locals);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy