Commit b63622ab authored by Niklas Fors's avatar Niklas Fors
Browse files

Add semantic analysis

parent ca14743b
<project name="RelAstCompiler" default="build" basedir=".">
<property name="srcdir" value="${basedir}/src" />
<property name="resourcedir" value="${srcdir}/resource" />
<property name="gendir" value="${srcdir}/generated" />
<property name="bindir" value="${basedir}/ant-bin" />
<property name="docdir" value="${basedir}/doc" />
......@@ -43,9 +42,6 @@
target="8"
source="8"
/>
<copy todir="${bindir}/">
<fileset dir="${resourcedir}"/>
</copy>
</target>
<!-- generate compiler source files -->
......@@ -93,7 +89,6 @@
<delete file="${jarfile}" />
<delete>
<fileset dir="." includes="**/*.class" excludes="bin/" />
<fileset dir="testfiles/code_generation/" includes="*.c *.executable *.out"/>
</delete>
</target>
......@@ -118,9 +113,8 @@
<target name="jar" depends="build">
<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="${resourcedir}" includes="**" />
<fileset dir="${gendir}" includes="**/*.java" />
<zipfileset includes="**/*.class" src="${tools}/beaver-rt.jar"/>
<manifest>
......
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);
}
}
\ No newline at end of file
aspect ComponentAnalysis {
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;
import java.util.LinkedList;
aspect Errors {
// coll Set<ErrorMessage> Program.errors()
// [new TreeSet<ErrorMessage>()]
// root Program;
coll Set<ErrorMessage> Program.errors()
[new TreeSet<ErrorMessage>()]
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 {
// inh Program ASTNode.program();
// eq Program.getChild().program() = this;
inh Program ASTNode.program();
eq Program.getChild().program() = this;
inh boolean TypeUse.isToken();
eq Program.getChild().isToken() = false;
eq TokenComponent.getTypeUse().isToken() = true;
}
aspect ErrorMessage {
......
......@@ -9,13 +9,11 @@ OptComponent : Component;
TypeUse ::= <ID>;
Relation ::= Left:RelationSide Direction Right:RelationSide;
abstract RelationSide ::= TypeUse <ID>;
OneRelationSide : RelationSide;
OptionalRelationSide : RelationSide;
ManyRelationSide : RelationSide;
Relation ::= Left:RelationComponent Direction Right:RelationComponent;
abstract RelationComponent : Component;
OneRelationComponent : RelationComponent;
OptionalRelationComponent : RelationComponent;
ManyRelationComponent : RelationComponent;
abstract Direction;
RightDirection : Direction;
Bidirectional : Direction;
\ No newline at end of file
......@@ -54,20 +54,20 @@ List relations =
;
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); :}
;
RelationSide relation_side =
RelationComponent relation_comp =
// One
type_use.u DOT ID {: return new OneRelationSide(u, ID); :}
| type_use.u {: return new OneRelationSide(u, ""); :}
type_use.u DOT ID {: return new OneRelationComponent(ID, u); :}
| type_use.u {: return new OneRelationComponent("", u); :}
// Optional
| LBRACKET type_use.u DOT ID RBRACKET {: return new OptionalRelationSide(u, ID); :}
| LBRACKET type_use.u RBRACKET {: return new OptionalRelationSide(u, ""); :}
| LBRACKET type_use.u DOT ID RBRACKET {: return new OptionalRelationComponent(ID, u); :}
| LBRACKET type_use.u RBRACKET {: return new OptionalRelationComponent("", u); :}
// Many
| type_use.u STAR DOT ID {: return new ManyRelationSide(u, ID); :}
| type_use.u STAR {: return new ManyRelationSide(u, ""); :}
| type_use.u STAR DOT ID {: return new ManyRelationComponent(ID, u); :}
| type_use.u STAR {: return new ManyRelationComponent("", u); :}
;
Direction direction =
......
......@@ -33,14 +33,16 @@ public class Compiler {
Program p = parseProgram(commandLine.getArguments().get(0));
System.out.println(p.dumpTree());
/*if (!p.errors().isEmpty()) {
if (!p.errors().isEmpty()) {
System.err.println("Errors:");
for (ErrorMessage e: p.errors()) {
System.err.println(e);
}
System.exit(1);
} else {
System.out.println(p.dumpTree());
}
/* } else {
if (optionPrintDot.isSet()) {
System.out.println(p.genDot());
} else if (optionFlatPrettyPrint.isSet()) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment