package org.metaborg.meta.lang.dynsem.interpreter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import org.metaborg.meta.lang.dynsem.interpreter.nodes.building.ITermBuildFactory;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.matching.ITermMatchPatternFactory;

public class DynSemContext {

	public static DynSemLanguage LANGUAGE;

	private final BufferedReader input;
	private final PrintWriter output;

	private final ITermRegistry termRegistry;
	private final RuleRegistry ruleRegistry;

	public DynSemContext(ITermRegistry termRegistry, RuleRegistry ruleRegistry) {
		this(termRegistry, ruleRegistry, new BufferedReader(
				new InputStreamReader(System.in)), new PrintWriter(System.out));
	}

	public DynSemContext(ITermRegistry termRegistry, RuleRegistry ruleRegistry,
			BufferedReader input, PrintWriter output) {
		this.termRegistry = termRegistry;
		this.ruleRegistry = ruleRegistry;
		this.input = input;
		this.output = output;
	}

	public RuleRegistry getRuleRegistry() {
		return ruleRegistry;
	}

	public ITermRegistry getTermRegistry() {
		return termRegistry;
	}

	public ITermBuildFactory lookupTermBuilder(String name, int arity) {
		return termRegistry.lookupBuildFactory(name, arity);
	}

	public ITermBuildFactory lookupNativeOpBuilder(String name, int arity) {
		return termRegistry.lookupNativeOpBuildFactory(name, arity);
	}

	public ITermMatchPatternFactory lookupMatchPattern(String name, int arity) {
		return termRegistry.lookupMatchFactory(name, arity);
	}

	public ITermBuildFactory lookupNativeTypeAdapterBuildFactory(String sort,
			String function, int arity) {
		return termRegistry.lookupNativeTypeAdapterBuildFactory(sort, function,
				arity);
	}

	public BufferedReader getInput() {
		return input;
	}

	public PrintWriter getOutput() {
		return output;
	}

}

@@ -1197,38 +1197,53 @@
  int arity) %7B%0A%09%09
-return
+ITermBuildFactory f =
  termRegistry.lo
@@ -1265,32 +1265,64 @@
 y(name, arity);%0A
+%09%09assert f != null;%0A%09%09return f;%0A
 %09%7D%0A%0A%09public ITer
@@ -1377,38 +1377,53 @@
  int arity) %7B%0A%09%09
-return
+ITermBuildFactory f =
  termRegistry.lo
@@ -1448,33 +1448,37 @@
 actory(name,
- 
+%0A%09%09%09%09
 arity);%0A
 %09%7D%0A%0A%09public 
@@ -1457,32 +1457,64 @@
 me,%0A%09%09%09%09arity);%0A
+%09%09assert f != null;%0A%09%09return f;%0A
 %09%7D%0A%0A%09public ITer
@@ -1573,38 +1573,60 @@
  int arity) %7B%0A%09%09
-return
+ITermMatchPatternFactory f =
  termRegistry.lo
@@ -1647,25 +1647,29 @@
 ry(name,
- 
+%0A%09%09%09%09
 arity);%0A
 %09%7D%0A%0A%09pub
@@ -1652,32 +1652,64 @@
 me,%0A%09%09%09%09arity);%0A
+%09%09assert f != null;%0A%09%09return f;%0A
 %09%7D%0A%0A%09public ITer
@@ -1802,30 +1802,45 @@
  arity) %7B%0A%09%09
-return
+ITermBuildFactory f =
  termRegistr
@@ -1877,16 +1877,21 @@
 Factory(
+%0A%09%09%09%09
 sort, fu
@@ -1897,27 +1897,55 @@
 unction,
-%0A%09%09%09%09arity)
+ arity);%0A%09%09assert f != null;%0A%09%09return f
 ;%0A%09%7D%0A%0A%09p

Add assertions to DynSemContext that various factories are found