package com.tinkerpop.gremlin.compiler.operations.util;

import com.tinkerpop.gremlin.compiler.context.GremlinScriptContext;
import com.tinkerpop.gremlin.compiler.operations.Operation;
import com.tinkerpop.gremlin.compiler.types.Atom;
import com.tinkerpop.gremlin.compiler.types.DynamicEntity;

/**
 * @author Pavel A. Yaskevich
 */
public class DeclareVariable implements Operation {

    private final String name;
    private final Operation valueOperation;
    private final GremlinScriptContext context;
    
    public DeclareVariable(final String name, final Operation valueOp, final GremlinScriptContext context) {
        this.name = name;
        this.valueOperation = valueOp;
        this.context = context;
    }

    public Atom compute() {
        final Atom atom = this.valueOperation.compute();
        final Atom value = (!(atom instanceof DynamicEntity)) ? atom : new Atom<Object>(atom.getValue());
        
        this.context.getVariableLibrary().declare(this.name, value);
        return atom;
    }
    
    public Type getType() {
        return Type.STATEMENT;
    }

}

@@ -50,16 +50,62 @@
 .util;%0A%0A
+import com.tinkerpop.gremlin.compiler.Tokens;%0A
 import c
@@ -164,16 +164,16 @@
 ontext;%0A
-
 import c
@@ -332,16 +332,65 @@
 cEntity;
+%0Aimport com.tinkerpop.gremlin.compiler.types.Var;
 %0A%0A/**%0A *
@@ -597,21 +597,17 @@
 ontext;%0A
-    
 %0A
+
     publ
@@ -971,51 +971,28 @@
 m : 
-new Atom%3CObject%3E(atom.getValue());%0A        
+dynamicValue(atom);%0A
 %0A   
@@ -1088,13 +1088,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -1151,10 +1151,371 @@
 %0A    %7D%0A%0A
+    private Atom%3CObject%3E dynamicValue(final Atom atom) %7B%0A        if (atom instanceof Var) %7B%0A            final Var variable = (Var) atom;%0A            if (variable.getVariableName().equals(Tokens.IDENTITY)) %7B%0A                return new Atom%3CObject%3E(this.context.getCurrentPoint());%0A            %7D%0A        %7D%0A%0A        return new Atom%3CObject%3E(atom.getValue());%0A    %7D%0A
 %7D%0A

fix for issue/70 - Allow for Gpath assignment using := notation (related to issue 60).