From 05166e9753647be681a147668605741974943490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Wed, 8 May 2024 10:10:44 +0200 Subject: [PATCH] added program and some typecheck --- .../abstractSyntaxTree/Class/FieldDecl.java | 19 +++++++++++++++++++ Source/abstractSyntaxTree/Class/IClass.java | 6 ++++-- .../abstractSyntaxTree/Class/MethodDecl.java | 8 ++++++++ .../abstractSyntaxTree/Datatype/RefType.java | 17 ++++++++--------- Source/abstractSyntaxTree/Program.java | 15 +++++++++++++++ 5 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 Source/abstractSyntaxTree/Program.java diff --git a/Source/abstractSyntaxTree/Class/FieldDecl.java b/Source/abstractSyntaxTree/Class/FieldDecl.java index c5d8ccd..7524350 100644 --- a/Source/abstractSyntaxTree/Class/FieldDecl.java +++ b/Source/abstractSyntaxTree/Class/FieldDecl.java @@ -1,4 +1,23 @@ package abstractSyntaxTree.Class; +import TypeCheck.TypeCheckResult; + +import java.util.ArrayList; +import java.util.List; + public class FieldDecl implements IClass { + private String type; + private String identifier; + + public TypeCheckResult typeCheck(List classFieldsIdentifier) throws Exception { + TypeCheckResult result = new TypeCheckResult(); + if (classFieldsIdentifier.contains(this.identifier)){ + throw new Exception("field already defined"); + } else { + classFieldsIdentifier.add(this); + } + result.type = this.type; + + return result; + } } diff --git a/Source/abstractSyntaxTree/Class/IClass.java b/Source/abstractSyntaxTree/Class/IClass.java index cb1cf92..85803f2 100644 --- a/Source/abstractSyntaxTree/Class/IClass.java +++ b/Source/abstractSyntaxTree/Class/IClass.java @@ -1,7 +1,9 @@ package abstractSyntaxTree.Class; -public interface IClass { - // not type or type check +import TypeCheck.TypeCheckResult; +import java.util.List; + +public interface IClass { // visit method for code generation } diff --git a/Source/abstractSyntaxTree/Class/MethodDecl.java b/Source/abstractSyntaxTree/Class/MethodDecl.java index 530ebec..3ae18c4 100644 --- a/Source/abstractSyntaxTree/Class/MethodDecl.java +++ b/Source/abstractSyntaxTree/Class/MethodDecl.java @@ -1,4 +1,12 @@ package abstractSyntaxTree.Class; +import TypeCheck.TypeCheckResult; + +import java.util.List; + public class MethodDecl implements IClass { + public TypeCheckResult typeCheck(List fieldsOrMethods) throws Exception { + return null; + } + } diff --git a/Source/abstractSyntaxTree/Datatype/RefType.java b/Source/abstractSyntaxTree/Datatype/RefType.java index 3077893..0855703 100644 --- a/Source/abstractSyntaxTree/Datatype/RefType.java +++ b/Source/abstractSyntaxTree/Datatype/RefType.java @@ -18,15 +18,14 @@ public class RefType extends AbstractType implements IDatatype { public TypeCheckResult typeCheck() throws Exception { TypeCheckResult result = new TypeCheckResult(); -// for (FieldDecl fieldDecl : fieldDecls) { -// fieldDecl.typeCheck(); -// } -// -// for (MethodDecl methodDecl : methodDecls) { -// methodDecl.typeCheck(); -// } -// -// return result; + for (FieldDecl fieldDecl : fieldDecls) { + fieldDecl.typeCheck(fieldDecls); + } + + for (MethodDecl methodDecl : methodDecls) { + methodDecl.typeCheck(methodDecls); + } + result.type = "class"; setTypeCheckResult(result); return result; diff --git a/Source/abstractSyntaxTree/Program.java b/Source/abstractSyntaxTree/Program.java new file mode 100644 index 0000000..a590977 --- /dev/null +++ b/Source/abstractSyntaxTree/Program.java @@ -0,0 +1,15 @@ +package abstractSyntaxTree; + +import TypeCheck.TypeCheckResult; +import abstractSyntaxTree.Datatype.RefType; + +import java.util.List; + +public class Program { + public List classes; + + public TypeCheckResult typeCheck() throws Exception{ + return null; + } + +}