Package kawa.lang

Class Translator

All Implemented Interfaces:
SourceLocator, SourceLocator, Locator
Direct Known Subclasses:
SchemeCompilation

public class Translator extends Compilation
Used to translate from source to Expression. The result has macros expanded, lexical names bound, etc, and is ready for code generation. This is sometimes called a "compilation environment", but we modify it as we go along - there is a single Translator for each top-level form.
  • Field Details

    • currentMacroDefinition

      public Macro currentMacroDefinition
      Set if we're processing (as opposed to expanding) a define-syntax or defmacro.
    • patternScope

      public PatternScope patternScope
      Innermost current scope of pattern variable, from a syntax-case.
    • templateScopeDecl

      public Declaration templateScopeDecl
    • matchArray

      public Declaration matchArray
      A variable to hold the matched values for syntax-case pattern variables.
    • pendingForm

      public Object pendingForm
    • curMethodLambda

      public LambdaExp curMethodLambda
    • xmlElementNamespaces

      public NamespaceBinding xmlElementNamespaces
    • getNamedPartDecl

      public static final Declaration getNamedPartDecl
    • formStack

      public Translator.FormStack formStack
  • Constructor Details

  • Method Details

    • getGlobalEnvironment

      public final Environment getGlobalEnvironment()
      Overrides:
      getGlobalEnvironment in class Compilation
    • parse

      public Expression parse(Object input)
      Description copied from class: Compilation
      This may not make sense, except for Lisp-like languages. For those, 'input' an s-expression from the reader.
      Overrides:
      parse in class Compilation
    • rewrite_car

      public final Expression rewrite_car(Pair pair, SyntaxForm syntax)
    • rewrite_car

      public final Expression rewrite_car(Pair pair, ScopeExp templateScope)
    • rewrite_car

      public final Expression rewrite_car(Pair pair, boolean function)
    • rewrite_car_for_lookup

      public final Expression rewrite_car_for_lookup(Pair pair)
      Similar to rewrite_car. However, we check for (quasiquote exp) specially, and handle that directly. This is in case quasiquote isn't in scope.
    • getCurrentSyntax

      public Syntax getCurrentSyntax()
    • keywordsAreSelfEvaluating

      public final boolean keywordsAreSelfEvaluating()
    • selfEvaluatingSymbol

      public final boolean selfEvaluatingSymbol(Object obj)
    • matches

      public final boolean matches(Object form, String literal)
      True iff a form matches a literal symbol.
    • matches

      public boolean matches(Object form, SyntaxForm syntax, String literal)
    • matches

      public boolean matches(Object form, SyntaxForm syntax, Symbol literal)
    • matchQuoted

      public Object matchQuoted(Pair pair)
    • lookup

      public Declaration lookup(Object name, int namespace)
      Overrides:
      lookup in class Compilation
    • lookupGlobal

      public Declaration lookupGlobal(Object name)
      Find global Declaration, creating one if not found.
    • lookupGlobal

      public Declaration lookupGlobal(Object name, int namespace)
      Find global Declaration, creating one if not found.
    • rewrite_pair

      public Expression rewrite_pair(Pair p, boolean function)
    • rewrite_lookup

      public Expression rewrite_lookup(Expression part1, Expression part2, boolean function)
    • namespaceResolvePrefix

      public Namespace namespaceResolvePrefix(Expression context)
    • namespaceResolve

      public Symbol namespaceResolve(Namespace ns, Expression member)
    • namespaceResolve

      public Symbol namespaceResolve(Expression context, Expression member)
    • stripSyntax

      public static Object stripSyntax(Object obj)
    • safeCar

      public static Object safeCar(Object obj)
    • safeCdr

      public static Object safeCdr(Object obj)
    • listLength

      public static int listLength(Object obj)
      Returns the length of a syntax list. Returns Integer.MIN_VALUE for cyclic lists. For impure lists returns the negative of one more than the number of pairs before the "dot". Similar to LList.listLength, but handles SyntaxForm more efficiently.
    • rewriteInBody

      public void rewriteInBody(Object exp)
    • getCompletions

      public int getCompletions(Environment env, String nameStart, Object property, String namespaceUri, List<? super String> matches)
    • namespaceResolve

      public Object namespaceResolve(Object name)
    • rewrite

      public Expression rewrite(Object exp)
      Re-write a Scheme expression in S-expression format into internal form.
    • rewrite

      public Expression rewrite(Object exp, boolean function)
      Re-write a Scheme expression in S-expression format into internal form.
    • rewrite

      public Expression rewrite(Object exp, char mode)
      Re-write a Scheme expression in S-expression format into internal form.
      Parameters:
      mode - either 'N' (normal), 'F' (function application context), 'M' (macro-checking) or 'Q' (colon-form in quote).
    • checkDefaultBinding

      public Expression checkDefaultBinding(Symbol symbol, Translator tr)
      If a symbol is lexically unbound, look for a default binding. The default implementation does the following: If the symbol is the name of an existing Java class, return that class. Handles both with and without (semi-deprecated) angle-brackets: <java.lang.Integer> and java.lang.Integer. Also handles arrays, such as java.lang.String[]. If the symbol starts with '@' parse as an annotation class. Recognizes quanties with units, such as 2m and 3m/s^2. Handles the xml and unit namespaces.
      Returns:
      null if no binding, otherwise an Expression. FIXME: This method should be refactored. The quantities parsing should be moved to its own method at least.
    • lookupStandardEntity

      public static String lookupStandardEntity(String key)
    • setLine

      public static void setLine(Expression exp, Object location)
    • setLine

      public static void setLine(Declaration decl, Object location)
    • pushPositionOf

      public Object pushPositionOf(Object pos)
      Note current line number position from a PairWithPosition. Return an object to pass to popPositionOf.
    • popPositionOf

      public void popPositionOf(Object saved)
      Restore line number position from a previous pushPositionOf.
      Parameters:
      saved - value returned by matching pushPositionOf.
    • errorWithPosition

      public void errorWithPosition(String message, Object form)
    • errorIfNonEmpty

      public void errorIfNonEmpty(Object form)
    • setLineOf

      public void setLineOf(Expression exp)
      Set the line position of the argument to the current position.
    • exp2Type

      public Type exp2Type(Pair typeSpecPair)
      Extract a type from the car of a pair.
    • exp2Type

      public Type exp2Type(Pair typeSpecPair, Declaration decl, SyntaxForm syntax)
    • rewrite_with_position

      public Expression rewrite_with_position(Object exp, boolean function, PairWithPosition pair)
    • wrapSyntax

      public static Object wrapSyntax(Object form, SyntaxForm syntax)
    • popForms

      public Values popForms(Pair beforeFirst)
      Pop from formStack all forms that come after beforeFirst.
    • scanForm

      public void scanForm(Object st, ScopeExp defs)
    • scanBody

      public LList scanBody(Object body, ScopeExp defs, boolean makeList)
      Recursive helper method for rewrite_body. Scan body for definitions, adding partially macro-expanded expressions into the formStack.
      Parameters:
      makeList - if true, return a list representation of the scanned forms (not including declarations); else forms are push on formStack
      Returns:
      a list of forms if makeList (possibly wrapped in a SyntaxForm); otherwise null.
    • makePair

      public static Pair makePair(Pair pair, Object car, Object cdr)
    • rewrite_body

      public Expression rewrite_body(Object exp)
      Re-write a Scheme 'body' in S-expression format into internal form.
    • rewriteBody

      protected void rewriteBody(LList forms)
    • makeBody

      protected Expression makeBody(Pair head, ScopeExp scope)
      Combine a list of zero or more expression forms into a "body".
    • appendBodyValues

      public boolean appendBodyValues()
    • makeBody

      public Expression makeBody(Expression[] exps)
      Combine a 'body' consisting of a list of expression.
    • noteAccess

      public void noteAccess(Object name, ScopeExp scope)
      Note that we reference name in a given scope. This may be called when defining a macro, at scan-time, and the name may be bound to a declaration we haven't seen yet.
    • processAccesses

      public void processAccesses()
      Check references recorded by noteAccess. Resolve now to a Declaration, and note the access. This is needed in case an exported macro references a private Declaration.
    • finishModule

      public void finishModule(ModuleExp mexp)
    • resolveModule

      public void resolveModule(ModuleExp mexp)
    • makeRenamedAlias

      public Declaration makeRenamedAlias(Declaration decl, ScopeExp templateScope)
    • makeRenamedAlias

      public Declaration makeRenamedAlias(Object name, Declaration decl, ScopeExp templateScope)
    • pushRenamedAlias

      public void pushRenamedAlias(Declaration alias)
      Push an alias for a declaration in a scope. If the name of decl came from a syntax template whose immediate scope is templateScope, then the same syntax template may contain local variable references that are also in the same templateScope. Such variable references will not look in the current "physical" scope, where we just created decl, but will instead search the "lexical" templateScope. So that such references can resolve to decl, we create an alias in templateScope that points to decl. We record that we did this in the renamedAliasStack, so we can remove the alias later.
    • renamedAliasCount

      public int renamedAliasCount()
    • popRenamedAlias

      public void popRenamedAlias(int count)
      Remove one or more aliases created by pushRenamedAlias.
    • define

      public Declaration define(Object name, ScopeExp defs)
    • define

      public Declaration define(Object name, SyntaxForm nameSyntax, ScopeExp defs)
    • define

      public Declaration define(Object name, TemplateScope templateScope, ScopeExp defs)
    • pushForm

      public void pushForm(Object value)
    • getScanContext

      public Translator.ScanContext getScanContext()
    • curScanNesting

      public int curScanNesting()
    • getScanContextStack

      public Stack<Translator.ScanContext> getScanContextStack()
    • pushScanContext

      public void pushScanContext(LambdaExp lambda)
    • popScanContext

      public void popScanContext()