gnu.expr

Class Language

Known Direct 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

static int
FUNCTION_NAMESPACE
static 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.
static int
VALUE_NAMESPACE
protected static ThreadLocation
current
protected static int
env_counter
protected Environment
environ
The environment for language built-ins and predefined bindings.
protected Environment
userEnv
If non-null, the user environment.

Constructor Summary

Language()

Method Summary

Type
asType(Object spec)
"Coerce" a language-specific "type specifier" object to a Type.
Object
booleanObject(boolean b)
Object
coerceFromObject(Class clas, Object obj)
Object
coerceToObject(Class clas, Object obj)
Object
coerceToObject(int val)
Declaration
declFromField(ModuleExp mod, Object fvalue, Field fld)
protected void
defAliasStFld(String name, String cname, String fname)
Declare in the current Environment a variable aliased to a static field.
protected void
defProcStFld(String name, String cname)
Declare in the current Environment a procedure bound to a static field.
protected void
defProcStFld(String name, String cname, String fname)
Declare in the current Environment a procedure bound to a static field.
void
define(String sym, Object p)
Enter a value into the current environment.
void
defineFunction(String name, Object proc)
Enter a function into the current environment.
void
defineFunction(Named proc)
Enter a named function into the current environment.
void
emitCoerceToBoolean(CodeAttr code)
Generate code to test if an object is considered true.
void
emitPushBoolean(boolean value, CodeAttr code)
Object
eval(Reader in)
Evaluate expression(s) read from a Reader.
void
eval(Reader in, Writer out)
Read expressions from a Reader and write the result to a Writer.
void
eval(Reader in, Consumer out)
Read expressions from a Reader and write the result to a Consumer.
Object
eval(String string)
Return the result of evaluating a string as a source expression.
void
eval(String string, Writer out)
Evaluate a string and write the result value(s) on a Writer.
void
eval(String string, Consumer out)
Evaluate a string and write the result value(s) to a Consumer.
void
eval(String string, PrintConsumer out)
Evaluate a string and write the result value(s) to a PrintConsumer.
Object
eval(InPort port)
Evaluate expression(s) read from an InPort.
void
eval(InPort port, CallContext ctx)
static Language
getDefaultLanguage()
Object
getEnvPropertyFor(Declaration decl)
Object
getEnvPropertyFor(java.lang.reflect.Field fld, Object value)
Environment
getEnvironment()
Get current user environment.
AbstractFormat
getFormat(boolean readable)
static Language
getInstance(String name)
Look for a language with the given name or extension.
static Language
getInstance(String langName, Class langClass)
static Language
getInstanceFromFilenameExtension(String filename)
Environment
getLangEnvironment()
static String[][]
getLanguages()
Get a list of all available languages
abstract Lexer
getLexer(InPort inp, SourceMessages messages)
String
getName()
int
getNamespaceOf(Declaration decl)
Return the namespace (e.g value or function) of a Declaration.
Environment
getNewEnvironment()
Consumer
getOutputConsumer(Writer out)
Procedure
getPrompter()
Symbol
getSymbol(String name)
Type
getTypeFor(Class clas)
Type
getTypeFor(String name)
Type
getTypeFor(Expression exp)
boolean
hasSeparateFunctionNamespace()
True if functions are in a separate anme space from variable.
boolean
isTrue(Object value)
Test if a value is considered "true" in this language.
void
loadClass(String name)
Object
lookup(String name)
NamedLocation
lookupBuiltin(Symbol name, Object property, int hash)
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(Object obj, OutPort out)
void
print(Object value, OutPort out, boolean readable)
static void
registerLanguage(String[] langMapping)
Add a language to the list.
void
runAsApplication(String[] args)
static void
setDefaultLanguage(Language language)
static void
setDefaults(Language lang)
static Type
string2Type(String name)

Field Details

FUNCTION_NAMESPACE

public static final int FUNCTION_NAMESPACE
Field Value:
2

NAMESPACE_PREFIX

public static final 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.

NAMESPACE_PREFIX_NAMESPACE

public static final int NAMESPACE_PREFIX_NAMESPACE
Field Value:
4

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.
Field Value:
1

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.
Field Value:
2

VALUE_NAMESPACE

public static final int VALUE_NAMESPACE
Field Value:
1

current

protected static final ThreadLocation current

env_counter

protected static int env_counter

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.

Constructor Details

Language

protected Language()

Method Details

asType

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

booleanObject

public Object booleanObject(boolean b)

coerceFromObject

public Object coerceFromObject(Class clas,
                               Object obj)

coerceToObject

public Object coerceToObject(Class clas,
                             Object obj)

coerceToObject

public Object coerceToObject(int val)

declFromField

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

defAliasStFld

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

defProcStFld

protected void defProcStFld(String name,
                            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.

defProcStFld

protected void defProcStFld(String name,
                            String cname,
                            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.

define

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

defineFunction

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

defineFunction

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

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.

emitPushBoolean

public void emitPushBoolean(boolean value,
                            CodeAttr code)

eval

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

eval

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

eval

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

eval

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

eval

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

eval

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

eval

public final void eval(String string,
                       PrintConsumer out)
            throws 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.

eval

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

eval

public void eval(InPort port,
                 CallContext ctx)
            throws Throwable

getDefaultLanguage

public static Language getDefaultLanguage()

getEnvPropertyFor

public Object getEnvPropertyFor(Declaration decl)

getEnvPropertyFor

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

getEnvironment

public final Environment getEnvironment()
Get current user environment.

getFormat

public AbstractFormat getFormat(boolean readable)

getInstance

public static Language getInstance(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(String langName,
                                   Class langClass)

getInstanceFromFilenameExtension

public static Language getInstanceFromFilenameExtension(String filename)

getLangEnvironment

public Environment getLangEnvironment()

getLanguages

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

getLexer

public abstract Lexer getLexer(InPort inp,
                               SourceMessages messages)

getName

public String getName()

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.

getNewEnvironment

public final Environment getNewEnvironment()

getOutputConsumer

public Consumer getOutputConsumer(Writer out)

getPrompter

public Procedure getPrompter()

getSymbol

public Symbol getSymbol(String name)

getTypeFor

public Type getTypeFor(Class clas)

getTypeFor

public Type getTypeFor(String name)

getTypeFor

public Type getTypeFor(Expression exp)

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.

isTrue

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

loadClass

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

lookup

public Object lookup(String name)

lookupBuiltin

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

noValue

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

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.

parse

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

print

public void print(Object obj,
                  OutPort out)

print

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

registerLanguage

public static void registerLanguage(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.

runAsApplication

public void runAsApplication(String[] args)

setDefaultLanguage

public static void setDefaultLanguage(Language language)

setDefaults

public static void setDefaults(Language lang)

string2Type

public static Type string2Type(String name)