error 28
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -3,4 +3,6 @@ build
|
|||||||
bin
|
bin
|
||||||
*.ll
|
*.ll
|
||||||
*.DS_Store
|
*.DS_Store
|
||||||
**/.antlr/
|
*/.antlr/VSLLexer.java
|
||||||
|
*.interp
|
||||||
|
*/.antlr/VSLParser.java
|
||||||
@@ -44,7 +44,7 @@ PRINT : 'PRINT'
|
|||||||
;
|
;
|
||||||
RETURN: 'RETURN'
|
RETURN: 'RETURN'
|
||||||
;
|
;
|
||||||
TEXT : '"' (ASCII)+ '"' {getText().substring(1,getText().length()-1)}
|
TEXT : '"' (ASCII)+ '"' {getText().substring(1,getText().length()-1);}
|
||||||
;
|
;
|
||||||
NUMBER : (DIGIT)+
|
NUMBER : (DIGIT)+
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -8,9 +8,7 @@ options {
|
|||||||
|
|
||||||
@header {
|
@header {
|
||||||
package TP2;
|
package TP2;
|
||||||
|
import TP2.asd.*;
|
||||||
import TP2.Program.Program;
|
|
||||||
import TP2.SymTable.SymTable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// On syntax error, raise exception rather than silently recovery
|
// On syntax error, raise exception rather than silently recovery
|
||||||
@@ -21,41 +19,42 @@ options {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
program returns [Program.Program p] :
|
program returns [Program p] :
|
||||||
func=function
|
func=function
|
||||||
EOF
|
EOF
|
||||||
{$p = new Program.Program($func.out);}
|
{$p = new Program($func.out);}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
function returns [List<Program.Function> out]
|
function returns [List<Function> out]
|
||||||
@init{
|
@init{
|
||||||
SymTable.SymTable sym_table = new SymTable.SymTable();
|
SymTable sym_table = new SymTable();
|
||||||
}:
|
}:
|
||||||
FUNCTION t=type i=ident ParO ParF instrs=list_instr[sym_table]
|
FUNCTION t=type i=ident ParO ParF instrs=list_instr[sym_table]
|
||||||
{
|
{
|
||||||
$out.add(new Program.Function($t.return_type, $i.out, $instrs.out));
|
$out.add(new Function($t.return_type, $i.out, $instrs.out));
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
prototype returns [Program.Program p]:
|
prototype returns [Program p]:
|
||||||
;
|
;
|
||||||
|
|
||||||
list_instr [SymTable.SymTable table] returns [List<Program.Instruction> out]
|
list_instr [SymTable table] returns [ArrayList<Instruction> out]
|
||||||
@init{
|
@init{
|
||||||
List<Program.Instruction> instructions = new List<Program.Instruction>();
|
$out = new ArrayList<>();
|
||||||
}:
|
}:
|
||||||
instruction [table] {
|
instruction [table] {
|
||||||
instructions.add($instruction.out);
|
$out.addAll($instruction.out);
|
||||||
$out=instructions;
|
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
instruction [SymTable.SymTable table] returns [List<Program.Instruction> out]:
|
instruction [SymTable table] returns [ArrayList<Instruction> out]:
|
||||||
RETURN expression[SymTable table]
|
RETURN e=expression [table]
|
||||||
|
{$out = new ArrayList<>();
|
||||||
|
$out.add(new Return_instr($e.out));}
|
||||||
;
|
;
|
||||||
|
|
||||||
expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
expression [SymTable table] returns [Type return_Type, Expression out]:
|
||||||
//Fix LL1 and compare add and sub first
|
//Fix LL1 and compare add and sub first
|
||||||
left=td_expression[table] {
|
left=td_expression[table] {
|
||||||
$out = $left.out;
|
$out = $left.out;
|
||||||
@@ -64,10 +63,10 @@ expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
|||||||
//maybe it will be binop(s) with add/sub here
|
//maybe it will be binop(s) with add/sub here
|
||||||
//if it is mul or div, they are all gonna be left=td_expression[table] to handle
|
//if it is mul or div, they are all gonna be left=td_expression[table] to handle
|
||||||
(op=(PLUS | MINUS) right=td_expression[table] {
|
(op=(PLUS | MINUS) right=td_expression[table] {
|
||||||
$out = new Program.BinopExpression(
|
$out = new BinopExpression(
|
||||||
switch($op.getType()) {
|
switch($op.getType()) {
|
||||||
case PLUS -> Op.PLUS;
|
case PLUS -> PLUS;
|
||||||
case MINUS -> Op.MINUS;
|
case MINUS -> MINUS;
|
||||||
default -> throw new IllegalArgumentException("Unknown operator");
|
default -> throw new IllegalArgumentException("Unknown operator");
|
||||||
},
|
},
|
||||||
$left.out,
|
$left.out,
|
||||||
@@ -77,7 +76,7 @@ expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
|||||||
})*
|
})*
|
||||||
;
|
;
|
||||||
|
|
||||||
td_expression [SymTable.SymTable table] returns [Type return_Type, Expression out]:
|
td_expression [SymTable table] returns [Type return_Type, Expression out]:
|
||||||
left=lit {
|
left=lit {
|
||||||
$out = $left.out;
|
$out = $left.out;
|
||||||
$return_Type = $left.return_Type;
|
$return_Type = $left.return_Type;
|
||||||
@@ -85,8 +84,8 @@ td_expression [SymTable.SymTable table] returns [Type return_Type, Expression ou
|
|||||||
(op=(TIMES | DIV) right=lit {
|
(op=(TIMES | DIV) right=lit {
|
||||||
$out = new Program.BinopExpression(
|
$out = new Program.BinopExpression(
|
||||||
switch($op.getType()) {
|
switch($op.getType()) {
|
||||||
case TIMES -> Op.TIMES;
|
case TIMES -> TIMES;
|
||||||
case DIV -> Op.DIV;
|
case DIV -> DIV;
|
||||||
default -> throw new IllegalArgumentException("Unknown operator");
|
default -> throw new IllegalArgumentException("Unknown operator");
|
||||||
},
|
},
|
||||||
$left.out,
|
$left.out,
|
||||||
@@ -98,8 +97,8 @@ td_expression [SymTable.SymTable table] returns [Type return_Type, Expression ou
|
|||||||
|
|
||||||
lit returns [Expression out, Type return_Type]:
|
lit returns [Expression out, Type return_Type]:
|
||||||
NUMBER {
|
NUMBER {
|
||||||
$return_Type = new Program.Type_int();
|
$return_Type = new Type_int();
|
||||||
$out = new Program.Const($NUMBER.int);
|
$out = new Const($NUMBER.int);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -108,6 +107,6 @@ ident returns [String out]:
|
|||||||
;
|
;
|
||||||
|
|
||||||
type returns [Type return_type]:
|
type returns [Type return_type]:
|
||||||
TYPE_INT { $return_type = new Program.Type.Type_int(); }
|
TYPE_INT { $return_type = new Type_int(); }
|
||||||
|TYPE_VOID { $return_type = new Program.Type.Type_void(); }
|
|TYPE_VOID { $return_type = new Type_void(); }
|
||||||
;
|
;
|
||||||
@@ -8,7 +8,8 @@ import org.antlr.runtime.CharStream;
|
|||||||
import org.antlr.runtime.CommonTokenStream;
|
import org.antlr.runtime.CommonTokenStream;
|
||||||
import org.antlr.runtime.RecognitionException;
|
import org.antlr.runtime.RecognitionException;
|
||||||
|
|
||||||
import TP2.asd.Program;
|
import TP2.asd.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package TP2.asd;
|
package TP2.asd;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Stack;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public record Program(ArrayList<Function> functions) {
|
public record Program(ArrayList<Function> functions) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user