public void NameTest() { PrologCodeVariable prologCodeVariable = new PrologCodeVariable("X"); string result = prologCodeVariable.Name; Assert.AreEqual("X", result); }
public PrologCodeTerm ConvertBinaryTreeToCodeDOM(BinaryTree tree) { // Clause if (tree.Name == ":-") { PrologCodeClause term = new PrologCodeClause(); if (tree.Left != null) { ArrayList goals = new ArrayList(); tree.Flatten(tree.Right, ref goals); foreach (BinaryTree goal in goals) { term.Goals.Add(ConvertGoalBinaryTreeToCodeDOM(goal)); } term.Head = (PrologCodePredicate)ConvertBinaryTreeToCodeDOM(tree.Left); return(term); } // Headless clause else { // process headless clause here PrologCodeHeadlessClause hClause = new PrologCodeHeadlessClause(); ArrayList goals = new ArrayList(); tree.Flatten(tree.Right, ref goals); foreach (BinaryTree goal in goals) { hClause.Goals.Add(ConvertGoalBinaryTreeToCodeDOM(goal)); } return(hClause); } } else if (tree.Name == ".") { return(ConvertBinaryListToCodeDOM(tree)); } // Variable else if (Char.IsUpper(tree.Name[0])) { if (tree.Left != null || tree.Right != null) { ParserError("Something was not parsed right. Variable has arity > 0", 0, 0); } PrologCodeVariable var = new PrologCodeVariable(tree.Name); return(var); } else { return(ConvertGoalBinaryTreeToCodeDOM(tree)); } //return null; }
private void CompileGoalRecordVariable(PrologCodeVariable var) { PrologVariableDictionaryEntry entry = _dictionary.GetVariable(var.Name); if (entry.IsReferenced) { /* Compile build value here ... */ if (entry.IsTemporary) { if (entry.IsGlobal) { _generator.Emit(OpCodes.Set_Value, X(entry.TemporaryIndex)); } else { _generator.Emit(OpCodes.Set_Local_Value, X(entry.TemporaryIndex)); entry.IsGlobal = true; } } else { if (entry.IsGlobal) { if (entry.TemporaryIndex != -1) { _generator.Emit(OpCodes.Set_Value, X(entry.TemporaryIndex)); } else { _generator.Emit(OpCodes.Set_Value, Y(entry.PermanentIndex)); } } else { if (entry.TemporaryIndex != -1) { _generator.Emit(OpCodes.Set_Local_Value, X(entry.TemporaryIndex)); } else { _generator.Emit(OpCodes.Set_Local_Value, Y(entry.PermanentIndex)); } } } } else { if (entry.IsTemporary) { if (entry.Occurrences == 1) { _generator.Emit(OpCodes.Set_Void, "1"); } else { _generator.Emit(OpCodes.Set_Variable, X(entry.TemporaryIndex)); } } else { _generator.Emit(OpCodes.Set_Variable, Y(entry.PermanentIndex)); } entry.IsGlobal = true; } }
public void GenerateCodeFromClause(PrologCodeClause clause, ArrayList instructions) { /* Do we need to allocate an environment? */ bool hasEnvironment = clause.Goals.Count > 1; /* Initialize variable dictionary */ _dictionary = new PrologVariableDictionary(); /* Build the variable dictionary for this clause */ _dictionary.Build(clause); /* Free all registers */ PrologRegisterTable registers = PrologRegisterTable.Instance; registers.FreeAllRegisters(); /* Prepare head variables for code generation */ int reg = 0; if (clause.Head.Arity > 0) { headArity = clause.Head.Arity; foreach (PrologCodeTerm argument in clause.Head.Arguments) { if (argument is PrologCodeVariable) { PrologCodeVariable var = (PrologCodeVariable)argument; PrologVariableDictionaryEntry entry = _dictionary.GetVariable(var.Name); if (entry != null) { if (entry.IsTemporary && entry.TemporaryIndex == -1) { entry.IsReferenced = true; _dictionary.AllocateTemporaryVariable(entry, reg); } } //BUG: reg++; } reg++; } } /* Prepare first goal variables */ int xreg = 0; PrologCodeTerm fg = null; if (clause.Goals.Count > 0) { fg = (PrologCodeTerm)clause.Goals[0]; } if (fg is PrologCodePredicate) { PrologCodePredicate firstGoal = (PrologCodePredicate)fg; if (firstGoal.Name == "!") { hasEnvironment = true; } if (firstGoal.Arity > 0) { foreach (PrologCodeTerm variable in firstGoal.Arguments) { if (variable is PrologCodeVariable) { PrologVariableDictionaryEntry entry = _dictionary.GetVariable(((PrologCodeVariable)variable).Name); if (entry != null) { if (entry.IsTemporary && entry.TemporaryIndex == -1) { if (!registers.InUse(xreg)) { _dictionary.AllocateTemporaryVariable(entry, xreg); } } } } xreg++; } } } /* Reserve required registers */ for (int i = 0; i < Math.Max(reg, xreg); i++) { registers.AllocateRegister(i); } /* Emit predicate label */ _generator.DeclareProcedure(clause.Head.Name, clause.Head.Arity); /* Allocate an environment if needed */ if (hasEnvironment) { _generator.Emit(OpCodes.Allocate); } /* Compile clause head */ CompileClauseHead(clause.Head, instructions); /* Set current goal to 1 */ _dictionary.CurrentGoalIndex = 1; if (clause.Goals.Count == 0) { _generator.EndProcedure(); /* Reset variable dictionary */ _dictionary.Reset(); instructions = _generator.Instructions; return; } /* Compile first goal */ CompileGoal(fg, instructions); _dictionary.CurrentGoalIndex++; /* Compile the rest of the goals */ for (int goalIndex = 1; goalIndex < clause.Goals.Count; goalIndex++) { PrologCodeTerm goal = (PrologCodeTerm)clause.Goals[goalIndex]; InitializeGoalTemporaryVariables(goal); /* reserve registers */ for (int i = 0; i < reg; i++) { registers.AllocateRegister(i); } /* Clear temporary index of permanent variables */ _dictionary.ClearTempIndexOfPermanentVariables(); /* Compile goal */ CompileGoal(goal, instructions); /* Advance to next goal */ _dictionary.CurrentGoalIndex += 1; } /* Reset instruction set, code pointer, and variable * dictionary. */ _dictionary.Reset(); }
private void CompileGoalVariable(PrologCodeVariable var, int i) { PrologVariableDictionaryEntry entry = _dictionary.GetVariable(var.Name); if (entry.IsTemporary) { if (entry.TemporaryIndex != i) { ResolveConflicts(var, i); } if (entry.IsReferenced) { if (entry.TemporaryIndex != i) { _generator.Emit(OpCodes.Put_Value, X(entry.TemporaryIndex), X(i)); } } else { if (entry.TemporaryIndex != i) { _generator.Emit(OpCodes.Put_Variable, X(entry.TemporaryIndex), X(i)); } else { _generator.Emit(OpCodes.Put_Variable, X(i), X(i)); } entry.IsGlobal = true; } } else { ResolveConflicts(var, i); if (entry.IsReferenced) { if (entry.IsUnsafe && !entry.IsGlobal && _dictionary.InLastGoal) { _generator.Emit(OpCodes.Put_Unsafe_Value, Y(entry.PermanentIndex), X(i)); entry.IsUnsafe = false; } else { if (entry.TemporaryIndex != -1) { _generator.Emit(OpCodes.Put_Value, X(entry.TemporaryIndex), X(i)); } else { _generator.Emit(OpCodes.Put_Value, Y(entry.PermanentIndex), X(i)); } } } else { _generator.Emit(OpCodes.Put_Variable, Y(entry.PermanentIndex), X(i)); } if (entry.TemporaryIndex == -1) { entry.TemporaryIndex = i; } else { // No } } }