Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
relational-rags
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
JastAdd
relational-rags
Commits
019406f5
Commit
019406f5
authored
6 years ago
by
Niklas Fors
Browse files
Options
Downloads
Patches
Plain Diff
Generate code for directed relations
parent
b55e635a
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
spec/jastadd/Backend.jadd
+80
-3
80 additions, 3 deletions
spec/jastadd/Backend.jadd
src/java/org/jastadd/relast/compiler/Compiler.java
+35
-33
35 additions, 33 deletions
src/java/org/jastadd/relast/compiler/Compiler.java
with
115 additions
and
36 deletions
spec/jastadd/Backend.jadd
+
80
−
3
View file @
019406f5
...
...
@@ -53,10 +53,14 @@ aspect BackendAbstractGrammar {
}
public String RelationComponent.generateAbstractGrammar() {
return "<
_impl_
" + getI
D
() + ":" + ofTypeDecl() + ">";
return "<" + getI
mplAttributeName
() + ":" + ofTypeDecl() + ">";
}
public String ManyRelationComponent.generateAbstractGrammar() {
return "<_impl_" + getID() + ":MySet<" + ofTypeDecl() + ">>";
return "<" + getImplAttributeName() + ":ArrayList<" + ofTypeDecl() + ">>";
}
public String RelationComponent.getImplAttributeName() {
return "_impl_" + getID();
}
}
...
...
@@ -68,6 +72,7 @@ aspect BackendAspect {
}
public void Program.generateAspect(StringBuilder sb) {
sb.append("import java.util.*;\n");
sb.append("aspect RelAstAPI {\n");
for (TypeDecl td: getTypeDecls()) {
if (td.needsConstructor()) {
...
...
@@ -78,6 +83,11 @@ aspect BackendAspect {
r.generateAPI(sb);
}
sb.append(ind(1) + "public void ASTNode.assertNotNull(Object obj) {\n");
sb.append(ind(2) + "if (obj == null) {\n");
sb.append(ind(3) + "throw new NullPointerException();\n");
sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n");
sb.append("}\n");
}
...
...
@@ -105,12 +115,79 @@ aspect BackendAPI {
}
public abstract void Direction.generateAPI(StringBuilder sb);
public void RightDirection.generateAPI(StringBuilder sb) {
relation().getLeft().generateDirectedAPI(sb);
}
public void Bidirectional.generateAPI(StringBuilder sb) {
}
public abstract void RelationComponent.generateDirectedAPI(StringBuilder sb);
public void OneRelationComponent.generateDirectedAPI(StringBuilder sb) {
generateDirectedZeroOneAPI(sb, false);
}
public void OptionalRelationComponent.generateDirectedAPI(StringBuilder sb) {
generateDirectedZeroOneAPI(sb, true);
// has
sb.append(ind(1) + "public boolean " + toTypeDecl());
sb.append(".has" + nameCapitalized() + "() {\n");
sb.append(ind(2) + "return " + name() + "() != null;\n");
sb.append(ind(1) + "}\n");
}
public void RelationComponent.generateDirectedZeroOneAPI(StringBuilder sb, boolean optional) {
// Get
sb.append(ind(1) + "public " + ofTypeDecl() + " " + toTypeDecl());
sb.append("." + name() + "() {\n");
sb.append(ind(2) + "return get" + getImplAttributeName() + "();\n");
sb.append(ind(1) + "}\n");
// Set
sb.append(ind(1) + "public void " + toTypeDecl());
sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!optional) {
sb.append(ind(2) + "assertNotNull(o);\n");
}
sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
sb.append(ind(1) + "}\n");
}
public void ManyRelationComponent.generateDirectedAPI(StringBuilder sb) {
// Get
sb.append(ind(1) + "public Collection<" + ofTypeDecl() + "> " + toTypeDecl());
sb.append("." + name() + "() {\n");
sb.append(ind(2) + "return get" + getImplAttributeName() + "();\n");
sb.append(ind(1) + "}\n");
// Add
sb.append(ind(1) + "public void " + toTypeDecl() + ".addTo");
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + "ArrayList<" + ofTypeDecl() + "> list = get" + getImplAttributeName() + "();\n");
sb.append(ind(2) + "if (list == null) {\n");
sb.append(ind(3) + "list = new ArrayList<>();\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "list.add(o);\n");
sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(1) + "}\n");
// Remove
sb.append(ind(1) + "public void " + toTypeDecl() + ".removeFrom");
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + "ArrayList<" + ofTypeDecl() + "> list = get" + getImplAttributeName() + "();\n");
sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n");
}
inh Relation Direction.relation();
eq Relation.getChild().relation() = this;
eq Program.getChild().relation() = null;
public String RelationComponent.nameCapitalized() {
return name().substring(0,1).toUpperCase() + name().substring(1);
}
}
aspect PrettyPrint {
...
...
This diff is collapsed.
Click to expand it.
src/java/org/jastadd/relast/compiler/Compiler.java
+
35
−
33
View file @
019406f5
package
org.jastadd.relast.compiler
;
import
org.jastadd.relast.ast.*
;
import
static
org
.
jastadd
.
relast
.
compiler
.
Utils
.
filterToList
;
import
java.io.*
;
...
...
@@ -20,8 +19,7 @@ import beaver.Parser;
public
class
Compiler
{
protected
ArrayList
<
Option
<?>>
options
;
protected
FlagOption
optionPrintDot
;
protected
FlagOption
optionFlatPrettyPrint
;
protected
FlagOption
optionWriteToFile
;
protected
CommandLine
commandLine
;
public
Compiler
(
String
args
[])
throws
CommandLineException
{
...
...
@@ -31,7 +29,12 @@ public class Compiler {
commandLine
=
new
CommandLine
(
options
);
commandLine
.
parse
(
args
);
Program
p
=
parseProgram
(
commandLine
.
getArguments
().
get
(
0
));
if
(
commandLine
.
getArguments
().
size
()
!=
1
)
{
error
(
"specify one input file"
);
}
String
filename
=
commandLine
.
getArguments
().
get
(
0
);
Program
p
=
parseProgram
(
filename
);
if
(!
p
.
errors
().
isEmpty
())
{
System
.
out
.
println
(
p
.
dumpTree
());
...
...
@@ -41,38 +44,33 @@ public class Compiler {
}
System
.
exit
(
1
);
}
else
{
System
.
out
.
println
(
p
.
generateAbstractGrammar
());
System
.
out
.
println
(
p
.
generateAspect
());
}
/* } else {
if (optionPrintDot.isSet()) {
System.out.println(p.genDot());
} else if (optionFlatPrettyPrint.isSet()) {
System.out.println(p.flatPrettyPrint());
} else {
System.out.println("# AST");
System.out.println(p.dumpTree());
FlatState s = p.stoppingState();
for (FlatDependency d: s.getDependencyList()) {
System.out.println(d.sourceDecl());
System.out.println(d.sourceDecl().getAccess() + " " + d.targetDecl().getAccess());
}
for (Node n: s.getNodes()) {
System.out.println(n + ", succ: " + n.succ());
}
System.out.println(s.startingNodes());
System.out.println(s.endingNodes());
System.out.println("# Transitive reduction");
System.out.println(p.startingState().transitiveReduction());
if
(
optionWriteToFile
.
isSet
())
{
File
file
=
new
File
(
filename
);
String
absPath
=
file
.
getAbsolutePath
();
String
absPathExclExt
=
absPath
.
substring
(
0
,
absPath
.
lastIndexOf
(
'.'
));
writeToFile
(
absPathExclExt
+
"Gen.ast"
,
p
.
generateAbstractGrammar
());
writeToFile
(
absPathExclExt
+
"Gen.jadd"
,
p
.
generateAspect
());
}
}*/
else
{
System
.
out
.
println
(
p
.
generateAbstractGrammar
());
System
.
out
.
println
(
p
.
generateAspect
());
}
}
}
protected
void
writeToFile
(
String
filename
,
String
str
)
{
try
{
PrintWriter
writer
=
new
PrintWriter
(
filename
,
"UTF-8"
);
writer
.
print
(
str
);
writer
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
System
.
exit
(
1
);
}
}
protected
void
addOptions
()
{
optionPrintDot
=
addOption
(
new
FlagOption
(
"dot"
,
"print DOT format"
));
optionFlatPrettyPrint
=
addOption
(
new
FlagOption
(
"flat"
,
"flat pretty print"
));
optionWriteToFile
=
addOption
(
new
FlagOption
(
"file"
,
"write output to file"
));
}
protected
<
OptionType
extends
Option
<?>>
OptionType
addOption
(
OptionType
option
)
{
...
...
@@ -98,8 +96,7 @@ public class Compiler {
try
{
return
(
Program
)
parser
.
parse
(
scanner
);
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
e
.
getMessage
());
System
.
exit
(
1
);
error
(
e
.
getMessage
());
}
catch
(
Parser
.
Exception
e
)
{
System
.
err
.
println
(
"Parse error in file "
+
file
);
System
.
err
.
println
(
e
.
getMessage
());
...
...
@@ -112,6 +109,11 @@ public class Compiler {
return
null
;
}
protected
void
error
(
String
message
)
{
System
.
err
.
println
(
"Error: "
+
message
);
System
.
exit
(
1
);
}
private
void
showUsageAndExit
()
{
//System.out.println("Usage: java -jar bloqqi-compiler.jar [--option1] [--option2=value] ... <filename1> [filename2] [filename3] ...");
//System.exit(1);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment