Git Product home page Git Product logo

clp's Introduction

CLP Project

Project for the AA 2023/2024 Complementi di programmazione's course of the Master Degree in Computer Science.

How to build

You first need to build the project using the Makefile.

make build

Run

You can run the software using the following command

java -cp lib/antlr-4.13.1-complete.jar:out Main <file.py>

you also can use three flags on the execution:

  • --optimize: runs the optimizer for the file <file.py>. The optimized version is auto saved at ./optimized.py.

  • --tree: shows the AST on video and on JFrame.

  • --exec: executes the SVM for the Assembly code generated in code.asm.

You can use all the three flags togheter if you want to.

Example

  • Optimizer
$ java -cp lib/antlr-4.13.1-complete.jar:out Main test/2a.py --optimize
n=int(input())
x=int(input())
m=1
tmp=2 * x
while n>1:

        m=m*tmp
        n=n-1


print(m+tmp)

Saving optimized file...
Everything is OK!
  • Tree
$ java -cp lib/antlr-4.13.1-complete.jar:out Main test/2a.py --tree
Root
  SimpleStmts
    SimpleStmt
      Assignment
        ExprList
          Expr
            AtomNode: n
        Augassign(=)
        ExprList
          Expr
            AtomNode: int
            TrailerNode
              ArglistNode
                Expr
      Assignment                                                                                                                                                                                             [40/225]
    [...] 
    SimpleStmt
      Expr
        AtomNode: print
        TrailerNode
          ArglistNode
            Expr
              Expr
                AtomNode: m
              Expr
                AtomNode: tmp
              Op(+)

Everything is OK!
  • Exec
$ java -cp lib/antlr-4.13.1-complete.jar:out Main test/3-1.py --exec
Creating VM code...
Executing assemply code...
0: 17 ----- SP = 999, FP = 999, AL = 998, RA = 0, A0 = 0, T1 = 0
1: 17	999 ----- SP = 998, FP = 999, AL = 998, RA = 0, A0 = 0, T1 = 0
2: 17	999	998 ----- SP = 997, FP = 999, AL = 998, RA = 0, A0 = 0, T1 = 0
3: 7	999	998	999 ----- SP = 996, FP = 999, AL = 998, RA = 0, A0 = 0, T1 = 0
4: 9	999	998	999 ----- SP = 996, FP = 996, AL = 998, RA = 0, A0 = 0, T1 = 0
5: 7	999	998	999 ----- SP = 996, FP = 997, AL = 998, RA = 0, A0 = 0, T1 = 0
[...]
216: 18	999	998	999	998	29	2	14	997	996	29	3	140	992	991 ----- SP = 985, FP = 987, AL = 986, RA = 140, A0 = 1, T1 = 1
217: 5	999	998	999	998	29	2	14	997	996	29	3	140	992 ----- SP = 986, FP = 987, AL = 986, RA = 140, A0 = 1, T1 = 1
218: 7	999	998	999	998	29	2	14	997	996	29	3	140	992 ----- SP = 986, FP = 992, AL = 986, RA = 140, A0 = 1, T1 = 1
219: 11	999	998	999	998	29	2	14	997	996	29	3	140	992 ----- SP = 986, FP = 992, AL = 992, RA = 140, A0 = 1, T1 = 1
220: 18	999	998	999	998	29	2	14	997	996	29	3	140	992 ----- SP = 986, FP = 992, AL = 991, RA = 140, A0 = 1, T1 = 1
221: 24	999	998	999	998	29	2	14	997	996	29	3	140 ----- SP = 987, FP = 992, AL = 991, RA = 140, A0 = 1, T1 = 1
222: 19	999	998	999	998	29	2	14	997	996	29	3	140 ----- SP = 987, FP = 992, AL = 991, RA = 140, A0 = 1, T1 = 1
223: 9	999	998	999	998	29	2	14	997	996	29	3 ----- SP = 988, FP = 992, AL = 991, RA = 140, A0 = 1, T1 = 1
224: 18	999	998	999	998	29	2	14	997	996 ----- SP = 990, FP = 992, AL = 991, RA = 140, A0 = 1, T1 = 1
225: 5	999	998	999	998	29	2	14	997 ----- SP = 991, FP = 992, AL = 991, RA = 140, A0 = 1, T1 = 1
226: 7	999	998	999	998	29	2	14	997 ----- SP = 991, FP = 997, AL = 991, RA = 140, A0 = 1, T1 = 1
227: 11	999	998	999	998	29	2	14	997 ----- SP = 991, FP = 997, AL = 997, RA = 140, A0 = 1, T1 = 1
228: 18	999	998	999	998	29	2	14	997 ----- SP = 991, FP = 997, AL = 996, RA = 140, A0 = 1, T1 = 1
229: 24	999	998	999	998	29	2	14 ----- SP = 992, FP = 997, AL = 996, RA = 140, A0 = 1, T1 = 1
230: 19	999	998	999	998	29	2	14 ----- SP = 992, FP = 997, AL = 996, RA = 140, A0 = 1, T1 = 1
231: 9	999	998	999	998	29	2 ----- SP = 993, FP = 997, AL = 996, RA = 14, A0 = 1, T1 = 1
232: 18	999	998	999	998 ----- SP = 995, FP = 997, AL = 996, RA = 14, A0 = 1, T1 = 1
233: 5	999	998	999 ----- SP = 996, FP = 997, AL = 996, RA = 14, A0 = 1, T1 = 1
234: 7	999	998	999 ----- SP = 996, FP = 999, AL = 996, RA = 14, A0 = 1, T1 = 1
235: 11	999	998	999 ----- SP = 996, FP = 999, AL = 999, RA = 14, A0 = 1, T1 = 1
236: 18	999	998	999 ----- SP = 996, FP = 999, AL = 998, RA = 14, A0 = 1, T1 = 1
237: 24	999	998 ----- SP = 997, FP = 999, AL = 998, RA = 14, A0 = 1, T1 = 1
238: 18	999	998 ----- SP = 997, FP = 999, AL = 998, RA = 14, A0 = 1, T1 = 1
239: 18	999 ----- SP = 998, FP = 999, AL = 998, RA = 14, A0 = 1, T1 = 1
240: 25 ----- SP = 999, FP = 999, AL = 998, RA = 14, A0 = 1, T1 = 1

