# HG changeset patch
# User William Stein <wstein@gmail.com>
# Date 1245890899 -7200
# Node ID eaa5e69309fa516d9be24ea467dfccda0a5fd082
# Parent  5c42902abd919bfe7776bc470fb8854544c5480a
trac 6391 -- part 2 -- libgap!

diff -r 5c42902abd91 -r eaa5e69309fa sage/libs/gap/gap.pyx
--- a/sage/libs/gap/gap.pyx	Wed Jun 24 18:43:02 2009 +0200
+++ b/sage/libs/gap/gap.pyx	Thu Jun 25 02:48:19 2009 +0200
@@ -1,5 +1,5 @@
 """
-PARI C-library interface
+GAP Library Interface.
 
 AUTHORS:
 
@@ -16,6 +16,7 @@
 #  The full text of the GPL is available at: http://www.gnu.org/licenses/
 ###############################################################################
 
+include "../../ext/stdsage.pxi"
 
 cdef extern from "":
     void PyErr_Clear()
@@ -31,9 +32,30 @@
     void* ReadEvalCommand()
     void* ReadEvalResult
 
-cdef extern from "":
+cdef extern from "gap/scanner.h":
+    void reset_output_buffer()
+    char* get_output_buffer()
+
+cdef extern from "gap/objects.h":
     void set_system_variables(char**, char**)
+    void set_next_input_line(char* line)
+    ctypedef void* Obj
 
+cdef extern from "gap/gasman.h":
+    pass
+
+cdef extern from "gap/ariths.h":
+    Obj SUM (Obj, Obj)
+    Obj DIFF(Obj, Obj)
+    Obj PROD(Obj, Obj)
+    Obj QUO(Obj, Obj)
+    Obj POW(Obj, Obj)
+    Obj MOD(Obj, Obj)
+    Obj CALL_1ARGS(Obj f, Obj a1)
+
+cdef extern from "gap/calls.h":
+    int IS_FUNC(Obj)
+  
 from sage.misc.cachefunc import cached_function
 @cached_function
 def gap_root():
@@ -53,7 +75,7 @@
     i = s.find('"')
     return s[:i]
 
-def command():
+def command(s):
     """
     Do one step of the GAP interpreters read/eval/print loop.  The
     first time this is called it spawns a GAP session.
@@ -64,6 +86,9 @@
     initialize_libgap()
 
     # Run the read-eval-print loop exactly once.
+    t = s+';\n'
+    set_next_input_line(t)
+        
     cdef ExecStatus status = ReadEvalCommand()
     if ReadEvalResult:
         ViewObjHandler( ReadEvalResult )
@@ -91,7 +116,81 @@
     set_system_variables(argv, environ)
     cdef int argc=3
     InitializeGap(&argc, argv)
+    reset_output_buffer()
     _initialized = True
     
 
     
+cdef class GapObj:
+    cdef Obj value
+    def __init__(self, s):
+        initialize_libgap()
+        # Run the read-eval-print loop exactly once.
+        t = s+';\n'
+        set_next_input_line(t)
+        ReadEvalCommand()
+        self.value = ReadEvalResult
+
+    def __repr__(self):
+        reset_output_buffer()
+        ViewObjHandler(self.value)
+        s = get_output_buffer()
+        return s[1:-2]
+
+    def __add__(GapObj self, GapObj right):
+        cdef GapObj r = PY_NEW(GapObj)
+        r.value = SUM(self.value, right.value)
+        return r
+
+    def __sub__(GapObj self, GapObj right):
+        cdef GapObj r = PY_NEW(GapObj)
+        r.value = DIFF(self.value, right.value)
+        return r
+        
+    def __mul__(GapObj self, GapObj right):
+        cdef GapObj r = PY_NEW(GapObj)
+        r.value = PROD(self.value, right.value)
+        return r
+
+    def __div__(GapObj self, GapObj right):
+        cdef GapObj r = PY_NEW(GapObj)
+        r.value = QUO(self.value, right.value)
+        return r
+
+    def __mod__(GapObj self, GapObj right):
+        cdef GapObj r = PY_NEW(GapObj)
+        r.value = MOD(self.value, right.value)
+        return r
+
+    def __pow__(GapObj self, GapObj right, dummy):
+        cdef GapObj r = PY_NEW(GapObj)
+        r.value = POW(self.value, right.value)
+        return r
+        
+    def is_func(self):
+        """
+        EXAMPLES::
+        
+            sage: a = sage.libs.gap.gap.GapObj("NormalSubgroups")
+            sage: a.is_func()
+            True
+            sage: a = sage.libs.gap.gap.GapObj("2/3")
+            sage: a.is_func()
+            False
+        """
+        return bool(IS_FUNC(self.value))
+
+    def __call__(self, GapObj x):
+        """
+        EXAMPLES::
+        
+            sage: G = sage.libs.gap.gap.GapObj
+            sage: a = G("NormalSubgroups")
+            sage: b = G("SymmetricGroup(4)")
+            sage: a(b)
+            [ Group(()), Group([ (1,4)(2,3), (1,3)(2,4) ]), Group([ (2,4,3), (1,4)(2,3), (1,3)(2,4) ]), Sym( [ 1 .. 4 ] ) 
+        """
+        cdef GapObj r = PY_NEW(GapObj)
+        r.value = CALL_1ARGS(self.value, x.value)
+        return r
+        
