See More

import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ErrorNode; import org.antlr.v4.runtime.tree.ParseTreeProperty; import org.antlr.v4.runtime.tree.TerminalNode; import javax.print.DocFlavor; import java.sql.SQLOutput; import java.util.Hashtable; import java.util.Stack; public class tinyPythonToJasmin extends tinyPythonBaseListener{ public StringBuilder fffffinal = new StringBuilder(); public ParseTreeProperty newTexts = new ParseTreeProperty<>(); public Hashtable hashtable_method_public = new Hashtable<>(); public Stack Stack = new Stack<>(); static int argumentCount = 0; static int labelCount = 0; int method = 0; @Override public void enterFile_input(tinyPythonParser.File_inputContext ctx) { super.enterFile_input(ctx); labelCount = 0; } @Override public void exitFile_input(tinyPythonParser.File_inputContext ctx) { super.exitFile_input(ctx); // init code String f = """ .class public Test .super java/lang/Object .method public ()V aload_0 invokespecial java/lang/Object/()V return .end method """; f += newTexts.get(ctx.defs())+"\n"; f +=""" .method public static main([Ljava/lang/String;)V .limit stack 32 .limit locals 32 """; for(int i = 0; i")) { newTexts.put(ctx, "if_icmple"); } else if (com.equals("==")) { newTexts.put(ctx, "if_icmpne"); } else if (com.equals(">=")) { newTexts.put(ctx, "if_icmplt"); } else if (com.equals("<=")) { newTexts.put(ctx, "if_icmpgt"); } else if (com.equals("!=")) { newTexts.put(ctx, "if_icmpeq"); } } @Override public void enterExpr(tinyPythonParser.ExprContext ctx) { super.enterExpr(ctx); } @Override public void exitExpr(tinyPythonParser.ExprContext ctx) { super.exitExpr(ctx); String s = ""; if(ctx.getChildCount() == 1){ //NUMBER String number = ctx.NUMBER().getText(); s = "ldc "+number; newTexts.put(ctx, s); } else if (ctx.getChild(1).getText().equals("+") || ctx.getChild(1).getText().equals("-")) { // + or - String expr1 = newTexts.get(ctx.expr(0)); String expr2 = newTexts.get(ctx.expr(1)); if (ctx.getChild(1).getText().equals("+")){ s = expr1+"\n"+expr2+"\n"+"iadd"; } else{ s = expr1+"\n"+expr2+"\n"+"isub"; } newTexts.put(ctx, s); } else if (ctx.getChild(0).getText().equals("(") && ctx.getChild(2).getText().equals(")")) { // ( expr ) String expr = newTexts.get(ctx.expr(0)); newTexts.put(ctx, expr); } else{ String opt = newTexts.get(ctx.opt_paren()); if (opt.equals("")){ // 지역변수 사용 int num =Stack.indexOf(ctx.NAME().getText()); s = "iload "+num; newTexts.put(ctx, s); } else if (opt.equals("()")) { // invokestatic [[NAME]]fs String func_info = hashtable_method_public.get(ctx.NAME().getText()); newTexts.put(ctx, func_info); } else { String func_info = hashtable_method_public.get(ctx.NAME().getText()); for (int i=0; i