-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.java
160 lines (149 loc) · 4.54 KB
/
README.java
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
// Lab_2
//This is my lab task in which i made a simple interpreter using java
import java.util.*;
import java.io.*;
public class Interpreter {
Map<String, Integer> m = new HashMap<String, Integer>(); //one-one maping between name and value of variable
@SuppressWarnings("null")
public void read_file(String file_Name) throws IOException{ //rading and parsing file
String operator = null; //use to svae operator as String
String variable_name = null;
int variable = 0,variable_1=0,variable_2=0;
FileReader fr = new FileReader(file_Name); //file reading using buffer reader
BufferedReader br = new BufferedReader(fr);
String buffer; //buffer used to read line by line
int z=0;
String var_name = null;
int var_value =0;
while((buffer= br.readLine())!=null){ //read file line by line
z=0;
operator=null; //use to store operator sign
boolean flag_ope=false; //flag use so no consective operand or variavle
String[] token = buffer.split(" "); //split each line by space as delimiter
if(token[0].compareTo("Let")==0){ //if line start by Let mean its intializing a variable
z++;
if(z>=token.length){
System.out.println("Syntax error");
System.exit(1);
}
var_name = token[z];
if(Character.isLetter(var_name.charAt(0))){
z++;
if(z>=token.length){
System.out.println("Syntax error");
System.exit(1);
}
if(m.containsKey(token[z-1])){
System.out.println("Syntax Error "+token[z-1]+" Already Declared.\n");
System.exit(1);
}
if(token[z].compareTo("=")==0){
if(z<token.length){
z++;
if(z<=token.length){
int is_number=1;
for(int i=0;i<token[z].length();i++){
if(!Character.isDigit(token[z].charAt(i)))
is_number=0;
}
if(is_number==1){
var_value = Integer.parseInt(token[z]);
m.put(var_name, var_value);
}
else{
System.out.println("Cant initalized character");
}
}
else
System.out.println("Syntax error");
}
else
System.out.println("Syntax error");
}
else{
System.out.println("Error: Unintialized variable;");
}
}
}
else if(token[z].compareTo("Print")==0){
z++;
if(m.containsKey(token[z])){
System.out.println("The value of "+token[z]+" = "+m.get(token[z]));
}
else{
System.out.println(token[z]+" is undecalared identifier");
}
}
else{
if(Character.isLetter(token[z].charAt(0))){
if(m.containsKey(token[z])){
variable_name = token[z];
variable = m.get(variable_name);
//z++;
}
else
System.out.println(token[z]+" is undecalared identifier");
z++;
if(token[z].compareTo("=")==0){
z++;
for(int x=z;x<token.length;x++){
if(Character.isLetter(token[x].charAt(0)) || Character.isDigit(token[x].charAt(0)) && flag_ope == false){
if(Character.isDigit(token[x].charAt(0)))
variable_1 = Integer.parseInt(token[x]);
else if(Character.isLetter(token[x].charAt(0))){
if(m.containsKey(token[x])){
variable_1 = m.get(token[x]);
m.put(variable_name,variable_1);
}
else
System.out.println(token[z]+" is undecalared identifier");
}
if(operator != null){
variable_2 = operate(variable ,variable_1,operator);
variable = variable_2;
m.put(variable_name, variable);
}
flag_ope =true;
}
else if(flag_ope == true){
if(token[x].compareTo("+")==0 ||token[x].compareTo("-")==0 || token[x].compareTo("*")==0||token[x].compareTo("/")==0){
operator = token[x];
if((x+1)>=token.length){
System.out.println("Syntax Error");
System.exit(1);
}
}
flag_ope = false;
}
}
}
else
System.out.println("Syntax Error:");
}
}
}
}
private int operate(int operand_1,int operand_2,String operator){
switch(operator){
case "+":
return (operand_1+operand_2);
case "-":
return (operand_1-operand_2);
case "*":
return (operand_1*operand_2);
case "/":
return (operand_1/operand_2);
default:
}
return 0;
}
public static void main(String[] args) throws IOException {
Interpreter i = new Interpreter();
Interpreter i1 = new Interpreter();
Interpreter i2 = new Interpreter();
i.read_file("testFile_1.txt");
i1.read_file("testFile_2.txt");
i2.read_file("testFile_3.txt");
// TODO Auto-generated method stub
}
}