gnu.expr
Class Language

java.lang.Object
  extended by gnu.expr.Language
Direct Known Subclasses:
LispLanguage, XQuery

public abstract class Language
extends java.lang.Object

Contains various language-dependent methods. Also contains "global" state about the executation environment, such as the global Environment. There can be multiple Languages associated with different threads, representing mutiple top-levels. (However, this functionality is incomplete.)


Field Summary
protected static ThreadLocation current
           
protected static int env_counter
           
protected  Environment environ
          The environment for language built-ins and predefined bindings.
static int FUNCTION_NAMESPACE
           
static java.lang.String NAMESPACE_PREFIX
          Used when defining a namespace alias (prefix), in the XML sense.
static int NAMESPACE_PREFIX_NAMESPACE
           
static int PARSE_IMMEDIATE
          Flag to tell parse that expression will be evaluated immediately.
static int PARSE_ONE_LINE
          Flag to tell parse to only read a single line if possible.
protected  Environment userEnv
          If non-null, the user environment.
static int VALUE_NAMESPACE
           
 
Constructor Summary
protected Language()
           
 
Method Summary
 Type asType(java.lang.Object spec)
          "Coerce" a language-specific "type specifier" object to a Type.
 java.lang.Object booleanObject(boolean b)
           
 java.lang.Object coerceFromObject(java.lang.Class clas, java.lang.Object obj)
           
 java.lang.Object coerceToObject(java.lang.Class clas, java.lang.Object obj)
           
 java.lang.Object coerceToObject(int val)
           
 Declaration declFromField(ModuleExp mod, java.lang.Object fvalue, Field fld)
           
protected  void defAliasStFld(java.lang.String name, java.lang.String cname, java.lang.String fname)
          Declare in the current Environment a variable aliased to a static field.
 void define(java.lang.String sym, java.lang.Object p)
          Enter a value into the current environment.
 void defineFunction(Named proc)
          Enter a named function into the current environment.
 void defineFunction(java.lang.String name, java.lang.Object proc)
          Enter a function into the current environment.
protected  void defProcStFld(java.lang.String name, java.lang.String cname)
          Declare in the current Environment a procedure bound to a static field.
protected  void defProcStFld(java.lang.String name, java.lang.String cname, java.lang.String fname)
          Declare in the current Environment a procedure bound to a static field.
 void emitCoerceToBoolean(CodeAttr code)
          Generate code to test if an object is considered true.
 void emitPushBoolean(boolean value, CodeAttr code)
           
 java.lang.Object eval(InPort port)
          Evaluate expression(s) read from an InPort.
 void eval(InPort port, CallContext ctx)
           
 java.lang.Object eval(java.io.Reader in)
          Evaluate expression(s) read from a Reader.
 void eval(java.io.Reader in, Consumer out)
          Read expressions from a Reader and write the result to a Consumer.
 void eval(java.io.Reader in, java.io.Writer out)
          Read expressions from a Reader and write the result to a Writer.
 java.lang.Object eval(java.lang.String string)
          Return the result of evaluating a string as a source expression.
 void eval(java.lang.String string, Consumer out)
          Evaluate a string and write the result value(s) to a Consumer.
 void eval(java.lang.String string, PrintConsumer out)
          Evaluate a string and write the result value(s) to a PrintConsumer.
 void eval(java.lang.String string, java.io.Writer out)
          Evaluate a string and write the result value(s) on a Writer.
static Language getDefaultLanguage()
           
 Environment getEnvironment()
          Get current user environment.
 java.lang.Object getEnvPropertyFor(Declaration decl)
           
 java.lang.Object getEnvPropertyFor(java.lang.reflect.Field fld, java.lang.Object value)
           
 AbstractFormat getFormat(boolean readable)
           
static Language getInstance(java.lang.String name)
          Look for a language with the given name or extension.
static Language getInstance(java.lang.String langName, java.lang.Class langClass)
           
static Language getInstanceFromFilenameExtension(java.lang.String filename)
           
 Environment getLangEnvironment()
           
static java.lang.String[][] getLanguages()
          Get a list of all available languages
abstract  Lexer getLexer(InPort inp, SourceMessages messages)
           
 java.lang.String getName()
           
 int getNamespaceOf(Declaration decl)
          Return the namespace (e.g value or function) of a Declaration.
 Environment getNewEnvironment()
           
 Consumer getOutputConsumer(java.io.Writer out)
           
 Procedure getPrompter()
           
 Symbol getSymbol(java.lang.String name)
           
 Type getTypeFor(java.lang.Class clas)
           
 Type getTypeFor(Expression exp)
           
 Type getTypeFor(java.lang.String name)
           
 boolean hasSeparateFunctionNamespace()
          True if functions are in a separate anme space from variable.
 boolean isTrue(java.lang.Object value)
          Test if a value is considered "true" in this language.
 void loadClass(java.lang.String name)
           
 java.lang.Object lookup(java.lang.String name)
           
 NamedLocation lookupBuiltin(Symbol name, java.lang.Object property, int hash)
           
 java.lang.Object noValue()
          The value to return for a "void" result.
 Compilation parse(InPort port, SourceMessages messages, int options)
          Parse one or more expressions.