Result: 1

Everything is OK!

clp's People

Contributors

boozec avatar gabrielegenovese avatar l0p0p avatar

Watchers

 avatar

clp's Issues

Da gestire gli assegnamenti multipli

Ci sono assegnamenti del tipo:
(left, right) = (1, 2)

e for del tipo:
for k,v in t.items():

Attualmente non controlliamo questi assegnamenti e abbiamo commentato tutti i codici che li presentano, sarebbero da gestire per fare le cose pulite o da ignorare se ci bastano tutti i codici che parsiamo.

Scope per blocchi diversi da funzioni

Guardando con @L0P0P abbiamo notato che per i blocchi diversi dalle funzioni non ha senso aumentarne l'offset.
Ad esempio,

x = 10
if x == 10:
	x = 3

print(x) # 3

invece ha senso per roba del tipo

x = 10
def foo():
	if x == 10:
		x = 3
	print(x) # 3

foo()
print(x) # 10

Ci sono due offset

Sono presenti due offset:

  • Uno generale dentro semanticanalysis.SymbolTable che corrisponde ad una lista di offset dove ogni indice è uno scope diverso.
  • Uno più specifico dentro semanticanalysis.STentry che corrisponde all'offset utilizzato da una singola variabile.

Bisognerebbe valutare quale dei due ritorna più utile durante l'analisi semantica ed eventualmente si potrebbe rimuovere l'altro.

Reaching definition su codice specifico while

Guardando le slide del prof abbiamo riscontrato dei dubbi su due codici

  1. Viene detto che l'assegnamento deve essere fatto in due righe diverse, ma questo è valido (a, b = b, a).
a = 3
b = 1

print(a, b) # 3, 1
a, b = b, a
print(a, b) # 1, 3
  1. In questo caso la x non è usata ma, stando alle slide, dovremmo spostarla fuori dal ciclo while (inutilmente) e non eliminarla (come ci dice qualsiasi altro testo). Inoltre qui dovremmo obbligatoriamente anche togliere il ciclo while e risparmiare i 10 cicli.
while n < 10:
  x = a, b # (3, 1)

Stampa errata dell' `ast`

Quando si prova a stampare l'ast di un codice come, ad esempio:

m = 1
for i in range(10):
    m = m + 1

L'ast del blocco del for viene stampato come:

Block
  SimpleStmts
    SimpleStmt
      Assignment
        ExprList
          Expr
            Atom(m)
          Augassign(=)
          ExprList
            Expr
              Atom(1)
            Op(+)

Ovvero, mostra solo il primo elemento dell' ExprList a destra dell'assegnamento (a questo punto probabilmente stampa sempre e solo il primo elemento in ogni caso).

Uso dei child

Utilizzare .getChild() al posto di singole visite su simple_stmts e compound_stmts. In questo modo abbiamo sequenzialmente l'ordine di quello che viene parsato.

Risolve il problema di avere un albero con, ad esempio,

  • simple
  • compound
  • simple

Qual è l'espressione `e` nel `for i in range(e): S`?

Da titolo, la e nel range dovrebbe essere un iteratore, non una espressione. Una roba del tipo for i range(n < 10) non è valida in Python.

Il punto è che un iteratore è una classe con dentro i metodi __iter__ e __next__. Come stra diamine dovremmo intendere sta roba? Nelle slide c'è scritto che e deve essere un'espressione.

`manageTlc` in Python3VisitorImpl

Nel file Python3VisitorImpl.java vi è questo Tlc. Ora, dato che attualmente fanno la medesima cosa, perché non modificare questo
sotto

        } else if (ctx.OPEN_BRACE() != null && ctx.CLOSE_BRACE() != null) {
            return manageTlc(tlc);
        } else if (ctx.OPEN_BRACK() != null && ctx.CLOSE_BRACK() != null) {
            return manageTlc(tlc);
        } else if (ctx.OPEN_PAREN() != null && ctx.CLOSE_PAREN() != null) {
            return manageTlc(tlc);
        }
        return new AtomNode(null, null);
    }

    public AtomNode manageTlc(Testlist_compContext tlc) {
        if (tlc != null) {
            Node testlist_comp = visit(tlc);
            return new AtomNode(null, testlist_comp);
        } else {
            return new AtomNode(null, null);
        }
    }

in

        } else if (ctx.OPEN_BRACE() != null && ctx.CLOSE_BRACE() != null
            || ctx.OPEN_BRACK() != null && ctx.CLOSE_BRACK() != null 
			| |ctx.OPEN_PAREN() != null && ctx.CLOSE_PAREN() != null) {
            if (tlc != null) {
            	Node testlist_comp = visit(tlc);
            	return new AtomNode(null, testlist_comp);
        	} else {
            	return new AtomNode(null, null);
        	}
        }
        return new AtomNode(null, null);
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.