kawa.lang

Class Translator

Implemented Interfaces:
org.xml.sax.Locator, SourceLocator

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
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.
Object
pendingForm
Declaration
templateScopeDecl

Fields inherited from class gnu.expr.Compilation

BODY_PARSED, CALL_WITH_CONSUMER, CALL_WITH_CONTINUATIONS, CALL_WITH_RETURN, CALL_WITH_TAILCALLS, CALL_WITH_UNSPECIFIED, CLASS_WRITTEN, COMPILED, COMPILE_SETUP, ERROR_SEEN, PROLOG_PARSED, PROLOG_PARSING, RESOLVED, WALKED, apply0args, apply0method, apply1args, apply1method, apply2args, apply2method, apply3method, apply4method, applyCpsMethod, applyNargs, applyNmethod, applymethods, argsCallContextField, classPrefix, classPrefixDefault, curClass, curLambda, currentOptions, current_scope, debugPrintExpr, debugPrintFinalExpr, defaultCallConvention, emitSourceDebugExtAttr, 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, 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

Constructor Summary

Translator(Language language, SourceMessages messages)

Method Summary

Declaration
define(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(Object obj)
Returns the length of a syntax list.
Declaration
lookup(Object name, int namespace)
Declaration
lookupGlobal(Object name)
Find global Declaration, creating one if not found.
Declaration
lookupGlobal(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, Object car, Object cdr)
Declaration
makeRenamedAlias(Object name, Declaration decl, ScopeExp templateScope)
Declaration
makeRenamedAlias(Declaration decl, ScopeExp templateScope)
boolean
matches(Object form, String literal)
True iff a form matches a literal symbol.
boolean
matches(Object form, SyntaxForm syntax, String literal)
Object
namespaceResolve(Object name)
Symbol
namespaceResolve(Expression context, Expression member)
void
noteAccess(Object name, ScopeExp scope)
Note that we reference name in a given scope.
Expression
parse(Object input)
Object
popForms(int first)
void
popPositionOf(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.
Object
pushPositionOf(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(Object exp)
Re-write a Scheme expression in S-expression format into internal form.
Expression
rewrite(Object exp, boolean function)
Re-write a Scheme expression in S-expression format into internal form.
void
rewriteBody(int first)
void
rewriteInBody(Object exp)
Expression
rewrite_body(Object exp)
Re-write a Scheme
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(Object exp, boolean function, PairWithPosition pair)
static Object
safeCar(Object obj)
static Object
safeCdr(Object obj)
Object
scanBody(Object body, ScopeExp defs, boolean makeList)
Recursive helper method for rewrite_body.
void
scanForm(Object st, ScopeExp defs)
void
setCurrentScope(ScopeExp scope)
static void
setLine(Declaration decl, Object location)
static void
setLine(Expression exp, Object location)
void
setLineOf(Expression exp)
Set the line position of the argument to the current position.
static Object
stripSyntax(Object obj)
static Object
wrapSyntax(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, lookup, loopBody, loopCond, loopEnter, loopRepeat, loopRepeat, loopRepeat, loopStart, loopVariable, mangleName, mangleName, mangleName, mangleNameIfNeeded, mangleURI, mustCompileHere, outputClass, parse, pop, pop, process, push, push, pushNewModule, pushNewModule, pushPendingImport, pushScope, registerForImmediateLiterals, resolve, setColumn, setCurrent, setCurrentScope, setFile, setLine, setLine, setLine, setLine, setLocation, setMessages, setModule, setState, setupLiterals, syntaxError, toString, usedClass, usingCPStyle, usingTailCalls, walkModule

Field Details

curMethodLambda

public LambdaExp curMethodLambda

currentMacroDefinition

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

firstForm

public int firstForm

formStack

public Stack formStack

matchArray

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

patternScope

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

pendingForm

public Object pendingForm

templateScopeDecl

public Declaration templateScopeDecl

Constructor Details

Translator

public Translator(Language language,
                  SourceMessages messages)

Method Details

define

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

exp2Type

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

finishModule

public void finishModule(ModuleExp mexp)

getCurrentSyntax

public Syntax getCurrentSyntax()

getGlobalEnvironment

public final Environment getGlobalEnvironment()

isLexical

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

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 descends into SyntaxForm.

lookup

public Declaration lookup(Object name,
                          int namespace)
Overrides:
lookup in interface 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.

makeBody

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

makePair

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

makeRenamedAlias

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

makeRenamedAlias

public Declaration makeRenamedAlias(Declaration decl,
                                    ScopeExp templateScope)

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)

namespaceResolve

public Object namespaceResolve(Object name)

namespaceResolve

public Symbol namespaceResolve(Expression context,
                               Expression member)

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.

parse

public Expression parse(Object input)
Overrides:
parse in interface Compilation

popForms

public Object popForms(int first)

popPositionOf

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

popRenamedAlias

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

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.

pushPositionOf

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

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.

resolveModule

public void resolveModule(ModuleExp mexp)

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.

rewriteBody

public void rewriteBody(int first)

rewriteInBody

public void rewriteInBody(Object exp)

rewrite_body

public Expression rewrite_body(Object exp)
Re-write a Scheme

rewrite_car

public final Expression rewrite_car(Pair pair,
                                    boolean function)

rewrite_car

public final Expression rewrite_car(Pair pair,
                                    SyntaxForm syntax)

rewrite_pair

public Expression rewrite_pair(Pair p,
                               boolean function)

rewrite_with_position

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

safeCar

public static Object safeCar(Object obj)

safeCdr

public static Object safeCdr(Object obj)

scanBody

public Object 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.

scanForm

public void scanForm(Object st,
                     ScopeExp defs)

setCurrentScope

public void setCurrentScope(ScopeExp scope)
Overrides:
setCurrentScope in interface Compilation

setLine

public static void setLine(Declaration decl,
                           Object location)

setLine

public static void setLine(Expression exp,
                           Object location)

setLineOf

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

stripSyntax

public static Object stripSyntax(Object obj)

wrapSyntax

public static Object wrapSyntax(Object form,
                                SyntaxForm syntax)