abstract  Compilation parse(Lexer lexer, int options)
           
 void print(java.lang.Object obj, OutPort out)
           
 void print(java.lang.Object value, OutPort out, boolean readable)
           
static void registerLanguage(java.lang.String[] langMapping)
          Add a language to the list.
 void runAsApplication(java.lang.String[] args)
           
static void setDefaultLanguage(Language language)
           
static void setDefaults(Language lang)
           
static Type string2Type(java.lang.String name)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

current

protected static final ThreadLocation current

environ

protected Environment environ
The environment for language built-ins and predefined bindings.


userEnv

protected Environment userEnv
If non-null, the user environment. This allows "bunding" an Environment with a Language. This is partly to match existing documentation, and partly for convenience from Java code. Normally, userEnv is null, in which case the user environment is extracted from the current thread.


PARSE_IMMEDIATE

public static final int PARSE_IMMEDIATE
Flag to tell parse that expression will be evaluated immediately. I.e. we're not creating class files for future execution.

See Also:
Constant Field Values

PARSE_ONE_LINE

public static final int PARSE_ONE_LINE
Flag to tell parse to only read a single line if possible. Multiple lines may be read if syntactically required.

See Also:
Constant Field Values

NAMESPACE_PREFIX

public static final java.lang.String NAMESPACE_PREFIX
Used when defining a namespace alias (prefix), in the XML sense. Define in a namespace prefix NS is equivalent to defining a constant named NAMESPACE_PREFIX+"NS" whose value is the namespace URI.

See Also:
Constant Field Values

VALUE_NAMESPACE

public static final int VALUE_NAMESPACE
See Also:
Constant Field Values

FUNCTION_NAMESPACE

public static final int FUNCTION_NAMESPACE
See Also:
Constant Field Values

NAMESPACE_PREFIX_NAMESPACE

public static final int NAMESPACE_PREFIX_NAMESPACE
See Also:
Constant Field Values

env_counter

protected static int env_counter
Constructor Detail

Language

protected Language()
Method Detail

getDefaultLanguage

public static Language getDefaultLanguage()

setDefaultLanguage

public static void setDefaultLanguage(Language language)

getLanguages

public static java.lang.String[][] getLanguages()
Get a list of all available languages


registerLanguage

public static void registerLanguage(java.lang.String[] langMapping)
Add a language to the list.

Parameters:
langMapping - is a language definition, the first index is the language name, subsequent indexes are file types that might cause the language to be used and the final index is the name of the class that implements the language.

getInstanceFromFilenameExtension

public static Language getInstanceFromFilenameExtension(java.lang.String filename)

getInstance

public static Language getInstance(java.lang.String name)
Look for a language with the given name or extension. If name is null, look for the first language available.


getInstance

public static Language getInstance(java.lang.String langName,
                                   java.lang.Class langClass)

isTrue

public boolean isTrue(java.lang.Object value)
Test if a value is considered "true" in this language.


booleanObject

public java.lang.Object booleanObject(boolean b)

noValue

public java.lang.Object noValue()
The value to return for a "void" result.


hasSeparateFunctionNamespace

public boolean hasSeparateFunctionNamespace()
True if functions are in a separate anme space from variable. Is true for e.g. Common Lisp, Emacs Lisp; false for Scheme.


getEnvironment

public final Environment getEnvironment()
Get current user environment.


getNewEnvironment

public final Environment getNewEnvironment()

getLangEnvironment

public Environment getLangEnvironment()

lookupBuiltin

public NamedLocation lookupBuiltin(Symbol name,
                                   java.lang.Object property,
                                   int hash)

define

public void define(java.lang.String sym,
                   java.lang.Object p)
Enter a value into the current environment.


defAliasStFld

protected void defAliasStFld(java.lang.String name,
                             java.lang.String cname,
                             java.lang.String fname)
Declare in the current Environment a variable aliased to a static field.


defProcStFld

protected void defProcStFld(java.lang.String name,
                            java.lang.String cname,
                            java.lang.String fname)
Declare in the current Environment a procedure bound to a static field.

Parameters:
name - the procedure's source-level name.
cname - the name of the class containing the field.
fname - the name of the field, which should be a static final field whose type extends gnu.mapping.Procedure.

defProcStFld

protected void defProcStFld(java.lang.String name,
                            java.lang.String cname)
Declare in the current Environment a procedure bound to a static field.

Parameters:
name - the procedure's source-level name.
cname - the name of the class containing the field. The name of the field is the mangling of name.

defineFunction

public final void defineFunction(Named proc)
Enter a named function into the current environment.


defineFunction

public void defineFunction(java.lang.String name,
                           java.lang.Object proc)
