Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
df94319
variable nesting
ErdemT09 Jun 17, 2025
d83f897
fix compile error
ErdemT09 Jun 17, 2025
203f2ac
lemms object initial
ErdemT09 Jun 17, 2025
fa31dd6
added parseClassDeclaration to parser
TamanegiTem Jun 17, 2025
e500ff9
Merge branch 'Variable-Nesting' of github.com:LukaDeka/PLDI into Vari…
TamanegiTem Jun 17, 2025
9b601b5
initial interpreter changes
ErdemT09 Jun 17, 2025
96faa5a
new operator logic
ErdemT09 Jun 17, 2025
ede8268
initial compiling code
ErdemT09 Jun 17, 2025
1040109
fix first 3 tests
ErdemT09 Jun 17, 2025
b14b6c9
environmentalize global functions
ErdemT09 Jun 17, 2025
58c6d2f
can do higher order functions
ErdemT09 Jun 17, 2025
4f8c0eb
different example
ErdemT09 Jun 17, 2025
e89fddb
add constructor
ErdemT09 Jun 17, 2025
1450b9e
test program
notkminq Jun 24, 2025
d03bc31
lemms object has environment
ErdemT09 Jun 24, 2025
a0c6d5c
add member access node
ErdemT09 Jun 24, 2025
564a066
implement visit member access node
ErdemT09 Jun 24, 2025
bb44718
visit member access node adjustments
ErdemT09 Jun 24, 2025
955a68c
use class environment signal
ErdemT09 Jun 24, 2025
8332c87
dot tokenizer
ErdemT09 Jun 24, 2025
6ce795e
initial proper member access expression parsing
ErdemT09 Jun 24, 2025
06e70fb
added canvas functionalities including snake example with .lemms equi…
SimoneEsposito-Git Jun 24, 2025
862bb09
Canvas API organisation and Package rename
SimoneEsposito-Git Jun 24, 2025
0ea0451
basic changes for creating member access nodes
ErdemT09 Jun 25, 2025
ef3ecb6
Merge branch 'Variable-Nesting' into Canvas
ErdemT09 Jun 25, 2025
aaa47ce
fix assignment
ErdemT09 Jun 25, 2025
1514da9
Merge pull request #49 from LukaDeka/Canvas
ErdemT09 Jun 25, 2025
b1573af
better parsing succession
ErdemT09 Jun 25, 2025
7a60cf7
working member access parser
ErdemT09 Jun 25, 2025
ac1c884
Support member access and assignment in parser
ErdemT09 Jun 25, 2025
2dfb8e3
Support member access function calls in parser and interpreter
ErdemT09 Jun 25, 2025
09c530f
Implement assignment for variables and member access
ErdemT09 Jun 25, 2025
39b5079
Update classTest.lemms
ErdemT09 Jun 25, 2025
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
3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/main/java/com/lemms/Canvas/Canvas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.lemms.Canvas;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Canvas implements ActionListener {

public ScriptCallback update;

private final CanvasPanel panel;
private final JFrame frame;
private final Timer timer;

public Canvas(int tickRate, int width, int height) {
frame = new JFrame();
panel = new CanvasPanel();
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
update = panel::repaint;
timer = new Timer(tickRate, this);
}

public int getWidth() {
return frame.getWidth();
}

public int getHeight() {
return frame.getHeight();
}

public Rect getBounds() {return new Rect(0,0, getWidth(), getHeight(), null);}

public void run() {
timer.start();
}

public void quit() {
timer.stop();
}

public void add(Drawable drawable) {
panel.addElement(drawable);
}

public void remove(Drawable drawable) {panel.removeElement(drawable);}

public void clear() {panel.clearElements();}

public void repaint() {panel.repaint();}

public void onKeyPress(int key, ScriptCallback callback) {
panel.addKeyEvent(key, callback);
}

@Override
public void actionPerformed(ActionEvent e) {
if ( e.getSource() == timer)
{
update.call();
}
}

}
59 changes: 59 additions & 0 deletions src/main/java/com/lemms/Canvas/CanvasPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.lemms.Canvas;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CanvasPanel extends JPanel implements KeyListener {
private final List<Drawable> elements = new ArrayList<>();
private final HashMap<Integer, ScriptCallback> keyEvents = new HashMap<>();

public CanvasPanel() {
setFocusable(true);
requestFocusInWindow();
addKeyListener(this);
}

public void addElement(Drawable d) {
elements.add(d);
}

public void removeElement(Drawable d) {
elements.remove(d);
}

public void clearElements() {
elements.clear();
}

public void addKeyEvent(int key, ScriptCallback callback) {
keyEvents.put(key, callback);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Drawable d : elements) {
d.draw(g);
}
}

@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
if(keyEvents.containsKey(e.getKeyCode())) {
keyEvents.get(e.getKeyCode()).call();
}
}

