package interpres.language;

import java.util.Arrays;

import interpres.ast.Symbol;
import interpres.ast.ListExpression;
import interpres.ast.QuoteExpression;

import interpres.language.values.Value;
import interpres.language.values.Integer;

public class SymbolResolver {
  private DefinitionTable definitionTable;

  public SymbolResolver(DefinitionTable definitionTable) {
    this.definitionTable = definitionTable;
  }

  public Value resolve(Symbol symbol) {
    String symbolName = symbol.getName();

    if (definitionTable.contains(symbolName))
      return definitionTable.lookup(symbolName);

    if (symbol.quote() instanceof Integer)
      return this.asInteger(symbol);

    // FIXME: Throw exception here?
    System.err.println("Couldn't resolve definition for " + symbolName);

    return null;
  }

  public Value asInteger(Symbol symbol) {
    return ListExpression.buildFunctionCall(
      "asm.loadl", new QuoteExpression(symbol)
    ).evaluate(definitionTable);
  }
}

@@ -310,16 +310,222 @@
 Table;%0A%0A
+  public static class IrresolvableSymbolException extends RuntimeException %7B%0A    public IrresolvableSymbolException(Symbol symbol) %7B%0A      super(%22Could not resolve symbol: %22 + symbol.getName());%0A    %7D%0A  %7D%0A%0A
   public
@@ -668,51 +668,8 @@
 ) %7B%0A
-    String symbolName = symbol.getName();%0A%0A
     
@@ -703,20 +703,16 @@
 s(symbol
-Name
 ))%0A     
@@ -744,28 +744,24 @@
 ookup(symbol
-Name
 );%0A%0A    if (
@@ -841,129 +841,53 @@
     
-// FIXME: Throw exception here?%0A    System.err.println(%22Couldn't resolve definition for %22 + symbolName);%0A%0A    return null
+throw new IrresolvableSymbolException(symbol)
 ;%0A  

Throw IrresolvableSymbolException in SymbolResolver