程序代写 COP5556 Programming Language Principles at the

/** This code is provided for solely for use of students in the course COP5556 Programming Language Principles at the
* University of Florida during the Fall Semester 2022 as part of the course project. No other use is authorized.

package edu.ufl.cise.plpfa22;

Copyright By PowCoder代写 加微信 powcoder

import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import edu.ufl.cise.plpfa22.CodeGenUtils.DynamicClassLoader;
import edu.ufl.cise.plpfa22.CodeGenUtils.GenClass;
import edu.ufl.cise.plpfa22.ast.ASTNode;
import edu.ufl.cise.plpfa22.ast.PrettyPrintVisitor;
import edu.ufl.cise.plpfa22.codeGenSamples.TestRecursiveDump;
import edu.ufl.cise.plpfa22.codeGenSamples.TestRecursive$pDump;
import edu.ufl.cise.plpfa22.codeGenSamples.TestRecursive$qDump;

public class CodeGenTests2 {

* Generates classfiles for the given source program. The classfile containing the main method has the given name and package.
* Other classfiles are synthetic inner classes that correspond to procedures.
* @param input
* @param className
* @param packageName
* @return List of CodeGenUtils.GenClass records
* @throws Exception
List compile(String input, String className, String packageName) throws Exception {
show(“*****************”);
show(input);
ILexer lexer = CompilerComponentFactory.getLexer(input);
ASTNode ast = CompilerComponentFactory.getParser(lexer).parse();
ast.visit(CompilerComponentFactory.getScopeVisitor(), null);
ast.visit(CompilerComponentFactory.getTypeInferenceVisitor(), null);
show(ast);
List classes = (List) ast.visit(CompilerComponentFactory.getCodeGenVisitor(className, packageName, “”), null);
show(classes);
show(“—————-“);
return classes;

Object loadClassesAndRunMain(List classes, String className) throws Exception{
DynamicClassLoader loader = new DynamicClassLoader();
Class mainClass = loader.define(classes);
Object[] args = new Object[1];
return runMethod(mainClass, “main”, args);

private Method findMethod(String name, Method[] methods) {
for (Method m : methods) {
String methodName = m.getName();
if (name.equals(methodName))
throw new RuntimeException(“Method ” + name + ” not found in generated bytecode”);

Object runMethod(Class testClass, String methodName, Object[] args) throws Exception {
Method[] methods = testClass.getDeclaredMethods();
Method m = findMethod(methodName, methods);
return m.invoke(null, args);

static boolean VERBOSE = true;
void show(Object o) {
if (VERBOSE) {
System.out.println(o);
void show(byte[] bytecode) {
show(CodeGenUtils.bytecodeToString(bytecode));

void show(GenClass genClass) {
show(genClass.className());
show(genClass.byteCode());

void show(List classes) {
for(GenClass aClass: classes) show(aClass);

void show(ASTNode ast) throws PLPException {
if(VERBOSE) {if (ast != null) {System.out.println(PrettyPrintVisitor.AST2String(ast));}
else {System.out.println(“ast = null”);}

@DisplayName(“numOut”)
public void numout(TestInfo testInfo)throws Exception {
String input = “””
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“stringOut”)
public void stringout(TestInfo testInfo)throws Exception {
String input = “””
! “hello world”
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“booleanOut”)
public void booleanOut(TestInfo testInfo)throws Exception {
String input = “””
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“statementBlock”)
public void statementBlock(TestInfo testInfo)throws Exception{
String input = “””
! “hey, it works!”
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“intOps”)
public void intOps(TestInfo testInfo)throws Exception{
String input = “””
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“intEqOps”)
public void intEqOps(TestInfo testInfo) throws Exception {
String input = “””

String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“boolEqOps”)
public void boolEqOps(TestInfo testInfo) throws Exception {
String input = “””
! TRUE = TRUE;
! TRUE # TRUE;
! FALSE = FALSE;
! FALSE # FALSE;
! TRUE = FALSE;
! TRUE # FALSE;
! FALSE = TRUE;
! FALSE # TRUE;

String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“intRelOps”)
public void intRelOps(TestInfo testInfo) throws Exception {
String input = “””

String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“boolRelOps”)
public void boolRelOps(TestInfo testInfo) throws Exception {
String input = “””
! FALSE < TRUE; ! FALSE <= TRUE; ! FALSE > TRUE;
! FALSE >= TRUE;
! TRUE < TRUE; ! TRUE <= TRUE; ! TRUE > TRUE;
! TRUE >= TRUE;
! TRUE < FALSE; ! TRUE <= FALSE; ! TRUE > FALSE;
! TRUE >= FALSE ;
! FALSE < FALSE; ! FALSE <= FALSE; ! FALSE > FALSE;
! FALSE >= FALSE

String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“stringEqOps”)
public void stringEqOps(TestInfo testInfo) throws Exception {
String input = “””
! “red” = “blue”;
! “red”= “red”;
! “red” # “blue”;
! “red” # “red”

String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“stringRelOps”)
public void stringRelOps(TestInfo testInfo) throws Exception {
String input = “””
! “FA” < "FALSE"; ! "FA" <= "FALSE"; ! "FA" > “FALSE”;
! “FA” >= “FALSE”;
! “FALSE” < "FALSE"; ! "FALSE" <= "FALSE"; ! "FALSE" > “FALSE”;
! “FALSE” >= “FALSE”;
! “FALSE” < "FA"; ! "FALSE" <= "FA"; ! "FALSE" > “FA”;
! “FALSE” >= “FA” ;
! “FA” < "FA"; ! "FA" <= "FA"; ! "FA" > “FA”;
! “FA” >= “FA”

String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“constants”)
public void constants(TestInfo testInfo) throws Exception{
String input = “””
CONST a=33, b=”HELLO”, c=FALSE;
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“template”)
public void template(TestInfo testInfo) throws Exception{
String input = “””
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“const1”)
public void const1(TestInfo testInfo) throws Exception{
String input = “””
CONST a = 3;
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“proc1”)
public void proc1(TestInfo testInfo) throws Exception{
String input = “””
CONST a = 22, b = “HELLO”, c = FALSE;
PROCEDURE p1;
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“var1”)
public void var1(TestInfo testInfo) throws Exception{
String input = “””
VAR a,b,c;
b := “hello”;
c := TRUE;
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“var2”)
public void var2(TestInfo testInfo) throws Exception{
String input = “””
VAR a,b,c;
PROCEDURE p;
b := “hello”;
c := TRUE;
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“var3”)
public void var3(TestInfo testInfo) throws Exception{
String input = “””
VAR a,b,c;
PROCEDURE p;
PROCEDURE q;
b := “hello”;
c := TRUE;
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“proc2”)
public void proc2(TestInfo testInfo) throws Exception{
String input = “””
VAR a,b,c;
PROCEDURE p;
PROCEDURE q;
! “in q, calling q1”;
//! “debug run, no CALL q1”;
//end of q
IF a > 0 THEN BEGIN ! “calling q”; CALL q END
//end of p
PROCEDURE q1;
PROCEDURE r;
! ” in r, calling p”;
//end of r
! “in q1 calling r”;
// end of q
BEGIN //main
! “in main calling p, a=1”;
! “terminating back in main”
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“rec2”)
public void rec2(TestInfo testInfo) throws Exception{
String input = “””
PROCEDURE p;
! “in p, a=”;
IF a >= 0 THEN CALL q
END;//end of p

PROCEDURE q;
! “in q, a=”;
IF a >= 0 THEN CALL p
END;//end of q
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“rec1”)
public void rec1(TestInfo testInfo) throws Exception{
String input = “””
PROCEDURE p;
! “in p, a=”;
IF a >= 0 THEN CALL p
END;//end of p

String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“while0”)
public void while0(TestInfo testInfo) throws Exception{
String input = “””
VAR a, minus1;

minus1 := 0-1;
WHILE a > minus1 DO BEGIN !a; a := a-1 END
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“paramtest0”)
public void paramtest0(TestInfo testInfo) throws Exception{
String input = “””
IF (“FA” <= "FALSE") ! ((3+4)*10)%2 String shortClassName = "prog"; String JVMpackageName = "edu/ufl/cise/plpfa22"; List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“paramtest1”)
public void paramtest1(TestInfo testInfo) throws Exception{
String input = “””
CONST a=123, b=346;
c:=((a*b)+(a+b));
d:=c%(a+b+c);
! (c/d) >= 0
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“paramtest2”)
public void paramtest2(TestInfo testInfo) throws Exception{
String input = “””
CONST d=23, e=34, f=45, g=TRUE;
VAR a,b,c;
a:=”IF PASSED”;
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“paramtest3”)
public void paramtest3(TestInfo testInfo) throws Exception{
String input = “””
! (FALSETRUE);
! (FALSE+TRUE)*(FALSE*TRUE);
! (FALSE*TRUE)*(FALSE*TRUE);
((FALSE+FALSE)+(FALSE*TRUE)+FALSE)+(FALSE*(FALSE+TRUE)*(FALSE*TRUE))
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“paramtest4”)
public void paramtest4(TestInfo testInfo) throws Exception{
String input = “””
! “Hello “+”World!”;
! (“Hello “+”World!”) = “Hello World!”
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“paramtest5”)
public void paramtest5(TestInfo testInfo) throws Exception{
String input = “””
CONST world=”World”;
VAR hello;
PROCEDURE p;
hello:=”HELLO+”;
! hello+” ” + “-” + world+ ” :”+” ” + name
String shortClassName = “prog”;
String JVMpackageName = “edu/ufl/cise/plpfa22”;
List classes = compile(input, shortClassName, JVMpackageName);
Object[] args = new Object[1];
String className = “edu.ufl.cise.plpfa22.prog”;
loadClassesAndRunMain(classes, className);

@DisplayName(“paramtest6”)
public void paramtest6(TestInfo testInfo) throws Exception{
String input = “””
CONST str=”Is this”, intstr = “12345”, intstr2 = “456”, str2=”equal”;
VAR a, b, c;
a:= str+str2;
WHILE (a = “Is this equal?”)
IF (“STRiNG CoMPaRe” > “strIng cOmpArE”)
! “THIS is”;
!”This is Equal!”;
a:=a+” not equal”;
b:=intstr+”6″;
WHILE ((b >= intstr2) * (b > “56”) * (“123” < intstr)) IF ((b >= intstr2) + (intstr2 > “56”) * (“123”

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com