Skip to content

Commit 29161f6

Browse files
authored
Merge pull request #28 from UCSDOalads/addCompilerByXiaoquan
add compiler
2 parents 6ad0daf + 4af9b98 commit 29161f6

20 files changed

+1499
-38
lines changed

src/actions/EnterScriptAction.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package actions;
2+
3+
import java.awt.HeadlessException;
4+
import java.util.Scanner;
5+
6+
import javax.swing.JOptionPane;
7+
8+
import actions.menu.ActionsMenuBarTitles;
9+
import ui.PaintPanel;
10+
import script.ExecutionErrorException;
11+
import script.Interpreter;
12+
13+
public class EnterScriptAction extends PaintAction {
14+
15+
public EnterScriptAction(PaintPanel panel) {
16+
super(panel);
17+
}
18+
19+
@Override
20+
public boolean canPerformAction() {
21+
return true;
22+
}
23+
24+
@Override
25+
public void performAction() {
26+
Interpreter interpreter = new Interpreter(panel);
27+
/*Scanner scanner = new Scanner(System.in);
28+
29+
System.out.println("Enter script:");
30+
while (scanner.hasNextLine()) {
31+
try {
32+
interpreter.interpreteLine(scanner.nextLine());
33+
} catch (ExecutionErrorException e) {
34+
System.out.println("Invalid script");
35+
}
36+
System.out.println("Enter script:");
37+
}
38+
scanner.close();
39+
*/
40+
try {
41+
interpreter.interpreteLine(JOptionPane.showInputDialog(panel, "Enter Script: "));
42+
} catch (HeadlessException e) {
43+
// TODO Auto-generated catch block
44+
e.printStackTrace();
45+
} catch (ExecutionErrorException e) {
46+
System.out.println("Invalid script");
47+
}
48+
}
49+
50+
@Override
51+
public String locationString() {
52+
return ActionsMenuBarTitles.Script().Enter_Script().toString();
53+
}
54+
55+
}

