package com.yagadi.enguage.expression;

import java.util.ArrayList;

import com.yagadi.enguage.sofa.Attributes;
import com.yagadi.enguage.util.Audit;
import com.yagadi.enguage.util.Strings;
/*  A difficult concept? With:
 *  [ [ one=>123, two=>456,  thr=>789  ],
 *    [ thr=>123, four=>456, five=>789 ]
 *  ]
 *  We may request one and get 123, five and get 456, or thr and get 789.
 *  
 *  Can this be done in a single Attributes list - returning the first value? What about
 *  removing a layer of context?
 */
public class Context {
	static Audit audit = new Audit( "Context" );
	static private ArrayList<Attributes> contexts = new ArrayList<Attributes>();
	static public void  push( Attributes ctx ){ contexts.add( 0, ctx ); }
	static public void  pop() { contexts.remove( 0 );}
	static public Attributes context() {
		if (contexts.size() == 0) push( new Attributes() );
		return contexts.get( 0 );
	}
	static public Strings deref( Strings words ) { return deref( words, false ); }
	static public Strings deref( Strings words, boolean expand ) {
		for (Attributes context : contexts)
			context.deref( words, expand );
		return words;
	}
	static public String get( String word ) {
		for (Attributes context : contexts) {
			String deref = context.get( word );
			if (!deref.equals(""))
				return deref;
		}
		return "";
	}
	static public String valueOf() {
		String s = "";
		for (Attributes context : contexts)
			s += context.toString();
		return s;
	}
}

@@ -189,36 +189,71 @@
 ;%0A/*
-  A difficult concept? With:
+* Context: a list of attributes, so a list of list of attribute
 %0A * 
@@ -349,12 +349,8 @@
  We 
-may 
 requ
@@ -383,19 +383,19 @@
 and get 
-456
+789
 , or thr
@@ -412,135 +412,8 @@
 89.%0A
- *  %0A *  Can this be done in a single Attributes list - returning the first value? What about%0A *  removing a layer of context?%0A
  */%0A
@@ -481,16 +481,18 @@
 ext%22 );%0A
+%09%0A
 %09static 
@@ -803,24 +803,95 @@
 et( 0 );%0A%09%7D%0A
+%09%0A%09// these need to be words? %5B %22hello%22, %22there%22 %5D -%3E %5B%22hi%22, %22martin%22%5D%0A
 %09static publ
@@ -1432,10 +1432,292 @@
 n s;%0A%09%7D%0A
+%09public static void main( String args%5B%5D) %7B%0A%09%09audit.log( %22hello there, %22+ get( %22martin%22 ) );%0A%09%09push( new Attributes().add( %22martin%22, %22world%22 ));%0A%09%09audit.log( %22hello there, %22+ get( %22martin%22 ) );%0A%09%09push( new Attributes().add( %22hello%22, %22there%22 ));%0A%09%09audit.log( %22ctx%3E%3E%3E%22+ valueOf() );%0A%7D%09
 %7D%0A

Tidy - added a main() and header comment