Compare commits
21 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
8496c4d3da | ||
![]() |
0566946c73 | ||
![]() |
8f027bace3 | ||
![]() |
d62a50a188 | ||
![]() |
98d5cf9c75 | ||
![]() |
36281fa034 | ||
![]() |
d627f1b1cf | ||
![]() |
4212bec61f | ||
![]() |
3be4ae1941 | ||
![]() |
a9deab12bd | ||
![]() |
a796ff0115 | ||
![]() |
5ac0413e9a | ||
![]() |
5c18c65a9f | ||
![]() |
778554400e | ||
![]() |
10742bd9d7 | ||
![]() |
984fd1c4f7 | ||
![]() |
d62f380a35 | ||
![]() |
5cbacbc251 | ||
![]() |
2e96a26de1 | ||
![]() |
105d868d0f | ||
![]() |
25e5a411a8 |
26 changed files with 513 additions and 32 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -31,3 +31,5 @@ bin/
|
||||||
.class
|
.class
|
||||||
/src/*.jar
|
/src/*.jar
|
||||||
/.idea
|
/.idea
|
||||||
|
hello_world.froj
|
||||||
|
testfile.froj
|
|
@ -28,7 +28,7 @@ Implement some standard functions and method such as:
|
||||||
|
|
||||||
# Hello World
|
# Hello World
|
||||||
## The simplest way to get started:
|
## The simplest way to get started:
|
||||||
* [Download the .jar under releases](https://github.com/jsaasta/Froj/releases/tag/stable)
|
* [Download the .jar under releases](https://github.com/jsaasta/Froj/releases)
|
||||||
* Create file ``helloworld.froj`` with the contents below:
|
* Create file ``helloworld.froj`` with the contents below:
|
||||||
|
|
||||||
print "Hello World";
|
print "Hello World";
|
||||||
|
|
35
froj.iml
35
froj.iml
|
@ -7,5 +7,40 @@
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library name="JUnit4">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library name="JUnit4">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library name="testng">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/testng/testng/7.1.0/testng-7.1.0.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/com/beust/jcommander/1.72/jcommander-1.72.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/com/google/inject/guice/4.1.0/guice-4.1.0-no_aop.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/javax/inject/javax.inject/1/javax.inject-1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/aopalliance/aopalliance/1.0/aopalliance-1.0.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/19.0/guava-19.0.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/yaml/snakeyaml/1.21/snakeyaml-1.21.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
|
@ -1,4 +1 @@
|
||||||
|
print "hello world";
|
||||||
|
|
||||||
|
|
||||||
print "Hello world!";
|
|
10
src/Makefile
10
src/Makefile
|
@ -1,4 +1,6 @@
|
||||||
FILE_PATH = ./com/jsaasta/froj
|
FILE_PATH = ./com/jsaasta/froj
|
||||||
|
FILE_PATH_STDLIB = $(FILE_PATH)/stdlib
|
||||||
|
FILE_PATH_STDLIB_SOCKET = $(FILE_PATH_STDLIB)/socket
|
||||||
FILE = ../hello_world.froj
|
FILE = ../hello_world.froj
|
||||||
FILE_NAME_JAR = froj.jar
|
FILE_NAME_JAR = froj.jar
|
||||||
|
|
||||||
|
@ -6,13 +8,17 @@ all: run
|
||||||
|
|
||||||
compile:
|
compile:
|
||||||
rm -f $(FILE_PATH)/*.class
|
rm -f $(FILE_PATH)/*.class
|
||||||
javac -d ./ $(FILE_PATH)/*.java
|
javac -d ./ $(FILE_PATH)/*.java $(FILE_PATH_STDLIB)/*.java $(FILE_PATH_STDLIB_SOCKET)/*.java
|
||||||
jar -cfm $(FILE_NAME_JAR) ./META-INF/MANIFEST.MF $(FILE_PATH)/*.class
|
jar -cfm $(FILE_NAME_JAR) ./META-INF/MANIFEST.MF $(FILE_PATH)/*.class $(FILE_PATH_STDLIB)/*.class $(FILE_PATH_STDLIB_SOCKET)/*.class
|
||||||
rm -f $(FILE_PATH)/*.class
|
rm -f $(FILE_PATH)/*.class
|
||||||
|
rm -f $(FILE_PATH_STDLIB)/*.class
|
||||||
|
rm -f $(FILE_PATH_STDLIB_SOCKET)/*.class
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(FILE_PATH)/*.class
|
rm -f $(FILE_PATH)/*.class
|
||||||
rm -f ./$(FILE_NAME_JAR)
|
rm -f ./$(FILE_NAME_JAR)
|
||||||
|
rm -f $(FILE_PATH_STDLIB)/*.class
|
||||||
|
rm -f $(FILE_PATH_STDLIB_SOCKET)/*.class
|
||||||
|
|
||||||
javac:
|
javac:
|
||||||
javac -d ./ $(FILE_PATH)/*.java
|
javac -d ./ $(FILE_PATH)/*.java
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class Froj {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runFile(String path) throws IOException {
|
public void runFile(String path) throws IOException {
|
||||||
try {
|
try {
|
||||||
byte[] bytes = Files.readAllBytes(Paths.get(path));
|
byte[] bytes = Files.readAllBytes(Paths.get(path));
|
||||||
run(new String(bytes, Charset.defaultCharset()));
|
run(new String(bytes, Charset.defaultCharset()));
|
||||||
|
@ -74,7 +74,7 @@ public class Froj {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void error(int line, String message) {
|
public static void error(int line, String message) {
|
||||||
report(line, "", message);
|
report(line, "", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
package com.jsaasta.froj;
|
package com.jsaasta.froj;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.stdlib.Clock;
|
||||||
|
import com.jsaasta.froj.stdlib.Input;
|
||||||
|
import com.jsaasta.froj.stdlib.Stdlib;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -12,23 +16,7 @@ public class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
|
||||||
private Environment environment = globals;
|
private Environment environment = globals;
|
||||||
|
|
||||||
public Interpreter() {
|
public Interpreter() {
|
||||||
globals.define("clock", new FrojCallable() {
|
Stdlib.define().forEach(globals::define);
|
||||||
@Override
|
|
||||||
public int arity() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object call(Interpreter interpreter, List<Object> arguments) {
|
|
||||||
return (double) System.currentTimeMillis() / 1000.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "<native fn>";
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void interpret(List<Stmt> statements) {
|
public void interpret(List<Stmt> statements) {
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class Parser {
|
||||||
private final List<Token> tokens;
|
private final List<Token> tokens;
|
||||||
private int current = 0;
|
private int current = 0;
|
||||||
|
|
||||||
Parser(List<Token> tokens) {
|
public Parser(List<Token> tokens) {
|
||||||
this.tokens = tokens;
|
this.tokens = tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package com.jsaasta.froj;
|
package com.jsaasta.froj;
|
||||||
|
|
||||||
public class RuntimeError extends RuntimeException {
|
public class RuntimeError extends RuntimeException {
|
||||||
final Token token;
|
public final Token token;
|
||||||
|
|
||||||
public RuntimeError(Token token, String message) {
|
public RuntimeError(Token token, String message) {
|
||||||
super(message);
|
super(message);
|
||||||
|
|
|
@ -37,7 +37,7 @@ public class Scanner {
|
||||||
private int line = 1;
|
private int line = 1;
|
||||||
|
|
||||||
|
|
||||||
Scanner(String source) {
|
public Scanner(String source) {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package com.jsaasta.froj;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
abstract class Stmt {
|
public abstract class Stmt {
|
||||||
interface Visitor<R> {
|
interface Visitor<R> {
|
||||||
R visitBlockStmt(Block stmt);
|
R visitBlockStmt(Block stmt);
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ public class Token {
|
||||||
public final TokenType type;
|
public final TokenType type;
|
||||||
public final String lexeme;
|
public final String lexeme;
|
||||||
public final Object literal;
|
public final Object literal;
|
||||||
final int line;
|
public final int line;
|
||||||
|
|
||||||
public Token(TokenType type, String lexeme, Object literal, int line) {
|
public Token(TokenType type, String lexeme, Object literal, int line) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
|
28
src/com/jsaasta/froj/stdlib/Clock.java
Normal file
28
src/com/jsaasta/froj/stdlib/Clock.java
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package com.jsaasta.froj.stdlib;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.FrojCallable;
|
||||||
|
import com.jsaasta.froj.Interpreter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Returns current time in nanoSeconds / 1000;
|
||||||
|
*/
|
||||||
|
public class Clock implements FrojCallable {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int arity() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object call(Interpreter interpreter, List<Object> arguments) {
|
||||||
|
return (double) System.nanoTime() / 1000.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "<native fn Clock()>";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
29
src/com/jsaasta/froj/stdlib/FileReader.java
Normal file
29
src/com/jsaasta/froj/stdlib/FileReader.java
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package com.jsaasta.froj.stdlib;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.Froj;
|
||||||
|
import com.jsaasta.froj.FrojCallable;
|
||||||
|
import com.jsaasta.froj.Interpreter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class FileReader implements FrojCallable {
|
||||||
|
@Override
|
||||||
|
public int arity() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object call(Interpreter interpreter, List<Object> arguments) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
byte[] bytes = Files.readAllBytes(Paths.get((String) arguments.get(0)));
|
||||||
|
return new String(bytes, Charset.defaultCharset());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new StdlibRuntimeError("Couldn't find file: " + arguments.get(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
src/com/jsaasta/froj/stdlib/Input.java
Normal file
42
src/com/jsaasta/froj/stdlib/Input.java
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package com.jsaasta.froj.stdlib;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.FrojCallable;
|
||||||
|
import com.jsaasta.froj.Interpreter;
|
||||||
|
import com.jsaasta.froj.RuntimeError;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Input implements FrojCallable {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int arity() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object call(Interpreter interpreter, List<Object> arguments) {
|
||||||
|
InputStreamReader input = new InputStreamReader(System.in);
|
||||||
|
BufferedReader reader = new BufferedReader(input);
|
||||||
|
System.out.print("> ");
|
||||||
|
try {
|
||||||
|
String line = reader.readLine();
|
||||||
|
try {
|
||||||
|
return Double.parseDouble(line);
|
||||||
|
} catch(NumberFormatException ex) {
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "<native fn Clock()>";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
23
src/com/jsaasta/froj/stdlib/Stdlib.java
Normal file
23
src/com/jsaasta/froj/stdlib/Stdlib.java
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package com.jsaasta.froj.stdlib;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.stdlib.socket.SocketServer;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class Stdlib {
|
||||||
|
|
||||||
|
private static final Map<String, Object> classes;
|
||||||
|
|
||||||
|
static {
|
||||||
|
classes = new HashMap<>();
|
||||||
|
classes.put("clock", new Clock());
|
||||||
|
classes.put("input", new Input());
|
||||||
|
classes.put("fileReader", new FileReader());
|
||||||
|
classes.put("socket", new SocketServer());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> define(){
|
||||||
|
return classes;
|
||||||
|
}
|
||||||
|
}
|
11
src/com/jsaasta/froj/stdlib/StdlibRuntimeError.java
Normal file
11
src/com/jsaasta/froj/stdlib/StdlibRuntimeError.java
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package com.jsaasta.froj.stdlib;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.Token;
|
||||||
|
|
||||||
|
public class StdlibRuntimeError extends RuntimeException{
|
||||||
|
|
||||||
|
|
||||||
|
public StdlibRuntimeError(String message) {
|
||||||
|
super(message, null, false, false);
|
||||||
|
}
|
||||||
|
}
|
39
src/com/jsaasta/froj/stdlib/socket/ClientHandler.java
Normal file
39
src/com/jsaasta/froj/stdlib/socket/ClientHandler.java
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
package com.jsaasta.froj.stdlib.socket;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
class ClientHandler implements Runnable {
|
||||||
|
private Socket socket;
|
||||||
|
|
||||||
|
public ClientHandler(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
InputStream in = socket.getInputStream();
|
||||||
|
OutputStream out = socket.getOutputStream();
|
||||||
|
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int bytesRead = in.read(buffer);
|
||||||
|
if (bytesRead == -1) {
|
||||||
|
System.out.println("Client closed the Connection");
|
||||||
|
}
|
||||||
|
|
||||||
|
String message = new String(buffer, 0, bytesRead);
|
||||||
|
System.out.println("Received message from client: " + message);
|
||||||
|
|
||||||
|
out.write(message.getBytes());
|
||||||
|
|
||||||
|
SocketServer.messageQueue.put(message);
|
||||||
|
|
||||||
|
socket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error handling client connection: " + e.getMessage());
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
72
src/com/jsaasta/froj/stdlib/socket/SocketServer.java
Normal file
72
src/com/jsaasta/froj/stdlib/socket/SocketServer.java
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
package com.jsaasta.froj.stdlib.socket;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.FrojCallable;
|
||||||
|
import com.jsaasta.froj.Interpreter;
|
||||||
|
import com.jsaasta.froj.stdlib.StdlibRuntimeError;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
|
public class SocketServer implements FrojCallable {
|
||||||
|
protected static BlockingQueue<String> messageQueue = new LinkedBlockingQueue<>();
|
||||||
|
private static ServerSocket serverSocket;
|
||||||
|
|
||||||
|
private final String EXIT_CODE = "EXIT";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int arity() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object call(Interpreter interpreter, List<Object> arguments) {
|
||||||
|
return startSocketServer(arguments.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object startSocketServer(Object port) {
|
||||||
|
try {
|
||||||
|
Double param = (Double) port;
|
||||||
|
final int PORT = param.intValue();
|
||||||
|
|
||||||
|
serverSocket = new ServerSocket(PORT);
|
||||||
|
|
||||||
|
Socket socket = serverSocket.accept();
|
||||||
|
System.out.println("Incoming connection from " + socket.getInetAddress());
|
||||||
|
|
||||||
|
ClientHandler clientHandler = new ClientHandler(socket);
|
||||||
|
clientHandler.run();
|
||||||
|
String nextMessage = getNextMessage();
|
||||||
|
if (nextMessage.toLowerCase().contentEquals("exit")) {
|
||||||
|
shutdown();
|
||||||
|
return EXIT_CODE;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Double.parseDouble(nextMessage);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return nextMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
throw new StdlibRuntimeError(e.getMessage());
|
||||||
|
} finally {
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getNextMessage() throws InterruptedException {
|
||||||
|
return messageQueue.take();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void shutdown() {
|
||||||
|
try {
|
||||||
|
serverSocket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error shutting down server: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
58
src/com/jsaasta/tool/SocketClient.java
Normal file
58
src/com/jsaasta/tool/SocketClient.java
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
package com.jsaasta.tool;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.ConnectException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
class SocketClient {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
while(true) {
|
||||||
|
InputStreamReader input = new InputStreamReader(System.in);
|
||||||
|
BufferedReader reader = new BufferedReader(input);
|
||||||
|
|
||||||
|
String message = reader.readLine();
|
||||||
|
if(message.contentEquals("SHUTMEDOWN")){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
String response = sendMessage(message);
|
||||||
|
if (response != null) {
|
||||||
|
System.out.println("Received response from server: " + response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String sendMessage(String message) throws IOException {
|
||||||
|
Socket socket = null;
|
||||||
|
try {
|
||||||
|
socket = new Socket("localhost", 8000);
|
||||||
|
System.out.println("Connected to server");
|
||||||
|
|
||||||
|
OutputStream out = socket.getOutputStream();
|
||||||
|
InputStream in = socket.getInputStream();
|
||||||
|
|
||||||
|
out.write(message.getBytes());
|
||||||
|
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int bytesRead = in.read(buffer);
|
||||||
|
String response = new String(buffer, 0, bytesRead);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (ConnectException e) {
|
||||||
|
System.out.println("Connection refused. Is the server running?");
|
||||||
|
return null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error sending message: " + e.getMessage());
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
if (socket != null) {
|
||||||
|
try {
|
||||||
|
socket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error closing socket: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
src/test/com/saasta/froj/FrojTest.java
Normal file
15
src/test/com/saasta/froj/FrojTest.java
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package test.com.saasta.froj;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class FrojTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHelloWorld() throws IOException {
|
||||||
|
Froj froj = new Froj();
|
||||||
|
froj.runFile("src/test/com/saasta/froj/froj_test.froj");
|
||||||
|
}
|
||||||
|
}
|
109
src/test/com/saasta/froj/froj_test.froj
Normal file
109
src/test/com/saasta/froj/froj_test.froj
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
class HelloParent {
|
||||||
|
welcome(){
|
||||||
|
print "Hello from Parent";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HelloWorld < HelloParent {
|
||||||
|
init(helloString){
|
||||||
|
this.helloString = helloString;
|
||||||
|
}
|
||||||
|
|
||||||
|
welcome(){
|
||||||
|
super.welcome();
|
||||||
|
print this.helloString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var hello = "Hello World!";
|
||||||
|
var helloWorldObject = HelloWorld(hello);
|
||||||
|
helloWorldObject.welcome();
|
||||||
|
|
||||||
|
// Or just a simple:
|
||||||
|
print "Hello World";
|
||||||
|
|
||||||
|
// -----------------------------------------
|
||||||
|
class Test {
|
||||||
|
|
||||||
|
init(aBoolean){
|
||||||
|
if(aBoolean){
|
||||||
|
print "in true";
|
||||||
|
} else {
|
||||||
|
print "in false";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(false);
|
||||||
|
// ----------------------------------------
|
||||||
|
|
||||||
|
function controlFlowTest(someValue){
|
||||||
|
if(someValue == "if"){
|
||||||
|
print "in " + someValue;
|
||||||
|
} else if(someValue == "else if") {
|
||||||
|
print "in " + someValue;
|
||||||
|
} else {
|
||||||
|
print "in 'else', because the value was: " + someValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
controlFlowTest("if");
|
||||||
|
controlFlowTest("else if");
|
||||||
|
controlFlowTest("something else entirely");
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------
|
||||||
|
function testWhile(){
|
||||||
|
var a = 0;
|
||||||
|
while(a < 10){
|
||||||
|
a = a+1;
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
function testFor() {
|
||||||
|
var a = "";
|
||||||
|
for(var i = 0; i < 10; i = i + 1){
|
||||||
|
a = a + "a";
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
print testWhile();
|
||||||
|
print testFor();
|
||||||
|
|
||||||
|
// ---------------------------------------
|
||||||
|
function addTwoNumbers(a, b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
function testCallBack(aFunction){
|
||||||
|
var a = 1;
|
||||||
|
var b = 2;
|
||||||
|
|
||||||
|
return aFunction(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
print addTwoNumbers(4,5);
|
||||||
|
print testCallBack(addTwoNumbers);
|
||||||
|
|
||||||
|
// ---------------------------------------
|
||||||
|
|
||||||
|
class Person {
|
||||||
|
|
||||||
|
init(name){
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
myNameIs() {
|
||||||
|
print this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
var janne = Person("Janne");
|
||||||
|
janne.name = "Janne";
|
||||||
|
janne.otherName = "Totally Not Janne";
|
||||||
|
janne.myNameIs();
|
||||||
|
|
||||||
|
print janne.otherName;
|
||||||
|
|
||||||
|
// ---------------------------------------
|
24
src/test/com/saasta/froj/stdlib/FrojStdlibTest.java
Normal file
24
src/test/com/saasta/froj/stdlib/FrojStdlibTest.java
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package test.com.saasta.froj.stdlib;
|
||||||
|
|
||||||
|
import com.jsaasta.froj.Froj;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
public class FrojStdlibTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_Stdlib_Clock() throws IOException {
|
||||||
|
Froj froj = new Froj();
|
||||||
|
froj.runFile("src/test/com/saasta/froj/stdlib/froj_stdlib_clock.froj");
|
||||||
|
froj.runFile("src/test/com/saasta/froj/stdlib/froj_stdlib_fileReader.froj");
|
||||||
|
try {
|
||||||
|
froj.runFile("src/test/com/saasta/froj/stdlib/froj_stdlib_socketserver.froj");
|
||||||
|
} catch(NullPointerException ex){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
src/test/com/saasta/froj/stdlib/froj_stdlib_clock.froj
Normal file
1
src/test/com/saasta/froj/stdlib/froj_stdlib_clock.froj
Normal file
|
@ -0,0 +1 @@
|
||||||
|
clock();
|
|
@ -0,0 +1 @@
|
||||||
|
fileReader("./hello_world.froj");
|
|
@ -0,0 +1 @@
|
||||||
|
socket("asdf");
|
Loading…
Reference in a new issue