1 package net.sf.yacas; 2 3 import java.io.*; 4 import java.net.*; 5 import java.nio.file.NoSuchFileException; 6 import java.util.zip.*; 7 8 /** 9 * Use this class in order to access the Yacas interpreter from an external application. 10 * Usage: 11 * import net.sf.yacas.YacasInterpreter; 12 * YacasInterpreter interpreter = new YacasInterpreter(); 13 * String output1 = interpreter.Evaluate("a := 5"); 14 * String output2 = interpreter.Evaluate("Solve(x*x == a, x)"); 15 * 16 * 17 * @author av 18 */ 19 public class YacasInterpreter { 20 21 private CYacas yacas; 22 23 /** Creates a new instance of YacasInterpreter */ YacasInterpreter()24 public YacasInterpreter() throws IOException, ZipException, URISyntaxException { 25 this(new StringWriter()); 26 } 27 YacasInterpreter(Writer out)28 public YacasInterpreter(Writer out) throws IOException, ZipException, URISyntaxException { 29 30 String scriptsDir = "scripts/"; 31 32 yacas = new CYacas(out); 33 34 URL initURL = yacas.getClass().getClassLoader().getResource(scriptsDir + "yacasinit.ys"); 35 36 if (initURL == null) 37 throw new NoSuchFileException("yacasinit.ys not found in " + scriptsDir); 38 39 String initPath = initURL.getPath(); 40 41 if (initPath.lastIndexOf('!') >= 0) { 42 String archive = initPath.substring(0, initPath.lastIndexOf('!')); 43 44 ZipFile z = new ZipFile(new File(new URI(archive))); 45 46 LispStandard.zipFile = z; 47 } else { 48 yacas.Evaluate("DefaultDirectory(\"" + scriptsDir + "\");"); 49 } 50 51 yacas.Evaluate("Load(\"yacasinit.ys\");"); 52 } 53 54 /** Use this method to pass an expression to the Yacas interpreter. 55 * Returns the output of the interpreter. 56 */ Evaluate(String input)57 public String Evaluate(String input) { 58 return yacas.Evaluate(input); 59 } 60 } 61