Package gnu.expr
Class Language
java.lang.Object
gnu.expr.Language
- Direct Known Subclasses:
LispLanguage
,XQuery
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
FieldsModifier and TypeFieldDescriptionprotected static final InheritableThreadLocal
<Language> protected Environment
The environment for language built-ins and predefined bindings.static final int
protected static Language
static final int
static final int
Flag to tell parse to use current NameLookup.static final int
static final int
Compilation explicitly requested, not just because of an import.static final int
static final int
static final int
static final int
static final int
Flag to tell parse that expression will be evaluated immediately.static final int
static final int
Flag to tell parse to only read a single line if possible.static final int
Flag to tell parser to continue until we have the module name.static boolean
protected Environment
If non-null, the user environment.static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal Type
"Coerce" a language-specific "type specifier" object to a Type.booleanObject
(boolean b) int
booleanValue
(Object value) Test if a value is considered "true" in this language.coerceFromObject
(Class clas, Object obj) coerceToObject
(Class clas, Object obj) declFromField
(ModuleExp mod, Object fvalue, Field fld) 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
Enter a value into the current environment.final 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 the programming language of a file based on its first line.encodeType
(Type type) Encode this type as a parseable string.final Object
eval
(gnu.kawa.io.InPort port) Evaluate expression(s) read from an InPort.void
eval
(gnu.kawa.io.InPort port, CallContext ctx) final Object
Evaluate expression(s) read from a Reader.void
Read expressions from a Reader and write the result to a Consumer.final void
Read expressions from a Reader and write the result to a Writer.final Object
Return the result of evaluating a string as a source expression.final void
Evaluate a string and write the result value(s) to a Consumer.final void
eval
(String string, PrintConsumer out) Evaluate a string and write the result value(s) to a PrintConsumer.final void
Evaluate a string and write the result value(s) on a Writer.formatType
(Type type) final Compilation
getCompilation
(Lexer lexer, int options, ModuleInfo info) final Compilation
getCompilation
(SourceMessages messages, NameLookup lexical) static Language
final Environment
Get current user environment.getEnvPropertyFor
(Declaration decl) getEnvPropertyFor
(Field fld, Object value) 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) final Type
getLangTypeFor
(Type type) static String[][]
Get a list of all available languagesabstract Lexer
getLexer
(gnu.kawa.io.InPort inp, SourceMessages messages) getName()
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.final Environment
getOutputConsumer
(Writer out) final Type
getTypeFor
(Expression exp) getTypeFor
(Expression exp, boolean lenient) Interpreting exp as a type specifier, get the actual type.getTypeFor
(Class clas) getTypeFor
(Object spec, boolean lenient) Convert a "type value" to a Type.getTypeFor
(String name) boolean
hasNamespace
(Declaration decl, int namespace) True if a Declaration is in the specified namespace.boolean
True if functions are in a separate name space from variable.boolean
Test if a value is considered "true" in this language.static boolean
isValidJavaName
(String name) void
lookupBuiltin
(Symbol name, Object property, int hash) static String
mangleName
(String name, int kind) Convert a string to a safe Java identifier.noValue()
The value to return for a "void" result.final boolean
parse
(Compilation tr) abstract boolean
parse
(Compilation comp, int options) final Compilation
parse
(gnu.kawa.io.InPort port, SourceMessages messages, int options) Parse one or more expressions.final Compilation
parse
(gnu.kawa.io.InPort port, SourceMessages messages, int options, ModuleInfo info) final 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
-
Field Details
-
current
-
global
-
environ
The environment for language built-ins and predefined bindings. -
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_IMMEDIATEFlag to tell parse that expression will be evaluated immediately. I.e. we're not creating class files for future execution.- See Also:
-
PARSE_CURRENT_NAMES
public static final int PARSE_CURRENT_NAMESFlag to tell parse to use current NameLookup. As opposed to creating a new instance.- See Also:
-
PARSE_ONE_LINE
public static final int PARSE_ONE_LINEFlag to tell parse to only read a single line if possible. Multiple lines may be read if syntactically required.- See Also:
-
PARSE_PROLOG
public static final int PARSE_PROLOGFlag 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:
-
PARSE_FOR_EVAL
public static final int PARSE_FOR_EVAL- See Also:
-
PARSE_FOR_APPLET
public static final int PARSE_FOR_APPLET- See Also:
-
PARSE_FOR_SERVLET
public static final int PARSE_FOR_SERVLET- See Also:
-
PARSE_EXPLICIT
public static final int PARSE_EXPLICITCompilation explicitly requested, not just because of an import.- See Also:
-
PARSE_INTERACTIVE_MODULE
public static final int PARSE_INTERACTIVE_MODULE- See Also:
-
PARSE_EMIT_MAIN
public static final int PARSE_EMIT_MAIN- See Also:
-
PARSE_FOR_LINT
public static final int PARSE_FOR_LINT- See Also:
-
requirePedantic
public static boolean requirePedantic -
VALUE_NAMESPACE
public static final int VALUE_NAMESPACE- See Also:
-
FUNCTION_NAMESPACE
public static final int FUNCTION_NAMESPACE- See Also:
-
NAMESPACE_PREFIX_NAMESPACE
public static final int NAMESPACE_PREFIX_NAMESPACE- See Also:
-
-
Constructor Details
-
Language
protected Language()
-
-
Method Details
-
getDefaultLanguage
-
setCurrentLanguage
-
setSaveCurrent
-
restoreCurrent
-
getLanguages
Get a list of all available languages -
registerLanguage
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
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
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
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
-
getInstance
Look for a language with the given name or extension. If name is null, look for the first language available. -
getInstance
-
isTrue
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
Test if a value is considered "true" in this language.- Returns:
- 1 if true; 0 if false; -1 if not a valid boolean
-
booleanObject
-
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
Get current user environment. -
getNewEnvironment
-
getLangEnvironment
-
lookupBuiltin
-
define
Enter a value into the current environment. -
getNamedType
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
Declare in the current Environment a variable aliased to a static field. -
defProcStFld
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
-
defProcStFld
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
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
Enter a named function into the current environment. -
defineFunction
Enter a function into the current environment. Same as define(name,proc) for Scheme, but not for (say) Common Lisp. -
getEnvPropertyFor
-
getEnvPropertyFor
-
loadClass
- Throws:
ClassNotFoundException
-
getSymbol
-
lookup
-
getFormat
public gnu.kawa.format.AbstractFormat getFormat(boolean readable) -
getOutputConsumer
-
getName
-
getExtensions
-
isValidJavaName
-
mangleName
Convert a string to a safe Java identifier.- Parameters:
kind
- -1 - non-reversible; 0: reversible, except that '$' is not mapped; 1: reversible
-
getLexer
-
getCompilationClass
-
getCompilation
-
getCompilation
-
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
- Throws:
IOException
SyntaxException
-
parse
- Throws:
IOException
SyntaxException
-
resolve
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
-
getLangTypeFor
-
formatType
-
getTypeFor
-
getTypeFor
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
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
-
asType
"Coerce" a language-specific "type specifier" object to a Type. -
getTypeFor
-
getTypeFor
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). -
unionType
-
declFromField
-
getNamespaceOf
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
True if a Declaration is in the specified namespace.- Parameters:
namespace
- normally a bitmask as returned by getNamespaceOf.
-
coerceFromObject
-
coerceToObject
-
setDefaults
-
getPrimaryPrompt
-
getSecondaryPrompt
-
eval
Return the result of evaluating a string as a source expression.- Throws:
Throwable
-
eval
Evaluate expression(s) read from a Reader. This just calls eval(InPort).- Throws:
Throwable
-
eval
Evaluate expression(s) read from an InPort.- Throws:
Throwable
-
eval
Evaluate a string and write the result value(s) on a Writer.- Throws:
Throwable
-
eval
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
Evaluate a string and write the result value(s) to a Consumer.- Throws:
Throwable
-
eval
Read expressions from a Reader and write the result to a Writer.- Throws:
Throwable
-
eval
Read expressions from a Reader and write the result to a Consumer.- Throws:
Throwable
-
eval
- Throws:
Throwable
-
runAsApplication
-