Package kawa.lang
Class Translator
- java.lang.Object
-
- gnu.expr.Compilation
-
- kawa.lang.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Translator.FormStack
A list of "forms" to be further processed.static class
Translator.ScanContext
static class
Translator.ValuesFromLList
An implementationof Values using a linked list.-
Nested classes/interfaces inherited from interface gnu.text.SourceLocator
SourceLocator.Simple
-
-
Field Summary
Fields Modifier and Type Field Description LambdaExp
curMethodLambda
Macro
currentMacroDefinition
Set if we're processing (as opposed to expanding) adefine-syntax
ordefmacro
.Translator.FormStack
formStack
static Declaration
getNamedPartDecl
Declaration
matchArray
A variable to hold the matched values for syntax-case pattern variables.PatternScope
patternScope
Innermost current scope of pattern variable, from asyntax-case
.Object
pendingForm
Declaration
templateScopeDecl
NamespaceBinding
xmlElementNamespaces
-
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, debugPrintANF, debugPrintExpr, debugPrintFinalExpr, defaultCallConvention, defaultClassFileVersion, emitSourceDebugExtAttr, enableANF, ERROR_SEEN, explicit, exprStack, falseConstant, fullTailCallsVariable, getCallContextInstanceMethod, getCurrentEnvironmentMethod, getNextArgMethod, immediate, inlineOk, int1Args, javaStringType, language, lexer, lexical, mainClass, mainLambda, mainMethodVariable, maxErrors, messages, method, MODULE_NONSTATIC, MODULE_STATIC, MODULE_STATIC_DEFAULT, MODULE_STATIC_RUN, moduleClass, moduleStatic, mustCompile, noArgsField, objArrayType, options, pcCallContextField, pendingImports, PRE_WALKED, procCallContextField, PROLOG_PARSED, PROLOG_PARSING, RESOLVED, scmBooleanType, scmKeywordType, scmListType, scmSequenceType, string1Arg, subModuleMap, sym1Arg, thisDecl, trueConstant, typeApplet, typeCallContext, typeCharSequence, typeClass, typeClassType, typeCompiledProc, typeConsumer, typeEnvironment, typeFieldLocation, typeLanguage, typeList, typeLocation, typeLocationProc, typeMethodProc, typeModuleBody, typeObject, typeObjectType, typePair, typeProcedure, typeProcedure0, typeProcedure1, typeProcedure2, typeProcedure3, typeProcedure4, typeProcedureArray, typeProcedureN, typeRunnable, typeRunnableModule, typeServlet, typeStaticFieldLocation, typeString, typeSymbol, typeType, typeValues, voidConsumerInstanceField, WALKED, warnAsError, warnInvokeUnknownMethod, warnUndefinedVariable, warnUninitialized, warnUnknownMember, warnUnreachable, warnUnused, warnVoidUsed, writeImplicitClasses
-
-
Constructor Summary
Constructors Constructor Description Translator(Language language, SourceMessages messages, NameLookup lexical)
Translator(Language language, SourceMessages messages, NameLookup lexical, Environment env)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
appendBodyValues()
Expression
checkDefaultBinding(Symbol symbol, Translator tr)
If a symbol is lexically unbound, look for a default binding.int
curScanNesting()
Declaration
define(Object name, ScopeExp defs)
Declaration
define(Object name, SyntaxForm nameSyntax, ScopeExp defs)
Declaration
define(Object name, TemplateScope templateScope, ScopeExp defs)
void
errorIfNonEmpty(Object form)
void
errorWithPosition(String message, Object form)
Type
exp2Type(Pair typeSpecPair)
Extract a type from the car of a pair.Type
exp2Type(Pair typeSpecPair, Declaration decl, SyntaxForm syntax)
void
finishModule(ModuleExp mexp)
int
getCompletions(Environment env, String nameStart, Object property, String namespaceUri, List<? super String> matches)
Syntax
getCurrentSyntax()
Environment
getGlobalEnvironment()
Translator.ScanContext
getScanContext()
Stack<Translator.ScanContext>
getScanContextStack()
boolean
keywordsAreSelfEvaluating()
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.static String
lookupStandardEntity(String key)
Expression
makeBody(Expression[] exps)
Combine a 'body' consisting of a list of expression.protected Expression
makeBody(Pair head, 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(Declaration decl, ScopeExp templateScope)
Declaration
makeRenamedAlias(Object name, Declaration decl, ScopeExp templateScope)
boolean
matches(Object form, String literal)
True iff a form matches a literal symbol.boolean
matches(Object form, SyntaxForm syntax, Symbol literal)
boolean
matches(Object form, SyntaxForm syntax, String literal)
Object
matchQuoted(Pair pair)
Symbol
namespaceResolve(Expression context, Expression member)
Symbol
namespaceResolve(Namespace ns, Expression member)
Object
namespaceResolve(Object name)
Namespace
namespaceResolvePrefix(Expression context)
void
noteAccess(Object name, ScopeExp scope)
Note that we reference name in a given scope.Expression
parse(Object input)
This may not make sense, except for Lisp-like languages.Values
popForms(Pair beforeFirst)
Pop from formStack all forms that come after beforeFirst.void
popPositionOf(Object saved)
Restore line number position from a previous pushPositionOf.void
popRenamedAlias(int count)
Remove one or more aliases created bypushRenamedAlias
.void
popScanContext()
void
processAccesses()
Check references recorded by noteAccess.void
pushForm(Object value)
Object
pushPositionOf(Object pos)
Note current line number position from a PairWithPosition.void
pushRenamedAlias(Declaration alias)
Push an alias for a declaration in a scope.void
pushScanContext(LambdaExp lambda)
int
renamedAliasCount()
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.Expression
rewrite(Object exp, char mode)
Re-write a Scheme expression in S-expression format into internal form.Expression
rewrite_body(Object exp)
Re-write a Scheme 'body' in S-expression format into internal form.Expression
rewrite_car(Pair pair, boolean function)
Expression
rewrite_car(Pair pair, ScopeExp templateScope)
Expression
rewrite_car(Pair pair, SyntaxForm syntax)
Expression
rewrite_car_for_lookup(Pair pair)
Similar to rewrite_car.Expression
rewrite_lookup(Expression part1, Expression part2, boolean function)
Expression
rewrite_pair(Pair p, boolean function)
Expression
rewrite_with_position(Object exp, boolean function, PairWithPosition pair)
protected void
rewriteBody(LList forms)
void
rewriteInBody(Object exp)
static Object
safeCar(Object obj)
static Object
safeCdr(Object obj)
LList
scanBody(Object body, ScopeExp defs, boolean makeList)
Recursive helper method for rewrite_body.void
scanForm(Object st, ScopeExp defs)
boolean
selfEvaluatingSymbol(Object obj)
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, applyFunction, asBooleanValue, cleanupAfterCompilation, compileConstant, compileConstant, compileToArchive, currentCallConvention, currentLambda, currentModule, currentScope, emitCoerceToBoolean, emitPushBoolean, error, error, error, error, findForImmediateLiterals, findNamedClass, freeLocalField, generateCheckMethod, generateCheckMethods, generateClassName, generateConstructor, generateConstructor, generateMainMethod, generatingApplet, generatingServlet, getBooleanOption, getBooleanOption, getCode, getColumnNumber, getConstructor, getConstructor, getCurrent, getEndColumn, getEndLine, getFileName, getForNameHelper, getLanguage, getLineNumber, getMessages, getMinfo, getModule, getModuleType, getPublicId, getSourceAbsPath, getStartColumn, getStartLine, getState, getSystemId, inlineOk, inlineOk, isApplyFunction, isInteractive, isPedantic, isSimpleApplyFunction, isStableSourceLocation, isStatic, letDone, letEnter, letStart, letVariable, letVariable, loadCallContext, loadClassRef, loopBody, loopCond, loopDone, loopEnter, loopRepeat, loopRepeatDone, loopStart, loopVariable, makeCoercion, makeCoercion, makeQuoteExp, makeRunnable, mangleURI, maxErrors, mustCompileHere, outputClass, pop, pop, process, push, push, pushNewModule, pushPendingImport, pushScope, registerForImmediateLiterals, resolve, restoreCurrent, setColumn, setCurrent, setCurrentScope, setEvalName, setFile, setInteractiveName, setLine, setLine, setLine, setLine, setLocation, setMessages, setModule, setModuleName, setPedantic, setPopCurrentScope, setPushCurrentScope, setSaveCurrent, setSharedModuleDefs, setState, setupLiterals, sharedModuleDefs, syntaxError, toString, usedClass, usingCallContext, usingCPStyle, usingTailCalls, warnAsError, warnInvokeUnknownMethod, warnUndefinedVariable, warnUninitialized, warnUnknownMember, warnUnreachable, warnUnused, warnVoidUsed
-
-
-
-
Field Detail
-
currentMacroDefinition
public Macro currentMacroDefinition
Set if we're processing (as opposed to expanding) adefine-syntax
ordefmacro
.
-
patternScope
public PatternScope patternScope
Innermost current scope of pattern variable, from asyntax-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 Detail
-
Translator
public Translator(Language language, SourceMessages messages, NameLookup lexical, Environment env)
-
Translator
public Translator(Language language, SourceMessages messages, NameLookup lexical)
-
-
Method Detail
-
getGlobalEnvironment
public final Environment getGlobalEnvironment()
- Overrides:
getGlobalEnvironment
in classCompilation
-
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 classCompilation
-
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)
-
lookup
public Declaration lookup(Object name, int namespace)
- Overrides:
lookup
in classCompilation
-
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)
-
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)
-
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>
andjava.lang.Integer
. Also handles arrays, such asjava.lang.String[]
. If the symbol starts with'@'
parse as an annotation class. Recognizes quanties with units, such as2m
and3m/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.
-
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.
-
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, 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.
-
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 theformStack
.- 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 aSyntaxForm
); otherwisenull
.
-
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 ofdecl
came from a syntax template whose immediate scope istemplateScope
, then the same syntax template may contain local variable references that are also in the sametemplateScope
. Such variable references will not look in the current "physical" scope, where we just createddecl
, but will instead search the "lexical"templateScope
. So that such references can resolve todecl
, we create an alias intemplateScope
that points todecl
. We record that we did this in therenamedAliasStack
, so we can remove the alias later.
-
renamedAliasCount
public int renamedAliasCount()
-
popRenamedAlias
public void popRenamedAlias(int count)
Remove one or more aliases created bypushRenamedAlias
.
-
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()
-
-