-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethods.java
More file actions
160 lines (141 loc) · 4.96 KB
/
Copy pathMethods.java
File metadata and controls
160 lines (141 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.ArrayList;
import java.util.Scanner;
/**
* Methods
* Code for executing elvish methods
*
* @author CC Coding Club
* @version 0.4
*/
public class Methods
{
Scanner scan = new Scanner(System.in);
/**
* Elvish method for displaying to console
*
* @param code the entire .elv file
* @param lParenLoc location of the ( before the @show argument to be printed
* @param variables list of variables and their contents in case one appears in arg
*/
public void show(String code, int lParenLoc, ArrayList<Variable> variables)
{
String arg = "";
String finalString = "";
int pointer = lParenLoc;
//find second quote symbol:
int rParenLoc = code.indexOf(")", lParenLoc + 1);
//get the argument being sent to show()
String contents = code.substring(lParenLoc + 1, rParenLoc);
boolean inString = false;
boolean inVariable = false;
String varName = "";
//loop through all characters, finding Strings or String variables:
for(int i = 0; i < contents.length(); i++)
{
//find characters between quotes:
String c = contents.substring(i, i + 1);
//if not a quote, add to String:
if(inString && !c.equals("\"")) arg += c;
if(c.equals("\"") && !inString)
{
inString = true; //beginning of String
}
else if(c.equals("\"") && inString)
{
inString = false;
}
//start or inside of variable name:
else if(Character.isAlphabetic(c.charAt(0)))
{
//variable name starting:
if(!inVariable)
{
inVariable = true;
varName = c;
}
else //adding to variable name:
{
varName += c;
}
if(i == contents.length() - 1)
{
//loop through all variables, checking .name
for(Variable v : variables)
{
if(v.name.equals(varName)) //found the variable object
{
System.out.print(arg);
arg += v.contents;
}
}
}
}
//reached end of variable name:
else if(inVariable)
{
//loop through all variables, checking .name
for(Variable v : variables)
{
if(v.name.equals(varName)) //found the variable object
{
arg += v.contents;
}
}
inVariable = false;
}
}
System.out.println(arg);
}
/**
* displays message and returns user input
*
* @param code the entire elvish file
* @param mostRecentPipeLoc location of end of previous line of code
* @param variables list of variables and their contents
* @param lParenLoc location of start of message argument
*
* POSTCONDITION: variable has been added with contents
* TODO: take into account, variable might already have been declared!!!
*/
public void get(String code, int mostRecentPipeLoc,
int pointer, ArrayList<Variable> variables, int lParenLoc)
{
//find the =, which implies variable assignment:
int equalsLoc = code.indexOf('=', mostRecentPipeLoc);
//determine variable name:
String var1name = code.substring(mostRecentPipeLoc, equalsLoc).trim();
//find end of argument to be printed:
int endQuoteLoc = code.indexOf("\"", lParenLoc + 2);
String arg = code.substring(pointer + 2, endQuoteLoc);
boolean varExists = false;
System.out.println(arg);
//wait for user input:
String var1 = scan.nextLine();
//check if variable already exists:
for(Variable v : variables)
{
//change contents of variable:
if(v.name.equals(var1name.substring(1, var1name.length())))
{
varExists = true;
v.contents = var1;
}
}
//create new variable:
if(!varExists)
{
Variable v;
//if variable is a number
if(Character.isDigit(var1.charAt(0)))
{
v = new Variable(var1name, var1, "num");
}
//if variable is a string
else
{
v = new Variable(var1name, var1, "string");
}
variables.add(v);
}
}
}