Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1-Beta")
testImplementation("org.junit.jupiter:junit-jupiter-params")
implementation("io.arrow-kt:arrow-core:1.2.1")

testImplementation("io.mockk:mockk:1.13.10")
testImplementation("org.assertj:assertj-core:3.25.1")
}

tasks {
compileJavacc {
dependsOn(compileKotlin)
inputDirectory = file("src/main/javacc")
outputDirectory = file(layout.buildDirectory.dir("generated/javacc"))
}
Expand All @@ -45,6 +50,9 @@ sourceSets {
java {
srcDir(file(layout.buildDirectory.dir("generated/javacc")))
}
kotlin {
srcDir("src/main/kotlin")
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/org/example/ast/ClassDeclExtends.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
@Builder
@AllArgsConstructor
public class ClassDeclExtends extends ClassDecl {
private Identifier className;
private Identifier parent;
public Identifier className;
public Identifier parent;
@Builder.Default
private VarDeclList fields = new VarDeclList();
public VarDeclList fields = new VarDeclList();
@Builder.Default
private MethodDeclList methods = new MethodDeclList();
public MethodDeclList methods = new MethodDeclList();

@Override
public void accept(ASTVisitor v) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/example/ast/ClassDeclList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@EqualsAndHashCode
public class ClassDeclList {
@Builder.Default
private ArrayList<ClassDecl> classDecls = new ArrayList<>();
public ArrayList<ClassDecl> classDecls = new ArrayList<>();

public void addClassDecl(ClassDecl classDecl) {
classDecls.add(classDecl);
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/org/example/ast/ClassDeclSimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
@Builder
@AllArgsConstructor
public class ClassDeclSimple extends ClassDecl {
private Identifier className;
public Identifier className;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utilize o getters e setters do lombok, não deixe todos os campos da AST publico

@Builder.Default
private VarDeclList fields = new VarDeclList();
public VarDeclList fields = new VarDeclList();
@Builder.Default
private MethodDeclList methods = new MethodDeclList();
public MethodDeclList methods = new MethodDeclList();

@Override
public void accept(ASTVisitor v) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/example/ast/ExpressionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@AllArgsConstructor
@NoArgsConstructor
public class ExpressionList {
private ArrayList<Expression> list = new ArrayList<>();
public ArrayList<Expression> list = new ArrayList<>();

public void addExpression(Expression expression) {
list.add(expression);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/example/ast/Formal.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@Builder
@AllArgsConstructor
public class Formal extends Node {
private Type type;
private String name;
public Type type;
public String name;

@Override
public void accept(ASTVisitor v) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/example/ast/FormalList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@EqualsAndHashCode
public class FormalList {
@Builder.Default
private ArrayList<Formal> formals = new ArrayList<>();
public ArrayList<Formal> formals = new ArrayList<>();

public void addFormal(Formal formal) {
formals.add(formal);
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/org/example/ast/Identifier.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package org.example.ast;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.*;
import org.example.visitor.ASTVisitor;
import org.example.visitor.TypeVisitor;

@EqualsAndHashCode(callSuper = false)
@ToString
@Data
@AllArgsConstructor
@Builder
public class Identifier extends Expression {
private String s;
public String s;
@Override
public void accept(ASTVisitor v) {
v.visit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Builder
@AllArgsConstructor
public class IdentifierExpression extends Expression {
private String id;
public String id;

@Override
public void accept(ASTVisitor v) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/example/ast/IdentifierType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Data
@AllArgsConstructor
public class IdentifierType extends Type {
private String s;
public String s;

@Override
public void accept(ASTVisitor v) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/example/ast/IntegerType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.example.ast;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import org.example.visitor.ASTVisitor;
import org.example.visitor.TypeVisitor;
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/org/example/ast/MainClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
@Builder
@AllArgsConstructor
public class MainClass extends Node {
private Identifier className;
private Identifier argsName;
private StatementList statements;
public Identifier className;
public Identifier argsName;
public StatementList statements;

@Override
public void accept(ASTVisitor v) {
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/org/example/ast/MethodDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
@Builder
@AllArgsConstructor
public class MethodDecl extends Node {
private Type type;
private String identifier;
private FormalList formals;
private VarDeclList varDecls;
private StatementList statements;
private Expression returnExpression;
public Type type;
public String identifier;
public FormalList formals;
public VarDeclList varDecls;
public StatementList statements;
public Expression returnExpression;

@Override
public void accept(ASTVisitor v) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/example/ast/MethodDeclList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@EqualsAndHashCode
public class MethodDeclList {
@Builder.Default
private ArrayList<MethodDecl> methodDecls = new ArrayList<>();
public ArrayList<MethodDecl> methodDecls = new ArrayList<>();

public void addMethodDecl(MethodDecl methodDecl) {
methodDecls.add(methodDecl);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/example/ast/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@Builder
@AllArgsConstructor
public class Program extends Node {
private MainClass mainClass;
private ClassDeclList classes;
public MainClass mainClass;
public ClassDeclList classes;

@Override
public void accept(ASTVisitor v) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/example/ast/Type.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package org.example.ast;

import lombok.Builder;

public abstract class Type extends Node {
}
4 changes: 2 additions & 2 deletions app/src/main/java/org/example/ast/VarDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@Builder
@AllArgsConstructor
public class VarDecl extends Node {
private Type type;
private String name;
public Type type;
public String name;

@Override
public void accept(ASTVisitor v) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/example/ast/VarDeclList.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@NoArgsConstructor
public class VarDeclList {
@Builder.Default
private ArrayList<VarDecl> varDecls = new ArrayList<>();
public ArrayList<VarDecl> varDecls = new ArrayList<>();

public void addVarDecl(VarDecl varDecl) {
varDecls.add(varDecl);
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/java/org/example/visitor/ASTVisitor.java

This file was deleted.

62 changes: 62 additions & 0 deletions app/src/main/kotlin/org/example/visitor/ClassDeclVisitor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.example.visitor

import arrow.core.Either
import arrow.core.raise.either
import arrow.core.raise.ensure
import org.example.ast.*
import org.example.ast2.org.example.visitor.MethodDeclListVisitor
import org.example.visitor.SymbolVisitor.Companion.dispatch

object ClassDeclListVisitor : SymbolVisitor<ClassDeclList> {
override fun Table.visit(entity: ClassDeclList): Either<Error, Table> =
ClassDeclVisitor.fold(
entity.classDecls.toList()
) { dispatch(it) }
}

object ClassDeclVisitor : SymbolVisitor<ClassDecl> {
private fun extractName(entity: ClassDecl): String =
when (entity) {
is ClassDeclSimple -> entity.className.s
is ClassDeclExtends -> entity.className.s
else -> throw IllegalArgumentException(
"ClassDeclVisitor: ClassDecl must be either ClassDeclSimple or ClassDeclExtends"
)
}

object ClassDeclSimpleVisitor : SymbolVisitor<ClassDeclSimple> {
override fun Table.visit(entity: ClassDeclSimple): Either<Error, Table> = either {
this@visit + Table(
ClassData(
name = extractName(entity),
fields = dispatch(entity.fields).bind(),
methods = dispatch(entity.methods).bind()
)
)
}
}

object ClassDeclExtendsVisitor : SymbolVisitor<ClassDeclExtends> {
override fun Table.visit(entity: ClassDeclExtends): Either<Error, Table> = either {
this@visit + Table(
ClassData(
name = extractName(entity),
fields = dispatch(entity.fields).bind(),
methods = dispatch(entity.methods).bind()
)
)
}
}

Comment on lines +17 to +50

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Como você planeja resolver os simbolos de herança da super classe que a subclasse herda?

class Super {
   public int a;
}

class Sub extends Super {
}

Onde essa classe sub terá o valor a como padrão na implementação. Como você irá resolver os casos em que há redeclaração de variável e de métodos? E se essa redeclaração alterar o tipo como você resolve?

override fun Table.visit(entity: ClassDecl): Either<Error, Table> = either {
val name = extractName(entity)

ensure(!this@visit.contains(name)) {
Error("ClassDeclVisitor: ClassDecl must have a unique name")
}

val newTable = dispatch(entity, table = this@visit).bind()

this@visit + newTable
}
}
42 changes: 42 additions & 0 deletions app/src/main/kotlin/org/example/visitor/MainClassVisitor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.example.visitor

import arrow.core.Either
import arrow.core.raise.either
import arrow.core.raise.ensure
import org.example.ast.IntegerLiteral
import org.example.ast.IntegerType
import org.example.ast.MainClass

object MainClassVisitor : SymbolVisitor<MainClass> {
override fun Table.visit(entity: MainClass): Either<Error, Table> = either {
ensure(!contains(entity.className.s)) {
Error("MainClassVisitor: MainClass must have a unique name")
}
ensure(entity.argsName.s == "args") {
Error("MainClassVisitor: MainClass args must be named 'args'")
}

Table(
ClassData(
name = entity.className.s,
fields = Table(),
methods = Table(
MethodData(
name = entity.className.s,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o nome do método é sempre main está na gramática
image

args = Table(
ParamData(
name = entity.argsName.s,
type = IntegerType()
)
),
varDeclList = Table(

),
)
)
)
)
}


}
63 changes: 63 additions & 0 deletions app/src/main/kotlin/org/example/visitor/MethodDeclVisitor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.example.ast2.org.example.visitor

import arrow.core.Either
import arrow.core.raise.either
import arrow.core.raise.ensure
import org.example.ast.Formal
import org.example.ast.FormalList
import org.example.ast.MethodDecl
import org.example.ast.MethodDeclList
import org.example.visitor.*
import org.example.visitor.SymbolVisitor.Companion.dispatch


object FormalsListVisitor : SymbolVisitor<FormalList> {
override fun Table.visit(entity: FormalList): Either<Error, Table> =
with(FormalsVisitor) {
fold(
entity.formals.toList(),
::dispatch
)
}
}

object FormalsVisitor : SymbolVisitor<Formal> {
override fun Table.visit(entity: Formal): Either<Error, Table> = either {
ensure(!contains(entity.name)) {
Error("FormalsVisitor: Formals must have unique names")
}

Table(
FormalData(
name = entity.name,
type = entity.type
)
)
}
}

object MethodDeclListVisitor : SymbolVisitor<MethodDeclList> {
override fun Table.visit(entity: MethodDeclList): Either<Error, Table> =
with(MethodDeclVisitor) {
fold(
entity.methodDecls.toList(),
::dispatch
)
}
}

object MethodDeclVisitor : SymbolVisitor<MethodDecl> {
override fun Table.visit(entity: MethodDecl): Either<Error, Table> = either {
ensure(!contains(entity.identifier)) {
Error("MethodDeclVisitor: MethodDecl must have a unique name")
}

Table(
MethodData(
name = entity.identifier,
args = dispatch(entity.formals).bind(),
varDeclList = dispatch(entity.varDecls).bind()
)
)
}
}
Loading