gnu.expr

Class Language

public abstract class Language extends 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 ThreadLocationcurrent
protected Environmentenviron
The environment for language built-ins and predefined bindings.
protected static intenv_counter
static intFUNCTION_NAMESPACE
static intNAMESPACE_PREFIX_NAMESPACE
static intPARSE_IMMEDIATE
Flag to tell parse that expression will be evaluated immediately.
static intPARSE_ONE_LINE
Flag to tell parse to only read a single line if possible.
static intPARSE_PROLOG
Flag to tell parser to continue until we have the module name.
static booleanrequirePedantic
protected EnvironmentuserEnv
If non-null, the user environment.
static intVALUE_NAMESPACE
Constructor Summary
protected Language()
Method Summary
TypeasType(Object spec)
"Coerce" a language-specific "type specifier" object to a Type.
ObjectbooleanObject(boolean b)
ObjectcoerceFromObject(Class clas, Object obj)
ObjectcoerceToObject(Class clas, Object obj)
ObjectcoerceToObject(int val)
DeclarationdeclFromField(ModuleExp mod, Object fvalue, Field fld)
protected voiddefAliasStFld(String name, String cname, String fname)
Declare in the current Environment a variable aliased to a static field.
voiddefine(String sym, Object p)
Enter a value into the current environment.
voiddefineFunction(Named proc)
Enter a named function into the current environment.
voiddefineFunction(String name, Object proc)
Enter a function into the current environment.
protected voiddefProcStFld(String name, String cname, String fname)
Declare in the current Environment a procedure bound to a static field.
protected voiddefProcStFld(String name, String cname)
Declare in the current Environment a procedure bound to a static field.
static Languagedetect(InputStream in)
Detect the programming language of a file based on its first line.
static Languagedetect(InPort port)
Detect the programming language of a file based on its first line.
static Languagedetect(String line)
Detect the programming language of a file based on its first line.
voidemitCoerceToBoolean(CodeAttr code)
Generate code to test if an object is considered true.
voidemitPushBoolean(boolean value, CodeAttr code)
Objecteval(String string)
Return the result of evaluating a string as a source expression.
Objecteval(Reader in)
Evaluate expression(s) read from a Reader.
Objecteval(InPort port)
Evaluate expression(s) read from an InPort.
voideval(String string, Writer out)
Evaluate a string and write the result value(s) on a Writer.
voideval(String string, PrintConsumer out)
Evaluate a string and write the result value(s) to a PrintConsumer.
voideval(String string, Consumer out)
Evaluate a string and write the result value(s) to a Consumer.
voideval(Reader in, Writer out)
Read expressions from a Reader and write the result to a Writer.
voideval(Reader in, Consumer out)
Read expressions from a Reader and write the result to a Consumer.
voideval(InPort port, CallContext ctx)
StringformatType(Type type)
CompilationgetCompilation(Lexer lexer, SourceMessages messages)
static LanguagegetDefaultLanguage()
EnvironmentgetEnvironment()
Get current user environment.
ObjectgetEnvPropertyFor(Field fld, Object value)
ObjectgetEnvPropertyFor(Declaration decl)
AbstractFormatgetFormat(boolean readable)
static LanguagegetInstance(String name)
Look for a language with the given name or extension.
static LanguagegetInstance(String langName, Class langClass)
static LanguagegetInstanceFromFilenameExtension(String filename)
EnvironmentgetLangEnvironment()
TypegetLangTypeFor(Type type)
static String[][]getLanguages()
Get a list of all available languages
abstract LexergetLexer(InPort inp, SourceMessages messages)
StringgetName()
intgetNamespaceOf(Declaration decl)
Return the namespace (e.g value or function) of a Declaration.
EnvironmentgetNewEnvironment()
ConsumergetOutputConsumer(Writer out)
ProceduregetPrompter()
SymbolgetSymbol(String name)
TypegetTypeFor(Class clas)
TypegetTypeFor(String name)
TypegetTypeFor(Object spec, boolean lenient)
TypegetTypeFor(Expression exp)
TypegetTypeFor(Expression exp, boolean lenient)
booleanhasNamespace(Declaration decl, int namespace)
True if a Declaration is in the specified namespace.
booleanhasSeparateFunctionNamespace()
True if functions are in a separate anme space from variable.
booleanisTrue(Object value)
Test if a value is considered "true" in this language.
voidloadClass(String name)
Objectlookup(String name)
NamedLocationlookupBuiltin(Symbol name, Object property, int hash)
ObjectnoValue()
The value to return for a "void" result.
Compilationparse(InPort port, SourceMessages messages, int options)
Parse one or more expressions.
Compilationparse(InPort port, SourceMessages messages, ModuleInfo info)
Compilationparse(Lexer lexer, int options, ModuleInfo info)
abstract booleanparse(Compilation comp, int options)
static voidregisterLanguage(String[] langMapping)
Add a language to the list.
voidresolve(Compilation comp)
Perform any need post-processing after we've read all the modules to be compiled.
voidrunAsApplication(String[] args)
static voidsetDefaultLanguage(Language language)
static voidsetDefaults(Language lang)
static Typestring2Type(String name)

