Algernon Software

org.algernon.aam.compile
Class AlgernonCompiler

java.lang.Object
  extended byorg.algernon.aam.compile.AlgernonCompiler

public class AlgernonCompiler
extends Object

This class compiles Algernon expressions into AAM abstract machine opcodes.


Field Summary
 LispValue ALL_LABEL
           
static int ASSERT_MODE
           
private  boolean DEBUG
           
 LispValue END_LABEL
           
protected  Algernon f_algy
           
private  Jatha f_lisp
           
private  OpcodeSet f_opcodeSet
           
private  Processor f_processor
           
 LispValue FAIL_LABEL
           
protected static int FORC_MODE
           
protected static int m_conjunctionIDCounter
           
protected static int m_labelCounter
           
protected static DecimalFormat m_labelFormat
           
protected static int m_variableCounter
           
protected static boolean NEWLINE
           
protected static boolean NO_NEWLINE
           
protected static int NORMAL_MODE
           
 Command OP_A
           
 Command OP_ADD_CLASS
           
 Command OP_ADD_FACET
           
 Command OP_ADD_INSTANCE
           
 Command OP_ADD_RELATION
           
 Command OP_ADD_RELATION_RULE
           
 Command OP_ADD_RULE
           
 Command OP_ALL_PATHS
           
 Command OP_ALL_PATHS_PART_1
           
 Command OP_ALL_PATHS_PART_2
           
 Command OP_ANY
           
 Command OP_ASK
           
 Command OP_ASSUME
           
 Command OP_BIND
           
 Command OP_BOUNDP
           
 Command OP_BRANCH
           
 Command OP_CHILD
           
 Command OP_CLASS
           
 Command OP_CLEAR_RELATION
           
 Command OP_CLEAR_SLOT
           
 Command OP_COUNT
           
 Command OP_DATE
           
 Command OP_DELETE
           
 Command OP_DELETE_CLASS
           
 Command OP_DELETE_FACET
           
 Command OP_DELETE_FRAME
           
 Command OP_DELETE_INSTANCE
           
 Command OP_DELETE_RELATION
           
 Command OP_DIRECT_CLASS
           
 Command OP_DIRECT_INSTANCE
           
 Command OP_DIRECT_SUBCLASS
           
 Command OP_DIRECT_SUPERCLASS
           
 Command OP_EVAL
           
 Command OP_FAIL
           
 Command OP_FIRE
           
 Command OP_FOR
           
 Command OP_FORC
           
 Command OP_FORC_CLASS
           
 Command OP_FORC_INSTANCE
           
 Command OP_INSTANCE
           
 Command OP_IS_CLASS
           
 Command OP_IS_FACET
           
 Command OP_IS_FRAME
           
 Command OP_IS_INSTANCE
           
 Command OP_IS_RELATION
           
 Command OP_LEAF_CLASS
           
 Command OP_LEAST_COMMON_SUPERCLASS
           
 Command OP_LIMIT
           
 Command OP_NAME
           
 Command OP_NEQ
           
 Command OP_NO_COMPLETION
           
 Command OP_NO_CONTINUATION
           
 Command OP_OR
           
 Command OP_PARENT
           
 Command OP_PRINT
           
 Command OP_PRINTLN
           
 Command OP_RETRIEVE
           
 Command OP_ROOT_CLASS
           
 Command OP_RULES
           
 Command OP_SHOW
           
 Command OP_SHOW_CODE
           
 Command OP_SLOT
           
 Command OP_SRULES
           
 Command OP_STOP
           
 Command OP_SUBCLASS
           
 Command OP_SUPERCLASS
           
 Command OP_TAXONOMY
           
 Command OP_TEMPLATE_RELATION
           
 Command OP_TEST
           
 Command OP_THE
           
 Command OP_THE_CLASS
           
 Command OP_THE_INSTANCE
           
 Command OP_TRACE
           
 Command OP_UNBOUNDP
           
 Command OP_UNP
           
 Command OP_USE_KB
           
static int QUERY_MODE
           
protected  LispValue STANDARD_CODE_TAIL
           
protected  LispValue STANDARD_RULE_CODE_TAIL
           
 LispValue SUCCEED_LABEL
           
protected  LispValue TAX_CLASS
           
protected  LispValue TAX_COMPLETE
           
protected  LispValue TAX_INSTANCE
           
protected static int TOO_FEW_ARGS
           
protected static int TOO_MANY_ARGS
           
 LispValue UNTYPED_VAR
           
static int VERIFY_MODE
           
protected static int ZERO_OR_MORE
           
 
Constructor Summary
AlgernonCompiler(Algernon algy)
           
 
Method Summary
 void checkForObsoleteMacro(LispValue macro, LispValue clause, ErrorSet errors)
          check for obsolete macro - puts a warning message in the error set.
 LispValue compile(Path path, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, ErrorSet compileErrors)
          Returns a list of AAM machine code to be executed.
