public static void Rule(Term lhs, Atom atom, OptionalTermBody optionalTermBody) { CodeCompoundTerm codeCompoundTerm; if (optionalTermBody.CodeTerms == null) { CodeFunctor codeFunctor = new CodeFunctor(atom.Text); codeCompoundTerm = new CodeCompoundTerm(codeFunctor); } else { CodeFunctor codeFunctor = new CodeFunctor(atom.Text, optionalTermBody.CodeTerms.Count); if (codeFunctor.Arity == 0) { codeCompoundTerm = new CodeCompoundTerm(codeFunctor); } else { codeCompoundTerm = new CodeCompoundTerm(codeFunctor, optionalTermBody.CodeTerms); } } lhs.CodeCompoundTerm = codeCompoundTerm; }
private void Get(CodeCompoundTerm codeCompoundTerm, WamInstructionRegister sourceRegister) { WamInstructionRegister[] childrenRegisters = new WamInstructionRegister[codeCompoundTerm.Children.Count]; InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.GetStructure, sourceRegister, Functor.Create(codeCompoundTerm.Functor))); for (int idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { CodeTerm child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeVariable) { string variableName = child.AsCodeVariable.Name; WamInstructionRegister variableRegister = GetRegisterAssignment(variableName); if (variableRegister.IsUnused) { variableRegister = GetNextPermanentRegister(variableName); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyUnboundVariable, variableRegister)); } else { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyBoundVariable, variableRegister)); } } else if (child.IsCodeCompoundTerm) { childrenRegisters[idx] = GetNextTemporaryRegister(); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyUnboundVariable, childrenRegisters[idx])); } else if (child.IsCodeValue) { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyValue, WamValue.Create(child.AsCodeValue))); } else { throw new InvalidOperationException("Unsupported codeTerm type."); } } // Build substructures. // for (int idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { CodeTerm child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeCompoundTerm) { Get(child, childrenRegisters[idx]); } } }
public static void Write(CodeTerm codeTerm, int indentation, TextWriter wtr) { CodeCompoundTerm codeCompoundTerm = codeTerm as CodeCompoundTerm; if (codeCompoundTerm != null) { Write(codeCompoundTerm, indentation, wtr); return; } CodeVariable codeVariable = codeTerm as CodeVariable; if (codeVariable != null) { Write(codeVariable, indentation, wtr); return; } CodeValue codeValue = codeTerm as CodeValue; if (codeValue != null) { Write(codeValue, indentation, wtr); return; } }
public static void Rule(Term lhs, Atom atom, OptionalTermBody optionalTermBody) { CodeCompoundTerm codeCompoundTerm; if (optionalTermBody.CodeTerms == null) { CodeFunctor codeFunctor = new CodeFunctor(atom.Text); codeCompoundTerm = new CodeCompoundTerm(codeFunctor); } else { CodeFunctor codeFunctor = new CodeFunctor(atom.Text, optionalTermBody.CodeTerms.Count); if (codeFunctor.Arity == 0) { codeCompoundTerm = new CodeCompoundTerm(codeFunctor); } else { codeCompoundTerm = new CodeCompoundTerm(codeFunctor, optionalTermBody.CodeTerms); } } lhs.CodeCompoundTerm = codeCompoundTerm; }
void Put(CodeCompoundTerm codeCompoundTerm, WamInstructionRegister targetRegister, LibraryList libraries) { var childrenRegisters = new WamInstructionRegister[codeCompoundTerm.Children.Count]; // Build substructures. // for (var idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { var child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeCompoundTerm) { childrenRegisters[idx] = GetNextTemporaryRegister(); Put(child, childrenRegisters[idx], libraries); } } var functor = Functor.Create(codeCompoundTerm.Functor); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.PutStructure, functor, targetRegister)); for (var idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { var child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeVariable) { var variableName = child.AsCodeVariable.Name; var variableRegister = GetRegisterAssignment(variableName); if (variableRegister.IsUnused) { variableRegister = GetNextPermanentRegister(variableName); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.SetUnboundVariable, variableRegister)); } else { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.SetBoundVariable, variableRegister)); } } else if (child.IsCodeCompoundTerm) { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.SetBoundVariable, childrenRegisters[idx])); } else if (child.IsCodeValue) { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.SetValue, WamValue.Create(child.AsCodeValue))); } else { throw new InvalidOperationException("Unsupported codeTerm type."); } } }
public static void Write(CodeCompoundTerm codeCompoundTerm, int indentation, TextWriter wtr) { wtr.WriteLine("{0}{1}/{2} - CodeCompoundTerm", Indentation(indentation), codeCompoundTerm.Functor.Name, codeCompoundTerm.Functor.Arity); foreach (CodeTerm codeTerm in codeCompoundTerm.Children) { Write(codeTerm, indentation + 1, wtr); } }
public static void Write(CodeCompoundTerm codeCompoundTerm, int indentation, TextWriter wtr) { wtr.WriteLine("{0}{1}/{2} - CodeCompoundTerm", Indentation(indentation), codeCompoundTerm.Functor.Name, codeCompoundTerm.Functor.Arity); foreach (var codeTerm in codeCompoundTerm.Children) { Write(codeTerm, indentation + 1, wtr); } }
public static WamCompoundTerm Create(CodeCompoundTerm codeCompoundTerm) { var functor = Functor.Create(codeCompoundTerm.Functor); var result = WamCompoundTerm.Create(functor); for (var index = 0; index < functor.Arity; ++index) { result.Children[index] = WamReferenceTarget.Create(codeCompoundTerm.Children[index]); } return result; }
/// <summary> /// Processes a <see cref="CodeTerm"/> that specifies a work period. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeCompoundTerm"/>.</param> /// <returns>The <see cref="ScheduleShift"/> specified by <paramref name="codeTerm"/>.</returns> /// <remarks> /// Work periods are compound terms of the form <code>workPeriod(day,shift)</code>. /// </remarks> private ScheduleShift ProcessWorkPeriod(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", "codeTerm"); } CodeCompoundTerm codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor != _workPeriodFunctor) { throw new ArgumentException("Functor workPeriod not specified.", "codeTerm"); } string day = ProcessDay(codeCompoundTerm.Children[0]); string shift = ProcessShift(codeCompoundTerm.Children[1]); ScheduleDay scheduleDay; switch (day) { case "monday": scheduleDay = Monday; break; case "tuesday": scheduleDay = Tuesday; break; case "wednesday": scheduleDay = Wednesday; break; case "thursday": scheduleDay = Thursday; break; case "friday": scheduleDay = Friday; break; default: throw new ArgumentException(string.Format("Unknown day {0}.", day), "codeTerm"); } ScheduleShift scheduleShift; switch (shift) { case "first": scheduleShift = scheduleDay.First; break; case "second": scheduleShift = scheduleDay.Second; break; case "third": scheduleShift = scheduleDay.Third; break; default: throw new ArgumentException(string.Format("Unknown shift {0}.", shift), "codeTerm"); } return(scheduleShift); }
public static WamCompoundTerm Create(CodeCompoundTerm codeCompoundTerm) { var functor = Functor.Create(codeCompoundTerm.Functor); var result = WamCompoundTerm.Create(functor); for (var index = 0; index < functor.Arity; ++index) { result.Children[index] = WamReferenceTarget.Create(codeCompoundTerm.Children[index]); } return(result); }
public static void Rule(StatementElement lhs, BinaryElementExpression700 binaryTermExpression) { CodeCompoundTerm codeCompoundTerm = binaryTermExpression.CodeTerm as CodeCompoundTerm; if (codeCompoundTerm == null) { throw new InvalidOperationException("Non-term expression specified."); } lhs.CodeCompoundTerm = codeCompoundTerm; }
CodeTerm ConvertCodeList(CodeList codeList) { var result = codeList.Tail; for (var index = codeList.Head.Count - 1; index >= 0; --index) { result = new CodeCompoundTerm( CodeFunctor.ListFunctor, new[] { codeList.Head[index], result }); } return(result); }
public static void Rule(ListBody lhs, ListItems listItems, OptionalListTail optionalListTail) { CodeTerm tail; if (optionalListTail.CodeTerm != null) { tail = optionalListTail.CodeTerm; } else { tail = new CodeCompoundTerm(CodeFunctor.NilFunctor); } lhs.CodeList = new CodeList(listItems.CodeTerms, tail); }
public static void Rule(ListBody lhs, ListItems listItems, OptionalListTail optionalListTail) { CodeTerm tail; if (optionalListTail.CodeTerm != null) { tail = optionalListTail.CodeTerm; } else { tail = new CodeCompoundTerm(CodeFunctor.NilFunctor); } lhs.CodeList = new CodeList(listItems.CodeTerms, tail); }
public static void Rule(Clause lhs, OptionalProcedureComments optionalProcedureComments, Term term, OptionalRuleBody optionalBody) { IEnumerable<CodeComment> comments = optionalProcedureComments.Comments; if (comments == null) { comments = new CodeComment[] { }; } IEnumerable<CodeCompoundTerm> codeCompoundTerms = optionalBody.CodeCompoundTerms; if (codeCompoundTerms == null) { codeCompoundTerms = new CodeCompoundTerm[] { }; } lhs.CodeSentence = new CodeSentence(comments, term.CodeCompoundTerm, codeCompoundTerms); }
public static void Rule(Clause lhs, OptionalProcedureComments optionalProcedureComments, Term term, OptionalRuleBody optionalBody) { IEnumerable <CodeComment> comments = optionalProcedureComments.Comments; if (comments == null) { comments = new CodeComment[] { }; } IEnumerable <CodeCompoundTerm> codeCompoundTerms = optionalBody.CodeCompoundTerms; if (codeCompoundTerms == null) { codeCompoundTerms = new CodeCompoundTerm[] { }; } lhs.CodeSentence = new CodeSentence(comments, term.CodeCompoundTerm, codeCompoundTerms); }
/// <summary> /// Processes a <see cref="CodeTerm"/> that specifies a person. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeAtom"/>.</param> /// <returns>The person specified by <paramref name="codeTerm"/>.</returns> private string ProcessPerson(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", "codeTerm"); } CodeCompoundTerm codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor.Arity != 0) { throw new ArgumentException("Non-zero functor arity specified.", "codeTerm"); } return(codeCompoundTerm.Functor.Name); }
private void ProcessPragma(CodeSentence codeSentence) { if (codeSentence.Body.Count != 0) { return; } CodeCompoundTerm pragma = codeSentence.Head; CodeCompoundTerm pragmaName = pragma.Children[0].AsCodeCompoundTerm; CodeTerm pragmaArgument = pragma.Children[1]; if (pragmaName != null && pragmaArgument != null) { if (pragmaName.Functor == PragmaOptimizeFunctor) { ProcessOptimizePragma(pragmaArgument); } } }
/// <summary> /// Adds the work period assignment specified by <see cref="CodeTerm"/> to the schedule. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeCompoundTerm"/>.</param> /// <remarks> /// Work periods assignment are compound terms of the form <code>workPeriodAssignment(person,workPeriod(day,shift))</code>. /// </remarks> private void ProcessWorkPeriodAssignment(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", "codeTerm"); } CodeCompoundTerm codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor != _workPeriodAssignmentFunctor) { throw new ArgumentException("Functor workPeriodAssignment not specified.", "codeTerm"); } string person = ProcessPerson(codeCompoundTerm.Children[0]); ScheduleShift scheduleShift = ProcessWorkPeriod(codeCompoundTerm.Children[1]); scheduleShift.Name = person; }
public static WamReferenceTarget Create(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } CodeValue codeValue = codeTerm as CodeValue; if (codeValue != null) { return(WamValue.Create(codeValue)); } CodeCompoundTerm codeCompoundTerm = codeTerm as CodeCompoundTerm; if (codeCompoundTerm != null) { return(WamCompoundTerm.Create(codeCompoundTerm)); } throw new ArgumentException("Invalid CodeTerm type."); }
private CodeTerm ConvertCodeList(CodeList codeList) { CodeTerm result = codeList.Tail; for (int index = codeList.Head.Count - 1; index >= 0; --index) { result = new CodeCompoundTerm( CodeFunctor.ListFunctor, new CodeTerm[] { codeList.Head[index], result }); } return result; }
private void Get(CodeCompoundTerm codeCompoundTerm, WamInstructionRegister sourceRegister) { WamInstructionRegister[] childrenRegisters = new WamInstructionRegister[codeCompoundTerm.Children.Count]; InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.GetStructure, sourceRegister, Functor.Create(codeCompoundTerm.Functor))); for (int idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { CodeTerm child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeVariable) { string variableName = child.AsCodeVariable.Name; WamInstructionRegister variableRegister = GetRegisterAssignment(variableName); if (variableRegister.IsUnused) { variableRegister = GetNextPermanentRegister(variableName); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyUnboundVariable, variableRegister)); } else { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyBoundVariable, variableRegister)); } } else if (child.IsCodeCompoundTerm) { childrenRegisters[idx] = GetNextTemporaryRegister(); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyUnboundVariable, childrenRegisters[idx])); } else if (child.IsCodeValue) { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyValue, WamValue.Create(child.AsCodeValue))); } else { throw new InvalidOperationException("Unsupported codeTerm type."); } } // Build substructures. // for (int idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { CodeTerm child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeCompoundTerm) { Get(child, childrenRegisters[idx]); } } }
public WamInstructionStream Compile(CodeSentence codeSentence, Functor functor, int index, bool isLast, LibraryList libraries, bool optimize) { Initialize(); // When true, indicates we are compiling code for a procedure clause. When false, indicates we // are compiling for an ad hoc query. // bool isClause = (functor != null); if (isClause) { WamInstructionStreamClauseAttribute clauseAttribute = new WamInstructionStreamClauseAttribute( m_instructionStreamBuilder.NextIndex, functor, index); m_instructionStreamBuilder.AddAttribute(clauseAttribute); } if (isClause) { if (isLast) { if (index == 0) { // Procedure only has one clause in it. No retry logic required. } else { TrustMe(); } } else { if (index == 0) { TryMeElse(functor, index + 1); } else { RetryMeElse(functor, index + 1); } } } Allocate(); if (codeSentence.Head != null) { for (int idx = 0; idx < codeSentence.Head.Children.Count; ++idx) { Get(codeSentence.Head.Children[idx], GetArgumentRegister(idx)); } } if (codeSentence.Body.Count > 0) { for (int idxProcedure = 0; idxProcedure < codeSentence.Body.Count; ++idxProcedure) { CodeCompoundTerm codeCompoundTerm = codeSentence.Body[idxProcedure]; for (int idxArgument = 0; idxArgument < codeCompoundTerm.Children.Count; ++idxArgument) { Put(codeCompoundTerm.Children[idxArgument], GetArgumentRegister(idxArgument), libraries); } bool isLastCall = (idxProcedure == codeSentence.Body.Count - 1); if (isClause) { if (isLastCall) { if (optimize && !libraries.Contains(Functor.Create(codeCompoundTerm.Functor)) && codeCompoundTerm.Functor != CodeFunctor.CutFunctor) { Deallocate(); Execute(codeCompoundTerm.Functor); } else { Call(codeCompoundTerm.Functor, libraries); Deallocate(); Proceed(); } } else { Call(codeCompoundTerm.Functor, libraries); } } else // isQuery { Call(codeCompoundTerm.Functor, libraries); if (isLastCall) { Success(); } } } } else // fact { if (isClause) { Deallocate(); Proceed(); } else // isQuery { // No action required. } } return(InstructionStreamBuilder.ToInstructionStream()); }
void Put(CodeCompoundTerm codeCompoundTerm, WamInstructionRegister targetRegister, LibraryList libraries) { var childrenRegisters = new WamInstructionRegister[codeCompoundTerm.Children.Count]; // Build substructures. // for (var idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { var child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeCompoundTerm) { childrenRegisters[idx] = GetNextTemporaryRegister(); Put(child, childrenRegisters[idx], libraries); } } var functor = Functor.Create(codeCompoundTerm.Functor); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.PutStructure, functor, targetRegister)); for (var idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { var child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeVariable) { var variableName = child.AsCodeVariable.Name; var variableRegister = GetRegisterAssignment(variableName); if (variableRegister.IsUnused) { variableRegister = GetNextPermanentRegister(variableName); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.SetUnboundVariable, variableRegister)); } else { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.SetBoundVariable, variableRegister)); } } else if (child.IsCodeCompoundTerm) { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.SetBoundVariable, childrenRegisters[idx])); } else if (child.IsCodeValue) { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.SetValue, WamValue.Create(child.AsCodeValue))); } else { throw new InvalidOperationException("Unsupported codeTerm type."); } } }