-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomInterpreter.java
More file actions
147 lines (147 loc) · 4.62 KB
/
CustomInterpreter.java
File metadata and controls
147 lines (147 loc) · 4.62 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
/*Imagine a fictional programming language, which supports basic arithmetic operations, +
(addition), - (subtraction), / (division) and (multiplication).
Declaring variables is done using the svar command:
svar a; svar b;
The basic assignment operator is supported:
a = 10; b = 3.14;
The print command outputs the value of a variable or an expression:
print a;
Each statement must and with a semicolon.
Implement a custom interpreter that reads and executes a given source code corresponding to the
rules defined above.
Example Source Code:
8
svar a;
a = 44;
svar b;
b = 31;
svar str;
str = "Hello";
print str;
print a + b;
The program above should output:
Hello
75
The custom interpreter should also handle and notify about syntax errors in the source code.
*/
import java.util.*;
public class CustomInterpreter{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the Estimated lines of Code");
int loc = in.nextInt();
in.nextLine();
String locs[] = new String[loc];
System.out.println("Write your code here");
for (int i=0;i<loc ;i++ ) {
locs[i] = in.nextLine();
}
System.out.println("\nCompiling Output...\n");
for (int i=0;i<loc ;i++ ) {
if (!locs[i].contains(";")) {
System.out.println("Semicolon missing at line number "+(i+1));
System.exit(1);
}
}
String variables[] = new String[loc];
int vid = 0;
String varvalues[] = new String[loc];
for (int i=0;i<loc ;i++ ) {
if (locs[i].contains("svar")) {
locs[i] = locs[i].replaceAll("[^a-zA-Z0-9 ]", "");
String strsplit[] = locs[i].split(" ");
variables[vid] = strsplit[1];
vid++;
}else if(locs[i].contains("=")){
locs[i] = locs[i].replaceAll("[^a-zA-Z0-9=]", "");
String strsplit[] = locs[i].split("=");
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(strsplit[0])) {
varvalues[j] = strsplit[1];
break;
}
}
}else if (locs[i].contains("print")) {
StringBuilder string = new StringBuilder(locs[i]);
string.setCharAt(locs[i].length()-1, ' ');
locs[i] = string.toString();
String strsplit[] = locs[i].split(" ");
String subsplit2[] = strsplit[1].split(" ");
if (strsplit[1].contains("+")||strsplit[1].contains("-")||strsplit[1].contains("*")||strsplit[1].contains("/")){
if (strsplit[1].contains("+")) {
String subsplit[] = strsplit[1].split("\\+");
int num1 = 0, num2 = 0;
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(subsplit[0])) {
num1 = Integer.parseInt(varvalues[j]);
break;
}
}
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(subsplit[1])) {
num2 = Integer.parseInt(varvalues[j]);
break;
}
}
System.out.println(num1+num2);
}else if (strsplit[1].contains("-")) {
String subsplit[] = strsplit[1].split("\\-");
int num1 = 0, num2 = 0;
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(subsplit[0])) {
num1 = Integer.parseInt(varvalues[j]);
break;
}
}
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(subsplit[1])) {
num2 = Integer.parseInt(varvalues[j]);
break;
}
}
System.out.println(num1-num2);
}else if (strsplit[1].contains("*")) {
String subsplit[] = strsplit[1].split("\\*");
int num1 = 0, num2 = 0;
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(subsplit[0])) {
num1 = Integer.parseInt(varvalues[j]);
break;
}
}
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(subsplit[1])) {
num2 = Integer.parseInt(varvalues[j]);
break;
}
}
System.out.println(num1*num2);
}else if (strsplit[1].contains("/")) {
String subsplit[] = strsplit[1].split("\\/");
int num1 = 0, num2 = 0;
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(subsplit[0])) {
num1 = Integer.parseInt(varvalues[j]);
break;
}
}
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(subsplit[1])) {
num2 = Integer.parseInt(varvalues[j]);
break;
}
}
System.out.println(num1/num2);
}
}else{
for (int j=0;j<variables.length ;j++ ) {
if (variables[j].equals(subsplit2[0])) {
System.out.println(varvalues[j]);
break;
}
}
}
}
}
}
}