protected  LispValue compileAssert(Clause clause, AlgernonKBSet kbSet, int index, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          compile-assert
protected  LispValue compileClause(Clause clause, AlgernonKBSet kbSet, int index, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          compiles one clause
protected  LispValue compileMacro(Clause clause, AlgernonKBSet kbSet, int index, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpAddClass(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          create a new frame and assert the path for that variable.
protected  LispValue compileOpAddInstance(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpAllPaths(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpAny(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpAsk(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpCount(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          This compiles the :COUNT macro.
protected  LispValue compileOpDelete(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpFail(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          This compiles the :FAIL and :UNP macros (which are synonyms of each other).
protected  LispValue compileOpFire(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpFor(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          This compiles the :FOR macro.
protected  LispValue compileOpForcClass(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpForcInstance(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpLimit(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          This compiles the :LIMIT macro.
protected  LispValue compileOpOr(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpPrint(Clause clause, AlgernonKBSet kbSet, int index, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors, boolean newline)
          compileOpPrint - compiles the PRINT and PRINTLN macros.
protected  LispValue compileOpTheClass(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compileOpTheInstance(Clause clause, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
           
protected  LispValue compilePath(Path path, AlgernonKBSet kbSet, int index, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, ErrorSet compileErrors)
          compilePath compiles an Algernon path into abstract machine code.
protected  LispValue compilePathAux(Path path, AlgernonKBSet kbSet, int index, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          compilePathAux is an internal routine that compiles a path.
protected  LispValue compilePathNoReturn(Path path, AlgernonKBSet kbSet, int index, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, ErrorSet compileErrors)
          compilePathAux is an internal routine that compiles a path.
protected  LispValue compileQuery(Clause clause, AlgernonKBSet kbSet, int index, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          compileQuery
 Collection compileRule(Rule rule, AlgernonKBSet kbSet, BindingList bindings, Map argTypes, AlgernonCompilerState compileState, int startIndex, ErrorSet errors)
          Compiles a rule.
protected  LispValue compileSpecial(Clause clause, AlgernonKBSet kbSet, int index, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          compileSpecial - general-purpose routine for special MACROS whose details are handled by the opcode.
protected  LispValue compileSpecialWithFail(Clause clause, AlgernonKBSet kbSet, int index, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          compileSpecialWithFail - general-purpose routine for special MACROS that need to check for failure afterward.
protected  LispValue compileSpecialWithFailAndMerge(Clause clause, AlgernonKBSet kbSet, int index, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          compileSpecialWithFailAndMerge - general-purpose routine for special MACROS that need to check for failure afterward.
protected  LispValue compileSpecialWithMerge(Clause clause, AlgernonKBSet kbSet, int index, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          compileSpecialWithMerge - general-purpose routine for special MACROS that need to do a merge-bindings afterward.
protected  LispValue compileSubroutine(LispValue variable, Path path, AlgernonKBSet kbSet, int compileMode, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, ErrorSet compileErrors)
          compiles a subroutine.
protected  LispValue compileVerify(Clause clause, AlgernonKBSet kbSet, int index, BindingList bindings, Map argumentTypes, Rule rule, AlgernonCompilerState compileState, LispValue code, ErrorSet compileErrors)
          compileVerify
protected  boolean constantTypeEQ(AlgernonKBSet kbSet, LispValue argument, Set type, Clause form, AlgernonCompilerState compileState, ErrorSet compileErrors)
           
 String deprecationWarning(LispValue oldOperator, String message, LispValue clause)
          General deprecation warning for old operators.
 String domainMismatchWarning(Clause clause, LispValue argument, Set variableType, Set desiredType)
          Returns an error about type mismatch on variables.
protected  LispValue doTaxonomyPreprocess(LispValue superclass, LispValue forms, BindingList bindings)
          This turns the TAXONOMY form into the :CLASS and :INSTANCE pseudo-code that is processed by makeTaxonomyCommands.
 Opcode findOpcodeForMacro(LispValue macro)
          Send in a macro name (that is, a keyword).
 LispValue generateRuleCode(Rule rule, AlgernonKBSet kbSet, int direction, Path antecedent, Path consequent, int index, Rule rootRule, BindingList boundVariables, Map argumentTypes, ErrorSet errors)
          Generates the actual code for the rule.
protected  String generateRuleName(Rule rule, BindingList variableSet)
          Generates a fake rule name based on the set of bound variables.
protected  Collection generateVariableSets(Clause clause)
          When a backward chaining rule is activated, some number of variables in the key will be bound.
protected  LispValue getNewConjunctionID()
          Generate a new label.
protected  LispValue getNewLabel()
          Generate a new label.
protected  LispValue getNewVariable()
          Generate a new variable.
private  boolean hasInvalidArgumentCount(LispValue command, int numberOfArguments, int requiredNumber)
          Checks the argument count against the requirements.
protected  void implicitTypeRestrict(AlgernonKBSet kbSet, Clause clause, Map argumentTypes, AlgernonCompilerState compileState, ErrorSet compileErrors)
          Added 29 Sep 1999 (mh) Added type restriction - if the path contains an ISA or MEMBER clause, this information is used to restrict the type to a new type, eliminating some unnecessary warning messages The clause should NOT be negated.
 void initializeArgumentTypes(AlgernonKBSet kbSet, Map typeList, Path path, AlgernonCompilerState compileState, ErrorSet errors, boolean clear)
          The compiler type-checks each clause.
protected  void initializeConstants()
          variable initializer for this class.
 boolean isContinuable(Clause clause)
          Returns true if continuations should be created for the given operation.
 boolean isFrameType(Set variableType)
           
 boolean isMacro(LispValue symbol)
          Returns true if the given symbol is an Algernon special form.
 boolean isOfSimpleType(LispValue arg, Set type)
          Returns true if the argument satisfies the simple type.
 boolean isPathUnrestricted(LispValue symbol)
          Returns true if the given symbol is not required to follow the Algernon path restrictions.
 boolean isSimpleType(LispValue type)
          Returns true if the type is not a frame type.
 boolean isSimpleType(Set types)
          Returns true if the type set contains only simple types.
protected  Set lookupArgumentType(LispValue argument, Map argumentTypes)
          Returns a set of argument types for an argument.
protected  Path makeTaxonomyCommands(AlgernonKBSet kbSet, LispValue pseudoTax, BindingList bindings)
          Converts pseudo-taxonomy commands into real taxonomy commands.
 String pathRestrictionWarning(Clause clause, Rule rule)
          Returns a warning about path restriction.
 Clause preprocessClause(Clause clause)
          Preprocesses a clause.
 Path preprocessPath(Path path)
          Preprocesses a path
protected  Path preprocessTaxonomy(AlgernonKBSet kbSet, LispValue form)
          preprocesses the :TAXONOMY form to turn it into a set of :THE-CLASS and :THE-INSTANCE commands.
 String relationLengthMismatchError(int type, Clause clause, LispValue slot)
           
 void setDebug(boolean state)
          Set to true to turn on compiler debug mode.
private  void trace(String message)
           
protected  boolean typeEQ(AlgernonKBSet kbSet, LispValue argument, Set type, Map argumentTypes, Clause form, AlgernonCompilerState compileState, ErrorSet compileErrors)
          Checks for equality and stores the type, if not previously known.
 boolean typesIntersect(AlgernonKBSet kbSet, Set types1, Set types2)
          Returns true if the two Sets of types intersect.
 boolean typeSubsumedBy(AlgernonKBSet kbSet, LispValue singleType, Set types)
          Returns true if one frame type is a subclass or equal to any of a set of frame types.
 String undefinedRelationError(Clause clause, LispValue slot)
           
 String undeterminedDomainWarning(Clause clause, LispValue argument, Set type)
           
 String unrecognizedOperatorError(Clause clause, LispValue slot)
          Returns an error string about an unrecognized command used in an Algernon clause.
protected  void validateArgumentTypes(Clause clause, AlgernonKBSet kbSet, Map argumentTypes, AlgernonCompilerState compileState, ErrorSet compileErrors)
          validates that arguments are of the correct type.
protected  void validateMacroArgumentTypes(Clause form, AlgernonKBSet kbSet, Map argumentTypes, AlgernonCompilerState compileState, ErrorSet compileErrors)
          Validates argument types for macros like :INSTANCE, :CLASS, and so on.
protected  void validateSpecialFormArgumentTypes(Clause form, AlgernonKBSet kbSet, Map argumentTypes, AlgernonCompilerState compileState, ErrorSet compileErrors)
          Validates argument types for macros like :THE, :A and :FORC, which have different forms of arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ASSERT_MODE

public static final int ASSERT_MODE
See Also:
Constant Field Values

QUERY_MODE

public static final int QUERY_MODE
See Also:
Constant Field Values

VERIFY_MODE

public static final int VERIFY_MODE
See Also:
Constant Field Values

FORC_MODE

protected static final int FORC_MODE
See Also:
Constant Field Values

NORMAL_MODE

protected static final int NORMAL_MODE
See Also:
Constant Field Values

TOO_FEW_ARGS

protected static final int TOO_FEW_ARGS
See Also:
Constant Field Values

TOO_MANY_ARGS

protected static final int TOO_MANY_ARGS
See Also:
Constant Field Values

ZERO_OR_MORE

protected static final int ZERO_OR_MORE
See Also:
Constant Field Values

NEWLINE

protected static final boolean NEWLINE
See Also:
Constant Field Values

NO_NEWLINE

protected static final boolean NO_NEWLINE
See Also:
Constant Field Values

DEBUG

private boolean DEBUG

TAX_CLASS

protected LispValue TAX_CLASS

TAX_INSTANCE

protected LispValue TAX_INSTANCE

TAX_COMPLETE

protected LispValue TAX_COMPLETE

UNTYPED_VAR

public LispValue UNTYPED_VAR

ALL_LABEL

public LispValue ALL_LABEL

END_LABEL

public LispValue END_LABEL

FAIL_LABEL

public LispValue FAIL_LABEL

SUCCEED_LABEL

public LispValue SUCCEED_LABEL

OP_A

public Command OP_A

OP_ADD_CLASS

public Command OP_ADD_CLASS

OP_ADD_FACET

public Command OP_ADD_FACET

OP_ADD_INSTANCE

public Command OP_ADD_INSTANCE

OP_ADD_RULE

public Command OP_ADD_RULE

OP_ADD_RELATION

public Command OP_ADD_RELATION

OP_ADD_RELATION_RULE

public Command OP_ADD_RELATION_RULE

OP_ALL_PATHS

public Command OP_ALL_PATHS

OP_ALL_PATHS_PART_1

public Command OP_ALL_PATHS_PART_1

OP_ALL_PATHS_PART_2

public Command OP_ALL_PATHS_PART_2

OP_ANY

public Command OP_ANY

OP_ASK

public Command OP_ASK

OP_ASSUME

public Command OP_ASSUME

OP_BIND

public Command OP_BIND

OP_BOUNDP

public Command OP_BOUNDP

OP_BRANCH

public Command OP_BRANCH

OP_CHILD

public Command OP_CHILD

OP_CLASS

public Command OP_CLASS

OP_CLEAR_RELATION

public Command OP_CLEAR_RELATION

OP_CLEAR_SLOT

public Command OP_CLEAR_SLOT

OP_LEAST_COMMON_SUPERCLASS

public Command OP_LEAST_COMMON_SUPERCLASS

OP_COUNT

public Command OP_COUNT

OP_DATE

public Command OP_DATE

OP_DELETE

public Command OP_DELETE

OP_DELETE_CLASS

public Command OP_DELETE_CLASS

OP_DELETE_FACET

public Command OP_DELETE_FACET

OP_DELETE_FRAME

public Command OP_DELETE_FRAME

OP_DELETE_INSTANCE

public Command OP_DELETE_INSTANCE

OP_DELETE_RELATION

public Command OP_DELETE_RELATION

OP_DIRECT_CLASS

public Command OP_DIRECT_CLASS

OP_DIRECT_INSTANCE

public Command OP_DIRECT_INSTANCE

OP_DIRECT_SUBCLASS

public Command OP_DIRECT_SUBCLASS

OP_DIRECT_SUPERCLASS

public Command OP_DIRECT_SUPERCLASS

OP_EVAL

public Command OP_EVAL

OP_FAIL

public Command OP_FAIL

OP_FIRE

public Command OP_FIRE

OP_FOR

public Command OP_FOR

OP_FORC

public Command OP_FORC

OP_FORC_CLASS

public Command OP_FORC_CLASS

OP_FORC_INSTANCE

public Command OP_FORC_INSTANCE

OP_INSTANCE

public Command OP_INSTANCE

OP_IS_CLASS

public Command OP_IS_CLASS

OP_IS_FACET

public Command OP_IS_FACET

OP_IS_FRAME

public Command OP_IS_FRAME

OP_IS_INSTANCE

public Command OP_IS_INSTANCE

OP_IS_RELATION

public Command OP_IS_RELATION

OP_LEAF_CLASS

public Command OP_LEAF_CLASS

OP_LIMIT

public Command OP_LIMIT

OP_NAME

public Command OP_NAME

OP_NEQ

public Command OP_NEQ

OP_NO_COMPLETION

public Command OP_NO_COMPLETION

OP_NO_CONTINUATION

public Command OP_NO_CONTINUATION

OP_OR

public Command OP_OR

OP_PARENT

public Command OP_PARENT

OP_PRINT

public Command OP_PRINT

OP_PRINTLN

public Command OP_PRINTLN

OP_RETRIEVE

public Command OP_RETRIEVE

OP_ROOT_CLASS

public Command OP_ROOT_CLASS

OP_RULES

public Command OP_RULES

OP_SHOW

public Command OP_SHOW

OP_SHOW_CODE

public Command OP_SHOW_CODE

OP_SLOT

public Command OP_SLOT

OP_SRULES

public Command OP_SRULES

OP_STOP

public Command OP_STOP

OP_SUBCLASS

public Command OP_SUBCLASS

OP_SUPERCLASS

public Command OP_SUPERCLASS

OP_TAXONOMY

public Command OP_TAXONOMY

OP_TEST

public Command OP_TEST

OP_TEMPLATE_RELATION

public Command OP_TEMPLATE_RELATION

OP_THE

public Command OP_THE

OP_THE_CLASS

public Command OP_THE_CLASS

OP_THE_INSTANCE

public Command OP_THE_INSTANCE

OP_TRACE

public Command OP_TRACE

OP_UNBOUNDP

public Command OP_UNBOUNDP

OP_UNP

public Command OP_UNP

OP_USE_KB

public Command OP_USE_KB

STANDARD_CODE_TAIL

protected LispValue STANDARD_CODE_TAIL

STANDARD_RULE_CODE_TAIL

protected LispValue STANDARD_RULE_CODE_TAIL

f_algy

protected Algernon f_algy

m_conjunctionIDCounter

protected static int m_conjunctionIDCounter

m_labelCounter

protected static int m_labelCounter

m_variableCounter

protected static int m_variableCounter

m_labelFormat

protected static DecimalFormat m_labelFormat

f_lisp

private Jatha f_lisp

f_processor

private Processor f_processor

f_opcodeSet

private OpcodeSet f_opcodeSet
Constructor Detail

AlgernonCompiler

public AlgernonCompiler(Algernon algy)
Method Detail

initializeConstants

protected void initializeConstants()
variable initializer for this class.


isMacro

public boolean isMacro(LispValue symbol)
Returns true if the given symbol is an Algernon special form.


isPathUnrestricted

public boolean isPathUnrestricted(LispValue symbol)
Returns true if the given symbol is not required to follow the Algernon path restrictions.


isContinuable

public boolean isContinuable(Clause clause)
Returns true if continuations should be created for the given operation.


checkForObsoleteMacro

public void checkForObsoleteMacro(LispValue macro,
                                  LispValue clause,
                                  ErrorSet errors)
check for obsolete macro - puts a warning message in the error set.


findOpcodeForMacro

public Opcode findOpcodeForMacro(LispValue macro)
                          throws AlgernonCompilerException
Send in a macro name (that is, a keyword). This method returns the opcode corresponding to the macro.

Throws:
AlgernonCompilerException

getNewConjunctionID

protected LispValue getNewConjunctionID()
Generate a new label. Use of static counter should generate a øunique label.


getNewLabel

protected LispValue getNewLabel()
Generate a new label. Use of static counter should generate a unique label.


getNewVariable

protected LispValue getNewVariable()
Generate a new variable. Use of static counter should generate a unique variable.


compile

public LispValue compile(Path path,
                         AlgernonKBSet kbSet,
                         int compileMode,
                         BindingList bindings,
                         Map argumentTypes,
                         Rule rule,
                         AlgernonCompilerState compileState,
                         ErrorSet compileErrors)
                  throws AlgernonException
Returns a list of AAM machine code to be executed. Bindings only occur in the case where a form is compiled during execution of a path. For example, when a rule is defined during execution of a rule, or when a rule is compiled at runtime. compileMode is ASSERT or QUERY.

Throws:
AlgernonException

compilePath

protected LispValue compilePath(Path path,
                                AlgernonKBSet kbSet,
                                int index,
                                int compileMode,
                                BindingList bindings,
                                Map argumentTypes,
                                Rule rule,
                                AlgernonCompilerState compileState,
                                ErrorSet compileErrors)
                         throws AlgernonException
compilePath compiles an Algernon path into abstract machine code.

Throws:
AlgernonException

compilePathNoReturn

protected LispValue compilePathNoReturn(Path path,
                                        AlgernonKBSet kbSet,
                                        int index,
                                        int compileMode,
                                        BindingList bindings,
                                        Map argumentTypes,
                                        Rule rule,
                                        AlgernonCompilerState compileState,
                                        ErrorSet compileErrors)
                                 throws AlgernonException
compilePathAux is an internal routine that compiles a path.

Throws:
AlgernonException

compilePathAux

protected LispValue compilePathAux(Path path,
                                   AlgernonKBSet kbSet,
                                   int index,
                                   int compileMode,
                                   BindingList bindings,
                                   Map argumentTypes,
                                   Rule rule,
                                   AlgernonCompilerState compileState,
                                   LispValue code,
                                   ErrorSet compileErrors)
                            throws AlgernonException
compilePathAux is an internal routine that compiles a path.

Throws:
AlgernonException

compileClause

protected LispValue compileClause(Clause clause,
                                  AlgernonKBSet kbSet,
                                  int index,
                                  int compileMode,
                                  BindingList bindings,
                                  Map argumentTypes,
                                  Rule rule,
                                  AlgernonCompilerState compileState,
                                  LispValue code,
                                  ErrorSet compileErrors)
                           throws AlgernonException
compiles one clause

Throws:
AlgernonException

compileSubroutine

protected LispValue compileSubroutine(LispValue variable,
                                      Path path,
                                      AlgernonKBSet kbSet,
                                      int compileMode,
                                      BindingList bindings,
                                      Map argumentTypes,
                                      Rule rule,
                                      AlgernonCompilerState compileState,
                                      ErrorSet compileErrors)
                               throws AlgernonException
compiles a subroutine.

Throws:
AlgernonException

compileSpecial

protected LispValue compileSpecial(Clause clause,
                                   AlgernonKBSet kbSet,
                                   int index,
                                   BindingList bindings,
                                   Map argumentTypes,
                                   Rule rule,
                                   AlgernonCompilerState compileState,
                                   LispValue code,
                                   ErrorSet compileErrors)
                            throws AlgernonCompilerException
compileSpecial - general-purpose routine for special MACROS whose details are handled by the opcode.

Throws:
AlgernonCompilerException

compileSpecialWithFailAndMerge

protected LispValue compileSpecialWithFailAndMerge(Clause clause,
                                                   AlgernonKBSet kbSet,
                                                   int index,
                                                   BindingList bindings,
                                                   Map argumentTypes,
                                                   Rule rule,
                                                   AlgernonCompilerState compileState,
                                                   LispValue code,
                                                   ErrorSet compileErrors)
                                            throws AlgernonCompilerException
compileSpecialWithFailAndMerge - general-purpose routine for special MACROS that need to check for failure afterward.

Throws:
AlgernonCompilerException

compileSpecialWithFail

protected LispValue compileSpecialWithFail(Clause clause,
                                           AlgernonKBSet kbSet,
                                           int index,
                                           BindingList bindings,
                                           Map argumentTypes,
                                           Rule rule,
                                           AlgernonCompilerState compileState,
                                           LispValue code,
                                           ErrorSet compileErrors)
                                    throws AlgernonCompilerException
compileSpecialWithFail - general-purpose routine for special MACROS that need to check for failure afterward.

Throws:
AlgernonCompilerException

compileSpecialWithMerge

protected LispValue compileSpecialWithMerge(Clause clause,
                                            AlgernonKBSet kbSet,
                                            int index,
                                            BindingList bindings,
                                            Map argumentTypes,
                                            Rule rule,
                                            AlgernonCompilerState compileState,
                                            LispValue code,
                                            ErrorSet compileErrors)
                                     throws AlgernonCompilerException
compileSpecialWithMerge - general-purpose routine for special MACROS that need to do a merge-bindings afterward.

Throws:
AlgernonCompilerException

compileAssert

protected LispValue compileAssert(Clause clause,
                                  AlgernonKBSet kbSet,
                                  int index,
                                  BindingList bindings,
                                  Map argumentTypes,
                                  Rule rule,
                                  AlgernonCompilerState compileState,
                                  LispValue code,
                                  ErrorSet compileErrors)
compile-assert


compileQuery

protected LispValue compileQuery(Clause clause,
                                 AlgernonKBSet kbSet,
                                 int index,
                                 BindingList bindings,
                                 Map argumentTypes,
                                 Rule rule,
                                 AlgernonCompilerState compileState,
                                 LispValue code,
                                 ErrorSet compileErrors)
compileQuery


compileVerify

protected LispValue compileVerify(Clause clause,
                                  AlgernonKBSet kbSet,
                                  int index,
                                  BindingList bindings,
                                  Map argumentTypes,
                                  Rule rule,
                                  AlgernonCompilerState compileState,
                                  LispValue code,
                                  ErrorSet compileErrors)
compileVerify


compileMacro

protected LispValue compileMacro(Clause clause,
                                 AlgernonKBSet kbSet,
                                 int index,
                                 int compileMode,
                                 BindingList bindings,
                                 Map argumentTypes,
                                 Rule rule,
                                 AlgernonCompilerState compileState,
                                 LispValue code,
                                 ErrorSet compileErrors)
                          throws AlgernonException
Throws:
AlgernonException

compileOpAllPaths

protected LispValue compileOpAllPaths(Clause clause,
                                      AlgernonKBSet kbSet,
                                      int compileMode,
                                      BindingList bindings,
                                      Map argumentTypes,
                                      Rule rule,
                                      AlgernonCompilerState compileState,
                                      LispValue code,
                                      ErrorSet compileErrors)
                               throws AlgernonException
Throws:
AlgernonException

compileOpAny

protected LispValue compileOpAny(Clause clause,
                                 AlgernonKBSet kbSet,
                                 int compileMode,
                                 BindingList bindings,
                                 Map argumentTypes,
                                 Rule rule,
                                 AlgernonCompilerState compileState,
                                 LispValue code,
                                 ErrorSet compileErrors)
                          throws AlgernonException
Throws:
AlgernonException

compileOpAsk

protected LispValue compileOpAsk(Clause clause,
                                 AlgernonKBSet kbSet,
                                 int compileMode,
                                 BindingList bindings,
                                 Map argumentTypes,
                                 Rule rule,
                                 AlgernonCompilerState compileState,
                                 LispValue code,
                                 ErrorSet compileErrors)
                          throws AlgernonException
Throws:
AlgernonException

compileOpAddClass

protected LispValue compileOpAddClass(Clause clause,
                                      AlgernonKBSet kbSet,
                                      int compileMode,
                                      BindingList bindings,
                                      Map argumentTypes,
                                      Rule rule,
                                      AlgernonCompilerState compileState,
                                      LispValue code,
                                      ErrorSet compileErrors)
                               throws AlgernonException
create a new frame and assert the path for that variable. Optionally assert that it is a member of the set.

Throws:
AlgernonException

compileOpCount

protected LispValue compileOpCount(Clause clause,
                                   AlgernonKBSet kbSet,
                                   int compileMode,
                                   BindingList bindings,
                                   Map argumentTypes,
                                   Rule rule,
                                   AlgernonCompilerState compileState,
                                   LispValue code,
                                   ErrorSet compileErrors)
                            throws AlgernonException
This compiles the :COUNT macro. Execute the clause in a subroutine, do a MERGE_BINDINGS, then call the COUNT opcode. Format is (:COUNT ?countVar ?countedVar )

Throws:
AlgernonException

compileOpDelete

protected LispValue compileOpDelete(Clause clause,
                                    AlgernonKBSet kbSet,
                                    int compileMode,
                                    BindingList bindings,
                                    Map argumentTypes,
                                    Rule rule,
                                    AlgernonCompilerState compileState,
                                    LispValue code,
                                    ErrorSet compileErrors)
                             throws AlgernonException
Throws:
AlgernonException

compileOpFail

protected LispValue compileOpFail(Clause clause,
                                  AlgernonKBSet kbSet,
                                  int compileMode,
                                  BindingList bindings,
                                  Map argumentTypes,
                                  Rule rule,
                                  AlgernonCompilerState compileState,
                                  LispValue code,
                                  ErrorSet compileErrors)
                           throws AlgernonException
This compiles the :FAIL and :UNP macros (which are synonyms of each other).

Throws:
AlgernonException

compileOpFire

protected LispValue compileOpFire(Clause clause,
                                  AlgernonKBSet kbSet,
                                  int compileMode,
                                  BindingList bindings,
                                  Map argumentTypes,
                                  Rule rule,
                                  AlgernonCompilerState compileState,
                                  LispValue code,
                                  ErrorSet compileErrors)

compileOpFor

protected LispValue compileOpFor(Clause clause,
                                 AlgernonKBSet kbSet,
                                 int compileMode,
                                 BindingList bindings,
                                 Map argumentTypes,
                                 Rule rule,
                                 AlgernonCompilerState compileState,
                                 LispValue code,
                                 ErrorSet compileErrors)
                          throws AlgernonException
This compiles the :FOR macro. Execute the clause in a subroutine, do a MERGE_BINDINGS, then call the FOR opcode. Format is (:FOR ?indexVar ?totalVar ?countedVar )

Throws:
AlgernonException

compileOpLimit

protected LispValue compileOpLimit(Clause clause,
                                   AlgernonKBSet kbSet,
                                   int compileMode,
                                   BindingList bindings,
                                   Map argumentTypes,
                                   Rule rule,
                                   AlgernonCompilerState compileState,
                                   LispValue code,
                                   ErrorSet compileErrors)
                            throws AlgernonException
This compiles the :LIMIT macro. Execute the clause in a subroutine, and before doing a MERGE_BINDINGS, limit the number of results to the number given. Format is (:LIMIT number )

Throws:
AlgernonException

compileOpPrint

protected LispValue compileOpPrint(Clause clause,
                                   AlgernonKBSet kbSet,
                                   int index,
                                   BindingList bindings,
                                   Map argumentTypes,
                                   Rule rule,
                                   AlgernonCompilerState compileState,
                                   LispValue code,
                                   ErrorSet compileErrors,
                                   boolean newline)
                            throws AlgernonCompilerException
compileOpPrint - compiles the PRINT and PRINTLN macros.

Throws:
AlgernonCompilerException

compileOpForcClass

protected LispValue compileOpForcClass(Clause clause,
                                       AlgernonKBSet kbSet,
                                       int compileMode,
                                       BindingList bindings,
                                       Map argumentTypes,
                                       Rule rule,
                                       AlgernonCompilerState compileState,
                                       LispValue code,
                                       ErrorSet compileErrors)
                                throws AlgernonException
Throws:
AlgernonException

compileOpForcInstance

protected LispValue compileOpForcInstance(Clause clause,
                                          AlgernonKBSet kbSet,
                                          int compileMode,
                                          BindingList bindings,
                                          Map argumentTypes,
                                          Rule rule,
                                          AlgernonCompilerState compileState,
                                          LispValue code,
                                          ErrorSet compileErrors)
                                   throws AlgernonException
Throws:
AlgernonException

compileOpAddInstance

protected LispValue compileOpAddInstance(Clause clause,
                                         AlgernonKBSet kbSet,
                                         int compileMode,
                                         BindingList bindings,
                                         Map argumentTypes,
                                         Rule rule,
                                         AlgernonCompilerState compileState,
                                         LispValue code,
                                         ErrorSet compileErrors)
                                  throws AlgernonException
Throws:
AlgernonException

compileOpOr

protected LispValue compileOpOr(Clause clause,
                                AlgernonKBSet kbSet,
                                int compileMode,
                                BindingList bindings,
                                Map argumentTypes,
                                Rule rule,
                                AlgernonCompilerState compileState,
                                LispValue code,
                                ErrorSet compileErrors)
                         throws AlgernonException
Throws:
AlgernonException

compileOpTheClass

protected LispValue compileOpTheClass(Clause clause,
                                      AlgernonKBSet kbSet,
                                      int compileMode,
                                      BindingList bindings,
                                      Map argumentTypes,
                                      Rule rule,
                                      AlgernonCompilerState compileState,
                                      LispValue code,
                                      ErrorSet compileErrors)
                               throws AlgernonException
Throws:
AlgernonException

compileOpTheInstance

protected LispValue compileOpTheInstance(Clause clause,
                                         AlgernonKBSet kbSet,
                                         int compileMode,
                                         BindingList bindings,
                                         Map argumentTypes,
                                         Rule rule,
                                         AlgernonCompilerState compileState,
                                         LispValue code,
                                         ErrorSet compileErrors)
                                  throws AlgernonException
Throws:
AlgernonException

preprocessPath

public Path preprocessPath(Path path)
                    throws AlgernonException
Preprocesses a path

Throws:
AlgernonException

preprocessClause

public Clause preprocessClause(Clause clause)
                        throws AlgernonException
Preprocesses a clause.

Throws:
AlgernonException

validateArgumentTypes

protected void validateArgumentTypes(Clause clause,
                                     AlgernonKBSet kbSet,
                                     Map argumentTypes,
                                     AlgernonCompilerState compileState,
                                     ErrorSet compileErrors)
                              throws AlgernonDataException
validates that arguments are of the correct type. May add new argument types to the incoming list.

Throws:
AlgernonDataException

validateSpecialFormArgumentTypes

protected void validateSpecialFormArgumentTypes(Clause form,
                                                AlgernonKBSet kbSet,
                                                Map argumentTypes,
                                                AlgernonCompilerState compileState,
                                                ErrorSet compileErrors)
                                         throws AlgernonDataException
Validates argument types for macros like :THE, :A and :FORC, which have different forms of arguments.

Throws:
AlgernonDataException

validateMacroArgumentTypes

protected void validateMacroArgumentTypes(Clause form,
                                          AlgernonKBSet kbSet,
                                          Map argumentTypes,
                                          AlgernonCompilerState compileState,
                                          ErrorSet compileErrors)
                                   throws AlgernonDataException
Validates argument types for macros like :INSTANCE, :CLASS, and so on.

Throws:
AlgernonDataException

lookupArgumentType

protected Set lookupArgumentType(LispValue argument,
                                 Map argumentTypes)
Returns a set of argument types for an argument.

Parameters:
argument -
argumentTypes -
Returns:
a Set of argument types for a variable.

typeEQ

protected boolean typeEQ(AlgernonKBSet kbSet,
                         LispValue argument,
                         Set type,
                         Map argumentTypes,
                         Clause form,
                         AlgernonCompilerState compileState,
                         ErrorSet compileErrors)
                  throws AlgernonDataException
Checks for equality and stores the type, if not previously known. Also will restrict a type if the clause is an ISA or member clause and the arg in question is in the appropriate position in the clause Returns true if the type of the argument matches the given type.

Parameters:
type - a Set of types for the argument.
argumentTypes - a map of argument types for all vars in the form.
Throws:
AlgernonDataException

isFrameType

public boolean isFrameType(Set variableType)

isSimpleType

public boolean isSimpleType(LispValue type)
Returns true if the type is not a frame type.


isSimpleType

public boolean isSimpleType(Set types)
Returns true if the type set contains only simple types.


isOfSimpleType

public boolean isOfSimpleType(LispValue arg,
                              Set type)
Returns true if the argument satisfies the simple type.


typesIntersect

public boolean typesIntersect(AlgernonKBSet kbSet,
                              Set types1,
                              Set types2)
                       throws AlgernonDataException
Returns true if the two Sets of types intersect. Typically the two sets are fairly small.

Throws:
AlgernonDataException

typeSubsumedBy

public boolean typeSubsumedBy(AlgernonKBSet kbSet,
                              LispValue singleType,
                              Set types)
                       throws AlgernonDataException
Returns true if one frame type is a subclass or equal to any of a set of frame types.

Throws:
AlgernonDataException

constantTypeEQ

protected boolean constantTypeEQ(AlgernonKBSet kbSet,
                                 LispValue argument,
                                 Set type,
                                 Clause form,
                                 AlgernonCompilerState compileState,
                                 ErrorSet compileErrors)
                          throws AlgernonDataException
Parameters:
type - may be either a list or a single symbol.
Throws:
AlgernonDataException

implicitTypeRestrict

protected void implicitTypeRestrict(AlgernonKBSet kbSet,
                                    Clause clause,
                                    Map argumentTypes,
                                    AlgernonCompilerState compileState,
                                    ErrorSet compileErrors)
Added 29 Sep 1999 (mh) Added type restriction - if the path contains an ISA or MEMBER clause, this information is used to restrict the type to a new type, eliminating some unnecessary warning messages The clause should NOT be negated.


undefinedRelationError

public String undefinedRelationError(Clause clause,
                                     LispValue slot)

relationLengthMismatchError

public String relationLengthMismatchError(int type,
                                          Clause clause,
                                          LispValue slot)

undeterminedDomainWarning

public String undeterminedDomainWarning(Clause clause,
                                        LispValue argument,
                                        Set type)

domainMismatchWarning

public String domainMismatchWarning(Clause clause,
                                    LispValue argument,
                                    Set variableType,
                                    Set desiredType)
Returns an error about type mismatch on variables.


unrecognizedOperatorError

public String unrecognizedOperatorError(Clause clause,
                                        LispValue slot)
Returns an error string about an unrecognized command used in an Algernon clause.


pathRestrictionWarning

public String pathRestrictionWarning(Clause clause,
                                     Rule rule)
Returns a warning about path restriction.


deprecationWarning

public String deprecationWarning(LispValue oldOperator,
                                 String message,
                                 LispValue clause)
General deprecation warning for old operators.


preprocessTaxonomy

protected Path preprocessTaxonomy(AlgernonKBSet kbSet,
                                  LispValue form)
                           throws AlgernonDataException
preprocesses the :TAXONOMY form to turn it into a set of :THE-CLASS and :THE-INSTANCE commands.

Throws:
AlgernonDataException

doTaxonomyPreprocess

protected LispValue doTaxonomyPreprocess(LispValue superclass,
                                         LispValue forms,
                                         BindingList bindings)
This turns the TAXONOMY form into the :CLASS and :INSTANCE pseudo-code that is processed by makeTaxonomyCommands.

Parameters:
superclass - the superclass of the forms
forms - the forms to be processed
bindings - Bindings from previous forms.
Returns:
a list of :CLASS, :INSTANCE and :COMPLETE forms
See Also:
makeTaxonomyCommands(org.algernon.kb.AlgernonKBSet, org.jatha.dynatype.LispValue, org.algernon.datatype.BindingList)

makeTaxonomyCommands

protected Path makeTaxonomyCommands(AlgernonKBSet kbSet,
                                    LispValue pseudoTax,
                                    BindingList bindings)
                             throws AlgernonDataException
Converts pseudo-taxonomy commands into real taxonomy commands. Given a list of pseudo-comamnds like (:CLASS new-class super-class), returns the actual Algernon command(s) to create the taxonomy.

Throws:
AlgernonDataException

compileRule

public Collection compileRule(Rule rule,
                              AlgernonKBSet kbSet,
                              BindingList bindings,
                              Map argTypes,
                              AlgernonCompilerState compileState,
                              int startIndex,
                              ErrorSet errors)
Compiles a rule. Index is the index of the first antecedent clause in the original rule. It is used to generate continuation commands.

Parameters:
rule -
kbSet -
bindings -
compileState -
startIndex -
errors -
Returns:
A collection of code sets for different combinations of variable bindings.

generateRuleName

protected String generateRuleName(Rule rule,
                                  BindingList variableSet)
Generates a fake rule name based on the set of bound variables.


generateVariableSets

protected Collection generateVariableSets(Clause clause)
                                   throws AlgernonDataException
When a backward chaining rule is activated, some number of variables in the key will be bound. This method generates subsets of variables that might be bound so we can pre-generate all the needed compiled code. At runtime we may run into a combination that was not anticipated. In that case a rule variation is compiled at runtime. Returns a Collection of BindingList instances.

Throws:
AlgernonDataException
See Also:
Collection, BindingList

initializeArgumentTypes

public void initializeArgumentTypes(AlgernonKBSet kbSet,
                                    Map typeList,
                                    Path path,
                                    AlgernonCompilerState compileState,
                                    ErrorSet errors,
                                    boolean clear)
                             throws AlgernonDataException
The compiler type-checks each clause. Because we generate several continuation rules from the user-defined rule, we need the types of all the variables so that the type-checking is done correctly. (The continuations are made from shorter and shorter antecedents, so later on the earlier clauses aren't available to deduce types from.

Throws:
AlgernonDataException

generateRuleCode

public LispValue generateRuleCode(Rule rule,
                                  AlgernonKBSet kbSet,
                                  int direction,
                                  Path antecedent,
                                  Path consequent,
                                  int index,
                                  Rule rootRule,
                                  BindingList boundVariables,
                                  Map argumentTypes,
                                  ErrorSet errors)
Generates the actual code for the rule.


hasInvalidArgumentCount

private boolean hasInvalidArgumentCount(LispValue command,
                                        int numberOfArguments,
                                        int requiredNumber)
Checks the argument count against the requirements. If not correct, Inserts an error in the error list and returns true. Otherwise, returns false. A negative number of required arguments means that a variable number is fine.
   ZERO_OR_MORE means 0 or more.
   -1 means 1 or more,
   -2 means 2 or more, etc.
 


trace

private void trace(String message)

setDebug

public void setDebug(boolean state)
Set to true to turn on compiler debug mode. This is done with (:TRACE :DEBUG-COMPILER-ON) and (:TRACE :DEBUG-COMPILER-OFF)

Parameters:
state -

Algernon Software