/*
 *  Copyright (C) 2004-2011 by ELIGIS.
 *  All Rights Reserved.
 *
 *  N O T I C E
 *
 *  THIS MATERIAL IS CONSIDERED A TRADE SECRET BY ELIGIS.
 *  UNAUTHORIZED ACCESS, USE, REPRODUCTION OR DISTRIBUTION IS PROHIBITED.
 */

// $Id: Main.java,v 1.24 2013/06/18 05:21:54 jullien Exp $

//
// Main.java:
//

import com.eligis.openlisp.JOpenLisp;
import com.eligis.openlisp.JOpenLispException;
import com.eligis.openlisp.JTerminal;

/**
 * class Main.
 *
 * @author C. Jullien
 * @version 1.10
 */
public class Main extends Object {

  /**
   * print to console.
   * @param o object to be printed.
   */
  private static void print(final Object o) {
    System.out.println(o);
  }

  /**
   * print to console (without newline).
   * @param o object to be printed.
   */
  private static void prin(final Object o) {
    System.out.print(o);
  }

  /**
   * main entry point.
   * @param argv command line arguments.
   */
  public static void main(final String[] argv) {
    System.out.println("Java: Architecture: " + System.getProperty("os.arch"));
    try {
      final JOpenLisp ol = JOpenLisp.getInstance(argv);

      JOpenLisp.setCallBack(JTerminal.class);
      final Thread t = new Thread(ol);
      t.start();
      t.join();
    } catch (InterruptedException e) {
      System.out.println("Java: " + e);
    } catch (JOpenLispException e) {
      throw new RuntimeException(e);
    }
    System.out.println("Java: Done!");
  }

  /**
   * main entry point.
   * @param argv command line arguments.
   */
  public static void mainAlt(final String[] argv) {
    try {
      final String initName = "JOpenLisp.ini";
      boolean initFile;
      final JOpenLisp ol = JOpenLisp.getInstance();

      JOpenLisp.setCallBack(JTerminal.class);

      ol.init();

      float ver = ol.version();
      print("Version: " + ver);

      initFile = ol.load(initName);

      if (initFile) {
        print(initName + " loaded");
      } else {
        print(initName + " not loaded");
      }

      ol.execute("(defglobal x (list 1 2 3 4))");
      ol.execute("(defun f (n) (if (= n 1) 1 (* n (f (1- n)))))");

      print(ol.eval("(length x)"));
      print(ol.eval("(gc)"));
      print(ol.eval("(cadr x)"));
      print(ol.eval("(f 5)"));
      print(ol.eval("(f t)"));
      print(ol.eval("(/ 6.4 2)"));
      print(ol.eval("(append \"foo\" \"-bar\")"));
      print(ol.eval("(elt \"foo\" 1)"));
      print(ol.eval("(consp 2)"));
      print(ol.eval("#(1 #(2 \"foo\" 2.5) 3)"));
      print(ol.eval("(cons 1 '(2 \"foo\" 2.5))"));

      if (initFile) {
        print("fib(20)=" +  ol.eval("(fib 20)"));
      }

      ol.close();

      print("Done JOpenLisp");
    } catch (JOpenLispException e) {
      throw new RuntimeException(e);
    }
  }
}