public override string ToString() { string predicateStr = _name; // For testing purposes, this can be fixed by implementing WriteTerm if (_name == "=") { predicateStr = ((PrologCodeTerm)_arguments[0]).ToString() + " = " + ((PrologCodeTerm)_arguments[1]).ToString(); } else { if (_arguments.Count != 0) { predicateStr += "("; for (int i = 0; i < _arguments.Count; i++) { PrologCodeTerm term = (PrologCodeTerm)_arguments[i]; predicateStr += term.ToString(); if (i != _arguments.Count - 1) { predicateStr += ","; } } predicateStr += ")"; } } return(predicateStr); }
public static bool IsList(PrologCodeTerm term) { if(term is PrologCodeNonEmptyList || term is PrologCodeEmptyList) { return true; } return false; }
public static bool IsList(PrologCodeTerm term) { if (term is PrologCodeNonEmptyList || term is PrologCodeEmptyList) { return(true); } return(false); }
public static bool IsVariable(PrologCodeTerm term) { if (term is PrologCodeVariable) { return(true); } return(false); }
public static bool IsAtom(PrologCodeTerm term) { if (term is PrologCodeAtom) { return(true); } return(false); }
public static bool IsAtom(PrologCodeTerm term) { if(term is PrologCodeAtom) { return true; } return false; }
public static bool IsVariable(PrologCodeTerm term) { if(term is PrologCodeVariable) { return true; } return false; }
public static bool IsAtomicPredicate(PrologCodeTerm term) { if (term is PrologCodeClause) { return(((PrologCodeClause)term).Arity == 0); } if (term is PrologCodeFact) { return(((PrologCodeFact)term).Arity == 0); } if (term is PrologCodePredicate) { return(((PrologCodePredicate)term).Arity == 0); } return(false); }
public static bool IsStruct(PrologCodeTerm term) { if (term is PrologCodeClause) { return(((PrologCodeClause)term).Arity > 0); } if (term is PrologCodeFact) { return(((PrologCodeFact)term).Arity > 0); } if (term is PrologCodePredicate) { return(((PrologCodePredicate)term).Arity > 0); } return(false); }
public static bool IsStruct(PrologCodeTerm term) { if(term is PrologCodeClause) { return ((PrologCodeClause)term).Arity > 0; } if(term is PrologCodeFact) { return ((PrologCodeFact)term).Arity > 0; } if(term is PrologCodePredicate) { return ((PrologCodePredicate)term).Arity > 0; } return false; }
public static bool IsAtomicPredicate(PrologCodeTerm term) { if(term is PrologCodeClause) { return ((PrologCodeClause)term).Arity == 0; } if(term is PrologCodeFact) { return ((PrologCodeFact)term).Arity == 0; } if(term is PrologCodePredicate) { return ((PrologCodePredicate)term).Arity == 0; } return false; }
public override string ToString() { string clauseStr = _head.ToString() + " :- "; for (int i = 0; i < _goals.Count; i++) { PrologCodeTerm term = (PrologCodeTerm)_goals[i]; clauseStr += term.ToString(); if (i == _goals.Count - 1) { clauseStr += " ."; } else { clauseStr += ", "; } } return(clauseStr); }
public PrologCodeNonEmptyList(PrologCodeTerm head, PrologCodeTerm tail) { this._head = head; this._tail = tail; }
/// <summary> /// Appends an item to the list. /// </summary> /// <param name="item">item to append.</param> public virtual void Append(PrologCodeTerm item) { }
public Record() { Term = null; TemporaryIndex = -1; }
private bool ResolveConflicts(PrologCodeTerm term, int index) { PrologVariableDictionaryEntry entry = _dictionary.GetVariable(index); if (_dictionary.CurrentGoalIndex != 1 || entry == null || entry.LastGoalArgument < index) { PrologRegisterTable.Instance.AllocateRegister(index); return false; } if (term is PrologCodePredicate) { PrologCodePredicate predicate = (PrologCodePredicate)term; for (int i = index + 1; i < entry.LastGoalArgument; i++) { if (predicate.Name == entry.Name && (_dictionary.GetVariable(i) == null || ResolveConflicts(term, i))) { entry.TemporaryIndex = i; _generator.Emit(OpCodes.Put_Value, X(index), X(i)); return true; } } // I WAS HERE } //return true; THIS WAS THERE entry.TemporaryIndex = PrologRegisterTable.Instance.FindRegister(); _generator.Emit(OpCodes.Put_Value, X(index), X(entry.TemporaryIndex)); return true; }
/* Initialize temporary goal variables in the dictionary */ private void InitializeGoalTemporaryVariables(PrologCodeTerm goal) { /* Free all registers */ PrologRegisterTable.Instance.FreeAllRegisters(); int reg = 0; if (goal is PrologCodePredicate) { PrologCodePredicate g = (PrologCodePredicate)goal; if (g.Arity > 0) { foreach (PrologCodeTerm var in g.Arguments) { if (var is PrologCodeVariable) { PrologVariableDictionaryEntry entry = _dictionary.GetVariable(((PrologCodeVariable)var).Name); if (entry != null) { if (entry.IsTemporary && entry.TemporaryIndex == -1) { _dictionary.AllocateTemporaryVariable(entry, reg); } } reg++; } } } } }
private void CompileMethod(PrologCodeTerm method) { PrologCodePredicate predicate = (PrologCodePredicate)method; _generator.EmitFCall(predicate.MethodInfo.PredicateName, predicate.MethodInfo.MethodName, predicate.MethodInfo.AssemblyName, predicate.MethodInfo.Class); if (_dictionary.InLastGoal) { if (_dictionary.GoalCount > 2) { _generator.Emit(OpCodes.Deallocate); } // Emit 'proceed' _generator.EndProcedure(); } }
private void CompileGoal(PrologCodeTerm goal, ArrayList instructions) { if (goal is PrologCodePredicate && ((PrologCodePredicate)goal).Arity == 0) { PrologCodePredicate goalPredicate = (PrologCodePredicate)goal; if (goalPredicate.Arity == 0) { if (goalPredicate.Name == "!") { _generator.Emit(OpCodes.Cut); if (_dictionary.InLastGoal) { _generator.Emit(OpCodes.Deallocate); _generator.EndProcedure(); } return; } if (goalPredicate.Name == "fail") { _generator.Emit(OpCodes.Fail); _generator.EndProcedure(); return; } // TODO: handle methods here... CompileCall(goalPredicate); } } else if (goal is PrologCodeVariable) { CompileGoalVariable((PrologCodeVariable)goal, 0); if (_dictionary.InLastGoal) { if (_dictionary.CurrentGoalIndex > 1) { _generator.Emit(OpCodes.Deallocate); } _generator.EmitExecuteVar(((PrologCodeVariable)goal).Name, 0); } else { _generator.EmitCallVar(((PrologCodeVariable)goal).Name, 0); } } else if (goal is PrologCodeNonEmptyList) { // TODO: compile list arguments, then call ./2 } else if (goal is PrologCodeIntegerAtom || goal is PrologCodeFloatAtom) { throw new PrologCompilerException("Clause goal cannot be a number."); } else if (goal is PrologCodePredicate) { CompileGoalArguments(((PrologCodePredicate)goal).Arguments); CompileCall(goal); } }
private void AddGoalVariables(PrologCodeTerm term) { // no variables to add: predicate/0 if(PrologCodeTerm.IsAtom(term) || PrologCodeTerm.IsAtomicPredicate(term)) { return; } // goal is a variable X else if(PrologCodeTerm.IsVariable(term)) { _currentArgumentIndex = 0; this.Add(((PrologCodeVariable)term).Name,null); return; } // goal is a list, [Term|Term] else if(PrologCodeTerm.IsList(term)) { _currentArgumentIndex = 0; if(term is PrologCodeNonEmptyList) { PrologCodeNonEmptyList list = (PrologCodeNonEmptyList)term; AddGoalArgumentVariables(list.Head); _currentArgumentIndex = 1; if(list.Tail != null) { if(list.Tail is PrologCodeNonEmptyList) { AddGoalArgumentVariables(list.Tail); } else { AddGoalArgumentVariables(list.Tail); } } } } // Goal is a predicate, term(term,...) else if(PrologCodeTerm.IsStruct(term)) { _currentArgumentIndex = 0; PrologCodePredicate goal = (PrologCodePredicate)term; foreach(PrologCodeTerm argument in goal.Arguments) { AddGoalArgumentVariables(argument); _currentArgumentIndex++; } } }
private void DefineNewOperator(int pri, string assoc, PrologCodeTerm op) { // Check operator name string opValue = ""; if (op is PrologCodeConstantAtom) { opValue = ((PrologCodeConstantAtom)op).Value; } else if (op is PrologCodeStringAtom) { opValue = ((PrologCodeStringAtom)op).Value; } else { _errors.Add(new PrologCompilerError("P0011", "Invalid operator definition.", "", false, _scanner.Current.Line, _scanner.Current.Column)); return; } if (opValue == "," || opValue == "','") { _errors.Add(new PrologCompilerError("P0011", "Invalid operator definition.", "", false, _scanner.Current.Line, _scanner.Current.Column)); return; } // Check operator priority if (pri > 0 && pri < 1200) { UpdateOperatorTable(pri, assoc, opValue); } else if (pri == 0) { // Remove an operator _operators.RemoveOperator(opValue); } else { // Error } }
public PrologCodeNonEmptyList(PrologCodeTerm head) { this._head = head; this._tail = null; }
private string GetAtomOrStringValue(PrologCodeTerm term) { if (term is PrologCodeConstantAtom) { return ((PrologCodeConstantAtom)term).Value; } else if (term is PrologCodeStringAtom) { return ((PrologCodeStringAtom)term).Value.Replace("'", ""); } return null; }
private int CompileGoalRecord(PrologCodeTerm term, int index) { ArrayList recs = new ArrayList(); int nRecs = 0; ArrayList arguments = new ArrayList(); if (term is PrologCodeNonEmptyList) { PrologCodeNonEmptyList NEList = (PrologCodeNonEmptyList)term; arguments.Add(NEList.Head); arguments.Add(NEList.Tail); } else if (term is PrologCodePredicate) { arguments = ((PrologCodePredicate)term).Arguments; } for (int i = 0; i < arguments.Count; i++) { PrologCodeTerm r = (PrologCodeTerm)arguments[i]; if (r is PrologCodeNonEmptyList || r is PrologCodePredicate) { nRecs = recs.Add(CompileGoalRecord(r, -1)); } } if (index == -1) { index = PrologRegisterTable.Instance.FindRegister(); } if (term is PrologCodeNonEmptyList) { _generator.Emit(OpCodes.Put_List, X(index)); } else if(term is PrologCodePredicate) { PrologCodePredicate s = (PrologCodePredicate)term; _generator.Emit(OpCodes.Put_Structure, s.Name + "/" + s.Arity, X(index)); } nRecs = 0; for (int i = 0; i < arguments.Count; i++) { PrologCodeTerm t = (PrologCodeTerm)arguments[i]; if (t is PrologCodeNilAtom || t is PrologCodeEmptyList) { _generator.Emit(OpCodes.Set_Constant, "[]"); } else if (t is PrologCodeAtom) { if (t is PrologCodeConstantAtom) { _generator.Emit(OpCodes.Set_Constant, ((PrologCodeConstantAtom)t).Value); } else if (t is PrologCodeStringAtom) { _generator.Emit(OpCodes.Set_Constant, ((PrologCodeStringAtom)t).Value); } else if (t is PrologCodeIntegerAtom) { _generator.Emit(OpCodes.Set_Constant, ((PrologCodeIntegerAtom)t).Value.ToString()); } else if (t is PrologCodeFloatAtom) { _generator.Emit(OpCodes.Set_Constant, ((PrologCodeFloatAtom)t).Value.ToString()); } } else if (t is PrologCodeVariable) { /* Compile Goal record variable */ CompileGoalRecordVariable((PrologCodeVariable)t); } else if (t is PrologCodePredicate || t is PrologCodeNonEmptyList) { _generator.Emit(OpCodes.Set_Value, X((int)recs[nRecs])); PrologRegisterTable.Instance.FreeRegister((int)recs[nRecs]); nRecs++; } } return index; }
public override void Append(PrologCodeTerm item) { }
private void ProcessHeadlessClause(PrologCodeTerm term) { PrologCodeHeadlessClause clause = (PrologCodeHeadlessClause)term; foreach (PrologCodeTerm t in clause.Goals) { if (t is PrologCodePredicate) { PrologCodePredicate pred = (PrologCodePredicate)t; switch (pred.Name) { case "op": ProcessOperator(pred); break; case "class": PrologCodeTerm arg = (PrologCodeTerm)pred.Arguments[0]; if (arg is PrologCodeConstantAtom) { PrologCodeConstantAtom atom = (PrologCodeConstantAtom)arg; _codeUnit.Class = atom.Value; } else if (arg is PrologCodeStringAtom) { PrologCodeStringAtom atom = (PrologCodeStringAtom)arg; _codeUnit.Class = atom.Value.Replace("'", ""); } else { _errors.Add(new PrologCompilerError("P0002", "Illegal class name.", "", false, _scanner.Current.Line, _scanner.Current.Column)); } break; case "foreign": ProcessForeignMethod(pred); break; case "load_assembly": ProcessAssemblyDirective(pred); break; case "using": ProcessUsingDirective(pred); break; } } else if (t is PrologCodeList) { } } }
private void CompileCall(PrologCodeTerm p) { AMPredicateSet builtins = AMPredicateSet.Instance; PrologCodePredicate predicate = (PrologCodePredicate)p; if (builtins.IsBuiltin(predicate.Name, predicate.Arity)) { CompileBuiltinPredicateCall(predicate); } else if (predicate.IsMethod) { CompileMethod(predicate); } else { CompilePrologPredicateCall(predicate); } }
private void CompileClauseHead(PrologCodeTerm head, ArrayList instructions) { if (head is PrologCodePredicate) { PrologCodePredicate headPredicate = (PrologCodePredicate)head; if (headPredicate.Arity == 0) { /* Do nothing */ } else { CompileHeadArguments(((PrologCodePredicate)head).Arguments); } } else if (head is PrologCodeNonEmptyList) { ArrayList headListArguments = new ArrayList(); PrologCodeNonEmptyList NEList = (PrologCodeNonEmptyList)head; headListArguments.Add(NEList.Head); headListArguments.Add(NEList.Tail); CompileHeadArguments(headListArguments); } else if (head is PrologCodeVariable) { throw new PrologCompilerException("Clause head cannot be a variable."); } else if (head is PrologCodeIntegerAtom || head is PrologCodeFloatAtom) { throw new PrologCompilerException("Clause head cannot be a number."); } }
private void AddGoalStructArgumentVariables(PrologCodeTerm term) { if (PrologCodeTerm.IsAtom(term)) { return; } else if (PrologCodeTerm.IsVariable(term)) { this.Add(((PrologCodeVariable)term).Name, null); } else if (PrologCodeTerm.IsList(term)) { if (term is PrologCodeNonEmptyList) { PrologCodeNonEmptyList list = (PrologCodeNonEmptyList)term; AddGoalStructArgumentVariables(list.Head); if (list.Tail is PrologCodeNonEmptyList) { AddGoalStructArgumentVariables(list.Tail); } else { AddGoalStructArgumentVariables(list.Tail); } } } else if (PrologCodeTerm.IsStruct(term)) { PrologCodePredicate structure = (PrologCodePredicate)term; foreach (PrologCodeTerm argument in structure.Arguments) { AddGoalStructArgumentVariables(argument); } } }