kawa.lang
Class Translator

java.lang.Object
  extended by gnu.expr.Compilation
      extended by kawa.lang.Translator
All Implemented Interfaces:
SourceLocator, org.xml.sax.Locator

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 Summary
 LambdaExp curMethodLambda
           
 Macro currentMacroDefinition
          Set if we're processing (as opposed to expanding) a define-syntax or defmacro.
 int firstForm
           
 java.util.Stack formStack
           
 Declaration matchArray
          A variable to hold the matched values for syntax-case pattern variables.
 PatternScope patternScope
          Innermost current scope of pattern variable, from a syntax-case.
 java.lang.Object pendingForm
           
 Declaration templateScopeDecl
           
 
Fields inherited from class gnu.expr.Compilation
apply0args, apply0method, apply1args, apply1method, apply2args, apply2method, apply3method, apply4method, applyCpsMethod, applymethods, applyNargs, applyNmethod, argsCallContextField, BODY_PARSED, CALL_WITH_CONSUMER, CALL_WITH_CONTINUATIONS, CALL_WITH_RETURN, CALL_WITH_TAILCALLS, CALL_WITH_UNSPECIFIED, CLASS_WRITTEN, classPrefix, classPrefixDefault, COMPILE_SETUP, COMPILED, curClass, curLambda, current_scope, currentOptions, debugPrintExpr, debugPrintFinalExpr, defaultCallConvention, emitSourceDebugExtAttr, ERROR_SEEN, exprStack, falseConstant, fewerClasses, generateApplet, generateAppletDefault, generateMain, generateMainDefault, generateServlet, generateServletDefault, getCallContextInstanceMethod, getCurrentEnvironmentMethod, getLocation1EnvironmentMethod, getLocation2EnvironmentMethod, getLocationMethod, getProcedureBindingMethod, getSymbolProcedureMethod, getSymbolValueMethod, immediate, inlineOk, int1Args, javaIntegerType, javaStringType, language, lexer, lexical, mainClass, mainLambda, messages, method, minfo, moduleClass, moduleStatic, mustCompile, noArgsField, objArrayType, options, pcCallContextField, pendingImports, procCallContextField, PROLOG_PARSED, PROLOG_PARSING, RESOLVED, scmBooleanType, scmKeywordType, scmListType, scmNamedType, scmPairType, scmSequenceType, string1Arg, sym1Arg, thisDecl, trueConstant, typeApplet, typeCallContext, typeClass, typeClassType, typeConsumer, typeEnvironment, typeLanguage, typeLocation, typeMethodProc, typeModuleBody, typeModuleMethod, typeModuleWithContext, typeObject, typeObjectType, typePair, typeProcedure, typeProcedure0, typeProcedure1, typeProcedure2, typeProcedure3, typeProcedure4, typeProcedureArray, typeProcedureN, typeRunnable, typeServlet, typeString, typeSymbol, typeType, typeValues, WALKED
 
Constructor Summary
Translator(Language language, SourceMessages messages)
           
 
Method Summary
 Declaration define(java.lang.Object name, SyntaxForm nameSyntax, ScopeExp defs)
           
 Type exp2Type(Pair typeSpecPair)
          Extract a type from the car of a pair.
 void finishModule(ModuleExp mexp)
           
 Syntax getCurrentSyntax()
           
 Environment getGlobalEnvironment()
           
 boolean isLexical(Declaration decl)
          Return true if decl is lexical and not fluid.
static int listLength(java.lang.Object obj)
          Returns the length of a syntax list.
 Declaration lookup(java.lang.Object name, int namespace)
           
 Declaration lookupGlobal(java.lang.Object name)
          Find global Declaration, creating one if not found.
 Declaration lookupGlobal(java.lang.Object name, int namespace)
          Find global Declaration, creating one if not found.
 Expression makeBody(int first, ScopeExp scope)
          Combine a list of zero or more expression forms into a "body".