Field Detail

current

protected static final ThreadLocation current

environ

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

env_counter

protected static int env_counter

FUNCTION_NAMESPACE

public static final int FUNCTION_NAMESPACE

NAMESPACE_PREFIX_NAMESPACE

public static final int NAMESPACE_PREFIX_NAMESPACE

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.

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.

PARSE_PROLOG

public static final int PARSE_PROLOG
Flag to tell parser to continue until we have the module name. The parser is allowed to continue further, but must stop before any module import.

requirePedantic

public static boolean requirePedantic

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.

VALUE_NAMESPACE

public static final int VALUE_NAMESPACE

Constructor Detail

Language

protected Language()

Method Detail

asType

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

define

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

defineFunction

public final void defineFunction(Named proc)
Enter a named function 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.

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.

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.

detect

public static Language detect(InputStream in)
Detect the programming language of a file based on its first line.

Returns: a suitable Language or null if we didn't recognize one.

detect

public static Language detect(InPort port)
Detect the programming language of a file based on its first line.

Returns: a suitable Language or null if we didn't recognize one.

detect

public static Language detect(String line)
Detect the programming language of a file based on its first line.

Parameters: line the first input line

Returns: a suitable Language or null if we didn't recognize one.

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(String string)
Return the result of evaluating a string as a source expression.

eval

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

eval

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

eval

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

eval

public final void eval(String string, PrintConsumer out)
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 void eval(String string, Consumer out)
Evaluate a string and write the result value(s) to a Consumer.

eval

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

eval

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

eval

public void eval(InPort port, CallContext ctx)

formatType

public String formatType(Type type)

getCompilation

public Compilation getCompilation(Lexer lexer, SourceMessages messages)

getDefaultLanguage

public static Language getDefaultLanguage()

getEnvironment

public final Environment getEnvironment()
Get current user environment.

getEnvPropertyFor

public Object getEnvPropertyFor(Field fld, Object value)

getEnvPropertyFor

public Object getEnvPropertyFor(Declaration decl)

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()

getLangTypeFor

public final Type getLangTypeFor(Type type)

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 namespaces "covered" by the Declaration. Note this isn't a namespace in the XML sense; if a Declaration has a specific namespace URI, then that is part of its symbol. This namespace bitmap is a separate dimension, for the use of languages that have separate namespaces for different kinds of declarations, such as variables and functions.

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 final Type getTypeFor(Object spec, boolean lenient)

getTypeFor

public final Type getTypeFor(Expression exp)

getTypeFor

public Type getTypeFor(Expression exp, boolean lenient)

hasNamespace

public boolean hasNamespace(Declaration decl, int namespace)
True if a Declaration is in the specified namespace.

Parameters: namespace normally a bitmask as returned by getNamespaceOf.

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)

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)
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 final Compilation parse(InPort port, SourceMessages messages, ModuleInfo info)

parse

public final Compilation parse(Lexer lexer, int options, ModuleInfo info)

parse

public abstract boolean parse(Compilation comp, int options)

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.

resolve

public void resolve(Compilation comp)
Perform any need post-processing after we've read all the modules to be compiled. Using a separate pass allows compiling mutually recursive modules.

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)