Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/robusta/compiler/JvsCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

public class JvsCompiler {
private static final File parentDirectory = new File(System.getProperty("user.dir"));
private static final String CLASS_NAME = "C";

public static void build(String jvsFilePath) {

Expand Down Expand Up @@ -44,7 +45,7 @@ public static void build(String jvsFilePath) {
}
String jvsCode = readFile(jvsFile);
String javaCode = translate(jvsCode);
JavaSourceFromString jsfs = new JavaSourceFromString("C", javaCode);
JavaSourceFromString jsfs = new JavaSourceFromString(CLASS_NAME, javaCode);
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(jsfs);
StringWriter output = new StringWriter();
boolean isCompilationSuccessful = compiler.getTask(output, fileManager, null, null, null, fileObjects).call();
Expand Down Expand Up @@ -72,7 +73,6 @@ private static String translate(String jvsCode) {
}

private static StringBuilder getHead() {
final String CLASS_NAME = "C";
StringBuilder head = new StringBuilder();
head.append("import static java.lang.Math.*;");
head.append("import static org.robusta.macros.Stdin.*;");
Expand All @@ -81,10 +81,16 @@ private static StringBuilder getHead() {
head.append("import static org.robusta.macros.IO.*;");
head.append("import static org.robusta.compiler.RunTimeErrorHandler.*;");
head.append("import org.robusta.macros.Console;");
head.append("public class " + CLASS_NAME + "{");
head.append("import org.robusta.macros.Drawer;");
head.append("import static org.robusta.macros.Drawer.reset;");
head.append("import static org.robusta.macros.Drawer.setPixel;");
head.append("import static org.robusta.macros.Drawer.getPixel;");
head.append(String.format("public class %s {", CLASS_NAME));
head.append("public static void main(String[] args) {");
head.append("Console.getInstance();");
head.append("try{ new C ().main(); } catch(Exception e) { handle(e); }");
head.append("Drawer drawer = Drawer.getInstance();");
head.append(String.format("try{ new %s ().main(); } catch(Exception e) { handle(e); }", CLASS_NAME));
head.append("drawer.start();");
head.append("}");
return head;
}
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/org/robusta/macros/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public class Console extends JFrame {
private static volatile JScrollPane scrollFrame;
private static final long serialVersionUID = 6543132165763L;

public static void main(String [] s) {
new Console();
Integer i = Stdin.readInteger();
}

private Console() {
super("Robusta");

Expand Down
68 changes: 68 additions & 0 deletions src/main/java/org/robusta/macros/Drawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.robusta.macros;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Drawer extends JFrame {

private static Drawer INSTANCE;
private static volatile DrawerSurface surface;
private static volatile boolean started = false;
private static final long serialVersionUID = 654313216000L;

private Drawer() {
super("Drawer");
surface = new DrawerSurface();
started = false;
}

public static synchronized Drawer getInstance() {
if(INSTANCE == null) {
return new Drawer();
} else {
return INSTANCE;
}
}

public static synchronized void reset(int width, int height) {
surface.reset(width * 2, height * 2);
surface.setPreferredSize(new Dimension(width * 2, height * 2));
started = true;
}

public void start() {
if(started) {
add(surface);

addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
surface.clear();
}
});

setTitle("Points");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);

pack();
}
}

public static synchronized void setPixel(int x, int y, int grayValue) {
surface.setPixel(x, y, grayValue);
}

public static synchronized void setPixel(int x, int y, String color) {
surface.setPixel(x, y, color);
}

public static synchronized int getPixel(int x, int y) {
return surface.getPixel(x, y);
}

}
178 changes: 178 additions & 0 deletions src/main/java/org/robusta/macros/DrawerSurface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package org.robusta.macros;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;


class DrawerSurface extends JPanel implements ActionListener {

private int width;
private int height;
private Set<Point> points;

public DrawerSurface() {
this.width = 0;
this.height = 0;
this.points = new TreeSet<>();
}

@Override
public int getWidth() {
return width;
}

@Override
public int getHeight() {
return height;
}

public void setPixel(int x, int y, int grayValue) {
checkBoundaries(x, y);

int x_translation = Math.abs(width / 2);
int y_translation = Math.abs(height / 2);

Point p = new Point(x + x_translation, y + y_translation, new Color(grayValue, grayValue, grayValue));
this.points.add(p);
}

public void setPixel(int x, int y, String color) {
checkBoundaries(x, y);

int x_translation = Math.abs(width / 2);
int y_translation = Math.abs(height / 2);

Point p = new Point(x + x_translation, y + y_translation, ColorFactory.getColor(color.toLowerCase()));
this.points.add(p);
}

public int getPixel(int x, int y) {
checkBoundaries(x, y);

int x_translation = Math.abs(width / 2);
int y_translation = Math.abs(height / 2);

for(Point p : points) {
if(p.x == x + x_translation && p.y == y + y_translation)
return p.getColor().getRed();
}

// default value
return 0;
}

public void reset(int width, int height) {
this.width = width;
this.height = height;
this.points.clear();
}

public void clear() {
this.width = 0;
this.height = 0;
this.points.clear();
}

private void draw(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

BasicStroke bs1 = new BasicStroke(2f);
g2d.setStroke(bs1);

for(Point p : this.points) {
g2d.setColor(p.getColor());
g2d.drawLine(p.x, p.y, p.x, p.y);
}

}

private void checkBoundaries(int x, int y) throws IllegalArgumentException {
if (x > width/2 || x < - width/2) {
System.out.println("Width out of bound");
throw new IllegalArgumentException("x value is out of window width boundary");

}

if (y > height/2 || y < - height/2) {
throw new IllegalArgumentException("y value is out of window height boundary");
}

}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}

@Override
public void actionPerformed(ActionEvent e) {
repaint();
}

}

class Point implements Comparable {
int x;
int y;
private Color color;

Point(int _x, int _y, Color c) {
x = _x;
y = _y;
color = c;
}

public void setColor(Color c) {
this.color = c;
}

public Color getColor() {
return color;
}

public int compareTo(Object o) {
Point p = (Point) o;
int xEquality = p.x - x;

if (xEquality != 0) return xEquality;

return p.y - y;
}

}

class ColorFactory {

private static HashMap<String, Color> colors = createColors();

private static HashMap<String, Color> createColors() {
HashMap<String, Color> colors = new HashMap<>(12);

colors.put("black", Color.BLACK);
colors.put("blue", Color.BLUE);
colors.put("cyan", Color.CYAN);
colors.put("gray", Color.GRAY);
colors.put("grey", Color.GRAY);
colors.put("green", Color.GREEN);
colors.put("magenta", Color.MAGENTA);
colors.put("orange", Color.ORANGE);
colors.put("red", Color.RED);
colors.put("white", Color.WHITE);
colors.put("yellow", Color.YELLOW);
colors.put("pink", Color.PINK);

return colors;
}


public static Color getColor(String color) {
return colors.getOrDefault(color, Color.BLACK);
}
}
8 changes: 5 additions & 3 deletions src/test/resources/hello-world.jvs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
void main(){
void main() {

String i = readString("");
String i = readString("");
println("hello world");
println("hello world");

}
reset(200, 100);
for (int j = 0; j < 200; j++) setPixel(3, j, (j % 255));
}