static Pair makePair(Pair pair, java.lang.Object car, java.lang.Object cdr)
           
 Declaration makeRenamedAlias(Declaration decl, ScopeExp templateScope)
           
 Declaration makeRenamedAlias(java.lang.Object name, Declaration decl, ScopeExp templateScope)
           
 boolean matches(java.lang.Object form, java.lang.String literal)
          True iff a form matches a literal symbol.
 boolean matches(java.lang.Object form, SyntaxForm syntax, java.lang.String literal)
           
 Symbol namespaceResolve(Expression context, Expression member)
           
 java.lang.Object namespaceResolve(java.lang.Object name)
           
 void noteAccess(java.lang.Object name, ScopeExp scope)
          Note that we reference name in a given scope.
 Expression parse(java.lang.Object input)
          This may not make sense, except for Lisp-like languages.
 java.lang.Object popForms(int first)
           
 void popPositionOf(java.lang.Object saved)
          Restore line number position from a previous pushPositionOf.
 void popRenamedAlias(int count)
          Remove one or more aliases created by pushRenamedAlias.
 void processAccesses()
          Check references recorded by noteAccess.
 java.lang.Object pushPositionOf(java.lang.Object pair)
          Note current line number position from a PairWithPosition.
 void pushRenamedAlias(Declaration alias)
          Push an alias for a declaration in a scope.
 void resolveModule(ModuleExp mexp)
           
 Expression rewrite_body(java.lang.Object exp)
          Re-write a Scheme in S-expression format into internal form.
 Expression rewrite_car(Pair pair, boolean function)
           
 Expression rewrite_car(Pair pair, SyntaxForm syntax)
           
 Expression rewrite_pair(Pair p, boolean function)
           
 Expression rewrite_with_position(java.lang.Object exp, boolean function, PairWithPosition pair)
           
 Expression rewrite(java.lang.Object exp)
          Re-write a Scheme expression in S-expression format into internal form.
 Expression rewrite(java.lang.Object exp, boolean function)
          Re-write a Scheme expression in S-expression format into internal form.
 void rewriteBody(int first)
           
 void rewriteInBody(java.lang.Object exp)
           
static java.lang.Object safeCar(java.lang.Object obj)
           
static java.lang.Object safeCdr(java.lang.Object obj)
           
 java.lang.Object scanBody(java.lang.Object body, ScopeExp defs, boolean makeList)
          Recursive helper method for rewrite_body.
 void scanForm(java.lang.Object st, ScopeExp defs)
           
 void setCurrentScope(ScopeExp scope)
          Set currentScope().
static void setLine(Declaration decl, java.lang.Object location)
           
static void setLine(Expression exp, java.lang.Object location)
           
 void setLineOf(Expression exp)
          Set the line position of the argument to the current position.
static java.lang.Object stripSyntax(java.lang.Object obj)
           
static java.lang.Object wrapSyntax(java.lang.Object form, SyntaxForm syntax)
           
 
Methods inherited from class gnu.expr.Compilation
addClass, addMainClass, allocLocalField, compileConstant, compileConstant, compileConstantToField, compileToArchive, currentLambda, currentModule, currentScope, demangle2, demangleName, demangleName, error, error, error, error, findForImmediateLiterals, findNamedClass, freeLocalField, generateApplyMethodsWithContext, generateApplyMethodsWithoutContext, generateClassName, generateConstructor, generateConstructor, generateMatchMethods, getBooleanOption, getBooleanOption, getCode, getColumnNumber, getConstructor, getConstructor, getCurrent, getFileName, getForNameHelper, getLanguage, getLineNumber, getMessages, getModule, getModuleType, getPublicId, getState, getSystemId, inlineOk, inlineOk, isPedantic, isStableSourceLocation, isStatic, isValidJavaName, letDone, letEnter, letStart, letVariable, loadCallContext, loadClassRef, loadClassRef, loopBody, loopCond, loopEnter, loopRepeat, loopRepeat, loopRepeat, loopStart, loopVariable, mangleName, mangleName, mangleName, mangleNameIfNeeded, mangleURI, mustCompileHere, outputClass, pop, pop, process, push, push, pushNewModule, pushNewModule, pushPendingImport, pushScope, registerForImmediateLiterals, resolve, setColumn, setCurrent, setFile, setLine, setLine, setLine, setLine, setLocation, setMessages, setModule, setState, setupLiterals, syntaxError, toString, usedClass, usingCPStyle, usingTailCalls, walkModule
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

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.


