Skip to content
Snippets Groups Projects
Commit b63622ab authored by Niklas Fors's avatar Niklas Fors
Browse files

Add semantic analysis

parent ca14743b
No related branches found
No related tags found
No related merge requests found
<project name="RelAstCompiler" default="build" basedir="."> <project name="RelAstCompiler" default="build" basedir=".">
<property name="srcdir" value="${basedir}/src" /> <property name="srcdir" value="${basedir}/src" />
<property name="resourcedir" value="${srcdir}/resource" />
<property name="gendir" value="${srcdir}/generated" /> <property name="gendir" value="${srcdir}/generated" />
<property name="bindir" value="${basedir}/ant-bin" /> <property name="bindir" value="${basedir}/ant-bin" />
<property name="docdir" value="${basedir}/doc" /> <property name="docdir" value="${basedir}/doc" />
...@@ -43,9 +42,6 @@ ...@@ -43,9 +42,6 @@
target="8" target="8"
source="8" source="8"
/> />
<copy todir="${bindir}/">
<fileset dir="${resourcedir}"/>
</copy>
</target> </target>
<!-- generate compiler source files --> <!-- generate compiler source files -->
...@@ -93,7 +89,6 @@ ...@@ -93,7 +89,6 @@
<delete file="${jarfile}" /> <delete file="${jarfile}" />
<delete> <delete>
<fileset dir="." includes="**/*.class" excludes="bin/" /> <fileset dir="." includes="**/*.class" excludes="bin/" />
<fileset dir="testfiles/code_generation/" includes="*.c *.executable *.out"/>
</delete> </delete>
</target> </target>
...@@ -118,9 +113,8 @@ ...@@ -118,9 +113,8 @@
<target name="jar" depends="build"> <target name="jar" depends="build">
<jar destfile="${jarfile}"> <jar destfile="${jarfile}">
<fileset dir="${bindir}" includes="**/*.class,resource/" excludes="org/jastadd/relast/tests/*" /> <fileset dir="${bindir}" includes="**/*.class" excludes="org/jastadd/relast/tests/*" />
<fileset dir="${srcdir}/java" includes="**/*.java" excludes="org/jastadd/relast/tests/*" /> <fileset dir="${srcdir}/java" includes="**/*.java" excludes="org/jastadd/relast/tests/*" />
<fileset dir="${resourcedir}" includes="**" />
<fileset dir="${gendir}" includes="**/*.java" /> <fileset dir="${gendir}" includes="**/*.java" />
<zipfileset includes="**/*.class" src="${tools}/beaver-rt.jar"/> <zipfileset includes="**/*.class" src="${tools}/beaver-rt.jar"/>
<manifest> <manifest>
......
import java.util.*; import java.util.*;
aspect Analysis { aspect TypeAnalysis {
syn TypeDecl TypeUse.decl() = lookupType(getID());
inh TypeDecl TypeUse.lookupType(String name);
eq Program.getChild().lookupType(String name) {
for (TypeDecl td: getTypeDecls()) {
if (td.getID().equals(name)) {
return td;
}
}
return null;
}
syn boolean TypeDecl.isAlreadyDeclared()
= lookupType(getID()) != this;
inh TypeDecl TypeDecl.lookupType(String name);
}
} aspect ComponentAnalysis {
\ No newline at end of file syn String Component.name() {
if (!getID().isEmpty()) {
return getID();
}
return otherRelationSideName();
}
inh String Component.otherRelationSideName();
eq Relation.getLeft().otherRelationSideName()
= getRight().getTypeUse().getID();
eq Relation.getRight().otherRelationSideName()
= getLeft().getTypeUse().getID();
eq Program.getChild().otherRelationSideName()
= "";
syn TypeDecl Component.typeDecl() = enclosingTypeDecl();
eq RelationComponent.typeDecl() = getTypeUse().decl();
inh TypeDecl Component.enclosingTypeDecl();
eq TypeDecl.getChild().enclosingTypeDecl() = this;
eq Program.getChild().enclosingTypeDecl() = null;
syn boolean Component.isAlreadyDeclared()
= lookupComponent(typeDecl(), name()) != this;
inh Component Component.lookupComponent(TypeDecl td, String name);
eq Program.getChild().lookupComponent(TypeDecl td, String name) {
if (td != null) {
for (Component c: td.getComponents()) {
if (c.name().equals(name)) {
return c;
}
}
}
for (Relation r: getRelations()) {
if (r.getLeft().typeDecl() == td && r.getLeft().name().equals(name)) {
return r.getLeft();
}
if (r.getRight().typeDecl() == td && r.getRight().name().equals(name)) {
return r.getRight();
}
}
return null;
}
}
aspect Utils {
public String TypeUse.toString() {
return getID();
}
public String TypeDecl.toString() {
return getID();
}
}
...@@ -3,14 +3,31 @@ import java.util.TreeSet; ...@@ -3,14 +3,31 @@ import java.util.TreeSet;
import java.util.LinkedList; import java.util.LinkedList;
aspect Errors { aspect Errors {
// coll Set<ErrorMessage> Program.errors() coll Set<ErrorMessage> Program.errors()
// [new TreeSet<ErrorMessage>()] [new TreeSet<ErrorMessage>()]
// root Program; root Program;
TypeUse contributes error("Type '" + getID() + "' not found")
when decl() == null && !isToken()
to Program.errors();
TypeDecl contributes error("Type '" + getID() + "' is already declared")
when isAlreadyDeclared()
to Program.errors();
Component contributes error("Component '" + name()
+ "' is already declared for type '" + typeDecl() + "'")
when isAlreadyDeclared()
to Program.errors();
} }
aspect HelpAttributes { aspect HelpAttributes {
// inh Program ASTNode.program(); inh Program ASTNode.program();
// eq Program.getChild().program() = this; eq Program.getChild().program() = this;
inh boolean TypeUse.isToken();
eq Program.getChild().isToken() = false;
eq TokenComponent.getTypeUse().isToken() = true;
} }
aspect ErrorMessage { aspect ErrorMessage {
......
...@@ -9,13 +9,11 @@ OptComponent : Component; ...@@ -9,13 +9,11 @@ OptComponent : Component;
TypeUse ::= <ID>; TypeUse ::= <ID>;
Relation ::= Left:RelationSide Direction Right:RelationSide; Relation ::= Left:RelationComponent Direction Right:RelationComponent;
abstract RelationSide ::= TypeUse <ID>; abstract RelationComponent : Component;
OneRelationSide : RelationSide; OneRelationComponent : RelationComponent;
OptionalRelationSide : RelationSide; OptionalRelationComponent : RelationComponent;
ManyRelationSide : RelationSide; ManyRelationComponent : RelationComponent;
abstract Direction; abstract Direction;
RightDirection : Direction; RightDirection : Direction;
Bidirectional : Direction; Bidirectional : Direction;
\ No newline at end of file
...@@ -54,20 +54,20 @@ List relations = ...@@ -54,20 +54,20 @@ List relations =
; ;
Relation relation = Relation relation =
RELATION relation_side.l direction relation_side.r SCOL RELATION relation_comp.l direction relation_comp.r SCOL
{: return new Relation(l, direction, r); :} {: return new Relation(l, direction, r); :}
; ;
RelationSide relation_side = RelationComponent relation_comp =
// One // One
type_use.u DOT ID {: return new OneRelationSide(u, ID); :} type_use.u DOT ID {: return new OneRelationComponent(ID, u); :}
| type_use.u {: return new OneRelationSide(u, ""); :} | type_use.u {: return new OneRelationComponent("", u); :}
// Optional // Optional
| LBRACKET type_use.u DOT ID RBRACKET {: return new OptionalRelationSide(u, ID); :} | LBRACKET type_use.u DOT ID RBRACKET {: return new OptionalRelationComponent(ID, u); :}
| LBRACKET type_use.u RBRACKET {: return new OptionalRelationSide(u, ""); :} | LBRACKET type_use.u RBRACKET {: return new OptionalRelationComponent("", u); :}
// Many // Many
| type_use.u STAR DOT ID {: return new ManyRelationSide(u, ID); :} | type_use.u STAR DOT ID {: return new ManyRelationComponent(ID, u); :}
| type_use.u STAR {: return new ManyRelationSide(u, ""); :} | type_use.u STAR {: return new ManyRelationComponent("", u); :}
; ;
Direction direction = Direction direction =
......
...@@ -33,14 +33,16 @@ public class Compiler { ...@@ -33,14 +33,16 @@ public class Compiler {
Program p = parseProgram(commandLine.getArguments().get(0)); Program p = parseProgram(commandLine.getArguments().get(0));
System.out.println(p.dumpTree()); if (!p.errors().isEmpty()) {
/*if (!p.errors().isEmpty()) {
System.err.println("Errors:"); System.err.println("Errors:");
for (ErrorMessage e: p.errors()) { for (ErrorMessage e: p.errors()) {
System.err.println(e); System.err.println(e);
} }
System.exit(1); System.exit(1);
} else { } else {
System.out.println(p.dumpTree());
}
/* } else {
if (optionPrintDot.isSet()) { if (optionPrintDot.isSet()) {
System.out.println(p.genDot()); System.out.println(p.genDot());
} else if (optionFlatPrettyPrint.isSet()) { } else if (optionFlatPrettyPrint.isSet()) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment