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>currentprotected EnvironmentenvironThe environment for language built-ins and predefined bindings.static intFUNCTION_NAMESPACEprotected static Languageglobalstatic intNAMESPACE_PREFIX_NAMESPACEstatic intPARSE_CURRENT_NAMESFlag to tell parse to use current NameLookup.static intPARSE_EMIT_MAINstatic intPARSE_EXPLICITCompilation explicitly requested, not just because of an import.static intPARSE_FOR_APPLETstatic intPARSE_FOR_EVALstatic intPARSE_FOR_LINTstatic intPARSE_FOR_SERVLETstatic intPARSE_IMMEDIATEFlag to tell parse that expression will be evaluated immediately.static intPARSE_INTERACTIVE_MODULEstatic intPARSE_ONE_LINEFlag to tell parse to only read a single line if possible.static intPARSE_PROLOGFlag to tell parser to continue until we have the module name.static booleanrequirePedanticprotected EnvironmentuserEnvIf non-null, the user environment.static intVALUE_NAMESPACE
-
Constructor Summary
Constructors Modifier Constructor Description protectedLanguage()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description TypeasType(Object spec)"Coerce" a language-specific "type specifier" object to a Type.ObjectbooleanObject(boolean b)intbooleanValue(Object value)Test if a value is considered "true" in this language.ObjectcoerceFromObject(Class clas, Object obj)ObjectcoerceToObject(Class clas, Object obj)DeclarationdeclFromField(ModuleExp mod, Object fvalue, Field fld)TypedecodeType(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.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(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(String line)Detect the programming language of a file based on its first line.StringencodeType(Type type)Encode this type as a parseable string.Objecteval(gnu.kawa.io.InPort port)Evaluate expression(s) read from an InPort.voideval(gnu.kawa.io.InPort port, CallContext ctx)Objecteval(Reader in)Evaluate expression(s) read from a Reader.voideval(Reader in, Consumer out)Read expressions from a Reader and write the result to a Consumer.voideval(Reader in, Writer out)Read expressions from a Reader and write the result to a Writer.Objecteval(String string)Return the result of evaluating a string as a source expression.voideval(String string, Consumer out)Evaluate a string and write the result value(s) to a Consumer.voideval(String string, PrintConsumer out)Evaluate a string and write the result value(s) to a PrintConsumer.voideval(String string, Writer out)Evaluate a string and write the result value(s) on a Writer.StringformatType(Type type)CompilationgetCompilation(Lexer lexer, int options, ModuleInfo info)CompilationgetCompilation(SourceMessages messages, NameLookup lexical)StringgetCompilationClass()static LanguagegetDefaultLanguage()EnvironmentgetEnvironment()Get current user environment.ObjectgetEnvPropertyFor(Declaration decl)ObjectgetEnvPropertyFor(Field fld, Object value)List<String>getExtensions()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)EnvironmentgetLangEnvironment()TypegetLangTypeFor(Type type)static String[][]getLanguages()Get a list of all available languagesabstract LexergetLexer(gnu.kawa.io.InPort inp, SourceMessages messages)StringgetName()TypegetNamedType(String name)Get the correspondingTypefor a given name.intgetNamespaceOf(Declaration decl)Return the namespace (e.g value or function) of a Declaration.EnvironmentgetNewEnvironment()ConsumergetOutputConsumer(Writer out)StringgetPrimaryPrompt()StringgetSecondaryPrompt()SymbolgetSymbol(String name)TypegetTypeFor(Expression exp)TypegetTypeFor(Expression exp, boolean lenient)Interpreting exp as a type specifier, get the actual type.TypegetTypeFor(Class clas)TypegetTypeFor(Object spec, boolean lenient)Convert a "type value" to a Type.TypegetTypeFor(String name)booleanhasNamespace(Declaration decl, int namespace)True if a Declaration is in the specified namespace.booleanhasSeparateFunctionNamespace()True if functions are in a separate name space from variable.booleanisTrue(Object value)Test if a value is considered "true" in this language.static booleanisValidJavaName(String name)voidloadClass(String name)Objectlookup(String name)NamedLocationlookupBuiltin(Symbol name, Object property, int hash)static StringmangleName(String name, int kind)Convert a string to a safe Java identifier.ObjectnoValue()The value to return for a "void" result.booleanparse(Compilation tr)abstract booleanparse(Compilation comp, int options)Compilationparse(gnu.kawa.io.InPort port, SourceMessages messages, int options)Parse one or more expressions.Compilationparse(gnu.kawa.io.InPort port, SourceMessages messages, int options, ModuleInfo info)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 TypeunionType(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 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
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:
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
public final boolean parse(Compilation tr) throws IOException, SyntaxException
- Throws:
IOExceptionSyntaxException
-
parse
public abstract boolean parse(Compilation comp, int options) throws IOException, SyntaxException
- Throws:
IOExceptionSyntaxException
-
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)
-
-