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 classTranslator.FormStackA list of "forms" to be further processed.static classTranslator.ScanContextstatic classTranslator.ValuesFromLListAn 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 LambdaExpcurMethodLambdaMacrocurrentMacroDefinitionSet if we're processing (as opposed to expanding) adefine-syntaxordefmacro.Translator.FormStackformStackstatic DeclarationgetNamedPartDeclDeclarationmatchArrayA variable to hold the matched values for syntax-case pattern variables.PatternScopepatternScopeInnermost current scope of pattern variable, from asyntax-case.ObjectpendingFormDeclarationtemplateScopeDeclNamespaceBindingxmlElementNamespaces-
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 booleanappendBodyValues()ExpressioncheckDefaultBinding(Symbol symbol, Translator tr)If a symbol is lexically unbound, look for a default binding.intcurScanNesting()Declarationdefine(Object name, ScopeExp defs)Declarationdefine(Object name, SyntaxForm nameSyntax, ScopeExp defs)Declarationdefine(Object name, TemplateScope templateScope, ScopeExp defs)voiderrorIfNonEmpty(Object form)voiderrorWithPosition(String message, Object form)Typeexp2Type(Pair typeSpecPair)Extract a type from the car of a pair.Typeexp2Type(Pair typeSpecPair, Declaration decl, SyntaxForm syntax)voidfinishModule(ModuleExp mexp)intgetCompletions(Environment env, String nameStart, Object property, String namespaceUri, List<? super String> matches)SyntaxgetCurrentSyntax()EnvironmentgetGlobalEnvironment()Translator.ScanContextgetScanContext()Stack<Translator.ScanContext>getScanContextStack()booleankeywordsAreSelfEvaluating()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.static StringlookupStandardEntity(String key)ExpressionmakeBody(Expression[] exps)Combine a 'body' consisting of a list of expression.protected ExpressionmakeBody(Pair head, 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, Symbol literal)booleanmatches(Object form, SyntaxForm syntax, String literal)ObjectmatchQuoted(Pair pair)SymbolnamespaceResolve(Expression context, Expression member)SymbolnamespaceResolve(Namespace ns, Expression member)ObjectnamespaceResolve(Object name)NamespacenamespaceResolvePrefix(Expression context)voidnoteAccess(Object name, ScopeExp scope)Note that we reference name in a given scope.Expressionparse(Object input)This may not make sense, except for Lisp-like languages.ValuespopForms(Pair beforeFirst)Pop from formStack all forms that come after beforeFirst.voidpopPositionOf(Object saved)Restore line number position from a previous pushPositionOf.voidpopRenamedAlias(int count)Remove one or more aliases created bypushRenamedAlias.voidpopScanContext()voidprocessAccesses()Check references recorded by noteAccess.voidpushForm(Object value)ObjectpushPositionOf(Object pos)Note current line number position from a PairWithPosition.voidpushRenamedAlias(Declaration alias)Push an alias for a declaration in a scope.voidpushScanContext(LambdaExp lambda)intrenamedAliasCount()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.Expressionrewrite(Object exp, char mode)Re-write a Scheme expression in S-expression format into internal form.Expressionrewrite_body(Object exp)Re-write a Scheme 'body' in S-expression format into internal form.Expressionrewrite_car(Pair pair, boolean function)Expressionrewrite_car(Pair pair, ScopeExp templateScope)Expressionrewrite_car(Pair pair, SyntaxForm syntax)Expressionrewrite_car_for_lookup(Pair pair)Similar to rewrite_car.Expressionrewrite_lookup(Expression part1, Expression part2, boolean function)Expressionrewrite_pair(Pair p, boolean function)Expressionrewrite_with_position(Object exp, boolean function, PairWithPosition pair)protected voidrewriteBody(LList forms)voidrewriteInBody(Object exp)static ObjectsafeCar(Object obj)static ObjectsafeCdr(Object obj)LListscanBody(Object body, ScopeExp defs, boolean makeList)Recursive helper method for rewrite_body.voidscanForm(Object st, ScopeExp defs)booleanselfEvaluatingSymbol(Object obj)static voidsetLine(Declaration decl, Object location)static voidsetLine(Expression exp, 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)-
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-syntaxordefmacro.
-
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:
getGlobalEnvironmentin classCompilation
-
parse
public Expression parse(Object input)
Description copied from class:CompilationThis may not make sense, except for Lisp-like languages. For those, 'input' an s-expression from the reader.- Overrides:
parsein 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:
lookupin 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 as2mand3m/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 ofdeclcame 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 intemplateScopethat 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()
-
-