-
Notifications
You must be signed in to change notification settings - Fork 0
Feat add symbol table visitor #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
4ad21d1
396890c
ab2c1f6
fb3b2a8
4ea65a3
421f7c5
590c4a2
86246c1
f992f0e
c42dc8c
5691806
d8ca793
f34ee0c
7b5ba5f
616a4bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
| } |
This file was deleted.
| 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
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } | ||
| 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, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| args = Table( | ||
| ParamData( | ||
| name = entity.argsName.s, | ||
| type = IntegerType() | ||
| ) | ||
| ), | ||
| varDeclList = Table( | ||
|
|
||
| ), | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| } | ||
| 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() | ||
| ) | ||
| ) | ||
| } | ||
| } |

There was a problem hiding this comment.
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