diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/libraries/antlr_4_11_1_complete.xml b/.idea/libraries/antlr_4_11_1_complete.xml new file mode 100644 index 0000000..4617dab --- /dev/null +++ b/.idea/libraries/antlr_4_11_1_complete.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/introspector_1_2.xml b/.idea/libraries/introspector_1_2.xml new file mode 100644 index 0000000..ac82870 --- /dev/null +++ b/.idea/libraries/introspector_1_2.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lib.xml b/.idea/libraries/lib.xml new file mode 100644 index 0000000..20ae51b --- /dev/null +++ b/.idea/libraries/lib.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c8b81d1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,23 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1af7880 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AG type-checking.txt b/AG type-checking.txt new file mode 100644 index 0000000..0f55c87 --- /dev/null +++ b/AG type-checking.txt @@ -0,0 +1,57 @@ +AG: type-checking + +// expressions +(1) IntLiteral: expression -> INT_CONSTANT +(2) CharLiteral: expression -> CHAR_CONSTANT +(3) DoubleLiteral: expression -> REAL_CONSTANT +(4) Variable: expression -> ID +(5) FuncInvocation: expression1 -> Variable expression2* +(6) Arithmetic: expression1 -> expression2 expression3 +(7) ArithComparison: expression1 -> expression2 expression3 +(8) LogComparison: expression1 -> expression2 expression3 +(9) Minus: expression1 -> expression2 +(10) Negation: expression1 -> expression2 +(11) FieldAccess: expression1 -> expression2 ID +(12) ArrayIndex: expression1 -> expression2 expression3 + +// definitions +(13) FuncDefinition: funcdefinition -> ID vardefinition* statement* type +(14) VarDefinition: vardefinition -> ID type + +// statements +(15) Assignment: statement -> expression1 expression2 +(16) IfStatement: statement -> expression statement1* statement2* +(17) Input: statement -> expression* +(18) Print: statement -> expression* +(19) Return: statement -> expression +(20) WhileStatement: statement -> expression statement* + +// Attributes +expression.type, statement.return + +// Semantic rules +(1) expression.type = new Int(expression.line, expression.column) // int +(2) expression.type = new Char(expression.line, expression.column) // char +(3) expression.type = new Real(expression.line, expression.column) // real +(4) expression.type = expresion.definition.type // variable +(5) expression1.type = variable.definition.type.returnType // function invocation +(6) expression1.type = expression2.type.arithmetic(expression3.type) // arithmetic +(7) expression1.type = expression2.type.arithComparison(expression3.type) // arith comparison +(8) expression1.type = expression2.type.logicComparison(expression3.type) // logic comparison +(9) expression1.type = expression2.type.unaryMinus() // minus +(10) expression1.type = expression2.type.unaryNegation() // negation +(11) expression1.type = expression2.type.access(ID) //fieldaccess +(12) expression1.type = expression2.type.squareBrackets(expression3) // array indexer + +(13) funcdefinition.type = new FunctionType(ID.line, ID.column, vardefinition*, type) +(14) vardefinition.type = type + +// statements +(15) expression2.type = expression2.type.assignment(expression1.type) // assignment +(16) expression.type = expression.type.isLogical() // if +(17) expression*.type = expression*.type.isBuiltin() // input +(18) expression*.type = expression*.type.isBuiltIn() // print +(19) Return: statement -> expression // not finished +(20) expression.type = expression.type.isLogical() // while + + diff --git a/AST.jpg b/AST.jpg new file mode 100644 index 0000000..54527dd Binary files /dev/null and b/AST.jpg differ diff --git a/DLP.iml b/DLP.iml new file mode 100644 index 0000000..1455bdf --- /dev/null +++ b/DLP.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Lexer-with-IntelliJ-and-ANTLR.pdf b/Lexer-with-IntelliJ-and-ANTLR.pdf new file mode 100644 index 0000000..c70e545 Binary files /dev/null and b/Lexer-with-IntelliJ-and-ANTLR.pdf differ diff --git a/MAPL.cmd b/MAPL.cmd new file mode 100644 index 0000000..bda5308 --- /dev/null +++ b/MAPL.cmd @@ -0,0 +1,3 @@ +cls +cd %~dp0 +start .\mapl\GVM.exe output.txt \ No newline at end of file diff --git a/big.input.txt b/big.input.txt new file mode 100644 index 0000000..1e232f2 --- /dev/null +++ b/big.input.txt @@ -0,0 +1,91 @@ +pair:struct { + integer:int; + character:char; +}; + +acme:[2]struct { + employees: [2]struct { age:int; }; +}; + +fibonacci:[8][3]int; + +#------------------------------- + +def p(): {} + +def f(r:double, e:int):int { + real:double; + c:char; + result:int; + real = r * 10.0; + c='0'; + if e < 256: { + c = (char)e; + result = (int)c; + } + else: + result = (int)real; + return result; +} + +def initialize(): { + i:int; + fibonacci[0][0] = 0; + fibonacci[0][1] = 1; + fibonacci[0][2] = 1; + i = 1; + while i < 8: { + fibonacci[i][0] = fibonacci[i - 1][1]; + fibonacci[i][1] = fibonacci[i - 1][2]; + fibonacci[i][2] = fibonacci[i][0] + fibonacci[i][1]; + i = i + 1; + } +} + +#------------------------------- + +def main(): { + i, j:int; + p(); + i=0; + pair.character='0'; + pair.integer=(int)'0'; + + # Shows 48 (decimal value of '0') + print f((double)i, (int)(pair.character)), '\n'; + f(1.3, 2); + + initialize(); + i = 0; + while i < 8: { + # Shows the Fib sequence, from 1 to 8 (34) + print '(', i + 1, ')', fibonacci[i][0], + '+', fibonacci[i][1] , '=' , + fibonacci[i][2] , '\n'; + i = i + 1; + } + + acme[1].employees[1].age = 56; + # Shows 56 + print acme[1].employees[1].age, '\n'; + i = 0; + while i<2: { + j = 0; + while j<2: { + acme[i].employees[j].age = i + j; + j = j+ 1; + } + i= i + 1; + } + + i = 0; + while i<2: { + j = 0; + while j<2: { + # Shows 0 1 1 2 + print acme[i].employees[j].age, ' '; + j = j+ 1; + } + i= i + 1; + } +} \ No newline at end of file diff --git a/big.output.txt b/big.output.txt new file mode 100644 index 0000000..951546f --- /dev/null +++ b/big.output.txt @@ -0,0 +1,722 @@ +#source "big.input.txt" + ' * Struct: Fields: Record Field: Name: integer - Type IntRecord Field: Name: character - Type Char pair (offset 0) + ' * Array of type: Struct: Fields: Record Field: Name: employees - Type Array of type: Struct: Fields: Record Field: Name: age - Type Int - Size: 2 - Size: 2 acme (offset 3) + ' * Array of type: Array of type: Int - Size: 3 - Size: 8 fibonacci (offset 11) +' Invocation to the main function + call main + halt + +#line 14 +p: + ' * Parameters + ' * Local variables + enter 0 + ret 0, 0, 0 + +#line 16 +f: + ' * Parameters + ' * Real r (offset 6) + ' * Int e (offset 4) + ' * Local variables + ' * Real real (offset -4) + ' * Char c (offset -5) + ' * Int result (offset -7) + enter 7 + +#line 20 + ' * Assignment + push bp + pushi -4 + addi + push bp + pushi 6 + addi + loadf + pushf 10.0 + mulf + storef + +#line 21 + ' * Assignment + push bp + pushi -5 + addi + pushb 48 + storeb + +#line 22 + ' * If + push bp + pushi 4 + addi + loadi + pushi 256 + lti + jz label0 + +#line 23 + ' * Assignment + push bp + pushi -5 + addi + push bp + pushi 4 + addi + loadi + i2b + storeb + +#line 24 + ' * Assignment + push bp + pushi -7 + addi + push bp + pushi -5 + addi + loadb + b2i + storei + jmp label1 +label0: + +#line 27 + ' * Assignment + push bp + pushi -7 + addi + push bp + pushi -4 + addi + loadf + f2i + storei +label1: + +#line 28 + ' * Return + push bp + pushi -7 + addi + loadi + ret 2, 7, 6 + +#line 31 +initialize: + ' * Parameters + ' * Local variables + ' * Int i (offset -2) + enter 2 + +#line 33 + ' * Assignment + pusha 11 + pushi 0 + pushi 6 + muli + addi + pushi 0 + pushi 2 + muli + addi + pushi 0 + storei + +#line 34 + ' * Assignment + pusha 11 + pushi 0 + pushi 6 + muli + addi + pushi 1 + pushi 2 + muli + addi + pushi 1 + storei + +#line 35 + ' * Assignment + pusha 11 + pushi 0 + pushi 6 + muli + addi + pushi 2 + pushi 2 + muli + addi + pushi 1 + storei + +#line 36 + ' * Assignment + push bp + pushi -2 + addi + pushi 1 + storei + +#line 37 + ' * While +label2: + push bp + pushi -2 + addi + loadi + pushi 8 + lti + jz label3 + ' * While body + +#line 38 + ' * Assignment + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 6 + muli + addi + pushi 0 + pushi 2 + muli + addi + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 1 + subi + pushi 6 + muli + addi + pushi 1 + pushi 2 + muli + addi + loadi + storei + +#line 39 + ' * Assignment + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 6 + muli + addi + pushi 1 + pushi 2 + muli + addi + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 1 + subi + pushi 6 + muli + addi + pushi 2 + pushi 2 + muli + addi + loadi + storei + +#line 40 + ' * Assignment + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 6 + muli + addi + pushi 2 + pushi 2 + muli + addi + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 6 + muli + addi + pushi 0 + pushi 2 + muli + addi + loadi + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 6 + muli + addi + pushi 1 + pushi 2 + muli + addi + loadi + addi + storei + +#line 41 + ' * Assignment + push bp + pushi -2 + addi + push bp + pushi -2 + addi + loadi + pushi 1 + addi + storei + jmp label2 +label3: + ret 0, 2, 0 + +#line 47 +main: + ' * Parameters + ' * Local variables + ' * Int i (offset -2) + ' * Int j (offset -4) + enter 4 + +#line 49 + call p + +#line 50 + ' * Assignment + push bp + pushi -2 + addi + pushi 0 + storei + +#line 51 + ' * Assignment + pusha 0 + pushi 2 + addi + pushb 48 + storeb + +#line 52 + ' * Assignment + pusha 0 + pushi 0 + addi + pushb 48 + b2i + storei + +#line 55 + ' * Write + push bp + pushi -2 + addi + loadi + i2f + pusha 0 + pushi 2 + addi + loadb + b2i + call f + outi + +#line 55 + ' * Write + pushb 10 + outb + +#line 56 + pushf 1.3 + pushi 2 + call f + popi + +#line 58 + call initialize + +#line 59 + ' * Assignment + push bp + pushi -2 + addi + pushi 0 + storei + +#line 60 + ' * While +label4: + push bp + pushi -2 + addi + loadi + pushi 8 + lti + jz label5 + ' * While body + +#line 62 + ' * Write + pushb 40 + outb + +#line 62 + ' * Write + push bp + pushi -2 + addi + loadi + pushi 1 + addi + outi + +#line 62 + ' * Write + pushb 41 + outb + +#line 62 + ' * Write + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 6 + muli + addi + pushi 0 + pushi 2 + muli + addi + loadi + outi + +#line 62 + ' * Write + pushb 43 + outb + +#line 62 + ' * Write + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 6 + muli + addi + pushi 1 + pushi 2 + muli + addi + loadi + outi + +#line 62 + ' * Write + pushb 61 + outb + +#line 62 + ' * Write + pusha 11 + push bp + pushi -2 + addi + loadi + pushi 6 + muli + addi + pushi 2 + pushi 2 + muli + addi + loadi + outi + +#line 62 + ' * Write + pushb 10 + outb + +#line 65 + ' * Assignment + push bp + pushi -2 + addi + push bp + pushi -2 + addi + loadi + pushi 1 + addi + storei + jmp label4 +label5: + +#line 68 + ' * Assignment + pusha 3 + pushi 1 + pushi 4 + muli + addi + pushi 0 + addi + pushi 1 + pushi 2 + muli + addi + pushi 0 + addi + pushi 56 + storei + +#line 70 + ' * Write + pusha 3 + pushi 1 + pushi 4 + muli + addi + pushi 0 + addi + pushi 1 + pushi 2 + muli + addi + pushi 0 + addi + loadi + outi + +#line 70 + ' * Write + pushb 10 + outb + +#line 71 + ' * Assignment + push bp + pushi -2 + addi + pushi 0 + storei + +#line 72 + ' * While +label6: + push bp + pushi -2 + addi + loadi + pushi 2 + lti + jz label7 + ' * While body + +#line 73 + ' * Assignment + push bp + pushi -4 + addi + pushi 0 + storei + +#line 74 + ' * While +label8: + push bp + pushi -4 + addi + loadi + pushi 2 + lti + jz label9 + ' * While body + +#line 75 + ' * Assignment + pusha 3 + push bp + pushi -2 + addi + loadi + pushi 4 + muli + addi + pushi 0 + addi + push bp + pushi -4 + addi + loadi + pushi 2 + muli + addi + pushi 0 + addi + push bp + pushi -2 + addi + loadi + push bp + pushi -4 + addi + loadi + addi + storei + +#line 76 + ' * Assignment + push bp + pushi -4 + addi + push bp + pushi -4 + addi + loadi + pushi 1 + addi + storei + jmp label8 +label9: + +#line 78 + ' * Assignment + push bp + pushi -2 + addi + push bp + pushi -2 + addi + loadi + pushi 1 + addi + storei + jmp label6 +label7: + +#line 81 + ' * Assignment + push bp + pushi -2 + addi + pushi 0 + storei + +#line 82 + ' * While +label10: + push bp + pushi -2 + addi + loadi + pushi 2 + lti + jz label11 + ' * While body + +#line 83 + ' * Assignment + push bp + pushi -4 + addi + pushi 0 + storei + +#line 84 + ' * While +label12: + push bp + pushi -4 + addi + loadi + pushi 2 + lti + jz label13 + ' * While body + +#line 86 + ' * Write + pusha 3 + push bp + pushi -2 + addi + loadi + pushi 4 + muli + addi + pushi 0 + addi + push bp + pushi -4 + addi + loadi + pushi 2 + muli + addi + pushi 0 + addi + loadi + outi + +#line 86 + ' * Write + pushb 32 + outb + +#line 87 + ' * Assignment + push bp + pushi -4 + addi + push bp + pushi -4 + addi + loadi + pushi 1 + addi + storei + jmp label12 +label13: + +#line 89 + ' * Assignment + push bp + pushi -2 + addi + push bp + pushi -2 + addi + loadi + pushi 1 + addi + storei + jmp label10 +label11: + ret 0, 4, 0 diff --git a/code generation.txt b/code generation.txt new file mode 100644 index 0000000..5f83c37 --- /dev/null +++ b/code generation.txt @@ -0,0 +1,148 @@ +code generation: + +1. add codegenerator.class providing as many methods as low level instructions (in a filewriter object). the main method must receive both the input and the output files. +2. code functions: + execute: generates the code to execute statements and definitions + value: generates the code to push value of an epression + address: generates the code to push the mem address of an lvalue expression +use a new abstract visitor (theory). +3. one method for each low level instruction. + +a[i] => we need the address of a and the value of i. + + +TODO: + specify all the code templates as a comment in the visitor in each visitor + + + +CODE TEMPLATES: + +execute[[Program: program -> definition*]]() = + for (Definition definition : definition*) --> 1st the variable definitions + if (definition instanceof VARDEF) + execute[[definition]]() + + + for (Definition definition: definition*) --> 2nd the function definitions + if (definition instanceof FUNDEF) + execute[[definition]]() + +execute[[Print: statement -> expression]]() = + value[[expression]]() + expression.tipo.suffix() + +execute[[Input: statement -> expression]]() = + address[[expresion]]() + expression.type.suffix() + expression.type.suffix() + +address[[Variable: expression -> ID]]() = + if (expression.definition.scope == 0) + expression.definition.offset + else + + expression.definition.offset + + +value[[Variable :expression -> ID]]() = + address[[expression]]() + expression.type.suffix() + + +specify code generation templates except: if, while, return, invocation, fieldaccess and indexing +implement all code generation visitors +new suffix() char method in type +modify main to include output.txt +test your implementation: input.txt and test result with mapl + + +# line 5 --> write this at the beginning of each statement and at the beginning of each function definition. for each statement of the funcdefinition write the line + +# the comments ' * -> write them in the execute variables + + + + +Control structures + +