kawa.lang

Class Translator

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
LambdaExpcurMethodLambda
MacrocurrentMacroDefinition
Set if we're processing (as opposed to expanding) a define-syntax or defmacro.
intfirstForm
StackformStack
DeclarationmatchArray
A variable to hold the matched values for syntax-case pattern variables.
PatternScopepatternScope
Innermost current scope of pattern variable, from a syntax-case.
ObjectpendingForm
DeclarationtemplateScopeDecl
Constructor Summary
Translator(Language language, SourceMessages messages)
Method Summary
Declarationdefine(Object name, SyntaxForm nameSyntax, ScopeExp defs)
Typeexp2Type(Pair typeSpecPair)
Extract a type from the car of a pair.
voidfinishModule(ModuleExp mexp)
SyntaxgetCurrentSyntax()
EnvironmentgetGlobalEnvironment()
booleanisLexical(Declaration decl)
Return true if decl is lexical and not fluid.
static intlistLength(Object obj)
Returns the length of a syntax list.
Declarationlookup(Object name, int namespace)
DeclarationlookupGlobal(Object name)
Find global Declaration, creating one if not found.
DeclarationlookupGlobal(Object name, int namespace)
Find global Declaration, creating one if not found.
ExpressionmakeBody(int first, ScopeExp scope)
Combine a list of zero or more expression forms into a "body".
static PairmakePair(Pair pair, Object car, Object cdr)
DeclarationmakeRenamedAlias(Declaration decl, ScopeExp templateScope)
DeclarationmakeRenamedAlias(Object name, Declaration decl, ScopeExp templateScope)
booleanmatches(Object form, String literal)
True iff a form matches a literal symbol.
booleanmatches(Object form, SyntaxForm syntax, String literal)
SymbolnamespaceResolve(Expression context, Expression member)
ObjectnamespaceResolve(Object name)
voidnoteAccess(Object name, ScopeExp scope)
Note that we reference name in a given scope.
Expressionparse(Object input)
ObjectpopForms(int first)
voidpopPositionOf(Object saved)
Restore line number position from a previous pushPositionOf.
voidpopRenamedAlias(int count)
Remove one or more aliases created by pushRenamedAlias.
voidprocessAccesses()
Check references recorded by noteAccess.
ObjectpushPositionOf(Object pair)
Note current line number position from a PairWithPosition.
voidpushRenamedAlias(Declaration alias)
Push an alias for a declaration in a scope.
voidresolveModule(ModuleExp mexp)
Expressionrewrite(Object exp)
Re-write a Scheme expression in S-expression format into internal form.
Expressionrewrite(Object exp, boolean function)
Re-write a Scheme expression in S-expression format into internal form.
voidrewriteBody(int first)
voidrewriteInBody(Object exp)
Expressionrewrite_body(Object exp)
Re-write a Scheme in S-expression format into internal form.
Expressionrewrite_car(Pair pair, SyntaxForm syntax)
Expressionrewrite_car(Pair pair, boolean function)
Expressionrewrite_pair(Pair p, boolean function)
Expressionrewrite_with_position(Object exp, boolean function, PairWithPosition pair)
static ObjectsafeCar(Object obj)
static ObjectsafeCdr(Object obj)
ObjectscanBody(Object body, ScopeExp defs, boolean makeList)
Recursive helper method for rewrite_body.
voidscanForm(Object st, ScopeExp defs)
voidsetCurrentScope(ScopeExp scope)
static voidsetLine(Expression exp, Object location)
static voidsetLine(Declaration decl, Object location)
voidsetLineOf(Expression exp)
Set the line position of the argument to the current position.
static ObjectstripSyntax(Object obj)
static ObjectwrapSyntax(Object form, SyntaxForm syntax)

Field Detail

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 Detail

Translator

public Translator(Language language, SourceMessages messages)

Method Detail

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)

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(Declaration decl, ScopeExp templateScope)

makeRenamedAlias

public Declaration makeRenamedAlias(Object name, 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 Symbol namespaceResolve(Expression context, Expression member)

namespaceResolve

public Object namespaceResolve(Object name)

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)

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 in S-expression format into internal form.

rewrite_car

public final Expression rewrite_car(Pair pair, SyntaxForm syntax)

rewrite_car

public final Expression rewrite_car(Pair pair, boolean function)

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)

setLine

public static void setLine(Expression exp, Object location)

setLine

public static void setLine(Declaration decl, 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)