Package gnu.expr
Class Language
- java.lang.Object
-
- gnu.expr.Language
-
- Direct Known Subclasses:
LispLanguage
,XQuery
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
Fields Modifier and Type Field Description protected static InheritableThreadLocal<Language>
current
protected Environment
environ
The environment for language built-ins and predefined bindings.static int
FUNCTION_NAMESPACE
protected static Language
global
static int
NAMESPACE_PREFIX_NAMESPACE
static int
PARSE_CURRENT_NAMES
Flag to tell parse to use current NameLookup.static int
PARSE_EMIT_MAIN
static int
PARSE_EXPLICIT
Compilation explicitly requested, not just because of an import.static int
PARSE_FOR_APPLET
static int
PARSE_FOR_EVAL
static int
PARSE_FOR_LINT
static int
PARSE_FOR_SERVLET
static int
PARSE_IMMEDIATE
Flag to tell parse that expression will be evaluated immediately.static int
PARSE_INTERACTIVE_MODULE
static int
PARSE_ONE_LINE
Flag to tell parse to only read a single line if possible.static int
PARSE_PROLOG
Flag to tell parser to continue until we have the module name.static boolean
requirePedantic
protected Environment
userEnv
If non-null, the user environment.static int
VALUE_NAMESPACE
-
Constructor Summary
Constructors Modifier Constructor Description protected
Language()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Type
asType(Object spec)
"Coerce" a language-specific "type specifier" object to a Type.Object
booleanObject(boolean b)
int
booleanValue(Object value)
Test if a value is considered "true" in this language.Object
coerceFromObject(Class clas, Object obj)
Object
coerceToObject(Class clas, Object obj)
Declaration
declFromField(ModuleExp mod, Object fvalue, Field fld)
Type
decodeType(Type javaType, String annotType, ParameterizedType parameterizedType)
protected void
defAliasStFld(String name, String cname, String fname)
Declare in the current Environment a variable aliased to a static field.void
define(String sym, Object p)
Enter a value into the current environment.void
defineFunction(Named proc)
Enter a named function into the current environment.void
defineFunction(String name, Object proc)
Enter a function into the current environment.protected void
defProcStFld(Symbol sym, String cname, String fname)
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.protected void
defProcStFldAs(String asName, String cname, String mname)
Declare by name in the current environment a procedure bound to a static field.static Language
detect(gnu.kawa.io.InPort port)
Detect the programming language of a file based on its first line.static Language
detect(InputStream in)
Detect the programming language of a file based on its first line.static Language
detect(String line)
Detect the programming language of a file based on its first line.String
encodeType(Type type)
Encode this type as a parseable string.Object
eval(gnu.kawa.io.InPort port)
Evaluate expression(s) read from an InPort.void
eval(gnu.kawa.io.InPort port, CallContext ctx)
Object
eval(Reader in)
Evaluate expression(s) read from a Reader.void
eval(Reader in, Consumer out)
Read expressions from a Reader and write the result to a Consumer.void
eval(Reader in, Writer out)
Read expressions from a Reader and write the result to a Writer.Object
eval(String string)
Return the result of evaluating a string as a source expression.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.void
eval(String string, Writer out)
Evaluate a string and write the result value(s) on a Writer.String
formatType(Type type)
Compilation
getCompilation(Lexer lexer, int options, ModuleInfo info)
Compilation
getCompilation(SourceMessages messages, NameLookup lexical)
String
getCompilationClass()
static Language
getDefaultLanguage()
Environment
getEnvironment()
Get current user environment.Object
getEnvPropertyFor(Declaration decl)
Object
getEnvPropertyFor(Field fld, Object value)
List<String>
getExtensions()
gnu.kawa.format.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()
Type
getLangTypeFor(Type type)
static String[][]
getLanguages()
Get a list of all available languagesabstract Lexer
getLexer(gnu.kawa.io.InPort inp, SourceMessages messages)
String
getName()
Type
getNamedType(String name)
Get the correspondingType
for a given name.int
getNamespaceOf(Declaration decl)
Return the namespace (e.g value or function) of a Declaration.Environment
getNewEnvironment()
Consumer
getOutputConsumer(Writer out)
String
getPrimaryPrompt()
String
getSecondaryPrompt()
Symbol
getSymbol(String name)
Type
getTypeFor(Expression exp)
Type
getTypeFor(Expression exp, boolean lenient)
Interpreting exp as a type specifier, get the actual type.Type
getTypeFor(Class clas)
Type
getTypeFor(Object spec, boolean lenient)
Convert a "type value" to a Type.Type
getTypeFor(String name)
boolean
hasNamespace(Declaration decl, int namespace)
True if a Declaration is in the specified namespace.boolean
hasSeparateFunctionNamespace()
True if functions are in a separate name space from variable.boolean
isTrue(Object value)
Test if a value is considered "true" in this language.static boolean
isValidJavaName(String name)
void
loadClass(String name)
Object
lookup(String name)
NamedLocation
lookupBuiltin(Symbol name, Object property, int hash)
static String
mangleName(String name, int kind)
Convert a string to a safe Java identifier.Object
noValue()
The value to return for a "void" result.boolean
parse(Compilation tr)
abstract boolean
parse(Compilation comp, int options)
Compilation
parse(gnu.kawa.io.InPort port, SourceMessages messages, int options)
Parse one or more expressions.Compilation
parse(gnu.kawa.io.InPort port, SourceMessages messages, int options, ModuleInfo info)
Compilation
parse(Lexer lexer, int options, ModuleInfo info)
static void
registerLanguage(String[] langMapping)
Add a language to the list.void
resolve(Compilation comp)
Perform any need post-processing after we've read all the modules to be compiled.static void
restoreCurrent(Language saved)
void
runAsApplication(String[] args)
static void
setCurrentLanguage(Language language)
static void
setDefaults(Language lang)
static Language
setSaveCurrent(Language language)
static Type
unionType(Type t1, Type t2)
-
-
-
Field Detail
-
current
protected static final InheritableThreadLocal<Language> current
-
global
protected static Language global
-
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_CURRENT_NAMES
public static final int PARSE_CURRENT_NAMES
Flag to tell parse to use current NameLookup. As opposed to creating a new instance.- 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
-
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.- See Also:
- Constant Field Values
-
PARSE_FOR_EVAL
public static final int PARSE_FOR_EVAL
- See Also:
- Constant Field Values
-
PARSE_FOR_APPLET
public static final int PARSE_FOR_APPLET
- See Also:
- Constant Field Values
-
PARSE_FOR_SERVLET
public static final int PARSE_FOR_SERVLET
- See Also:
- Constant Field Values
-
PARSE_EXPLICIT
public static final int PARSE_EXPLICIT
Compilation explicitly requested, not just because of an import.- See Also:
- Constant Field Values
-
PARSE_INTERACTIVE_MODULE
public static final int PARSE_INTERACTIVE_MODULE
- See Also:
- Constant Field Values
-
PARSE_EMIT_MAIN
public static final int PARSE_EMIT_MAIN
- See Also:
- Constant Field Values
-
PARSE_FOR_LINT
public static final int PARSE_FOR_LINT
- See Also:
- Constant Field Values
-
requirePedantic
public static boolean requirePedantic
-
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
-
-
Method Detail
-
getDefaultLanguage
public static Language getDefaultLanguage()
-
setCurrentLanguage
public static void setCurrentLanguage(Language language)
-
restoreCurrent
public static void restoreCurrent(Language saved)
-
getLanguages
public static String[][] getLanguages()
Get a list of all available languages
-
registerLanguage
public static void registerLanguage(String[] langMapping)
Add a language to the list.- Parameters:
langMapping
- is a language definition, the first element 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.
-
detect
public static Language detect(InputStream in) throws IOException
Detect the programming language of a file based on its first line.- Returns:
- a suitable Language or null if we didn't recognize one.
- Throws:
IOException
-
detect
public static Language detect(gnu.kawa.io.InPort port) throws IOException
Detect the programming language of a file based on its first line.- Returns:
- a suitable Language or null if we didn't recognize one.
- Throws:
IOException
-
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.
-
getInstanceFromFilenameExtension
public static Language getInstanceFromFilenameExtension(String filename)
-
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.
-
isTrue
public boolean isTrue(Object value)
Test if a value is considered "true" in this language. Throws an exception if value can't be used in a boolean context. Use booleanValue to catch that exception.
-
booleanValue
public int booleanValue(Object value)
Test if a value is considered "true" in this language.- Returns:
- 1 if true; 0 if false; -1 if not a valid boolean
-
booleanObject
public Object booleanObject(boolean b)
-
noValue
public Object noValue()
The value to return for a "void" result.
-
hasSeparateFunctionNamespace
public boolean hasSeparateFunctionNamespace()
True if functions are in a separate name 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, Object property, int hash)
-
getNamedType
public Type getNamedType(String name)
Get the correspondingType
for a given name. This is currently used as a hook in the conversion of type designators to types.LispLanguage
uses it to check for package style type designators such asemacs:buffer
, and CommonLisp uses it to check for a type designator "boolean", so that is can return the appropriate boolean type. This is a bit over-specialised, but it beats actually overriding getTypeFor(String) in LispLanguage, CommonLisp just for these minor changes... FIXME!- Parameters:
name
- The name of a type to search for.- Returns:
- The corresponding
Type
if a suitable one can be found, otherwisenull
.
-
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, 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 ofname
.
-
defProcStFldAs
protected void defProcStFldAs(String asName, String cname, String mname)
Declare by name in the current environment a procedure bound to a static field.- Parameters:
asName
- The procedure's source-level name.cname
- The name of the class containing the field.mname
- The name of the (mangled) field incname
-
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.
-
getEnvPropertyFor
public Object getEnvPropertyFor(Declaration decl)
-
loadClass
public void loadClass(String name) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
getFormat
public gnu.kawa.format.AbstractFormat getFormat(boolean readable)
-
getName
public String getName()
-
isValidJavaName
public static boolean isValidJavaName(String name)
-
mangleName
public static String mangleName(String name, int kind)
Convert a string to a safe Java identifier.- Parameters:
kind
- -1 - non-reversible; 0: reversible, except that '$' is not mapped; 1: reversible
-
getLexer
public abstract Lexer getLexer(gnu.kawa.io.InPort inp, SourceMessages messages)
-
getCompilationClass
public String getCompilationClass()
-
getCompilation
public final Compilation getCompilation(SourceMessages messages, NameLookup lexical)
-
getCompilation
public final Compilation getCompilation(Lexer lexer, int options, ModuleInfo info)
-
parse
public final Compilation parse(gnu.kawa.io.InPort port, SourceMessages messages, int options) throws IOException, SyntaxException
Parse one or more expressions.- Parameters:
port
- the InPort to read the expressions from.messages
- where to send error messages and warningsoptions
- various flags, including PARSE_IMMEDIATE and PARSE_ONE_LINE- Returns:
- a new Compilation. May return null if PARSE_ONE_LINE on end-of-file.
- Throws:
IOException
SyntaxException
-
parse
public final Compilation parse(gnu.kawa.io.InPort port, SourceMessages messages, int options, ModuleInfo info) throws IOException, SyntaxException
- Throws:
IOException
SyntaxException
-
parse
public final Compilation parse(Lexer lexer, int options, ModuleInfo info) throws IOException, SyntaxException
- Throws:
IOException
SyntaxException
-
parse
public final boolean parse(Compilation tr) throws IOException, SyntaxException
- Throws:
IOException
SyntaxException
-
parse
public abstract boolean parse(Compilation comp, int options) throws IOException, SyntaxException
- Throws:
IOException
SyntaxException
-
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.
-
getTypeFor
public Type getTypeFor(Object spec, boolean lenient)
Convert a "type value" to a Type. This is used to process types from source code. Normally, an identifier that resolves to a class name should resolve to the raw ClassType, rather than a language specific class (which might e.g. use a different constructor). These may be exceptions: specifically, given java.lang.String, we do want to use language-specific conversion to String.
-
encodeType
public String encodeType(Type type)
Encode this type as a parseable string. Stored in SourceType or SourceMethodType annotations.- Returns:
- A string suitable for
decodeType(gnu.bytecode.Type, java.lang.String, gnu.bytecode.ParameterizedType)
, or null if the type is "uninteresting" in the sense we can infer it from the Java typing.
-
decodeType
public Type decodeType(Type javaType, String annotType, ParameterizedType parameterizedType)
-
asType
public final Type asType(Object spec)
"Coerce" a language-specific "type specifier" object to a Type.
-
getTypeFor
public final Type getTypeFor(Expression exp)
-
getTypeFor
public Type getTypeFor(Expression exp, boolean lenient)
Interpreting exp as a type specifier, get the actual type. (Does not get the type "of" but exp treats/casts exp "to" ("as") a type).
-
declFromField
public Declaration declFromField(ModuleExp mod, 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 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.
-
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.
-
setDefaults
public static void setDefaults(Language lang)
-
getPrimaryPrompt
public String getPrimaryPrompt()
-
getSecondaryPrompt
public String getSecondaryPrompt()
-
eval
public final Object eval(String string) throws Throwable
Return the result of evaluating a string as a source expression.- Throws:
Throwable
-
eval
public final Object eval(Reader in) throws Throwable
Evaluate expression(s) read from a Reader. This just calls eval(InPort).- Throws:
Throwable
-
eval
public final Object eval(gnu.kawa.io.InPort port) throws Throwable
Evaluate expression(s) read from an InPort.- Throws:
Throwable
-
eval
public final void eval(String string, Writer out) throws Throwable
Evaluate a string and write the result value(s) on a Writer.- Throws:
Throwable
-
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.- Throws:
Throwable
-
eval
public final void eval(String string, Consumer out) throws Throwable
Evaluate a string and write the result value(s) to a Consumer.- Throws:
Throwable
-
eval
public final void eval(Reader in, Writer out) throws Throwable
Read expressions from a Reader and write the result to a Writer.- Throws:
Throwable
-
eval
public void eval(Reader in, Consumer out) throws Throwable
Read expressions from a Reader and write the result to a Consumer.- Throws:
Throwable
-
eval
public void eval(gnu.kawa.io.InPort port, CallContext ctx) throws Throwable
- Throws:
Throwable
-
runAsApplication
public void runAsApplication(String[] args)
-
-