Package sage :: Package misc :: Module interpreter
[show private | hide private]
[frames | no frames]

Module sage.misc.interpreter

Preparses input from the interpreter

Modified input.

  -- All ^'s (not in strings) are replaced by **'s.

  -- If M is a variable and i an integer,
     then M.i is replaced by M._i, so generators can be
     accessed as in MAGMA.

  -- quit alone on a line quits.

  -- load to load in scripts

  -- Most int literals n are replaced by ZZ(n)
     and all float literals x by RR(x).  Thus 2/3 is a
     rational number.  If they are in []'s, they
     aren't replaced (see comment below -- need to
     improve this!)

  -- 
All other input is processed normally.

I rewrote the SAGE/IPython preparser and uploaded it in the newest version of SAGE.  (2005-04-15). 
  
It automatically converts *most* integer literals to SAGE Integer's and  
decimal literals to SAGE Reals.  It does not convert indexes into 
1-d arrays, since those have to be ints.   
 
I also extended the load command so it *really* works exactly 
like the SAGE interpreter, so e.g., ^ for exponentiation is 
allowed.  Also files being loaded can themselves load other files. 
Finally, I added an "attach" command, e.g., 
    attach 'file' 
that works kind of like attach in MAGMA.  Whenever you enter a blank 
line in the SAGE interpreter, *all* attached files that have changed 
are automatically reloaded.  Moreover, the attached files work according 
to the SAGE interpreter rules, i.e., ^ --> **, etc.  
 
I also fixed it so ^ is not replaced by ** inside strings. 
 
Finally, I added back the M.n notation for the n-th generator 
of object M, again like in MAGMA.

EXAMPLE:

sage: 2/3
      2/3
sage: type(2/3)
      <type 'rational.Rational'>
sage: a = 49928420832092
sage: type(a)
      <type 'integer.Integer'>
sage: a.factor()
      [(2, 2), (11, 1), (1134736837093, 1)]
sage: v = [1,2,3]
sage: type(v[0])
      <type 'integer.Integer'>
sage: w = [5]    #<--- could be an index, so not converted to Integer
sage: w = [5/7] # this is the main gotcha..
sage: w
      [0]
sage: type(w[0])
      <type 'int'>

If we don't make potential list indices int's, then lots of stuff
breaks, or users have to type v[int(7)], which is insane.  
A fix would be to only not make what's in the brackets an
Integer if what's before the bracket is a valid identifier,
so the w = [5] above would work right. 

sage: s = "x^3 + x + 1"
sage: s
      'x^3 + x + 1'
sage: pari(s)
      x^3 + x + 1
sage: f = pari(s)
sage: f^2
      x^6 + 2*x^4 + 2*x^3 + x^2 + 2*x + 1
sage: V = VectorSp
VectorSpace                      VectorSpace_subspace
VectorSpace_ambient              VectorSpace_subspace_with_basis
VectorSpace_generic
sage: V = VectorSpace(Q,3)
sage: V.0
      (1 0 0)
sage: V.1
      (0 1 0)
sage: s = "This. Is. It."
sage: print s
This. Is. It.

Function Summary
  compile_file(name)
  custom_subst(line)
  do_prefilter_paste(line, continuation)
Alternate prefilter for input.
  isalphadigit_(s)
  last_bracket_is_after_identifier(line, i)
Return True if and only if the previous bracket before line[i] is after a valid identifier followed possibly by some space.

Variable Summary
str __author__ = 'William Stein <was@math.harvard.edu>'
str __date__ = 'March 2005'
str __license__ = 'GPL'
str __version__ = '0.2.0'
dict attached = {}

Function Details

do_prefilter_paste(line, continuation)

Alternate prefilter for input.

last_bracket_is_after_identifier(line, i)

Return True if and only if the previous bracket before line[i] is after a valid identifier followed possibly by some space.

Variable Details

__author__

Type:
str
Value:
'William Stein <was@math.harvard.edu>'                                 

__date__

Type:
str
Value:
'March 2005'                                                           

__license__

Type:
str
Value:
'GPL'                                                                  

__version__

Type:
str
Value:
'0.2.0'                                                                

attached

Type:
dict
Value:
{}                                                                     

Generated by Epydoc 2.1 on Mon May 9 17:57:03 2005 http://epydoc.sf.net