2019-12-28 21:29:41 +00:00
|
|
|
/**
|
|
|
|
* Serene (simple) - A PoC lisp to collect data on Serenes concepts
|
|
|
|
* Copyright (C) 2019-2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-12-10 00:02:24 +00:00
|
|
|
package serene.simple;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.Console;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
public class Main {
|
2019-12-28 21:29:41 +00:00
|
|
|
private static String licenseHeader = "\nSerene(simple), Copyright (C) 2019-2020" +
|
|
|
|
"Sameer Rahmani <lxsameer@gnu.org>\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";
|
2019-12-28 12:44:49 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
if (args.length == 0) {
|
|
|
|
startRepl();
|
|
|
|
return;
|
2019-12-10 00:02:24 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
runSerene(args[0]);
|
|
|
|
}
|
2019-12-10 00:02:24 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
private static void startRepl() throws IOException {
|
|
|
|
BaseScope rootScope = new RootScope();
|
2019-12-10 00:02:24 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
Console console = System.console();
|
2019-12-10 00:02:24 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
System.out.println("Serene 'simple' v0.1.0");
|
2019-12-28 21:29:41 +00:00
|
|
|
System.out.println(licenseHeader);
|
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
while(true) {
|
|
|
|
String inputData = console.readLine("serene-> ");
|
|
|
|
//String inputData = "(fn (x) x)";
|
2019-12-10 00:02:24 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
if (inputData == null) break;
|
2019-12-18 17:45:43 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
|
2019-12-10 17:28:58 +00:00
|
|
|
|
2019-12-18 14:02:04 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
try {
|
|
|
|
ListNode<Node> nodes = Reader.read(inputStream);
|
|
|
|
Object result = ListNode.EMPTY;
|
2019-12-10 17:28:58 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
for (Node n : nodes) {
|
|
|
|
result = n.eval(rootScope);
|
|
|
|
}
|
2019-12-15 21:31:25 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
if (result != ListNode.EMPTY) {
|
|
|
|
System.out.print(";; ");
|
|
|
|
if (result == null) {
|
|
|
|
System.out.println("nil");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.out.println(result.toString());
|
|
|
|
}
|
2019-12-10 00:02:24 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
}
|
|
|
|
catch(Exception e) {
|
|
|
|
System.out.println("Error: ");
|
|
|
|
e.printStackTrace(System.out);
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:02:24 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
}
|
2019-12-10 00:02:24 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
private static void runSerene(String filePath) throws IOException {
|
|
|
|
BaseScope rootScope = new RootScope();
|
2019-12-10 00:02:24 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
ListNode<Node> nodes = Reader.read(new FileInputStream(filePath));
|
2019-12-10 00:02:24 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
for (Node n : nodes) {
|
|
|
|
n.eval(rootScope);
|
2019-12-10 00:02:24 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
}
|
2019-12-10 00:02:24 +00:00
|
|
|
}
|