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 Details

    • 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:
    • 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:
    • 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:
    • 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:
    • 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_EXPLICIT
      Compilation 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

      public static Language getDefaultLanguage()
    • setCurrentLanguage

      public static void setCurrentLanguage(Language language)
    • setSaveCurrent

      public static Language setSaveCurrent(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.
    • getInstance

      public static Language getInstance(String langName, Class langClass)
    • 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)
    • define

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

      public Type getNamedType(String name)
      Get the corresponding Type 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 as emacs: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, otherwise null.
    • 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(Symbol sym, String cname, String fname)
    • 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.
    • 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 in cname
    • 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(Field fld, Object value)
    • getEnvPropertyFor

      public Object getEnvPropertyFor(Declaration decl)
    • loadClass

      public void loadClass(String name) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • getSymbol

      public Symbol getSymbol(String name)
    • lookup

      public Object lookup(String name)
    • getFormat

      public gnu.kawa.format.AbstractFormat getFormat(boolean readable)
    • getOutputConsumer

      public Consumer getOutputConsumer(Writer out)
    • getName

      public String getName()
    • getExtensions

      public List<String> getExtensions()
    • 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 warnings
      options - 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(Class clas)
    • getLangTypeFor

      public final Type getLangTypeFor(Type type)
    • formatType

      public String formatType(Type type)
    • getTypeFor

      public Type getTypeFor(String name)
    • 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).
    • unionType

      public static Type unionType(Type t1, Type t2)
    • 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.
    • coerceFromObject

      public Object coerceFromObject(Class clas, Object obj)
    • coerceToObject

      public Object coerceToObject(Class clas, Object obj)
    • 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)