formStack

public java.util.Stack formStack

firstForm

public int firstForm

pendingForm

public java.lang.Object pendingForm

curMethodLambda

public LambdaExp curMethodLambda
Constructor Detail

Translator

public Translator(Language language,
                  SourceMessages messages)
Method Detail

isLexical

public boolean isLexical(Declaration decl)
Return true if decl is lexical and not fluid.


getGlobalEnvironment

public final Environment getGlobalEnvironment()

parse

public Expression parse(java.lang.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,
                                    boolean function)

getCurrentSyntax

public Syntax getCurrentSyntax()

matches

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


matches

public boolean matches(java.lang.Object form,
                       SyntaxForm syntax,
                       java.lang.String literal)

lookup

public Declaration lookup(java.lang.Object name,
                          int namespace)
Overrides:
lookup in class Compilation

lookupGlobal

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


lookupGlobal

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


rewrite_pair

public Expression rewrite_pair(Pair p,
                               boolean function)

namespaceResolve

public Symbol namespaceResolve(Expression context,
                               Expression member)

stripSyntax

public static java.lang.Object stripSyntax(java.lang.Object obj)

safeCar

public static java.lang.Object safeCar(java.lang.Object obj)

safeCdr

public static java.lang.Object safeCdr(java.lang.Object obj)

listLength

public static int listLength(java.lang.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 descends into SyntaxForm.


rewriteInBody

public void rewriteInBody(java.lang.Object exp)

rewrite

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


namespaceResolve

public java.lang.Object namespaceResolve(java.lang.Object name)

setCurrentScope

public void setCurrentScope(ScopeExp scope)
Description copied from class: Compilation
Set currentScope(). Also update the nesting object.

Overrides:
setCurrentScope in class Compilation

rewrite

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


setLine

public static void setLine(Expression exp,
                           java.lang.Object location)

setLine

public static void setLine(Declaration decl,
                           java.lang.Object location)

pushPositionOf

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


popPositionOf

public void popPositionOf(java.lang.Object saved)
Restore line number position from a previous pushPositionOf.

Parameters:
saved - value returned by matching pushPositionOf.

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.


rewrite_with_position

public Expression rewrite_with_position(java.lang.Object exp,
                                        boolean function,
                                        PairWithPosition pair)

wrapSyntax

public static java.lang.Object wrapSyntax(java.lang.Object form,
                                          SyntaxForm syntax)

popForms

public java.lang.Object popForms(int first)

scanForm

public void scanForm(java.lang.Object st,
                     ScopeExp defs)

scanBody

public java.lang.Object scanBody(java.lang.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,
                            java.lang.Object car,
                            java.lang.Object cdr)

rewrite_body

public Expression rewrite_body(java.lang.Object exp)
Re-write a Scheme in S-expression format into internal form.


rewriteBody

public void rewriteBody(int first)

makeBody

public Expression makeBody(int first,
                           ScopeExp scope)
Combine a list of zero or more expression forms into a "body".


noteAccess

public void noteAccess(java.lang.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(java.lang.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 renamedLiasStack, so we can remove the alias later.


popRenamedAlias

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


define

public Declaration define(java.lang.Object name,
                          SyntaxForm nameSyntax,
                          ScopeExp defs)