Enter a function into the current environment. Same as define(name,proc) for Scheme, but not for (say) Common Lisp.


getEnvPropertyFor

public java.lang.Object getEnvPropertyFor(java.lang.reflect.Field fld,
                                          java.lang.Object value)

getEnvPropertyFor

public java.lang.Object getEnvPropertyFor(Declaration decl)

loadClass

public void loadClass(java.lang.String name)
               throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException

getSymbol

public Symbol getSymbol(java.lang.String name)

lookup

public java.lang.Object lookup(java.lang.String name)

print

public void print(java.lang.Object obj,
                  OutPort out)

print

public void print(java.lang.Object value,
                  OutPort out,
                  boolean readable)

getFormat

public AbstractFormat getFormat(boolean readable)

getOutputConsumer

public Consumer getOutputConsumer(java.io.Writer out)

getName

public java.lang.String getName()

getLexer

public abstract Lexer getLexer(InPort inp,
                               SourceMessages messages)

parse

public final Compilation parse(InPort port,
                               SourceMessages messages,
                               int options)
                        throws java.io.IOException,
                               SyntaxException
Parse one or more expressions.

Parameters:
port - the InPort to read the expressions from.
messages - where to send error messages and warnings
options - various flags, includding PARSE_IMMEDIATE and PARSE_ONE_LINE
Returns:
a new Compilation. May return null if PARSE_ONE_LINE on end-of-file.
Throws:
java.io.IOException
SyntaxException

parse

public abstract Compilation parse(Lexer lexer,
                                  int options)
                           throws java.io.IOException,
                                  SyntaxException
Throws:
java.io.IOException
SyntaxException

getTypeFor

public Type getTypeFor(java.lang.Class clas)

string2Type

public static Type string2Type(java.lang.String name)

getTypeFor

public Type getTypeFor(java.lang.String name)

asType

public Type asType(java.lang.Object spec)
"Coerce" a language-specific "type specifier" object to a Type.


getTypeFor

public Type getTypeFor(Expression exp)

declFromField

public Declaration declFromField(ModuleExp mod,
                                 java.lang.Object fvalue,
                                 Field fld)

getNamespaceOf

public int getNamespaceOf(Declaration decl)
Return the namespace (e.g value or function) of a Declaration. Return a bitmask of all the namespces "covered" by the Declaration.


emitPushBoolean

public void emitPushBoolean(boolean value,
                            CodeAttr code)

emitCoerceToBoolean

public void emitCoerceToBoolean(CodeAttr code)
Generate code to test if an object is considered true. Assume the object has been pushed on the JVM stack. Generate code to push true or false as appropriate.


coerceFromObject

public java.lang.Object coerceFromObject(java.lang.Class clas,
                                         java.lang.Object obj)

coerceToObject

public java.lang.Object coerceToObject(java.lang.Class clas,
                                       java.lang.Object obj)

coerceToObject

public java.lang.Object coerceToObject(int val)

setDefaults

public static void setDefaults(Language lang)

getPrompter

public Procedure getPrompter()

eval

public final java.lang.Object eval(java.lang.String string)
                            throws java.lang.Throwable
Return the result of evaluating a string as a source expression.

Throws:
java.lang.Throwable

eval

public final java.lang.Object eval(java.io.Reader in)
                            throws java.lang.Throwable
Evaluate expression(s) read from a Reader. This just calls eval(InPort).

Throws:
java.lang.Throwable

eval

public final java.lang.Object eval(InPort port)
                            throws java.lang.Throwable
Evaluate expression(s) read from an InPort.

Throws:
java.lang.Throwable

eval

public final void eval(java.lang.String string,
                       java.io.Writer out)
                throws java.lang.Throwable
Evaluate a string and write the result value(s) on a Writer.

Throws:
java.lang.Throwable

eval

public final void eval(java.lang.String string,
                       PrintConsumer out)
                throws java.lang.Throwable
Evaluate a string and write the result value(s) to a PrintConsumer. This is to disambiguate calls using OutPort or XMLPrinter, which are both Writer and Consumer.

Throws:
java.lang.Throwable

eval

public final void eval(java.lang.String string,
                       Consumer out)
                throws java.lang.Throwable
Evaluate a string and write the result value(s) to a Consumer.

Throws:
java.lang.Throwable

eval

public final void eval(java.io.Reader in,
                       java.io.Writer out)
                throws java.lang.Throwable
Read expressions from a Reader and write the result to a Writer.

Throws:
java.lang.Throwable

eval

public void eval(java.io.Reader in,
                 Consumer out)
          throws java.lang.Throwable
Read expressions from a Reader and write the result to a Consumer.

Throws:
java.lang.Throwable

eval

public void eval(InPort port,
                 CallContext ctx)
          throws java.lang.Throwable
Throws:
java.lang.Throwable

runAsApplication

public void runAsApplication(java.lang.String[] args)