source: branches/1.1.x/examples/java-to-lisp-2/Main.java

Last change on this file was 12307, checked in by ehuelsmann, 15 years ago

Complete "catch (Throwable" review by reviewing ../examples.

  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1/*
2 * Main.java
3 *
4 * Copyright (C) 2008 Ville Voutilainen
5 * $Id: Main.java 12307 2009-12-25 21:54:31Z ehuelsmann $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22import org.armedbear.lisp.*;
23
24public class Main
25{
26    /**
27     * This example creates an Interpreter instance, loads our
28     * lisp code from a file and then looks up two functions defined
29     * in the loaded lisp file and executes the functions.
30     *
31     * The first function takes a single parameter and prints its value,
32     * so we can provide any Object, so we use a String.
33     *
34     * The second function takes two numbers, adds them together, prints
35     * the parameters and the result, and returns the result.
36     * We use two integers as parameters and just print the result
37     * from java side.
38     */
39    public static void main(String[] argv)
40    {
41  try
42      {
43    Interpreter interpreter = Interpreter.createInstance();
44    interpreter.eval("(load \"lispfunctions.lisp\")");
45    // the function is not in a separate package, thus the
46    // correct package is CL-USER. Symbol names are
47    // upper case. Package needs the prefix, because java
48    // also has a class named Package.
49    org.armedbear.lisp.Package defaultPackage = 
50        Packages.findPackage("CL-USER");
51
52    Symbol voidsym = 
53        defaultPackage.findAccessibleSymbol("VOID-FUNCTION");
54    Function voidFunction = (Function) voidsym.getSymbolFunction();
55    voidFunction.execute(new JavaObject("String given from java"));
56
57    Symbol intsym = 
58        defaultPackage.findAccessibleSymbol("INT-FUNCTION");
59    Function intFunction = (Function) intsym.getSymbolFunction();
60    LispObject result = 
61        intFunction.execute(new JavaObject(1), 
62          new JavaObject(6));
63    System.out.print("The result on the java side: ");
64    System.out.println(result.intValue());
65      }
66  catch (Throwable t)
67      {
68    System.out.println("exception!");
69    t.printStackTrace();
70      }
71    }
72}
Note: See TracBrowser for help on using the repository browser.