@Override
public void keyReleased(KeyEvent e) {
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/lemms/Canvas/Collidable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.lemms.Canvas;

public interface Collidable {
int getX();
int getY();
int getWidth();
int getHeight();

boolean intersects(Collidable other);
boolean contains(Collidable other);
}
7 changes: 7 additions & 0 deletions src/main/java/com/lemms/Canvas/Drawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lemms.Canvas;

import java.awt.*;

public interface Drawable {
void draw(Graphics g);
}
26 changes: 26 additions & 0 deletions src/main/java/com/lemms/Canvas/Pixel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.lemms.Canvas;

import java.awt.*;

public class Pixel implements Drawable {
private int x, y;
private Color color;

public Pixel(int x, int y, Color color) {
this.x = x; this.y = y; this.color = color;
}

public int getX() {return x;}
public int getY() {return y;}
public Color getColor() {return color;}

public void setX(int x) {this.x = x;}
public void setY(int y) {this.y = y;}
public void setColor(Color color) {this.color = color;}

@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillRect(x, y, 1, 1); // Or larger if needed
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/lemms/Canvas/Rect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.lemms.Canvas;

import java.awt.*;

public class Rect implements Drawable, Collidable {
private int x, y, width, height;
private Color color;

public Rect(int x, int y, int w, int h, Color color) {
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.color = color;
}

@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillRect(x, y, width, height);
}

public Color getColor() {return color;}

@Override
public int getX() {return x;}
@Override
public int getY() {return y;}
@Override
public int getWidth() {return width;}
@Override
public int getHeight() {return height;}

public void setX(int x) {this.x = x;}
public void setY(int y) {this.y = y;}
public void setWidth(int w) {this.width = w;}
public void setHeight(int h) {this.height = h;}

@Override
public boolean intersects(Collidable other) {
if (other == null) {
return false;
}
return this.x < other.getX() + other.getWidth()
&& this.x + this.width > other.getX()
&& this.y < other.getY() + other.getHeight()
&& this.y + this.height > other.getY();
}

@Override
public boolean contains(Collidable other) {
if (other == null) {
return false;
}
return other.getX() >= this.x
&& other.getY() >= this.y
&& other.getX() + other.getWidth() <= this.x + this.width
&& other.getY() + other.getWidth() <= this.y + this.height;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/lemms/Canvas/ScriptCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.lemms.Canvas;

public interface ScriptCallback {
void call();
}
105 changes: 105 additions & 0 deletions src/main/java/com/lemms/Canvas/Snake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.lemms.Canvas;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

public class Snake {
public static void main(String[] args) {
//create canvas with single snake cell
Canvas canvas = new Canvas(500, 500,500);
ArrayList<Rect> snake = new ArrayList<>(){};
snake.add(new Rect(0,0, 20,20, Color.BLUE));

//generate food at random location
int gridWidth = canvas.getWidth()/20;
int gridHeight = canvas.getHeight()/20;

Random random = new Random();
int food_x = random.nextInt(gridWidth-1)*20;
int food_y = random.nextInt(gridHeight-1)*20;
Rect food = new Rect(food_x, food_y, 20, 20, Color.RED);

canvas.run();
canvas.add(snake.get(0));
canvas.add(food);

int[] dir = {1, 0};

canvas.onKeyPress(KeyEvent.VK_W, ()->{
dir[0] = 0;
dir[1] = -1;
});
canvas.onKeyPress(KeyEvent.VK_A, ()->{
dir[0] = -1;
dir[1] = 0;
});
canvas.onKeyPress(KeyEvent.VK_S, ()->{
dir[0] = 0;
dir[1] = 1;
});
canvas.onKeyPress(KeyEvent.VK_D, ()->{
dir[0] = 1;
dir[1] = 0;
});

canvas.update = () -> {
// Compute next head position
Rect oldHead = snake.get(0);
int newX = oldHead.getX() + dir[0]*20;
int newY = oldHead.getY() + dir[1]*20;
Rect newHead = new Rect(newX, newY, 20, 20, Color.BLUE);

// Check wall collision
if (!canvas.getBounds().contains(newHead)) {
GameOver(canvas, snake, false);
return;
}

// Self‐collision
for (Rect segment : snake) {
if (segment.getX() == newX && segment.getY() == newY) {
// game over
GameOver(canvas, snake, false);
return;
}
}

// Add head to front
snake.add(0, newHead);
canvas.add(newHead);

boolean ate = newHead.intersects(food);
if (ate) {
// move the food
food.setX(random.nextInt(gridWidth-1)*20);
food.setY(random.nextInt(gridHeight-1)*20);

if(snake.size() == gridHeight*gridWidth) {
GameOver(canvas, snake, true);
}
}

if (!ate) {
Rect tail = snake.remove(snake.size() - 1);
canvas.remove(tail);
}

canvas.repaint();
};
}

private static void GameOver(Canvas canvas, ArrayList<Rect> snake, boolean won) {
canvas.clear();
snake.clear();
String text = won ? "Game Won!" : "Game Over!";
Text GameOver = new Text(text,canvas.getWidth()/2, canvas.getHeight()/2, 72,
Color.BLACK);
GameOver.align(0,0);
canvas.add(GameOver);
canvas.repaint();
canvas.quit();
}

}
Loading
Loading