6 new revisions:
Revision: a85b5e9ef9
Author: Alex Eagle <
alex...@google.com>
Date: Fri Apr 16 14:27:28 2010
Log: Port java-based model from my git repo. Begin shifting the model to
a ...
http://code.google.com/p/noop/source/detail?r=a85b5e9ef9
Revision: 8a18f4a525
Author: Alex Eagle <
alex...@google.com>
Date: Fri Apr 16 14:53:57 2010
Log: Introduce plain XML POM files. They work better with tooling.
http://code.google.com/p/noop/source/detail?r=8a18f4a525
Revision: 91e17133c8
Author: Alex Eagle <
alex...@google.com>
Date: Sun Apr 18 00:49:40 2010
Log: Rename compiler to translator, create pom, and change to Java
http://code.google.com/p/noop/source/detail?r=91e17133c8
Revision: 80da50496b
Author: Alex Eagle <
alex...@google.com>
Date: Sun Apr 18 01:54:12 2010
Log: Move stdlib to a separate class for reuse
http://code.google.com/p/noop/source/detail?r=80da50496b
Revision: 2a51630295
Author: Alex Eagle <
alex...@google.com>
Date: Mon Apr 19 07:04:03 2010
Log: Make new constructor for NewNodeOperation rather than passing null
http://code.google.com/p/noop/source/detail?r=2a51630295
Revision: 1b619c23a7
Author: Alex Eagle <
alex...@google.com>
Date: Mon Apr 19 10:04:58 2010
Log: Add another printing visitor, which allows the ordering of
statements ...
http://code.google.com/p/noop/source/detail?r=1b619c23a7
==============================================================================
Revision: a85b5e9ef9
Author: Alex Eagle <
alex...@google.com>
Date: Fri Apr 16 14:27:28 2010
Log: Port java-based model from my git repo. Begin shifting the model to a
delta-built graph representation in anticipation of this being the way of
representing diffs.
http://code.google.com/p/noop/source/detail?r=a85b5e9ef9
Added:
/core/src/main/java/noop/graph/Controller.java
/core/src/main/java/noop/graph/DotGraphPrintingVisitor.java
/core/src/main/java/noop/graph/Edge.java
/core/src/main/java/noop/graph/ModelVisitor.java
/core/src/main/java/noop/graph/Workspace.java
/core/src/main/java/noop/model/Assignment.java
/core/src/main/java/noop/model/Block.java
/core/src/main/java/noop/model/Clazz.java
/core/src/main/java/noop/model/Documentation.java
/core/src/main/java/noop/model/Expression.java
/core/src/main/java/noop/model/IdentifierDeclaration.java
/core/src/main/java/noop/model/IntegerLiteral.java
/core/src/main/java/noop/model/LanguageElement.java
/core/src/main/java/noop/model/Library.java
/core/src/main/java/noop/model/MethodInvocation.java
/core/src/main/java/noop/model/Modifier.java
/core/src/main/java/noop/model/Parameter.java
/core/src/main/java/noop/model/Project.java
/core/src/main/java/noop/model/Return.java
/core/src/main/java/noop/model/StringLiteral.java
/core/src/main/java/noop/operations/EditNodeOperation.java
/core/src/main/java/noop/operations/MutationOperation.java
/core/src/main/java/noop/operations/NewNodeOperation.java
/core/src/main/scala/noop/model/ABlock.scala
/core/src/main/scala/noop/model/AExpression.scala
/core/src/main/scala/noop/model/AModifier.scala
/core/src/main/scala/noop/model/AParameter.scala
/core/src/test/java/noop/graph/ControllerTest.java
/core/src/test/java/noop/graph/HelloWorldExampleMain.java
Deleted:
/core/src/main/scala/noop/model/Block.scala
/core/src/main/scala/noop/model/Expression.scala
/core/src/main/scala/noop/model/Modifier.scala
/core/src/main/scala/noop/model/Parameter.scala
Modified:
/buildfile
=======================================
--- /dev/null
+++ /core/src/main/java/noop/graph/Controller.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.graph;
+
+import noop.graph.Edge;
+import noop.graph.Edge.EdgeType;
+import noop.model.LanguageElement;
+import noop.graph.Workspace;
+import noop.operations.EditNodeOperation;
+import noop.operations.NewNodeOperation;
+
+import java.util.Map.Entry;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Controller {
+ private Workspace workspace;
+
+ public Controller(Workspace workspace) {
+ this.workspace = workspace;
+ }
+
+ public void apply(NewNodeOperation operation) {
+ workspace.elements.add(operation.newElement);
+ int newNodeId = workspace.elements.size() - 1;
+ addEdge(newNodeId, operation.container, EdgeType.CONTAIN, true);
+ for (Entry<EdgeType, LanguageElement> edgeTypeLanguageNodeEntry :
operation.edges.entries()) {
+ LanguageElement destElement = edgeTypeLanguageNodeEntry.getValue();
+ EdgeType edgeType = edgeTypeLanguageNodeEntry.getKey();
+ addEdge(newNodeId, destElement, edgeType, false);
+ }
+ }
+
+ private void addEdge(int newNodeId, LanguageElement destElement,
EdgeType edgeType, boolean backwards) {
+ int destId = workspace.elements.indexOf(destElement);
+ if (destId < 0) {
+ throw new IllegalStateException(String.format("Cannot add edge [%s
-> %s] due to non-existant dest",
+ newNodeId, destId));
+ }
+ Edge newEdge = backwards ? new Edge(destId, edgeType, newNodeId) : new
Edge(newNodeId, edgeType, destId);
+ workspace.edges.add(newEdge);
+ }
+
+ public void applyAll(NewNodeOperation... operations) {
+ for (NewNodeOperation operation : operations) {
+ apply(operation);
+ }
+ }
+
+ public void apply(EditNodeOperation operation) {
+ LanguageElement currentValue = workspace.elements.get(
operation.id);
+ if (currentValue.getClass() != operation.newValue.getClass()) {
+ throw new IllegalArgumentException(String.format("Cannot edit
node %d with %s because the current type is %s",
+
operation.id, operation.newValue, currentValue.getClass()));
+ }
+
+ operation.newValue.setPreviousVersion(currentValue);
+ workspace.elements.set(
operation.id, operation.newValue);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/graph/DotGraphPrintingVisitor.java Fri Apr 16
14:27:28 2010
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.graph;
+
+import noop.model.*;
+
+import java.io.PrintStream;
+
+import static java.lang.System.identityHashCode;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class DotGraphPrintingVisitor extends ModelVisitor {
+ private final Workspace workspace;
+ private final PrintStream out;
+
+ public DotGraphPrintingVisitor(Workspace workspace, PrintStream out) {
+ this.workspace = workspace;
+ this.out = out;
+ }
+
+ private int idFor(LanguageElement element) {
+ return workspace.elements.indexOf(element);
+ }
+
+ @Override
+ public void visit(Workspace workspace) {
+ out.format("digraph workspace\n{\n");
+ out.format("%s [label=\"%s\", shape=house]\n",
idFor(workspace), "Workspace");
+ }
+
+ @Override
+ public void visit(Project project) {
+ out.format("%s [label=\"%s (%s)\", shape=box]\n",
+ idFor(project), project.getName(), project.getNamespace());
+
+ out.format("%s [label=\"%s\", shape=none]\n",
identityHashCode(project.getCopyright()),
+ escape(project.getCopyright()));
+ out.format("%s -> %s\n", idFor(project),
identityHashCode(project.getCopyright()));
+ }
+
+ @Override
+ public void visit(Library library) {
+ out.format("%s [label=\"%s\", shape=hexagon]\n", idFor(library),
library.name);
+ }
+
+ @Override
+ public void visit(Block block) {
+ out.format("%s [label=\"%s {}\"]\n", idFor(block),
block.name);
+ for (Parameter parameter : block.parameters) {
+ out.format("%s -> %s [label=param, style=dotted]\n",
+ idFor(block), idFor(parameter));
+ }
+ if (block.returnType != null) {
+ out.format("%s -> %s [label=return, style=dotted]\n",
+ idFor(block), idFor(block.returnType));
+ }
+ }
+
+ @Override
+ public void visit(Return aReturn) {
+ out.format("%s [label=\"%s\"]\n", idFor(aReturn), "[return]");
+ out.format("%s -> %s [label=arg, style=dotted]\n",
+ idFor(aReturn), idFor(aReturn.returned));
+ }
+
+ @Override
+ public void visit(IntegerLiteral integerLiteral) {
+ out.format("%s [label=\"%s\"]\n", idFor(integerLiteral),
integerLiteral.value);
+ }
+
+ @Override
+ public void visit(Parameter parameter) {
+ out.format("%s [label=\"%s\"]\n", idFor(parameter),
parameter.name);
+ }
+
+ @Override
+ public void visit(MethodInvocation methodInvocation) {
+ out.format("%s [label=\"%s\"]\n", idFor(methodInvocation), "[invoke]");
+ for (Expression argument : methodInvocation.arguments) {
+ out.format("%s -> %s [label=arg, style=dotted]\n",
+ idFor(methodInvocation), idFor(argument));
+ }
+ }
+
+ @Override
+ public void visit(Documentation documentation) {
+ out.format("%s [label=\"%s\", shape=none]\n", idFor(documentation),
escape(documentation.summary));
+ }
+
+ private String escape(String value) {
+ return value.replaceAll("\n", "\\\\n");
+ }
+
+ @Override
+ public void visit(StringLiteral stringLiteral) {
+ out.format("%s [label=\"\\\"%s\\\"\"]\n", idFor(stringLiteral),
stringLiteral.value);
+ }
+
+ @Override
+ public void visit(Clazz clazz) {
+ out.format("%s [label=\"%s\"]\n", idFor(clazz),
clazz.name);
+ }
+
+ @Override
+ public void leave(Workspace workspace) {
+ out.println("}");
+ }
+
+ @Override
+ public void visit(Edge edge) {
+ out.format("%d -> %d ", edge.src, edge.dest);
+ switch (edge.type) {
+ case CONTAIN:
+ out.print("\n");
+ break;
+ default:
+ out.println("[label=\"" +
edge.type.name().toLowerCase() + "\",
style=dashed]");
+ break;
+ }
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/graph/Edge.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.graph;
+
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Edge {
+ public Edge(int src, EdgeType type, int dest) {
+ this.type = type;
+ this.src = src;
+ this.dest = dest;
+ }
+
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+
+ public enum EdgeType {
+ INVOKE,
+ CONTAIN,
+ OVERRIDE,
+ TYPEOF,
+ TARGET
+ }
+
+ public final EdgeType type;
+ public final int src;
+ public final int dest;
+
+ @Override
+ public String toString() {
+ return src + "->" + dest;
+ }
+
+ @Override
+ public int hashCode() {
+ return new HashCodeBuilder()
+ .append(src)
+ .append(dest)
+ .append(type)
+ .toHashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) { return false; }
+ if (obj == this) { return true; }
+ if (obj.getClass() != getClass()) {
+ return false;
+ }
+ Edge rhs = (Edge) obj;
+ return new EqualsBuilder()
+ .append(src, rhs.src)
+ .append(dest, rhs.dest)
+ .append(type, rhs.type)
+ .isEquals();
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/graph/ModelVisitor.java Fri Apr 16 14:27:28
2010
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.graph;
+
+import noop.model.*;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public abstract class ModelVisitor {
+ public void visit(Edge edge) {}
+
+ public void visit(Workspace workspace) {}
+
+ public void visit(Block block) {}
+
+ public void visit(Project project) {}
+
+ public void visit(MethodInvocation methodInvocation) {}
+
+ public void visit(Parameter parameter) {}
+
+ public void visit(Library library) {}
+
+ public void visit(Clazz clazz) {}
+
+ public void leave(Workspace workspace) {}
+
+ public void visit(StringLiteral stringLiteral) {}
+
+ public void visit(Return aReturn) {}
+
+ public void visit(IntegerLiteral integerLiteral) {}
+
+ public void visit(Documentation documentation) {}
+
+ public void visit(Assignment assignment) {}
+
+ public void visit(IdentifierDeclaration identifierDeclaration) {}
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/graph/Workspace.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.graph;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import noop.graph.Edge;
+import noop.graph.ModelVisitor;
+import noop.model.LanguageElement;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Workspace extends LanguageElement<Workspace> {
+ public final Set<Edge> edges = Sets.newHashSet();
+ public final List<LanguageElement> elements =
Lists.<LanguageElement>newArrayList(this);
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ for (LanguageElement element : elements) {
+ if (element != this) {
+ element.accept(v);
+ }
+ }
+ for (Edge edge : edges) {
+ edge.accept(v);
+ }
+ v.leave(this);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Assignment.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Assignment extends Expression<Assignment> {
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Block.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+import java.util.List;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Block extends LanguageElement<Block> {
+ public final String name;
+ public final Clazz returnType;
+ public final List<Parameter> parameters;
+
+ public Block(String name, Clazz returnType, Parameter... parameters) {
+
this.name = name;
+ this.returnType = returnType;
+ this.parameters = asList(parameters);
+ }
+
+ public Block() {
+
this.name = null;
+ this.returnType = null;
+ this.parameters = emptyList();
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+
+ public boolean isMethod() {
+ return name != null;
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Clazz.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Clazz extends LanguageElement<Clazz> {
+ public final String name;
+
+ public Clazz(String name) {
+
this.name = name;
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Documentation.java Fri Apr 16 14:27:28
2010
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Documentation extends LanguageElement<Documentation> {
+ public final String summary;
+
+ public Documentation(String summary) {
+ this.summary = summary;
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Expression.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public abstract class Expression<T> extends LanguageElement<T> {
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/IdentifierDeclaration.java Fri Apr 16
14:27:28 2010
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class IdentifierDeclaration extends
Expression<IdentifierDeclaration> {
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/IntegerLiteral.java Fri Apr 16 14:27:28
2010
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class IntegerLiteral extends Expression<IntegerLiteral> {
+ public final int value;
+
+ public IntegerLiteral(int value) {
+ this.value = value;
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/LanguageElement.java Fri Apr 16 14:27:28
2010
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public abstract class LanguageElement<T> implements Serializable {
+ protected T previousVersion;
+ public abstract void accept(ModelVisitor v);
+ public T getPreviousVersion() {
+ return previousVersion;
+ }
+ public void setPreviousVersion(T previousVersion) {
+ this.previousVersion = previousVersion;
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Library.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Library extends LanguageElement<Library> {
+ public final String name;
+ public String copyright;
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+
+ public Library(String name) {
+
this.name = name;
+
+
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/MethodInvocation.java Fri Apr 16
14:27:28 2010
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class MethodInvocation extends Expression<MethodInvocation> {
+ public final List<Expression> arguments;
+
+ public MethodInvocation(Expression... arguments) {
+ this.arguments = Arrays.asList(arguments);
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ for (Expression argument : arguments) {
+ argument.accept(v);
+ }
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Modifier.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public enum Modifier {
+ MUTABLE, NATIVE, DELEGATE
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Parameter.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Parameter extends LanguageElement<Parameter> {
+ public final String name;
+
+ public Parameter(String name) {
+
this.name = name;
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Project.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Project extends LanguageElement<Project> {
+ private final String name;
+ private final String namespace;
+ private final String copyright;
+
+ public String getCopyright() {
+ return copyright;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getNamespace() {
+ return namespace;
+ }
+
+ public Project(String name, String namespace, String copyright) {
+
this.name = name;
+ this.namespace = namespace;
+ this.copyright = copyright;
+ }
+
+ @Override
+ public int hashCode() {
+ return new HashCodeBuilder()
+ .append(name)
+ .append(namespace)
+ .toHashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) { return false; }
+ if (obj == this) { return true; }
+ if (obj.getClass() != getClass()) {
+ return false;
+ }
+ Project rhs = (Project) obj;
+ return new EqualsBuilder()
+ .append(name,
rhs.name)
+ .append(namespace, rhs.namespace)
+ .isEquals();
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/Return.java Fri Apr 16 14:27:28 2010
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class Return extends Expression {
+ public final Expression returned;
+
+ public Return(Expression returned) {
+ this.returned = returned;
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ returned.accept(v);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/model/StringLiteral.java Fri Apr 16 14:27:28
2010
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.model;
+
+import noop.graph.ModelVisitor;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class StringLiteral extends Expression<StringLiteral> {
+ public final String value;
+
+ public StringLiteral(String value) {
+ this.value = value;
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/operations/EditNodeOperation.java Fri Apr 16
14:27:28 2010
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.operations;
+
+import noop.model.LanguageElement;
+
+import java.io.Serializable;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class EditNodeOperation implements MutationOperation, Serializable {
+ public final int id;
+ public final LanguageElement newValue;
+
+ public EditNodeOperation(int id, LanguageElement newValue) {
+
this.id = id;
+ this.newValue = newValue;
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/operations/MutationOperation.java Fri Apr 16
14:27:28 2010
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.operations;
+
+import java.io.Serializable;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public interface MutationOperation extends Serializable {
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/operations/NewNodeOperation.java Fri Apr 16
14:27:28 2010
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.operations;
+
+import com.google.common.base.Supplier;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
+import noop.model.LanguageElement;
+
+import java.util.Collection;
+import java.util.List;
+
+import static noop.graph.Edge.EdgeType;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class NewNodeOperation implements MutationOperation {
+ public final LanguageElement newElement;
+ public final LanguageElement container;
+ public final Multimap<EdgeType, LanguageElement> edges =
Multimaps.newListMultimap(
+ Maps.<EdgeType, Collection<LanguageElement>>newHashMap(), new
Supplier<List<LanguageElement>>() {
+ public List<LanguageElement> get() {
+ return Lists.newArrayList();
+ }
+ });
+
+ public NewNodeOperation(LanguageElement newElement, LanguageElement
container) {
+ this.newElement = newElement;
+ this.container = container;
+ }
+
+ public NewNodeOperation(LanguageElement newElement, LanguageElement
container,
+ EdgeType edgeType, LanguageElement dest) {
+ this(newElement, container);
+ edges.put(edgeType, dest);
+ }
+
+ public NewNodeOperation(LanguageElement newElement, LanguageElement
container,
+ EdgeType edgeType, LanguageElement dest,
+ EdgeType edge2Type, LanguageElement dest2) {
+ this(newElement, container, edgeType, dest);
+ edges.put(edge2Type, dest2);
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/scala/noop/model/ABlock.scala Fri Apr 16 14:27:28 2010
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package noop.model;
+
+import collection.mutable.{ArrayBuffer, Buffer};
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ * @author
toc...@gmail.com (Jeremie Lenfant-Engelmann)
+ */
+class Block extends Expression {
+
+ val statements: Buffer[Expression] = new ArrayBuffer[Expression]();
+ val anonymousBindings: Buffer[BindingDeclaration] = new
ArrayBuffer[BindingDeclaration];
+ var namedBinding: Option[String] = None;
+
+ override def accept(visitor: Visitor): Unit = {
+ for (anonBinding <- anonymousBindings) {
+ anonBinding.accept(visitor);
+ }
+ for (statement <- statements) {
+ statement.accept(visitor);
+ if (statement.isInstanceOf[ReturnExpression]) {
+ return; // break is not in scala 2.7 !
+ }
+ visitor.visit(this);
+ }
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/scala/noop/model/AExpression.scala Fri Apr 16 14:27:28
2010
@@ -0,0 +1,25 @@
+/**
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package noop.model;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ * @author
toc...@gmail.com (Jeremie Lenfant-Engelmann)
+ */
+trait Expression {
+
+ def accept(visitor: Visitor): Unit;
+}
=======================================
--- /dev/null
+++ /core/src/main/scala/noop/model/AModifier.scala Fri Apr 16 14:27:28 2010
@@ -0,0 +1,27 @@
+/**
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package noop.model;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+object Modifier extends Enumeration {
+
+ type Modifier = Value;
+ val native = Value("native");
+ val mutable = Value("mutable");
+ val delegate = Value("delegate");
+}
=======================================
--- /dev/null
+++ /core/src/main/scala/noop/model/AParameter.scala Fri Apr 16 14:27:28
2010
@@ -0,0 +1,29 @@
+/**
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package noop.model
+
+import collection.mutable.{ArrayBuffer, Buffer}
+
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ * @author
toc...@gmail.com (Jeremie Lenfant-Engelmann)
+ */
+class Parameter(val name:String, var noopType: String) {
+ val modifiers: Buffer[Modifier.Value] = new ArrayBuffer[Modifier.Value];
+
+ override def toString() = String.format("Param[%s %s]", noopType, name);
+}
=======================================
--- /dev/null
+++ /core/src/test/java/noop/graph/ControllerTest.java Fri Apr 16 14:27:28
2010
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.graph;
+
+import noop.model.*;
+import noop.operations.EditNodeOperation;
+import noop.operations.NewNodeOperation;
+import org.junit.Before;
+import org.junit.Test;
+
+import static noop.graph.Edge.EdgeType.CONTAIN;
+import static noop.graph.Edge.EdgeType.TYPEOF;
+import static org.junit.Assert.*;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class ControllerTest {
+ private Controller controller;
+ private Workspace workspace;
+
+ @Before
+ public void setUp() {
+ workspace = new Workspace();
+ controller = new Controller(workspace);
+ }
+
+ @Test public void shouldMakeNewProject() {
+ LanguageElement newNode = new Project("helloWorld", "
com.google", "");
+ controller.apply(new NewNodeOperation(newNode, workspace));
+ assertTrue(workspace.elements.contains(newNode));
+ assertEquals(1, workspace.edges.size());
+ assertEquals(new Edge(0, CONTAIN, 1),
workspace.edges.iterator().next());
+ }
+
+ @Test public void shouldCreateAdditionalEdges() {
+ LanguageElement stringType = new Clazz("String");
+ controller.apply(new NewNodeOperation(stringType, workspace));
+
+ LanguageElement newNode = new StringLiteral("yes");
+ controller.apply(new NewNodeOperation(newNode, workspace, TYPEOF,
stringType));
+ assertEquals(3, workspace.edges.size());
+ assertTrue(workspace.edges.contains(new Edge(0, CONTAIN, 1)));
+ assertTrue(workspace.edges.contains(new Edge(0, CONTAIN, 2)));
+ assertTrue(workspace.edges.contains(new Edge(2, TYPEOF, 1)));
+ }
+
+ @Test public void shouldAllowEditingAStringLiteral() {
+ StringLiteral aString = new StringLiteral("hello");
+ controller.apply(new NewNodeOperation(aString, workspace));
+
+ controller.apply(new EditNodeOperation(1, new
StringLiteral("goodbye")));
+ assertEquals(2, workspace.elements.size());
+ assertEquals("goodbye", ((StringLiteral)
workspace.elements.get(1)).value);
+ assertEquals("hello", ((StringLiteral)
workspace.elements.get(1).getPreviousVersion()).value);
+ }
+
+ @Test public void shouldErrorWhenEditingWithWrongType() {
+ IntegerLiteral anInt = new IntegerLiteral(12);
+ controller.apply(new NewNodeOperation(anInt, workspace));
+
+ try {
+ controller.apply(new EditNodeOperation(1, new StringLiteral("String
is not Int")));
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage(),
e.getMessage().contains("IntegerLiteral"));
+ }
+ }
+}
=======================================
--- /dev/null
+++ /core/src/test/java/noop/graph/HelloWorldExampleMain.java Fri Apr 16
14:27:28 2010
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.graph;
+
+import noop.model.*;
+import noop.operations.NewNodeOperation;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
+
+import static noop.graph.Edge.EdgeType.*;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class HelloWorldExampleMain {
+ private static Clazz stringClazz;
+ private static Clazz consoleClazz;
+ private static Clazz intClazz;
+ private static Block printMethod;
+
+ public static void main(String[] args) throws FileNotFoundException {
+ Workspace workspace = new Workspace();
+ Controller controller = new Controller(workspace);
+ createNoopStdLib(workspace, controller);
+ createHelloWorldProgram(workspace, controller);
+ PrintStream out = new PrintStream(new FileOutputStream(new
File(args[0])));
+ workspace.accept(new DotGraphPrintingVisitor(workspace, out));
+ }
+
+ public static void createNoopStdLib(Workspace workspace, Controller
controller) {
+ Project project = new Project("Noop", "com.google.noop", "Apache 2");
+ controller.apply(new NewNodeOperation(project, workspace));
+
+ Library lang = new Library("lang");
+ controller.apply(new NewNodeOperation(lang, project));
+
+ stringClazz = new Clazz("String");
+ controller.apply(new NewNodeOperation(stringClazz, lang));
+
+ Library io = new Library("io");
+ controller.apply(new NewNodeOperation(io, project));
+
+ consoleClazz = new Clazz("Console");
+ controller.apply(new NewNodeOperation(consoleClazz, io));
+
+ printMethod = new Block("print", null);
+ controller.apply(new NewNodeOperation(printMethod, consoleClazz));
+
+ Parameter printArg = new Parameter("s");
+ controller.apply(new NewNodeOperation(printArg, printMethod, TYPEOF,
stringClazz));
+
+ intClazz = new Clazz("Integer");
+ controller.apply(new NewNodeOperation(intClazz, lang));
+ }
+
+ public static void createHelloWorldProgram(Workspace workspace,
Controller controller) {
+ Project project = new Project("Hello World", "com.example", "Copyright
2010\nExample Co.");
+ controller.apply(new NewNodeOperation(project, workspace));
+
+ Library library = new Library("hello");
+ controller.apply(new NewNodeOperation(library, project));
+
+ Parameter consoleDep = new Parameter("console");
+
+ Block sayHello = new Block("say hello", intClazz, consoleDep);
+ controller.applyAll(new NewNodeOperation(sayHello, library),
+ new NewNodeOperation(consoleDep, sayHello, TYPEOF,
consoleClazz));
+
+ Documentation sayHelloDoc = new Documentation("This is the entry point
for the Hello World app");
+ controller.apply(new NewNodeOperation(sayHelloDoc, sayHello));
+
+ StringLiteral helloWorld = new StringLiteral("Hello, World!");
+ controller.apply(new NewNodeOperation(helloWorld, sayHello, TYPEOF,
stringClazz));
+
+ Expression printHello = new MethodInvocation(helloWorld);
+ controller.apply(new NewNodeOperation(printHello, sayHello, TARGET,
consoleDep, INVOKE, printMethod));
+
+ IntegerLiteral zero = new IntegerLiteral(0);
+ controller.applyAll(new NewNodeOperation(zero, sayHello, TYPEOF,
intClazz),
+ new NewNodeOperation(new Return(zero), sayHello));
+ }
+
+}
=======================================
--- /core/src/main/scala/noop/model/Block.scala Sun Feb 21 17:25:15 2010
+++ /dev/null
@@ -1,42 +0,0 @@
-/**
- * Copyright 2009 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package noop.model;
-
-import collection.mutable.{ArrayBuffer, Buffer};
-
-/**
- * @author
alex...@google.com (Alex Eagle)
- * @author
toc...@gmail.com (Jeremie Lenfant-Engelmann)
- */
-class Block extends Expression {
-
- val statements: Buffer[Expression] = new ArrayBuffer[Expression]();
- val anonymousBindings: Buffer[BindingDeclaration] = new
ArrayBuffer[BindingDeclaration];
- var namedBinding: Option[String] = None;
-
- override def accept(visitor: Visitor): Unit = {
- for (anonBinding <- anonymousBindings) {
- anonBinding.accept(visitor);
- }
- for (statement <- statements) {
- statement.accept(visitor);
- if (statement.isInstanceOf[ReturnExpression]) {
- return; // break is not in scala 2.7 !
- }
- visitor.visit(this);
- }
- }
-}
=======================================
--- /core/src/main/scala/noop/model/Expression.scala Thu Oct 8 10:25:57
2009
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * Copyright 2009 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package noop.model;
-
-/**
- * @author
alex...@google.com (Alex Eagle)
- * @author
toc...@gmail.com (Jeremie Lenfant-Engelmann)
- */
-trait Expression {
-
- def accept(visitor: Visitor): Unit;
-}
=======================================
--- /core/src/main/scala/noop/model/Modifier.scala Thu Oct 8 10:25:57 2009
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
- * Copyright 2009 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package noop.model;
-
-/**
- * @author
alex...@google.com (Alex Eagle)
- */
-object Modifier extends Enumeration {
-
- type Modifier = Value;
- val native = Value("native");
- val mutable = Value("mutable");
- val delegate = Value("delegate");
-}
=======================================
--- /core/src/main/scala/noop/model/Parameter.scala Mon Nov 30 08:13:22 2009
+++ /dev/null
@@ -1,29 +0,0 @@
-/**
- * Copyright 2009 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package noop.model
-
-import collection.mutable.{ArrayBuffer, Buffer}
-
-
-/**
- * @author
alex...@google.com (Alex Eagle)
- * @author
toc...@gmail.com (Jeremie Lenfant-Engelmann)
- */
-class Parameter(val name:String, var noopType: String) {
- val modifiers: Buffer[Modifier.Value] = new ArrayBuffer[Modifier.Value];
-
- override def toString() = String.format("Param[%s %s]", noopType, name);
-}
=======================================
--- /buildfile Fri Mar 26 14:40:04 2010
+++ /buildfile Fri Apr 16 14:27:28 2010
@@ -16,7 +16,7 @@
ENV['JAVA_OPTS'] ||= '-Xmx512m -XX:MaxPermSize=256m'
require 'buildr/antlr'
-require 'buildr/scala'
+# require 'buildr/scala'
VERSION_NUMBER = "0.1.0-SNAPSHOT"
GROUP = "
com.google"
@@ -29,6 +29,9 @@
SLF4J =
["org.slf4j:slf4j-api:jar:1.5.6", "org.slf4j:slf4j-simple:jar:1.5.6"]
GUICE = ["aopalliance:aopalliance:jar:1.0",
"com.google.inject:guice:jar:2.0", "com.google.inject.extensions:guice-assisted-inject:jar:2.0"
]
+GCOLLECT = ["com.google.collections:google-collections:jar:1.0"]
+JUNIT = ["junit:junit:jar:4.7"]
+COMMONS_LANG = ["commons-lang:commons-lang:jar:2.4"]
# Force Buildr Antlr integration to use the version we specify
Buildr::ANTLR::REQUIRES.clear
@@ -40,7 +43,7 @@
manifest["Implementation-Vendor"] = COPYRIGHT
define "core" do
- compile.with [SLF4J]
+ compile.with [SLF4J, GCOLLECT, JUNIT, COMMONS_LANG]
package :jar
end
==============================================================================
Revision: 8a18f4a525
Author: Alex Eagle <
alex...@google.com>
Date: Fri Apr 16 14:53:57 2010
Log: Introduce plain XML POM files. They work better with tooling.
http://code.google.com/p/noop/source/detail?r=8a18f4a525
Added:
/core/pom.xml
/pom.xml
Deleted:
/pom.yml
Modified:
/core/src/test/java/noop/graph/HelloWorldExampleMain.java
=======================================
--- /dev/null
+++ /core/pom.xml Fri Apr 16 14:53:57 2010
@@ -0,0 +1,34 @@
+<project xmlns="
http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
+
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>com.google.noop</groupId>
+ <artifactId>noop</artifactId>
+ <version>0.1.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>noop-core</artifactId>
+ <name>Noop Core</name>
+ <description>Core library for representing Noop source
code.</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.7</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.google.collections</groupId>
+ <artifactId>google-collections</artifactId>
+ <version>0.9</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.4</version>
+ </dependency>
+ </dependencies>
+</project>
=======================================
--- /dev/null
+++ /pom.xml Fri Apr 16 14:53:57 2010
@@ -0,0 +1,47 @@
+<project xmlns="
http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
+
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>
com.google</groupId>
+ <artifactId>google</artifactId>
+ <version>1</version>
+ </parent>
+
+ <name>Noop</name>
+ <artifactId>noop</artifactId>
+ <groupId>com.google.noop</groupId>
+ <version>0.1.0-SNAPSHOT</version>
+ <packaging>pom</packaging>
+ <description>
+ The parent metadata project for the Noop programming language, for use
with the maven build system.
+ </description>
+ <url>
http://noop.googlecode.com/</url>
+ <scm>
+ <connection>scm:hg:
http://noop.googlecode.com/hg/trunk/</connection>
+
<developerConnection>scm:hg:
https://noop.googlecode.com/hg/trunk</developerConnection>
+ <url>
http://code.google.com/p/noop/source/browse/</url>
+ </scm>
+ <modules>
+ <module>core</module>
+ </modules>
+
+ <properties>
+ <mavenVersion>2.1</mavenVersion>
+ </properties>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>${mavenVersion}</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
=======================================
--- /pom.yml Mon Oct 19 04:42:14 2009
+++ /dev/null
@@ -1,11 +0,0 @@
-groupId: com.google.code.noop
-artifactId: noop-parent
-version: 0.1.0-SNAPSHOT
-packaging: pom
-name: Noop - Parent Project
-description: "The parent metadata project for the Noop programming
language, for use with the maven build system."
-build:
- pluginManagement:
- plugins:
- - { artifactId: maven-surefire-plugin, version: 2.4.3 }
-modules: [ core ]
=======================================
--- /core/src/test/java/noop/graph/HelloWorldExampleMain.java Fri Apr 16
14:27:28 2010
+++ /core/src/test/java/noop/graph/HelloWorldExampleMain.java Fri Apr 16
14:53:57 2010
@@ -96,5 +96,4 @@
controller.applyAll(new NewNodeOperation(zero, sayHello, TYPEOF,
intClazz),
new NewNodeOperation(new Return(zero), sayHello));
}
-
-}
+}
==============================================================================
Revision: 91e17133c8
Author: Alex Eagle <
alex...@google.com>
Date: Sun Apr 18 00:49:40 2010
Log: Rename compiler to translator, create pom, and change to Java
http://code.google.com/p/noop/source/detail?r=91e17133c8
Added:
/translator/pom.xml
/translator/src/main/resources/noop/Java.stg
/translator/src/test/java/noop/ClassTranslationTest.java
Deleted:
/compiler/src/main/scala/noop/to/java/README
/compiler/src/main/scala/noop/to/noop/README
/compiler/src/main/stringtemplate/noop/to/java/Java.stg
/compiler/src/test/scala/noop/to/java/JavaSpec.scala
/compiler/src/test/scala/noop/to/java/README
/compiler/src/test/scala/noop/to/noop/README
Modified:
/pom.xml
=======================================
--- /dev/null
+++ /translator/pom.xml Sun Apr 18 00:49:40 2010
@@ -0,0 +1,34 @@
+<project xmlns="
http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
+
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>com.google.noop</groupId>
+ <artifactId>noop</artifactId>
+ <version>0.1.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>noop-java-translator</artifactId>
+ <name>Noop Java Translator</name>
+ <description>Produces Java sources which are semantically equivalent to
Noop source code.</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.7</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.antlr</groupId>
+ <artifactId>antlr</artifactId>
+ <version>3.1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.antlr</groupId>
+ <artifactId>antlr-runtime</artifactId>
+ <version>3.1.1</version>
+ </dependency>
+ </dependencies>
+</project>
=======================================
--- /dev/null
+++ /translator/src/main/resources/noop/Java.stg Sun Apr 18 00:49:40 2010
@@ -0,0 +1,33 @@
+group Java;
+
+file(namespace, imports, class) ::= <<
+package <namespace>;
+
+<imports:{import <it>;}; separator="\n">
+
+<class>
+>>
+
+class(documentation, modifiers, name, interfaces, methods) ::= <<
+<documentation>
+<modifiers; separator=" "> class <name><if(interfaces)> implements
<interfaces; separator=", "><endif> {
+ <methods; separator="\n">
+}
+>>
+
+documentation(contents) ::= <<
+/**
+ <contents:{* <it>}; separator="\n">
+ */
+>>
+
+method(documentation, modifiers, returnType, name, parameters, body) ::= <<
+<documentation>
+<modifiers; separator=" "> <returnType> <name>(<parameters;
separator=", ">) {
+ <body>
+}
+>>
+
+parameter(type, name) ::= <<
+<type> <name>
+>>
=======================================
--- /dev/null
+++ /translator/src/test/java/noop/ClassTranslationTest.java Sun Apr 18
00:49:40 2010
@@ -0,0 +1,64 @@
+/**
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package noop;
+
+import junit.framework.Assert;
+import org.antlr.stringtemplate.CommonGroupLoader;
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.stringtemplate.StringTemplateGroup;
+import org.antlr.tool.ErrorManager;
+import org.junit.Before;
+import org.junit.Test;
+
+import static junit.framework.Assert.assertEquals;
+
+/**
+ * @author
rdi...@google.com (Robert Dionne)
+ *
+ * TODO(rdionne): Write the code generation classes that use noop.model.*
and test those,
+ * not String Template.
+ */
+public class ClassTranslationTest {
+ private StringTemplateGroup group;
+
+ @Before public void setUp() {
+ group = StringTemplateGroup.loadGroup("Java");
+ }
+
+ @Test public void shouldOutputAFileTemplate() {
+ StringTemplate file = group.getInstanceOf("file");
+
+ file.setAttribute("namespace", "
com.google");
+ file.setAttribute("imports", "org.json.JSONObject");
+ file.setAttribute("class", "class Empty {}");
+
+ assertEquals("package
com.google;\n\nimport
org.json.JSONObject;\n\nclass Empty {}", file.toString());
+
+ }
+
+ @Test public void shouldOutputAClassTemplate() {
+ StringTemplate clazz = group.getInstanceOf("class");
+
+ clazz.setAttribute("documentation", "/**\n * @author\n */");
+ clazz.setAttribute("modifiers", "public final");
+ clazz.setAttribute("name", "MyClass");
+ clazz.setAttribute("interfaces", "List");
+ clazz.setAttribute("methods", "void add(int a, int b) {}");
+
+ assertEquals("/**\n * @author\n */\npublic final class MyClass
implements List {\n void add(int a, int b) {}\n}",
+ clazz.toString());
+ }
+}
=======================================
--- /compiler/src/main/scala/noop/to/java/README Fri Oct 23 15:34:30 2009
+++ /dev/null
@@ -1,1 +0,0 @@
-TODO(robertsdionne): Place Noop to Java compiler classes here.
=======================================
--- /compiler/src/main/scala/noop/to/noop/README Fri Oct 23 15:34:30 2009
+++ /dev/null
@@ -1,1 +0,0 @@
-TODO(robertsdionne): Place Noop to Noop compiler classes here.
=======================================
--- /compiler/src/main/stringtemplate/noop/to/java/Java.stg Sat Oct 24
16:11:00 2009
+++ /dev/null
@@ -1,33 +0,0 @@
-group Java;
-
-file(namespace, imports, class) ::= <<
-package <namespace>;
-
-<imports:{import <it>;}; separator="\n">
-
-<class>
->>
-
-class(documentation, modifiers, name, interfaces, methods) ::= <<
-<documentation>
-<modifiers; separator=" "> class <name><if(interfaces)> implements
<interfaces; separator=", "><endif> {
- <methods; separator="\n">
-}
->>
-
-documentation(contents) ::= <<
-/**
- <contents:{* <it>}; separator="\n">
- */
->>
-
-method(documentation, modifiers, returnType, name, parameters, body) ::= <<
-<documentation>
-<modifiers; separator=" "> <returnType> <name>(<parameters;
separator=", ">) {
- <body>
-}
->>
-
-parameter(type, name) ::= <<
-<type> <name>
->>
=======================================
--- /compiler/src/test/scala/noop/to/java/JavaSpec.scala Sat Oct 24
15:30:48 2009
+++ /dev/null
@@ -1,65 +0,0 @@
-/**
- * Copyright 2009 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package noop.to.java;
-
-import org.antlr.stringtemplate.CommonGroupLoader;
-import org.antlr.stringtemplate.StringTemplate;
-import org.antlr.stringtemplate.StringTemplateGroup;
-import org.antlr.tool.ErrorManager;
-
-import org.scalatest.matchers.ShouldMatchers;
-import org.scalatest.Spec;
-
-/**
- * @author
rdi...@google.com (Robert Dionne)
- *
- * TODO(rdionne): Write the code generation classes that use noop.model.*
and test those,
- * not String Template.
- */
-class JavaSpec extends Spec with ShouldMatchers {
-
- StringTemplateGroup.registerGroupLoader(
- new CommonGroupLoader(
- "noop/to/java",
- ErrorManager.getStringTemplateErrorListener()));
- val group = StringTemplateGroup.loadGroup("Java");
-
- describe("A file template") {
- val file = group.getInstanceOf("file");
-
- it("should evaluate correctly") {
- file.setAttribute("namespace", "
com.google");
- file.setAttribute("imports", "org.json.JSONObject");
- file.setAttribute("class", "class Empty {}");
-
- file.toString() should be ("package
com.google;\n\nimport
org.json.JSONObject;\n\nclass Empty {}");
- }
- }
-
- describe("A class template") {
- val clazz = group.getInstanceOf("class");
-
- it("should evaluate correctly") {
- clazz.setAttribute("documentation", "/**\n * @author\n */");
- clazz.setAttribute("modifiers", "public final");
- clazz.setAttribute("name", "MyClass");
- clazz.setAttribute("interfaces", "List");
- clazz.setAttribute("methods", "void add(int a, int b) {}");
-
- clazz.toString() should be ("/**\n * @author\n */\npublic final
class MyClass implements List {\n void add(int a, int b) {}\n}");
- }
- }
-}
=======================================
--- /compiler/src/test/scala/noop/to/java/README Fri Oct 23 15:34:30 2009
+++ /dev/null
@@ -1,1 +0,0 @@
-TODO(robertsdionne): Place Noop to Java compiler tests here.
=======================================
--- /compiler/src/test/scala/noop/to/noop/README Fri Oct 23 15:34:30 2009
+++ /dev/null
@@ -1,1 +0,0 @@
-TODO(robertsdionne): Place Noop to Noop compiler tests here.
=======================================
--- /pom.xml Fri Apr 16 14:53:57 2010
+++ /pom.xml Sun Apr 18 00:49:40 2010
@@ -25,6 +25,7 @@
</scm>
<modules>
<module>core</module>
+ <module>translator</module>
</modules>
<properties>
==============================================================================
Revision: 80da50496b
Author: Alex Eagle <
alex...@google.com>
Date: Sun Apr 18 01:54:12 2010
Log: Move stdlib to a separate class for reuse
http://code.google.com/p/noop/source/detail?r=80da50496b
Added:
/core/src/main/java/noop/stdlib/StandardLibraryBuilder.java
/core/src/test/java/noop/graph/HelloWorldExampleTest.java
Modified:
/core/src/main/java/noop/graph/Controller.java
/core/src/main/java/noop/graph/Workspace.java
/core/src/test/java/noop/graph/HelloWorldExampleMain.java
=======================================
--- /dev/null
+++ /core/src/main/java/noop/stdlib/StandardLibraryBuilder.java Sun Apr 18
01:54:12 2010
@@ -0,0 +1,51 @@
+package noop.stdlib;
+
+import com.google.common.collect.Lists;
+import noop.graph.Controller;
+import noop.model.*;
+import noop.operations.MutationOperation;
+import noop.operations.NewNodeOperation;
+
+import java.util.List;
+
+import static noop.graph.Edge.EdgeType.TYPEOF;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class StandardLibraryBuilder {
+ public Clazz intClazz;
+ public Clazz consoleClazz;
+ public Clazz stringClazz;
+ public Block printMethod;
+
+ public List<MutationOperation> build() {
+ List<MutationOperation> result = Lists.newArrayList();
+
+ Project project = new Project("Noop", "com.google.noop", "Apache 2");
+ result.add(new NewNodeOperation(project, null));
+
+ Library lang = new Library("lang");
+ result.add(new NewNodeOperation(lang, project));
+
+ stringClazz = new Clazz("String");
+ result.add(new NewNodeOperation(stringClazz, lang));
+
+ Library io = new Library("io");
+ result.add(new NewNodeOperation(io, project));
+
+ consoleClazz = new Clazz("Console");
+ result.add(new NewNodeOperation(consoleClazz, io));
+
+ printMethod = new Block("print", null);
+ result.add(new NewNodeOperation(printMethod, consoleClazz));
+
+ Parameter printArg = new Parameter("s");
+ result.add(new NewNodeOperation(printArg, printMethod, TYPEOF,
stringClazz));
+
+ intClazz = new Clazz("Integer");
+ result.add(new NewNodeOperation(intClazz, lang));
+
+ return result;
+ }
+}
=======================================
--- /dev/null
+++ /core/src/test/java/noop/graph/HelloWorldExampleTest.java Sun Apr 18
01:54:12 2010
@@ -0,0 +1,14 @@
+package noop.graph;
+
+import org.junit.Test;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class HelloWorldExampleTest {
+
+ @Test
+ public void shouldRunSuccessfully() {
+ new HelloWorldExampleMain(System.out);
+ }
+}
=======================================
--- /core/src/main/java/noop/graph/Controller.java Fri Apr 16 14:27:28 2010
+++ /core/src/main/java/noop/graph/Controller.java Sun Apr 18 01:54:12 2010
@@ -18,9 +18,11 @@
import noop.graph.Edge;
import noop.graph.Edge.EdgeType;
+import noop.model.Clazz;
import noop.model.LanguageElement;
import noop.graph.Workspace;
import noop.operations.EditNodeOperation;
+import noop.operations.MutationOperation;
import noop.operations.NewNodeOperation;
import java.util.Map.Entry;
@@ -47,20 +49,29 @@
}
private void addEdge(int newNodeId, LanguageElement destElement,
EdgeType edgeType, boolean backwards) {
- int destId = workspace.elements.indexOf(destElement);
+ int destId = destElement == null ? 0 :
workspace.elements.indexOf(destElement);
if (destId < 0) {
- throw new IllegalStateException(String.format("Cannot add edge [%s
-> %s] due to non-existant dest",
- newNodeId, destId));
+ throw new IllegalStateException(String.format("Cannot add edge [%s
-> %s] due to non-existant dest %s",
+ newNodeId, destId, destElement));
}
Edge newEdge = backwards ? new Edge(destId, edgeType, newNodeId) : new
Edge(newNodeId, edgeType, destId);
workspace.edges.add(newEdge);
}
- public void applyAll(NewNodeOperation... operations) {
- for (NewNodeOperation operation : operations) {
+ public void applyAll(Iterable<? extends MutationOperation> operations) {
+ for (MutationOperation operation : operations) {
apply(operation);
}
}
+
+ private void apply(MutationOperation operation) {
+ if (operation instanceof NewNodeOperation) {
+ apply((NewNodeOperation)operation);
+ } else if (operation instanceof EditNodeOperation) {
+ apply((EditNodeOperation)operation);
+ }
+
+ }
public void apply(EditNodeOperation operation) {
LanguageElement currentValue = workspace.elements.get(
operation.id);
=======================================
--- /core/src/main/java/noop/graph/Workspace.java Fri Apr 16 14:27:28 2010
+++ /core/src/main/java/noop/graph/Workspace.java Sun Apr 18 01:54:12 2010
@@ -18,8 +18,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
-import noop.graph.Edge;
-import noop.graph.ModelVisitor;
import noop.model.LanguageElement;
import java.util.List;
=======================================
--- /core/src/test/java/noop/graph/HelloWorldExampleMain.java Fri Apr 16
14:53:57 2010
+++ /core/src/test/java/noop/graph/HelloWorldExampleMain.java Sun Apr 18
01:54:12 2010
@@ -18,11 +18,14 @@
import noop.model.*;
import noop.operations.NewNodeOperation;
+import noop.stdlib.StandardLibraryBuilder;
+import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
+import java.util.Arrays;
import static noop.graph.Edge.EdgeType.*;
@@ -30,47 +33,30 @@
* @author
alex...@google.com (Alex Eagle)
*/
public class HelloWorldExampleMain {
- private static Clazz stringClazz;
- private static Clazz consoleClazz;
- private static Clazz intClazz;
- private static Block printMethod;
+ private Controller controller;
+ private Workspace workspace;
+ private StandardLibraryBuilder stdLib;
+ private final PrintStream out;
+
+ public HelloWorldExampleMain(PrintStream out) {
+ this.out = out;
+ }
public static void main(String[] args) throws FileNotFoundException {
- Workspace workspace = new Workspace();
- Controller controller = new Controller(workspace);
- createNoopStdLib(workspace, controller);
- createHelloWorldProgram(workspace, controller);
- PrintStream out = new PrintStream(new FileOutputStream(new
File(args[0])));
- workspace.accept(new DotGraphPrintingVisitor(workspace, out));
+ new HelloWorldExampleMain(new PrintStream(new FileOutputStream(new
File(args[0])))).run();
}
- public static void createNoopStdLib(Workspace workspace, Controller
controller) {
- Project project = new Project("Noop", "com.google.noop", "Apache 2");
- controller.apply(new NewNodeOperation(project, workspace));
-
- Library lang = new Library("lang");
- controller.apply(new NewNodeOperation(lang, project));
-
- stringClazz = new Clazz("String");
- controller.apply(new NewNodeOperation(stringClazz, lang));
-
- Library io = new Library("io");
- controller.apply(new NewNodeOperation(io, project));
-
- consoleClazz = new Clazz("Console");
- controller.apply(new NewNodeOperation(consoleClazz, io));
-
- printMethod = new Block("print", null);
- controller.apply(new NewNodeOperation(printMethod, consoleClazz));
-
- Parameter printArg = new Parameter("s");
- controller.apply(new NewNodeOperation(printArg, printMethod, TYPEOF,
stringClazz));
-
- intClazz = new Clazz("Integer");
- controller.apply(new NewNodeOperation(intClazz, lang));
+ private void run() {
+ workspace = new Workspace();
+ controller = new Controller(workspace);
+ stdLib = new StandardLibraryBuilder();
+ System.out.println("stdLib = " + stdLib);
+ controller.applyAll(stdLib.build());
+ createHelloWorldProgram();
+ workspace.accept(new DotGraphPrintingVisitor(workspace, out));
}
- public static void createHelloWorldProgram(Workspace workspace,
Controller controller) {
+ public void createHelloWorldProgram() {
Project project = new Project("Hello World", "com.example", "Copyright
2010\nExample Co.");
controller.apply(new NewNodeOperation(project, workspace));
@@ -79,21 +65,21 @@
Parameter consoleDep = new Parameter("console");
- Block sayHello = new Block("say hello", intClazz, consoleDep);
- controller.applyAll(new NewNodeOperation(sayHello, library),
- new NewNodeOperation(consoleDep, sayHello, TYPEOF,
consoleClazz));
+ Block sayHello = new Block("say hello", stdLib.intClazz, consoleDep);
+ controller.applyAll(Arrays.asList(new NewNodeOperation(sayHello,
library),
+ new NewNodeOperation(consoleDep, sayHello, TYPEOF,
stdLib.consoleClazz)));
Documentation sayHelloDoc = new Documentation("This is the entry point
for the Hello World app");
controller.apply(new NewNodeOperation(sayHelloDoc, sayHello));
StringLiteral helloWorld = new StringLiteral("Hello, World!");
- controller.apply(new NewNodeOperation(helloWorld, sayHello, TYPEOF,
stringClazz));
+ controller.apply(new NewNodeOperation(helloWorld, sayHello, TYPEOF,
stdLib.stringClazz));
Expression printHello = new MethodInvocation(helloWorld);
- controller.apply(new NewNodeOperation(printHello, sayHello, TARGET,
consoleDep, INVOKE, printMethod));
+ controller.apply(new NewNodeOperation(printHello, sayHello, TARGET,
consoleDep, INVOKE, stdLib.printMethod));
IntegerLiteral zero = new IntegerLiteral(0);
- controller.applyAll(new NewNodeOperation(zero, sayHello, TYPEOF,
intClazz),
- new NewNodeOperation(new Return(zero), sayHello));
+ controller.applyAll(Arrays.asList(new NewNodeOperation(zero, sayHello,
TYPEOF, stdLib.intClazz),
+ new NewNodeOperation(new Return(zero), sayHello)));
}
}
==============================================================================
Revision: 2a51630295
Author: Alex Eagle <
alex...@google.com>
Date: Mon Apr 19 07:04:03 2010
Log: Make new constructor for NewNodeOperation rather than passing null
http://code.google.com/p/noop/source/detail?r=2a51630295
Modified:
/core/src/main/java/noop/operations/NewNodeOperation.java
/core/src/main/java/noop/stdlib/StandardLibraryBuilder.java
/core/src/test/java/noop/graph/HelloWorldExampleMain.java
=======================================
--- /core/src/main/java/noop/operations/NewNodeOperation.java Fri Apr 16
14:27:28 2010
+++ /core/src/main/java/noop/operations/NewNodeOperation.java Mon Apr 19
07:04:03 2010
@@ -22,6 +22,7 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import noop.model.LanguageElement;
+import noop.model.Project;
import java.util.Collection;
import java.util.List;
@@ -41,10 +42,24 @@
}
});
+ /**
+ * Create a new node with the given parent
+ * @param newElement any language element
+ * @param container the containing element
+ */
public NewNodeOperation(LanguageElement newElement, LanguageElement
container) {
this.newElement = newElement;
this.container = container;
}
+
+ /**
+ * Create a new node whose parent is the workspace.
+ * This may create orphaned elements, but is appropriate for projects
+ * @param newElement the element
+ */
+ public NewNodeOperation(LanguageElement newElement) {
+ this(newElement, null);
+ }
public NewNodeOperation(LanguageElement newElement, LanguageElement
container,
EdgeType edgeType, LanguageElement dest) {
=======================================
--- /core/src/main/java/noop/stdlib/StandardLibraryBuilder.java Sun Apr 18
01:54:12 2010
+++ /core/src/main/java/noop/stdlib/StandardLibraryBuilder.java Mon Apr 19
07:04:03 2010
@@ -23,7 +23,7 @@
List<MutationOperation> result = Lists.newArrayList();
Project project = new Project("Noop", "com.google.noop", "Apache 2");
- result.add(new NewNodeOperation(project, null));
+ result.add(new NewNodeOperation(project));
Library lang = new Library("lang");
result.add(new NewNodeOperation(lang, project));
=======================================
--- /core/src/test/java/noop/graph/HelloWorldExampleMain.java Sun Apr 18
01:54:12 2010
+++ /core/src/test/java/noop/graph/HelloWorldExampleMain.java Mon Apr 19
07:04:03 2010
@@ -50,7 +50,6 @@
workspace = new Workspace();
controller = new Controller(workspace);
stdLib = new StandardLibraryBuilder();
- System.out.println("stdLib = " + stdLib);
controller.applyAll(stdLib.build());
createHelloWorldProgram();
workspace.accept(new DotGraphPrintingVisitor(workspace, out));
@@ -58,7 +57,7 @@
public void createHelloWorldProgram() {
Project project = new Project("Hello World", "com.example", "Copyright
2010\nExample Co.");
- controller.apply(new NewNodeOperation(project, workspace));
+ controller.apply(new NewNodeOperation(project));
Library library = new Library("hello");
controller.apply(new NewNodeOperation(library, project));
==============================================================================
Revision: 1b619c23a7
Author: Alex Eagle <
alex...@google.com>
Date: Mon Apr 19 10:04:58 2010
Log: Add another printing visitor, which allows the ordering of statements
to be displayed correctly. To support it, change the visitor to be
recursive down the CONTAIN tree, which means providing each element
references to its children. This should point to the position in the
elements list instead, to allow edits.
http://code.google.com/p/noop/source/detail?r=1b619c23a7
Added:
/core/src/main/java/noop/graph/OutlinePrintingVisitor.java
/core/src/main/java/noop/graph/PrintingVisitor.java
Modified:
/core/src/main/java/noop/graph/Controller.java
/core/src/main/java/noop/graph/DotGraphPrintingVisitor.java
/core/src/main/java/noop/graph/Edge.java
/core/src/main/java/noop/graph/ModelVisitor.java
/core/src/main/java/noop/graph/Workspace.java
/core/src/main/java/noop/model/Block.java
/core/src/main/java/noop/model/Clazz.java
/core/src/main/java/noop/model/IdentifierDeclaration.java
/core/src/main/java/noop/model/LanguageElement.java
/core/src/main/java/noop/model/Library.java
/core/src/main/java/noop/model/Parameter.java
/core/src/main/java/noop/model/Project.java
/core/src/main/java/noop/stdlib/StandardLibraryBuilder.java
/core/src/test/java/noop/graph/HelloWorldExampleMain.java
/core/src/test/java/noop/graph/HelloWorldExampleTest.java
=======================================
--- /dev/null
+++ /core/src/main/java/noop/graph/OutlinePrintingVisitor.java Mon Apr 19
10:04:58 2010
@@ -0,0 +1,102 @@
+package noop.graph;
+
+import noop.model.*;
+
+import java.io.PrintStream;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import static com.google.common.collect.Iterables.filter;
+import static noop.graph.Edge.notContain;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class OutlinePrintingVisitor extends PrintingVisitor {
+ private final PrintStream out;
+
+ public OutlinePrintingVisitor(PrintStream out) {
+ this.out = out;
+ }
+
+ @Override
+ public void visit(Workspace workspace) {
+ this.workspace = workspace;
+ print(workspace, "Contents of workspace %s at %s",
+ System.getProperty("
user.name"), new SimpleDateFormat().format(new
Date()));
+ }
+
+ @Override
+ public void visit(Project project) {
+ print(project, "Project \"%s\"->\"%s\" (copyright: \"%s\")",
+ project.getNamespace(), project.getName(),
escape(project.getCopyright()));
+ }
+
+ @Override
+ public void visit(Library library) {
+ print(library, "Library \"%s\"",
library.name);
+ }
+
+ @Override
+ public void visit(Clazz clazz) {
+ print(clazz, "Class \"%s\"",
clazz.name);
+ }
+
+ @Override
+ public void visit(Block block) {
+ print(block, "%s{}",
block.name);
+ }
+
+ @Override
+ public void visit(MethodInvocation methodInvocation) {
+ print(methodInvocation, "invocation");
+ }
+
+ @Override
+ public void visit(Parameter parameter) {
+ print(parameter, "parameter %s",
parameter.name);
+ }
+
+ @Override
+ public void visit(Documentation documentation) {
+ print(documentation, "Documentation: %s", documentation.summary);
+ }
+
+ @Override
+ public void visit(IdentifierDeclaration identifierDeclaration) {
+ print(identifierDeclaration, "Declare %s", identifierDeclaration.name);
+ }
+
+ @Override
+ public void visit(Assignment assignment) {
+ print(assignment, "Assign");
+ }
+
+ @Override
+ public void visit(IntegerLiteral integerLiteral) {
+ print(integerLiteral, "literal %s",
String.valueOf(integerLiteral.value));
+ }
+
+ @Override
+ public void visit(StringLiteral stringLiteral) {
+ print(stringLiteral, "literal \"%s\"", stringLiteral.value);
+ }
+
+ private void print(LanguageElement element, String message, String...
params) {
+ out.format("%s%s [#%d]", indent(), String.format(message, params),
idFor(element));
+ for (Edge edge : filter(workspace.edgesFrom(idFor(element)),
notContain())) {
+ out.format(" %s -> #%d",
edge.type.name(), edge.dest);
+ }
+ out.println();
+ }
+
+ private String indent() {
+ StringBuilder builder = new StringBuilder((currentDepth - 1) * 2);
+ for (int i = 0; i < currentDepth - 1; i++) {
+ builder.append(" ");
+ }
+ return builder.toString();
+ }
+
+
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/graph/PrintingVisitor.java Mon Apr 19 10:04:58
2010
@@ -0,0 +1,31 @@
+package noop.graph;
+
+import noop.model.LanguageElement;
+
+/**
+ * @author
alex...@google.com (Alex Eagle)
+ */
+public class PrintingVisitor extends ModelVisitor {
+ protected Workspace workspace;
+ protected int currentDepth;
+
+ @Override
+ public void enter(LanguageElement element) {
+ System.out.println("Enter " + element);
+ currentDepth++;
+ }
+
+ @Override
+ public void leave(LanguageElement element) {
+ System.out.println("Leave " + element);
+ currentDepth--;
+ }
+
+ protected int idFor(LanguageElement element) {
+ return workspace.elements.indexOf(element);
+ }
+
+ protected String escape(String value) {
+ return value.replaceAll("\n", "\\\\n");
+ }
+}
=======================================
--- /core/src/main/java/noop/graph/Controller.java Sun Apr 18 01:54:12 2010
+++ /core/src/main/java/noop/graph/Controller.java Mon Apr 19 10:04:58 2010
@@ -36,26 +36,43 @@
public Controller(Workspace workspace) {
this.workspace = workspace;
}
+
+ private void addEdge(int newNodeId, LanguageElement destElement,
EdgeType edgeType, boolean backwards) {
+ int destId = workspace.elements.indexOf(destElement);
+ if (destId < 0) {
+ throw new IllegalStateException(String.format("Cannot add edge [%s
-> %s] due to non-existant dest %s",
+ newNodeId, destId, destElement));
+ }
+ Edge newEdge = backwards ? new Edge(destId, edgeType, newNodeId) : new
Edge(newNodeId, edgeType, destId);
+ workspace.edges.add(newEdge);
+ }
public void apply(NewNodeOperation operation) {
+ int nextNodeId = workspace.elements.size();
+ LanguageElement container = operation.container == null ? workspace :
operation.container;
+
+ if (!container.adoptChild(operation.newElement)) {
+ throw new IllegalArgumentException("Element " + operation.newElement
+ + " not allowed as child of " + container);
+ }
+ addEdge(nextNodeId, container, EdgeType.CONTAIN, true);
workspace.elements.add(operation.newElement);
- int newNodeId = workspace.elements.size() - 1;
- addEdge(newNodeId, operation.container, EdgeType.CONTAIN, true);
for (Entry<EdgeType, LanguageElement> edgeTypeLanguageNodeEntry :
operation.edges.entries()) {
LanguageElement destElement = edgeTypeLanguageNodeEntry.getValue();
EdgeType edgeType = edgeTypeLanguageNodeEntry.getKey();
- addEdge(newNodeId, destElement, edgeType, false);
+ addEdge(nextNodeId, destElement, edgeType, false);
}
}
- private void addEdge(int newNodeId, LanguageElement destElement,
EdgeType edgeType, boolean backwards) {
- int destId = destElement == null ? 0 :
workspace.elements.indexOf(destElement);
- if (destId < 0) {
- throw new IllegalStateException(String.format("Cannot add edge [%s
-> %s] due to non-existant dest %s",
- newNodeId, destId, destElement));
- }
- Edge newEdge = backwards ? new Edge(destId, edgeType, newNodeId) : new
Edge(newNodeId, edgeType, destId);
- workspace.edges.add(newEdge);
+ public void apply(EditNodeOperation operation) {
+ LanguageElement currentValue = workspace.elements.get(
operation.id);
+ if (currentValue.getClass() != operation.newValue.getClass()) {
+ throw new IllegalArgumentException(String.format("Cannot edit
node %d with %s because the current type is %s",
+
operation.id, operation.newValue, currentValue.getClass()));
+ }
+
+ operation.newValue.setPreviousVersion(currentValue);
+ workspace.elements.set(
operation.id, operation.newValue);
}
public void applyAll(Iterable<? extends MutationOperation> operations) {
@@ -72,15 +89,4 @@
}
}
-
- public void apply(EditNodeOperation operation) {
- LanguageElement currentValue = workspace.elements.get(
operation.id);
- if (currentValue.getClass() != operation.newValue.getClass()) {
- throw new IllegalArgumentException(String.format("Cannot edit
node %d with %s because the current type is %s",
-
operation.id, operation.newValue, currentValue.getClass()));
- }
-
- operation.newValue.setPreviousVersion(currentValue);
- workspace.elements.set(
operation.id, operation.newValue);
- }
-}
+}
=======================================
--- /core/src/main/java/noop/graph/DotGraphPrintingVisitor.java Fri Apr 16
14:27:28 2010
+++ /core/src/main/java/noop/graph/DotGraphPrintingVisitor.java Mon Apr 19
10:04:58 2010
@@ -25,21 +25,16 @@
/**
* @author
alex...@google.com (Alex Eagle)
*/
-public class DotGraphPrintingVisitor extends ModelVisitor {
- private final Workspace workspace;
+public class DotGraphPrintingVisitor extends PrintingVisitor {
private final PrintStream out;
- public DotGraphPrintingVisitor(Workspace workspace, PrintStream out) {
- this.workspace = workspace;
+ public DotGraphPrintingVisitor(PrintStream out) {
this.out = out;
}
-
- private int idFor(LanguageElement element) {
- return workspace.elements.indexOf(element);
- }
@Override
public void visit(Workspace workspace) {
+ this.workspace = workspace;
out.format("digraph workspace\n{\n");
out.format("%s [label=\"%s\", shape=house]\n",
idFor(workspace), "Workspace");
}
@@ -71,6 +66,12 @@
idFor(block), idFor(block.returnType));
}
}
+
+ @Override
+ public void visit(IdentifierDeclaration identifierDeclaration) {
+ out.format("%s [label=\"%s\"]\n", idFor(identifierDeclaration),
identifierDeclaration.name);
+
+ }
@Override
public void visit(Return aReturn) {
@@ -103,8 +104,12 @@
out.format("%s [label=\"%s\", shape=none]\n", idFor(documentation),
escape(documentation.summary));
}
- private String escape(String value) {
- return value.replaceAll("\n", "\\\\n");
+ protected String escape(String value) {
+ String escaped = super.escape(value);
+ if (escaped.length() > 15) {
+ escaped = escaped.substring(0, 12) + "...";
+ }
+ return escaped;
}
@Override
@@ -118,8 +123,11 @@
}
@Override
- public void leave(Workspace workspace) {
- out.println("}");
+ public void leave(LanguageElement element) {
+ super.leave(element);
+ if (currentDepth == 0) {
+ out.println("}");
+ }
}
@Override
=======================================
--- /core/src/main/java/noop/graph/Edge.java Fri Apr 16 14:27:28 2010
+++ /core/src/main/java/noop/graph/Edge.java Mon Apr 19 10:04:58 2010
@@ -16,6 +16,8 @@
package noop.graph;
+import com.google.common.base.Nullable;
+import com.google.common.base.Predicate;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
@@ -32,6 +34,15 @@
public void accept(ModelVisitor v) {
v.visit(this);
}
+
+ public static Predicate<? super Edge> notContain() {
+ return new Predicate<Edge>() {
+ @Override
+ public boolean apply(Edge input) {
+ return input.type != EdgeType.CONTAIN;
+ }
+ };
+ }
public enum EdgeType {
INVOKE,
=======================================
--- /core/src/main/java/noop/graph/ModelVisitor.java Fri Apr 16 14:27:28
2010
+++ /core/src/main/java/noop/graph/ModelVisitor.java Mon Apr 19 10:04:58
2010
@@ -22,6 +22,10 @@
* @author
alex...@google.com (Alex Eagle)
*/
public abstract class ModelVisitor {
+ public void enter(LanguageElement element) {}
+
+ public void leave(LanguageElement element) {}
+
public void visit(Edge edge) {}
public void visit(Workspace workspace) {}
@@ -37,8 +41,6 @@
public void visit(Library library) {}
public void visit(Clazz clazz) {}
-
- public void leave(Workspace workspace) {}
public void visit(StringLiteral stringLiteral) {}
=======================================
--- /core/src/main/java/noop/graph/Workspace.java Sun Apr 18 01:54:12 2010
+++ /core/src/main/java/noop/graph/Workspace.java Mon Apr 19 10:04:58 2010
@@ -16,9 +16,13 @@
package noop.graph;
+import com.google.common.base.Nullable;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import noop.model.LanguageElement;
+import noop.model.Project;
import java.util.List;
import java.util.Set;
@@ -29,18 +33,40 @@
public class Workspace extends LanguageElement<Workspace> {
public final Set<Edge> edges = Sets.newHashSet();
public final List<LanguageElement> elements =
Lists.<LanguageElement>newArrayList(this);
+ private List<Project> projects = Lists.newArrayList();
+ private List<LanguageElement> orphans = Lists.newArrayList();
+
+ @Override
+ public boolean adoptChild(LanguageElement child) {
+ if (child instanceof Project) {
+ projects.add((Project) child);
+ } else {
+ orphans.add(child);
+ }
+ return true;
+ }
@Override
public void accept(ModelVisitor v) {
+ v.enter(this);
v.visit(this);
- for (LanguageElement element : elements) {
- if (element != this) {
- element.accept(v);
- }
+ for (Project project : projects) {
+ v.enter(project);
+ project.accept(v);
+ v.leave(project);
}
for (Edge edge : edges) {
edge.accept(v);
}
v.leave(this);
}
-}
+
+ public Iterable<Edge> edgesFrom(final int id) {
+ return Iterables.filter(edges, new Predicate<Edge>() {
+ @Override
+ public boolean apply(@Nullable Edge input) {
+ return input.src == id;
+ }
+ });
+ }
+}
=======================================
--- /core/src/main/java/noop/model/Block.java Fri Apr 16 14:27:28 2010
+++ /core/src/main/java/noop/model/Block.java Mon Apr 19 10:04:58 2010
@@ -16,8 +16,10 @@
package noop.model;
+import com.google.common.collect.Lists;
import noop.graph.ModelVisitor;
+import java.util.Collections;
import java.util.List;
import static java.util.Arrays.asList;
@@ -29,26 +31,69 @@
public class Block extends LanguageElement<Block> {
public final String name;
public final Clazz returnType;
- public final List<Parameter> parameters;
-
- public Block(String name, Clazz returnType, Parameter... parameters) {
+ public final List<Parameter> parameters = Lists.newArrayList();
+ private final List<Expression> statements = Lists.newArrayList();
+ public final boolean test;
+ public final boolean instance;
+
+ private Block(String name, Clazz returnType, boolean isTest, boolean
instance) {
this.name = name;
this.returnType = returnType;
- this.parameters = asList(parameters);
+ this.test = isTest;
+ this.instance = instance;
}
- public Block() {
-
this.name = null;
- this.returnType = null;
- this.parameters = emptyList();
+ @Override
+ public boolean adoptChild(LanguageElement child) {
+ if (child instanceof Parameter) {
+ parameters.add((Parameter) child);
+ return true;
+ }
+ if (child instanceof Expression) {
+ statements.add((Expression) child);
+ return true;
+ }
+ return super.adoptChild(child);
+ }
+
+ public static Block unitTest(String name) {
+ return new Block(name, null, true, false);
+ }
+
+ public static Block function(String name, Clazz returnType) {
+ return new Block(name, returnType, false, false);
+ }
+
+ public static Block method(String name, Clazz returnType) {
+ // TODO: should hold a reference to the instance clazz?
+ return new Block(name, returnType, false, true);
}
@Override
public void accept(ModelVisitor v) {
v.visit(this);
+ for (Parameter parameter : parameters) {
+ v.enter(parameter);
+ parameter.accept(v);
+ v.leave(parameter);
+ }
+ for (Expression statement : statements) {
+ v.enter(statement);
+ statement.accept(v);
+ v.leave(statement);
+ }
}
- public boolean isMethod() {
- return name != null;
+ @Override
+ public String toString() {
+ return "Block " + name;
+ }
+
+ public boolean isFunction() {
+ return !instance;
+ }
+
+ public boolean isTest() {
+ return test;
}
}
=======================================
--- /core/src/main/java/noop/model/Clazz.java Fri Apr 16 14:27:28 2010
+++ /core/src/main/java/noop/model/Clazz.java Mon Apr 19 10:04:58 2010
@@ -16,20 +16,38 @@
package noop.model;
+import com.google.common.collect.Sets;
import noop.graph.ModelVisitor;
+import java.util.Set;
+
/**
* @author
alex...@google.com (Alex Eagle)
*/
public class Clazz extends LanguageElement<Clazz> {
public final String name;
+ private final Set<Block> blocks = Sets.newHashSet();
public Clazz(String name) {
this.name = name;
}
+
+ @Override
+ public boolean adoptChild(LanguageElement child) {
+ if (child instanceof Block) {
+ blocks.add((Block) child);
+ return true;
+ }
+ return super.adoptChild(child);
+ }
@Override
public void accept(ModelVisitor v) {
v.visit(this);
+ for (Block block : blocks) {
+ v.enter(block);
+ block.accept(v);
+ v.leave(block);
+ }
}
}
=======================================
--- /core/src/main/java/noop/model/IdentifierDeclaration.java Fri Apr 16
14:27:28 2010
+++ /core/src/main/java/noop/model/IdentifierDeclaration.java Mon Apr 19
10:04:58 2010
@@ -22,6 +22,22 @@
* @author
alex...@google.com (Alex Eagle)
*/
public class IdentifierDeclaration extends
Expression<IdentifierDeclaration> {
+ public final String name;
+ private Expression initialValue;
+
+ public IdentifierDeclaration(String name) {
+
this.name = name;
+ }
+
+ @Override
+ public boolean adoptChild(LanguageElement child) {
+ if (child instanceof Expression) {
+ initialValue = (Expression) child;
+ return true;
+ }
+ return false;
+ }
+
@Override
public void accept(ModelVisitor v) {
v.visit(this);
=======================================
--- /core/src/main/java/noop/model/LanguageElement.java Fri Apr 16 14:27:28
2010
+++ /core/src/main/java/noop/model/LanguageElement.java Mon Apr 19 10:04:58
2010
@@ -16,15 +16,18 @@
package noop.model;
+import com.google.common.collect.Sets;
import noop.graph.ModelVisitor;
import java.io.Serializable;
-import java.util.List;
+import java.util.Set;
/**
* @author
alex...@google.com (Alex Eagle)
*/
public abstract class LanguageElement<T> implements Serializable {
+ protected Documentation documentation;
+ protected Set<Block> unitTests = Sets.newHashSet();
protected T previousVersion;
public abstract void accept(ModelVisitor v);
public T getPreviousVersion() {
@@ -33,4 +36,18 @@
public void setPreviousVersion(T previousVersion) {
this.previousVersion = previousVersion;
}
-}
+ public boolean adoptChild(LanguageElement child) {
+ if (child instanceof Documentation) {
+ this.documentation = (Documentation) child;
+ return true;
+ }
+ if (child instanceof Block) {
+ Block block = (Block) child;
+ if (block.isTest()) {
+ unitTests.add(block);
+ return true;
+ }
+ }
+ return false;
+ }
+}
=======================================
--- /core/src/main/java/noop/model/Library.java Fri Apr 16 14:27:28 2010
+++ /core/src/main/java/noop/model/Library.java Mon Apr 19 10:04:58 2010
@@ -16,23 +16,51 @@
package noop.model;
+import com.google.common.collect.Lists;
import noop.graph.ModelVisitor;
+import java.util.List;
+
/**
* @author
alex...@google.com (Alex Eagle)
*/
public class Library extends LanguageElement<Library> {
public final String name;
- public String copyright;
-
- @Override
- public void accept(ModelVisitor v) {
- v.visit(this);
- }
+ private final List<Clazz> classes = Lists.newArrayList();
+ private final List<Block> functions = Lists.newArrayList();
public Library(String name) {
this.name = name;
-
-
+ }
+
+ @Override
+ public boolean adoptChild(LanguageElement child) {
+ if (child instanceof Clazz) {
+ classes.add((Clazz) child);
+ return true;
+ }
+ if (child instanceof Block) {
+ Block block = (Block) child;
+ if (block.isFunction()) {
+ functions.add(block);
+ return true;
+ }
+ }
+ return super.adoptChild(child);
+ }
+
+ @Override
+ public void accept(ModelVisitor v) {
+ v.visit(this);
+ for (Block function : functions) {
+ v.enter(function);
+ function.accept(v);
+ v.leave(function);
+ }
+ for (Clazz clazz : classes) {
+ v.enter(clazz);
+ clazz.accept(v);
+ v.leave(clazz);
+ }
}
}
=======================================
--- /core/src/main/java/noop/model/Parameter.java Fri Apr 16 14:27:28 2010
+++ /core/src/main/java/noop/model/Parameter.java Mon Apr 19 10:04:58 2010
@@ -32,4 +32,9 @@
public void accept(ModelVisitor v) {
v.visit(this);
}
-}
+
+ @Override
+ public String toString() {
+ return "Parameter " + name;
+ }
+}
=======================================
--- /core/src/main/java/noop/model/Project.java Fri Apr 16 14:27:28 2010
+++ /core/src/main/java/noop/model/Project.java Mon Apr 19 10:04:58 2010
@@ -16,10 +16,13 @@
package noop.model;
+import com.google.common.collect.Lists;
import noop.graph.ModelVisitor;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
+import java.util.List;
+
/**
* @author
alex...@google.com (Alex Eagle)
*/
@@ -27,6 +30,7 @@
private final String name;
private final String namespace;
private final String copyright;
+ private final List<Library> libraries = Lists.newArrayList();
public String getCopyright() {
return copyright;
@@ -45,6 +49,15 @@
this.namespace = namespace;
this.copyright = copyright;
}
+
+ @Override
+ public boolean adoptChild(LanguageElement child) {
+ if (child instanceof Library) {
+ libraries.add((Library) child);
+ return true;
+ }
+ return super.adoptChild(child);
+ }
@Override
public int hashCode() {
@@ -71,5 +84,10 @@
@Override
public void accept(ModelVisitor v) {
v.visit(this);
+ for (Library library : libraries) {
+ v.enter(library);
+ library.accept(v);
+ v.leave(library);
+ }
}
}
=======================================
--- /core/src/main/java/noop/stdlib/StandardLibraryBuilder.java Mon Apr 19
07:04:03 2010
+++ /core/src/main/java/noop/stdlib/StandardLibraryBuilder.java Mon Apr 19
10:04:58 2010
@@ -1,7 +1,6 @@
package noop.stdlib;
import com.google.common.collect.Lists;
-import noop.graph.Controller;
import noop.model.*;
import noop.operations.MutationOperation;
import noop.operations.NewNodeOperation;
@@ -9,6 +8,7 @@
import java.util.List;
import static noop.graph.Edge.EdgeType.TYPEOF;
+import static noop.model.Block.method;
/**
* @author
alex...@google.com (Alex Eagle)
@@ -17,6 +17,7 @@
public Clazz intClazz;
public Clazz consoleClazz;
public Clazz stringClazz;
+ public Clazz voidClazz;
public Block printMethod;
public List<MutationOperation> build() {
@@ -31,13 +32,16 @@
stringClazz = new Clazz("String");
result.add(new NewNodeOperation(stringClazz, lang));
+ voidClazz = new Clazz("Void");
+ result.add(new NewNodeOperation(voidClazz, lang));
+
Library io = new Library("io");
result.add(new NewNodeOperation(io, project));
consoleClazz = new Clazz("Console");
result.add(new NewNodeOperation(consoleClazz, io));
- printMethod = new Block("print", null);
+ printMethod = method("print", voidClazz);
result.add(new NewNodeOperation(printMethod, consoleClazz));
Parameter printArg = new Parameter("s");
=======================================
--- /core/src/test/java/noop/graph/HelloWorldExampleMain.java Mon Apr 19
07:04:03 2010
+++ /core/src/test/java/noop/graph/HelloWorldExampleMain.java Mon Apr 19
10:04:58 2010
@@ -19,7 +19,6 @@
import noop.model.*;
import noop.operations.NewNodeOperation;
import noop.stdlib.StandardLibraryBuilder;
-import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
@@ -28,6 +27,8 @@
import java.util.Arrays;
import static noop.graph.Edge.EdgeType.*;
+import static noop.model.Block.function;
+import static noop.model.Block.unitTest;
/**
* @author
alex...@google.com (Alex Eagle)
@@ -36,23 +37,41 @@
private Controller controller;
private Workspace workspace;
private StandardLibraryBuilder stdLib;
+ private final Output output;
private final PrintStream out;
- public HelloWorldExampleMain(PrintStream out) {
+ public HelloWorldExampleMain(Output output, PrintStream out) {
+ this.output = output;
this.out = out;
}
+
+ public enum Output {
+ OUTLINE, DOT
+ }
public static void main(String[] args) throws FileNotFoundException {
- new HelloWorldExampleMain(new PrintStream(new FileOutputStream(new
File(args[0])))).run();
+ new HelloWorldExampleMain(Output.valueOf(args[0].toUpperCase()),
+ new PrintStream(new FileOutputStream(new File(args[1])))).run();
}
- private void run() {
+ public void run() {
workspace = new Workspace();
controller = new Controller(workspace);
stdLib = new StandardLibraryBuilder();
controller.applyAll(stdLib.build());
createHelloWorldProgram();
- workspace.accept(new DotGraphPrintingVisitor(workspace, out));
+ PrintingVisitor graphPrintingVisitor;
+ switch (output) {
+ case DOT:
+ graphPrintingVisitor = new DotGraphPrintingVisitor(out);
+ break;
+ case OUTLINE:
+ graphPrintingVisitor = new OutlinePrintingVisitor(out);
+ break;
+ default:
+ throw new RuntimeException("unknown output type " + output);
+ }
+ workspace.accept(graphPrintingVisitor);
}
public void createHelloWorldProgram() {
@@ -64,9 +83,10 @@
Parameter consoleDep = new Parameter("console");
- Block sayHello = new Block("say hello", stdLib.intClazz, consoleDep);
- controller.applyAll(Arrays.asList(new NewNodeOperation(sayHello,
library),
- new NewNodeOperation(consoleDep, sayHello, TYPEOF,
stdLib.consoleClazz)));
+ Block sayHello = function("Say hello", stdLib.intClazz);
+ controller.applyAll(Arrays.asList(
+ new NewNodeOperation(sayHello, library),
+ new NewNodeOperation(consoleDep, sayHello, TYPEOF,
stdLib.consoleClazz)));
Documentation sayHelloDoc = new Documentation("This is the entry point
for the Hello World app");
controller.apply(new NewNodeOperation(sayHelloDoc, sayHello));
@@ -80,5 +100,17 @@
IntegerLiteral zero = new IntegerLiteral(0);
controller.applyAll(Arrays.asList(new NewNodeOperation(zero, sayHello,
TYPEOF, stdLib.intClazz),
new NewNodeOperation(new Return(zero), sayHello)));
+
+ Block unitTest = unitTest("Should say hello");
+ controller.apply(new NewNodeOperation(unitTest, sayHello));
+
+ IdentifierDeclaration resultDecl = new IdentifierDeclaration("result");
+ controller.apply(new NewNodeOperation(resultDecl, unitTest, TYPEOF,
stdLib.intClazz));
+
+ Expression callMain = new MethodInvocation();
+ controller.apply(new NewNodeOperation(callMain, resultDecl, INVOKE,
sayHello));
+
+ Expression assertion = new MethodInvocation();
+ controller.apply(new NewNodeOperation(assertion, unitTest));
}
}
=======================================
--- /core/src/test/java/noop/graph/HelloWorldExampleTest.java Sun Apr 18
01:54:12 2010
+++ /core/src/test/java/noop/graph/HelloWorldExampleTest.java Mon Apr 19
10:04:58 2010
@@ -1,5 +1,6 @@
package noop.graph;
+import noop.graph.HelloWorldExampleMain.Output;
import org.junit.Test;
/**
@@ -8,7 +9,12 @@
public class HelloWorldExampleTest {
@Test
- public void shouldRunSuccessfully() {
- new HelloWorldExampleMain(System.out);
+ public void shouldCreateDotSuccessfully() {
+ new HelloWorldExampleMain(Output.DOT, System.out).run();
+ }
+
+ @Test
+ public void shouldCreateOutlineSuccessfully() {
+ new HelloWorldExampleMain(Output.OUTLINE, System.out).run();
}
}
--
You received this message because you are subscribed to the Google Groups "Noop project changes from the version control system" group.
To post to this group, send email to
noop-c...@googlegroups.com.
To unsubscribe from this group, send email to
noop-changes...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/noop-changes?hl=en.