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 EnvironmentThe environment for language built-ins and predefined bindings.static final intprotected static Languagestatic final intstatic final intFlag to tell parse to use current NameLookup.static final intstatic final intCompilation explicitly requested, not just because of an import.static final intstatic final intstatic final intstatic final intstatic final intFlag to tell parse that expression will be evaluated immediately.static final intstatic final intFlag to tell parse to only read a single line if possible.static final intFlag to tell parser to continue until we have the module name.static booleanprotected EnvironmentIf 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) intbooleanValue(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 voiddefAliasStFld(String name, String cname, String fname) Declare in the current Environment a variable aliased to a static field.voidEnter a value into the current environment.final 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(Symbol sym, String cname, String fname) protected voiddefProcStFld(String name, String cname) Declare in the current Environment a procedure bound to a static field.protected voiddefProcStFld(String name, String cname, String fname) Declare in the current Environment a procedure bound to a static field.protected voiddefProcStFldAs(String asName, String cname, String mname) Declare by name in the current environment a procedure bound to a static field.static Languagedetect(gnu.kawa.io.InPort port) Detect the programming language of a file based on its first line.static Languagedetect(InputStream in) Detect the programming language of a file based on its first line.static LanguageDetect the programming language of a file based on its first line.encodeType(Type type) Encode this type as a parseable string.final Objecteval(gnu.kawa.io.InPort port) Evaluate expression(s) read from an InPort.voideval(gnu.kawa.io.InPort port, CallContext ctx) final ObjectEvaluate expression(s) read from a Reader.voidRead expressions from a Reader and write the result to a Consumer.final voidRead expressions from a Reader and write the result to a Writer.final ObjectReturn the result of evaluating a string as a source expression.final voidEvaluate a string and write the result value(s) to a Consumer.final voideval(String string, PrintConsumer out) Evaluate a string and write the result value(s) to a PrintConsumer.final voidEvaluate a string and write the result value(s) on a Writer.formatType(Type type) final CompilationgetCompilation(Lexer lexer, int options, ModuleInfo info) final CompilationgetCompilation(SourceMessages messages, NameLookup lexical) static Languagefinal EnvironmentGet current user environment.getEnvPropertyFor(Declaration decl) getEnvPropertyFor(Field fld, Object value) gnu.kawa.format.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) final TypegetLangTypeFor(Type type) static String[][]Get a list of all available languagesabstract LexergetLexer(gnu.kawa.io.InPort inp, SourceMessages messages) getName()getNamedType(String name) Get the correspondingTypefor a given name.intgetNamespaceOf(Declaration decl) Return the namespace (e.g value or function) of a Declaration.final EnvironmentgetOutputConsumer(Writer out) final TypegetTypeFor(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) booleanhasNamespace(Declaration decl, int namespace) True if a Declaration is in the specified namespace.booleanTrue if functions are in a separate name space from variable.booleanTest if a value is considered "true" in this language.static booleanisValidJavaName(String name) voidlookupBuiltin(Symbol name, Object property, int hash) static StringmangleName(String name, int kind) Convert a string to a safe Java identifier.noValue()The value to return for a "void" result.final booleanparse(Compilation tr) abstract booleanparse(Compilation comp, int options) final Compilationparse(gnu.kawa.io.InPort port, SourceMessages messages, int options) Parse one or more expressions.final Compilationparse(gnu.kawa.io.InPort port, SourceMessages messages, int options, ModuleInfo info) final Compilationparse(Lexer lexer, int options, ModuleInfo info) 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.static voidrestoreCurrent(Language saved) voidrunAsApplication(String[] args) static voidsetCurrentLanguage(Language language) static voidsetDefaults(Language lang) static LanguagesetSaveCurrent(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 correspondingTypefor a given name. This is currently used as a hook in the conversion of type designators to types.LispLanguageuses 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
Typeif 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:
IOExceptionSyntaxException
-
parse
public final Compilation parse(gnu.kawa.io.InPort port, SourceMessages messages, int options, ModuleInfo info) throws IOException, SyntaxException - Throws:
IOExceptionSyntaxException
-
parse
public final Compilation parse(Lexer lexer, int options, ModuleInfo info) throws IOException, SyntaxException - Throws:
IOExceptionSyntaxException
-
parse
- Throws:
IOExceptionSyntaxException
-
parse
- Throws:
IOExceptionSyntaxException
-
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
-