/** * Serene (simple) - A PoC lisp to collect data on Serenes concepts * Copyright (C) 2019-2020 Sameer Rahmani * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ package serene.simple; import java.io.IOException; import java.io.ByteArrayInputStream; import java.io.Console; import java.io.FileInputStream; public class Main { private static String licenseHeader = "\nSerene(simple), Copyright (C) 2019-2020" + "Sameer Rahmani \n" + "Serene(simple) comes with ABSOLUTELY NO WARRANTY;\n" + "This is free software, and you are welcome\n" + "to redistribute it under certain conditions; \n" + "for details take a look at the LICENSE file.\n"; private static void isJava8() throws RuntimeException { if (System.getProperty("java.vm.specification.version").equals("1.8")) throw new RuntimeException("Minimume required version of JDK is 1.8"); } public static void main(String[] args) throws IOException, RuntimeException { Main.isJava8(); if (args.length == 0) { startRepl(); return; } runSerene(args[0]); } private static void startRepl() throws IOException { BaseScope rootScope = new RootScope(); Console console = System.console(); System.out.println("Serene 'simple' v0.1.0"); System.out.println(licenseHeader); while(true) { String inputData = console.readLine("serene-> "); //String inputData = "(fn (x) x)"; if (inputData == null) break; ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes()); try { ListNode nodes = Reader.read(inputStream); Object result = ListNode.EMPTY; for (Node n : nodes) { result = n.eval(rootScope); } if (result != ListNode.EMPTY) { System.out.print(";; "); if (result == null) { System.out.println("nil"); } else { System.out.println(result.toString()); } } } catch(Exception e) { System.out.println("Error: "); e.printStackTrace(System.out); } } } private static void runSerene(String filePath) throws IOException { BaseScope rootScope = new RootScope(); ListNode nodes = Reader.read(new FileInputStream(filePath)); for (Node n : nodes) { n.eval(rootScope); } } }