src/actions/menu/ActionsMenuBar.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import actions.EditAnnotationSizeAction;
2626
import actions.EditRedoAction;
2727
import actions.EditUndoAction;
28+
import actions.EnterScriptAction;
2829
import actions.FileOpen;
2930
import actions.FileSaveAs;
3031
import actions.GeneratePolygonSourceJava;
@@ -98,6 +99,10 @@ public ActionsMenuBar(PaintPanel panel){
9899
addAction(new UpdateFontSizeOperation(panel));
99100
addAction(new SetPointSizeOperation(panel));
100101

102+
103+
//script
104+
addAction(new EnterScriptAction(panel));
105+
101106
// add data annotation
102107
addAction(new AddAnnotationAction(panel));
103108
addAction(new RemoveAnnotationAction(panel));
Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package actions.menu;
22

33
public class ActionsMenuBarTitles {
4-
4+
55
public String pending;
6-
6+
77
public ActionsMenuBarTitles(String string) {
88
pending = string;
99
}
@@ -12,64 +12,65 @@ public ActionsMenuBarTitles(String string) {
1212
public String toString() {
1313
return pending;
1414
}
15-
16-
public static ActionsMenuBarTitles Data(){
15+
16+
public static ActionsMenuBarTitles Data() {
1717
return new ActionsMenuBarTitles("Data");
1818
}
19-
19+
2020
public static ActionsMenuBarTitles Edit() {
2121
return new ActionsMenuBarTitles("Edit");
2222
}
23+
2324
/**
2425
* Returns the string construct for Developer Only Feature
25-
* @param remainingTitle the remaining menu titles, this should not begin with /
26+
*
27+
* @param remainingTitle
28+
* the remaining menu titles, this should not begin with /
2629
* @return
2730
*/
28-
public static ActionsMenuBarTitles Developer(String remainingTitle){
31+
public static ActionsMenuBarTitles Developer(String remainingTitle) {
2932
return new ActionsMenuBarTitles("Developer" + "/" + remainingTitle);
3033
}
31-
32-
public static ActionsMenuBarTitles File(){
34+
35+
public static ActionsMenuBarTitles File() {
3336
return new ActionsMenuBarTitles("File");
3437
}
35-
36-
37-
public ActionsMenuBarTitles Save(){
38+
39+
public ActionsMenuBarTitles Save() {
3840
append("Save...");
3941
return this;
4042
}
4143

42-
private void append(String str){
44+
private void append(String str) {
4345
this.pending += "/" + str;
4446
}
45-
46-
47-
public ActionsMenuBarTitles Input_Box(){
47+
48+
public ActionsMenuBarTitles Input_Box() {
4849
append("Input Box");
4950
return this;
5051
}
51-
52-
public ActionsMenuBarTitles Display_Box(){
52+
53+
public ActionsMenuBarTitles Display_Box() {
5354
append("Display Box");
5455
return this;
5556
}
56-
57-
public ActionsMenuBarTitles Add(){
57+
58+
public ActionsMenuBarTitles Add() {
5859
append("Add");
5960
return this;
6061
}
61-
62-
public ActionsMenuBarTitles Update(){
62+
63+
public ActionsMenuBarTitles Update() {
6364
append("Update");
6465
return this;
6566
}
66-
67-
public ActionsMenuBarTitles Construct(){
67+
68+
public ActionsMenuBarTitles Construct() {
6869
append("Construct");
6970
return this;
7071
}
71-
72-
public ActionsMenuBarTitles Line_Segment(){
72+
73+
public ActionsMenuBarTitles Line_Segment() {
7374
append("Line Segment");
7475
return this;
7576
}
@@ -78,6 +79,10 @@ public static ActionsMenuBarTitles Lazy() {
7879
return new ActionsMenuBarTitles("Lazy");
7980
}
8081

82+
public static ActionsMenuBarTitles Script() {
83+
return new ActionsMenuBarTitles("Script");
84+
}
85+
8186
public ActionsMenuBarTitles Java_Class() {
8287
append("Java Class");
8388
return this;
@@ -87,7 +92,7 @@ public ActionsMenuBarTitles Java_Constructor() {
8792
append("Java Constructor");
8893
return this;
8994
}
90-
95+
9196
public ActionsMenuBarTitles Java_Method() {
9297
append("Java Method");
9398
return this;
@@ -97,17 +102,17 @@ public ActionsMenuBarTitles Java_Fields() {
97102
append("Java Fields");
98103
return this;
99104
}
100-
105+
101106
public ActionsMenuBarTitles Undo() {
102107
append("Undo");
103108
return this;
104109
}
105-
110+
106111
public ActionsMenuBarTitles Redo() {
107112
append("Redo");
108-
return this;
109-
}
110-
113+
return this;
114+
}
115+
111116
public ActionsMenuBarTitles Remove() {
112117
append("Remove");
113118
return this;
@@ -125,37 +130,42 @@ public ActionsMenuBarTitles Font_Size() {
125130

126131
public ActionsMenuBarTitles Point_Size() {
127132
append("Point Size...");
128-
return this;
129-
}
133+
return this;
134+
}
130135

131136
public ActionsMenuBarTitles Zoom_In() {
132137
append("Zoom In");
133138
return this;
134139
}
135-
140+
136141
public ActionsMenuBarTitles Zoom_Out() {
137142
append("Zoom Out");
138143
return this;
139144
}
140145

146+
public ActionsMenuBarTitles Enter_Script() {
147+
append("Enter Script");
148+
return this;
149+
}
150+
141151
public ActionsMenuBarTitles Annotations() {
142152
append("Annotations");
143153
return this;
144154
}
145155

146-
147156
public ActionsMenuBarTitles Instance_Operation() {
148157
append("Instance Operation Component");
149158
return this;
150159
}
151-
160+
152161
public ActionsMenuBarTitles Add_Instance_Method() {
153162
append("Add Instance Method");
154163
return this;
155164
}
156-
165+
157166
public ActionsMenuBarTitles Annotation_Font_Size() {
158167
append("Annotation Font Size");
168+
159169
return this;
160170
}
161171
}

src/script/ComponentMap.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package script;
2+
3+
import java.util.HashMap;
4+
5+
import paintcomponents.PaintComponent;
6+
7+
public class ComponentMap {
8+
public static HashMap<String, PaintComponent> map = new HashMap<String, PaintComponent>();
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package script;
2+
3+
public class ExecutionErrorException extends Exception {
4+
5+
public ExecutionErrorException(String message) {
6+
super(message);
7+
// TODO Auto-generated constructor stub
8+
}
9+
10+
}

src/script/Interpreter.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package script;
2+
3+
import java.util.ArrayList;
4+
5+
import ui.PaintPanel;
6+
import paintcomponents.data.DataDisplayPaintComponent;
7+
import actions.edit.undoredo.SharedUndoRedoActionManager;
8+
import actions.edit.undoredo.UndoRedoableInterface;
9+
import paintcomponents.*;
10+
11+
/**
12+
* Interpret and execute scripts
13+
* @author Xiaoquan Jiang
14+
*/
15+
public class Interpreter {
16+
17+
private static final String SELECT = "select";
18+
private static final String ZOOM = "zoom";
19+
private static final String REMOVE = "remove";
20+
private static final String UPDATE = "update";
21+
private static final String GENERATE_POLYGON = "generatePolygon";
22+
private static final String FILE = "file";
23+
private static final String EDIT = "edit";
24+
private static final String CONSTRUCT = "construct";
25+
private static final String ADD = "add";
26+
protected PaintPanel panel;
27+
28+
public Interpreter(PaintPanel panel) {
29+
this.panel = panel;
30+
}
31+
32+
public void interpreteLine(String script) throws ExecutionErrorException {
33+
34+
Tokenizer tokenizer = new Tokenizer(script);
35+
36+
// interpret the first token and pass the tokenizer to a sub interpreter
37+
if (tokenizer.hasNext()) {
38+
switch (tokenizer.next()) {
39+
case ADD:
40+
new InterpreterAddActions(tokenizer, panel);
41+
break;
42+
43+
case CONSTRUCT:
44+
new InterpreterConstructActions(tokenizer, panel);
45+
break;
46+
47+
case EDIT:
48+
new InterpreterEditActions(tokenizer, panel);
49+
break;
50+
51+
case FILE:
52+
new InterpreterFileActions(tokenizer, panel);
53+
break;
54+
55+
case GENERATE_POLYGON:
56+
new InterpreterGeneratePolygonAction(tokenizer, panel);
57+
break;
58+
59+
case UPDATE:
60+
new InterpreterUpdateActions(tokenizer, panel);
61+
break;
62+
63+
case REMOVE:
64+
new InterpreterRemoveAction(tokenizer, panel);
65+
break;
66+
67+
case SELECT:
68+
new InterpreterSelectAction(tokenizer, panel);
69+
break;
70+
71+
default:
72+
throw new ExecutionErrorException("invalid script");
73+
}
74+
} else {
75+
throw new ExecutionErrorException("Script is empty");
76+
}
77+
}
78+
}
79+
80+
/* Scripts:
81+
add
82+
data
83+
displayBox
84+
inputBox
85+
haskell
86+
component
87+
haskellCompoment
88+
lazy
89+
javaClass
90+
javaConstructor
91+
javaFieldsComponent
92+
javaMethodComponent
93+
textBox
94+
construct
95+
dataLineSegment
96+
lineSegment
97+
edit
98+
redo
99+
undo
100+
file
101+
saveAs == save
102+
open
103+
generatePolygon
104+
update
105+
dataBox
106+
inputBox
107+
remove
108+
*/

0 commit comments

Comments
 (0)