Skip to content

Instantly share code, notes, and snippets.

@otrack
Created December 5, 2023 16:41
Show Gist options
  • Save otrack/8237de09a27816bad4bfe0da359b2ddc to your computer and use it in GitHub Desktop.
Save otrack/8237de09a27816bad4bfe0da359b2ddc to your computer and use it in GitHub Desktop.
public class Add extends Operation{
Add(Node left, Node right) {
super(left, right);
}
@Override
public int execute() {
return op(0).execute() + op(1).execute();
}
@Override
public String opString() {
return "+";
}
}
public abstract class Operation implements Node{
private Node[] ops;
public Node op(int i) {
return ops[i];
}
public int nbOps() {
return ops.length;
}
public Operation(Node... ops) {
this.ops = ops;
}
public abstract String opString();
public String toString(){
String ret = "( "+this.opString() +" ";
for (int i=0; i<nbOps(); i++) {
ret+=op(i).toString()+" ";
}
ret += ")";
return ret;
}
}
public class Variable extends Scalar {
String name;
Variable(String name) {
super(0);
this.name = name;
}
public void set(int v) {
this.value = v;
}
@Override
public String toString() {
return name;
}
}
public interface Node {
int execute();
}
public class Neg extends Operation{
Neg(Operation op) {
super(op);
}
@Override
public int execute() {
return - op(0).execute();
}
@Override
public String opString() {
return "-";
}
}
public class Main {
public static void main(String[] args) {
Add op1 = new Add(new Scalar(1),new Scalar(2));
Add op2 = new Add(new Scalar(3), new Scalar(4));
Add op3 = new Add(op1, new Neg(op2));
System.out.println(op3.execute());
Variable x = new Variable("x");
Set set = new Set(x,new Scalar(22));
System.out.println(set);
System.out.println(set.execute());
Add op4 = new Add(x, new Scalar(20));
System.out.println(op4);
System.out.println(op4.execute());
}
}public class Scalar implements Node{
protected int value;
Scalar(int value) {
this.value = value;
}
public int get(){
return value;
}
@Override
public String toString(){
return Integer.toString(value);
}
@Override
public int execute() {
return value;
}
}
public class Set extends Operation{
Variable variable;
Node node;
Set(Variable variable, Node node) {
super(variable, node);
this.variable = variable;
this.node = node;
}
@Override
public int execute() {
this.variable.set(this.node.execute());
return 1;
}
@Override
public String opString() {
return "set!";
}
}
public class Add extends Operation{
Add(Node left, Node right) {
super(left, right);
}
@Override
public int execute() {
return op(0).execute() + op(1).execute();
}
@Override
public String opString() {
return "+";
}
}
public abstract class Operation implements Node{
private Node[] ops;
public Node op(int i) {
return ops[i];
}
public int nbOps() {
return ops.length;
}
public Operation(Node... ops) {
this.ops = ops;
}
public abstract String opString();
public String toString(){
String ret = "( "+this.opString() +" ";
for (int i=0; i<nbOps(); i++) {
ret+=op(i).toString()+" ";
}
ret += ")";
return ret;
}
}
public class Variable extends Scalar {
String name;
Variable(String name) {
super(0);
this.name = name;
}
public void set(int v) {
this.value = v;
}
@Override
public String toString() {
return name;
}
}
public interface Node {
int execute();
}
public class Neg extends Operation{
Neg(Operation op) {
super(op);
}
@Override
public int execute() {
return - op(0).execute();
}
@Override
public String opString() {
return "-";
}
}
public class Main {
public static void main(String[] args) {
Add op1 = new Add(new Scalar(1),new Scalar(2));
Add op2 = new Add(new Scalar(3), new Scalar(4));
Add op3 = new Add(op1, new Neg(op2));
System.out.println(op3.execute());
Variable x = new Variable("x");
Set set = new Set(x,new Scalar(22));
System.out.println(set);
System.out.println(set.execute());
Add op4 = new Add(x, new Scalar(20));
System.out.println(op4);
System.out.println(op4.execute());
}
}public class Scalar implements Node{
protected int value;
Scalar(int value) {
this.value = value;
}
public int get(){
return value;
}
@Override
public String toString(){
return Integer.toString(value);
}
@Override
public int execute() {
return value;
}
}
public class Set extends Operation{
Variable variable;
Node node;
Set(Variable variable, Node node) {
super(variable, node);
this.variable = variable;
this.node = node;
}
@Override
public int execute() {
this.variable.set(this.node.execute());
return 1;
}
@Override
public String opString() {
return "set!";
}
}
public class Add extends Operation{
Add(Node left, Node right) {
super(left, right);
}
@Override
public int execute() {
return op(0).execute() + op(1).execute();
}
@Override
public String opString() {
return "+";
}
}
public abstract class Operation implements Node{
private Node[] ops;
public Node op(int i) {
return ops[i];
}
public int nbOps() {
return ops.length;
}
public Operation(Node... ops) {
this.ops = ops;
}
public abstract String opString();
public String toString(){
String ret = "( "+this.opString() +" ";
for (int i=0; i<nbOps(); i++) {
ret+=op(i).toString()+" ";
}
ret += ")";
return ret;
}
}
public class Variable extends Scalar {
String name;
Variable(String name) {
super(0);
this.name = name;
}
public void set(int v) {
this.value = v;
}
@Override
public String toString() {
return name;
}
}
public interface Node {
int execute();
}
public class Neg extends Operation{
Neg(Operation op) {
super(op);
}
@Override
public int execute() {
return - op(0).execute();
}
@Override
public String opString() {
return "-";
}
}
public class Main {
public static void main(String[] args) {
Add op1 = new Add(new Scalar(1),new Scalar(2));
Add op2 = new Add(new Scalar(3), new Scalar(4));
Add op3 = new Add(op1, new Neg(op2));
System.out.println(op3.execute());
Variable x = new Variable("x");
Set set = new Set(x,new Scalar(22));
System.out.println(set);
System.out.println(set.execute());
Add op4 = new Add(x, new Scalar(20));
System.out.println(op4);
System.out.println(op4.execute());
}
}public class Scalar implements Node{
protected int value;
Scalar(int value) {
this.value = value;
}
public int get(){
return value;
}
@Override
public String toString(){
return Integer.toString(value);
}
@Override
public int execute() {
return value;
}
}
public class Set extends Operation{
Variable variable;
Node node;
Set(Variable variable, Node node) {
super(variable, node);
this.variable = variable;
this.node = node;
}
@Override
public int execute() {
this.variable.set(this.node.execute());
return 1;
}
@Override
public String opString() {
return "set!";
}
}
public class Add extends Operation{
Add(Node left, Node right) {
super(left, right);
}
@Override
public int execute() {
return op(0).execute() + op(1).execute();
}
@Override
public String opString() {
return "+";
}
}
public abstract class Operation implements Node{
private Node[] ops;
public Node op(int i) {
return ops[i];
}
public int nbOps() {
return ops.length;
}
public Operation(Node... ops) {
this.ops = ops;
}
public abstract String opString();
public String toString(){
String ret = "( "+this.opString() +" ";
for (int i=0; i<nbOps(); i++) {
ret+=op(i).toString()+" ";
}
ret += ")";
return ret;
}
}
public class Variable extends Scalar {
String name;
Variable(String name) {
super(0);
this.name = name;
}
public void set(int v) {
this.value = v;
}
@Override
public String toString() {
return name;
}
}
public interface Node {
int execute();
}
public class Neg extends Operation{
Neg(Operation op) {
super(op);
}
@Override
public int execute() {
return - op(0).execute();
}
@Override
public String opString() {
return "-";
}
}
public class Main {
public static void main(String[] args) {
Add op1 = new Add(new Scalar(1),new Scalar(2));
Add op2 = new Add(new Scalar(3), new Scalar(4));
Add op3 = new Add(op1, new Neg(op2));
System.out.println(op3.execute());
Variable x = new Variable("x");
Set set = new Set(x,new Scalar(22));
System.out.println(set);
System.out.println(set.execute());
Add op4 = new Add(x, new Scalar(20));
System.out.println(op4);
System.out.println(op4.execute());
}
}
public class Scalar implements Node{
protected int value;
Scalar(int value) {
this.value = value;
}
public int get(){
return value;
}
@Override
public String toString(){
return Integer.toString(value);
}
@Override
public int execute() {
return value;
}
}
public class Set extends Operation{
Variable variable;
Node node;
Set(Variable variable, Node node) {
super(variable, node);
this.variable = variable;
this.node = node;
}
@Override
public int execute() {
this.variable.set(this.node.execute());
return 1;
}
@Override
public String opString() {
return "set!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment