Skip to content

Instantly share code, notes, and snippets.

@hakxcore
Created May 9, 2022 02:44
Show Gist options
  • Select an option

  • Save hakxcore/bbf2b5692aeb55252ce57ee2deeea851 to your computer and use it in GitHub Desktop.

Select an option

Save hakxcore/bbf2b5692aeb55252ce57ee2deeea851 to your computer and use it in GitHub Desktop.
## input
void main(){
printf("hello world");
int i=0;
while(i<5){
printf("Hello");
printf("World");
i++;
}
printf("loop done");
## WhileConverter.java
import java.io.*;
public class WhileConverter {
String init="";
String condititon="";
String update="";
String prevCode="";
String loopCode="";
String latterCode="";
public void converter(BufferedReader br,String cur) {
int index=6;
String prev="",newLine="";
while(cur.charAt(index)!= ')'){
condititon=condititon+cur.charAt(index);
index++;
}
try{
while(!newLine.equals("}")){
loopCode=loopCode+"\n"+prev;
prev=newLine;
newLine=br.readLine();
}
}catch(Exception e){
System.out.println(e);
}
update=prev.split(";")[0];
try{
while(newLine != null){
newLine=br.readLine();
latterCode=latterCode+"\n"+newLine;
}
}catch(Exception e){
System.out.println(e);
}
}
void Printer(BufferedWriter bw){
try{
bw.write(prevCode.trim()+"\n");
bw.write("for("+init+condititon+";"+update+")");
bw.write("{\n"+"\t"+loopCode.trim()+"\n}\n");
latterCode=latterCode.split("null")[0];
bw.write(latterCode.trim());
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) {
WhileConverter obj = new WhileConverter();
try{
FileReader fr = new FileReader("input");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("output");
BufferedWriter bw = new BufferedWriter(fw);
String cur="",prev="";
while(cur != null){
obj.prevCode=obj.prevCode+"\n"+prev;
prev = cur;
cur = br.readLine();
if(cur.contains("while")){
obj.init=prev;
obj.converter(br,cur);
obj.Printer(bw);
break;
}
}
br.close();
bw.close();
}catch(Exception e){
System.out.println(e);
}
}
}
## output
void main(){
printf("hello world");
for(int i=0;i<5; i++){
printf("Hello");
printf("World");
}
printf("loop done");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment