NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import java.io.*;
import java.util.*;

public class Main {

public static ArrayList<String> variables = new ArrayList<>();
public static int variableCounter = 1;
public static BufferedWriter bw;
public static void main(String[] args) throws IOException {

FileWriter outFile = new FileWriter(args[0].substring(args[0].indexOf("."))+"ll");
BufferedWriter bw = new BufferedWriter(outFile);

if (errorCheck(args)) {
printHead();
Scanner console = new Scanner(new File("input.txt"));
String line = "", lhs = "", rhs = "";
while (console.hasNextLine()) {
line = console.nextLine();
line = line.replaceAll("\s+", "");
if (line.contains("=")) {
lhs = line.substring(0, line.indexOf("="));
if (!variables.contains(lhs))
bw.write("%" + lhs + " = alloca i32");
variables.add(lhs);
rhs = line.substring(line.indexOf("=") + 1);
rhs = inf2postf(rhs);
expression(rhs, true, lhs); // expression, willBeAssigned, lhs
} else {
if (line.contains("*") || line.contains("/") || line.contains("+") || line.contains("-")) {
line = inf2postf(line);
line = expression(line, false, ""); // expression, willBeAssigned, lhs
bw.write("call i32 (i8*, ...)* @printf(i8* getelementptr ([4 x i8]* @print.str, i32 0, i32 0), i32 %" + (variableCounter - 1) + " )");
} else {
if (!isInteger(line)) bw.write("%" + variableCounter++ + " = load i32* %" + line);
bw.write("call i32 (i8*, ...)* @printf(i8* getelementptr ([4 x i8]* @print.str, i32 0, i32 0), i32 %" + (variableCounter - 1) + " )");
variableCounter++;
}
}
}
bw.write("ret i32 0n}");
}
}

private static boolean errorCheck(String[] args) throws FileNotFoundException{
Scanner console = new Scanner(new File("input.txt"));
ArrayList<String> definedVariables = new ArrayList<>();
String line = "";
int lineNum = 0;
while (console.hasNextLine()){
line = console.nextLine();
line = line.replaceAll("\s+", "");
lineNum++;

// parenthesis check
if (!parenthesisCheck(line)){
System.out.print("Error: Line: "+ lineNum + ": parenthesis error");
return false;
}

// lhs have operand
if (line.contains("=")) {
String lhs = line.substring(0, line.indexOf("="));
if (lhs.contains("+") || lhs.contains("-") || lhs.contains("*") || lhs.contains("/")) {
System.out.print("Error: Line: " + lineNum + ": lefthandside cannot have an operand");
return false;
}
}

// string starts and ends with operand
if ("/*-+".contains(line.charAt(0)+"") || "/*-+".contains(line.charAt(line.length()-1)+"")){
System.out.print("Error: Line: " + lineNum + ": an expression cannot start or end with an operand");
return false;
}
if (line.contains("=")){
if("/*-+".contains(line.charAt(line.indexOf("=")+1)+"") || "/*-+".contains(line.charAt(line.indexOf("=")-1)+"")){
System.out.print("Error: Line: " + lineNum + ": an expression cannot start or end with an operand");
return false;
}
}
// successive operand
String temp = "";
String newLine = line.substring(line.indexOf("=") + 1);
for (int i = 0; i < newLine.length(); i++) {
if ("+/-*".contains(newLine.charAt(i)+"")){
if (i + 1 < newLine.length()){
if ("+/-*".contains(newLine.charAt(i+1)+"")){
System.out.print("Error: Line: "+ lineNum + ": successive operand");
return false;
}
}
}
}

// undefined variable
if (line.contains("=")){
String lhs = line.substring(0, line.indexOf("="));
if (!definedVariables.contains(lhs)) definedVariables.add(lhs);
String rhs = line.substring(line.indexOf("=")+1);
rhs = inf2postf(rhs);
String str = "";
if (rhs.length() != 0 && !rhs.contains(",")){
if (!isInteger(rhs) && !definedVariables.contains(rhs)){
System.out.print("Error: Line: "+ lineNum + ": undefined variable");
return false;
} else continue;
}
for (int i = 0; i < rhs.length(); i++) {
if (!(rhs.charAt(i)+"").equals(",")){
str += rhs.charAt(i);
continue;
}
if (!"+-*/".contains(str) && !isInteger(str) && !definedVariables.contains(str)){
System.out.print("Error: Line: "+ lineNum + ": undefined variable");
return false;
}
str = "";
}
}

}
return true;
}

private static boolean parenthesisCheck(String line) {
int c = 0;
for (int i = 0; i < line.length(); i++) {
if ((""+line.charAt(i)).equals("(")) c++;
else if ((""+line.charAt(i)).equals(")")) c--;
if (c < 0) return false;
}
return c == 0;
}

private static String expression(String line, Boolean isAssign, String lhs) {
Stack<String> stack = new Stack<>();
while (line.length() != 0){
String temp = "";
if (line.contains(",")){
temp = line.substring(0, line.indexOf(","));
line = line.substring(line.indexOf(",") + 1);
if (!"+-*/".contains(temp)) {
stack.push(temp);
} else {
stack.push(calculate(stack.pop(), stack.pop(), temp));
}
} else if (!stack.empty()){
stack.push(calculate(stack.pop(), stack.pop(), line));
line = "";
} else {
stack.push(line);
line = "";
}
}
if (isAssign){
try {
if (isInteger(stack.peek())) bw.write("store i32 " + stack.peek() + ", i32* " + "%" + lhs);
else bw.write("store i32 %" + (variableCounter - 1) + ", i32* " + "%" + lhs);
}catch (IOException e) {
e.printStackTrace();
}
}
return stack.pop();
}
private static String calculate(String f, String s, String opr){
f = loadIfNot(f);
s = loadIfNot(s);
if (f.substring(0,1).equals("%")) f = "%" + (Integer.parseInt(f.substring(1))-1) + "";
if (s.substring(0,1).equals("%")) s = "%" + (Integer.parseInt(s.substring(1))-1) + "";
try {
if (opr.equals("*")) {
bw.write("%" + variableCounter++ + " = mul i32 " + s + "," + f);
} else if (opr.equals("/")) {
bw.write("%" + variableCounter++ + " = sdiv i32 " + s + "," + f);
} else if (opr.equals("+")) {
bw.write("%" + variableCounter++ + " = add i32 " + s + "," + f);
} else {
bw.write("%" + variableCounter++ + " = sub i32 " + s + "," + f);
}
}catch (IOException e) {
e.printStackTrace();
}
return "%"+ variableCounter;
}
private static String loadIfNot(String str) {
if (isInteger(str) || str.substring(0,1).equals("%")) return str;
else try {
bw.write("%" + variableCounter++ + " = load i32* %" + str);
} catch (IOException e) {
e.printStackTrace();
}
return "%"+variableCounter;
}

private static String inf2postf(String infix) {
String postfix = "";
Stack<String> operator = new Stack<>();
String popped;
for (int i = 0; i < infix.length(); i++) {
String get = infix.charAt(i) + "";
while (!"+-*/()".contains(get) && i < infix.length()-1 && !"+-*/()".contains(infix.charAt(i+1)+"")){
get += infix.charAt(i+1)+"";
i++;
}
if (!isOperator(get))
postfix += get + ",";
else if (get.equals(")"))
while (!(popped = operator.pop()).equals("("))
postfix += popped + ",";
else {
while (!operator.isEmpty() && !get.equals("(") && precedence(operator.peek()) >= precedence(get))
postfix += operator.pop() + ",";
operator.push(get);
}
}
while (!operator.isEmpty())
postfix += operator.pop() + ",";
return postfix.substring(0,postfix.length()-1);
}
private static boolean isOperator(String i) {
return precedence(i) > 0;
}
private static int precedence(String i) {
if (i.equals("(") || i.equals(")")) return 1;
else if (i.equals("-") || i.equals("+")) return 2;
else if (i.equals("*") || i.equals("/")) return 3;
else return 0;
}

private static boolean isInteger(String str) {
if (str == null) return false;
int length = str.length();
if (length == 0) return false;
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) return false;
i = 1;
}
for (; i < length; i++) {
char c = str.charAt(i);
if (c < '0' || c > '9') return false;
}
return true;
}
private static void printHead() {
try {
bw.write("; ModuleID = 'stm2ir'n" +
"declare i32 @printf(i8*, ...)n" +
"@print.str = constant [4 x i8] c"%d\0A\00"nn" +
"define i32 @main() {");
} catch (IOException e) {
e.printStackTrace();
}
}

}



     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.