public void Consult(TextReader inStream)
{
sourceFiles.NoteFile(Prolog.CurrentSourceFile);
var reader = new ISOPrologReader(inStream);
reader.SkipLayout();
int lastLine = reader.LineNumber;
using (var context = PrologContext.Allocate(this, this))
{
try
{
object unexpanded;
Prolog.CurrentSourceLineNumber = lastLine;
while ((unexpanded = reader.ReadTerm()) != Symbol.EndOfFile)
{
// Perform user-level macroexpansion.
object assertion = TermExpansion(unexpanded);
if (ELProlog.IsELTerm(assertion))
// It's an EL term.
ELProlog.Update(assertion, this);
else
{
// It's a normal Prolog term
var t = Term.Structurify(
assertion,
"Assertions in prolog files must be valid propositions or predicates.");
// Perform built-in macroexpansion.
t = t.Expand();
if (t.IsFunctor(Symbol.Implication, 1))
{
context.Reset();
var goal = Term.Structurify(
t.Argument(0),
"Argument to a :- directive must be an atom or structure.");
// Run t once, but don't backtrack for a second solution (since it's presumably an imperative anyway).
Prove(goal.Functor, goal.Arguments, context, 0).GetEnumerator().MoveNext();
}
else
Assert(t, true, true);
}
reader.SkipLayout();
lastLine = reader.LineNumber;
Prolog.CurrentSourceLineNumber = lastLine;
}
}
catch (InferenceStepsExceededException e)
{
Repl.RecordExceptionSourceLocation(e, lastLine);
throw;
}
catch (Exception e)
{
#if !DisableUnity
Debug.LogException(e);
Repl.RecordExceptionSourceLocation(e, lastLine);
#endif
throw new PrologError(
e,
context.StackTrace(Prolog.CurrentSourceFile, Prolog.CurrentSourceLineNumber, "consult/1", false));
}
}
}