[noop] push by aeagle22206 - Introduce a protocol buffer as the in-memory representation of the AST... on 2009-12-18 17:41 GMT

2 views
Skip to first unread message

no...@googlecode.com

unread,
Dec 18, 2009, 12:42:15 PM12/18/09
to noop-c...@googlegroups.com
Revision: 8826103b81
Author: Alex Eagle <alex...@google.com>
Date: Fri Dec 18 09:39:36 2009
Log: Introduce a protocol buffer as the in-memory representation of the
AST. This may be useful later, as it allows us to serialize the program at
the parsed level rather than in source code. So far we have started from
the literal expressions.
http://code.google.com/p/noop/source/detail?r=8826103b81

Added:
/core/src/main/proto/noop_ast.proto
/core/src/main/scala/noop/model/ExpressionWrapper.scala
Modified:
/buildfile
/core/src/main/antlr3/noop/grammar/antlr/NoopAST.g
/core/src/main/scala/noop/model/BooleanLiteralExpression.scala
/core/src/main/scala/noop/model/IntLiteralExpression.scala
/core/src/main/scala/noop/model/StringLiteralExpression.scala
/core/src/test/scala/noop/grammar/BindingSpec.scala
/core/src/test/scala/noop/grammar/BlockSpec.scala
/core/src/test/scala/noop/grammar/ExpressionsSpec.scala
/core/src/test/scala/noop/grammar/LiteralsSpec.scala
/core/src/test/scala/noop/grammar/LoopSpec.scala
/interpreter/src/test/scala/noop/interpreter/ConditionalSpec.scala
/interpreter/src/test/scala/noop/interpreter/ControlStructureSpec.scala

/interpreter/src/test/scala/noop/interpreter/IdentifierDeclarationSpec.scala
/interpreter/src/test/scala/noop/interpreter/InterpreterSpec.scala
/interpreter/src/test/scala/noop/interpreter/MethodInvocationSpec.scala
/interpreter/src/test/scala/noop/interpreter/ShouldExpressionSpec.scala

=======================================
--- /dev/null
+++ /core/src/main/proto/noop_ast.proto Fri Dec 18 09:39:36 2009
@@ -0,0 +1,47 @@
+// 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.
+
+// Defines the data model which stores Noop programs.
+
+// Generate the java file with:
+// protoc core/src/main/proto/noop_ast.proto --java_out=core/src/main/java
+// TODO(alexeagle): have buildr run the proto compiler
+
+option java_package = "noop.model.proto";
+
+message Expr {
+ enum Type {
+ BOOLEAN_LITERAL = 1;
+ STRING_LITERAL = 2;
+ INT_LITERAL = 3;
+ }
+ // identifies what sort of expression this is
+ required Type type = 1;
+
+ optional BooleanLiteral booleanLiteral = 2;
+ optional StringLiteral stringLiteral = 3;
+ optional IntLiteral intLiteral = 4;
+}
+
+message IntLiteral {
+ required sint32 value = 1;
+}
+
+message StringLiteral {
+ required string value = 1;
+}
+
+message BooleanLiteral {
+ required bool value = 1;
+}
=======================================
--- /dev/null
+++ /core/src/main/scala/noop/model/ExpressionWrapper.scala Fri Dec 18
09:39:36 2009
@@ -0,0 +1,29 @@
+package noop.model
+
+import noop.model.proto.NoopAst
+import proto.NoopAst.Expr;
+import noop.model.proto.NoopAst.Expr.Type;
+
+/**
+ * Converts proto buffer "inheritance", which is done by enum, into typed
expressions.
+ * @author alex...@google.com (Alex Eagle)
+ */
+class ExpressionWrapper(data: Expr) extends Expression {
+ def accept(visitor: Visitor): Unit = {
+ getTypedExpression.accept(visitor);
+ }
+
+ def getTypedExpression: Expression = {
+ data.getType match {
+ case Type.STRING_LITERAL => {
+ new StringLiteralExpression(data.getStringLiteral)
+ }
+ case Type.INT_LITERAL => {
+ new IntLiteralExpression(data.getIntLiteral)
+ }
+ case Type.BOOLEAN_LITERAL => {
+ new BooleanLiteralExpression(data.getBooleanLiteral)
+ }
+ }
+ }
+}
=======================================
--- /buildfile Sat Dec 5 16:49:22 2009
+++ /buildfile Fri Dec 18 09:39:36 2009
@@ -29,6 +29,8 @@
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"
]
+PROTO = ["com.google.protobuf:protobuf-java:jar:2.2.0"]
+
# Force Buildr Antlr integration to use the version we specify
Buildr::ANTLR::REQUIRES.clear
Buildr::ANTLR::REQUIRES.concat(ANTLR)
@@ -44,7 +46,7 @@
_('src/main/antlr3/noop/grammar/antlr/NoopAST.g')],
:in_package=>'noop.grammar.antlr')
compile.from(antlr).
- with [ANTLR, SLF4J]
+ with [ANTLR, SLF4J, PROTO]
package :jar
end

@@ -62,7 +64,7 @@
# TODO - only want examples as a test resource
resources.from [_('src/main/noop'), project("examples")._('noop')]
package(:jar).with(:manifest=>{'Main-Class'
=> 'noop.interpreter.InterpreterMain'})
- compile.with [project("core"), ANTLR_RUNTIME, SLF4J, GUICE]
+ compile.with [project("core"), ANTLR_RUNTIME, SLF4J, GUICE, PROTO]
package(:zip).
include(compile.dependencies, :path=>'lib').
include(_("target/#{id}-#{version}.jar"), :path=>'lib').
=======================================
--- /core/src/main/antlr3/noop/grammar/antlr/NoopAST.g Thu Dec 3 21:48:53
2009
+++ /core/src/main/antlr3/noop/grammar/antlr/NoopAST.g Fri Dec 18 09:39:36
2009
@@ -31,6 +31,8 @@
package noop.grammar.antlr;

import noop.model.*;
+ import noop.model.proto.NoopAst;
+ import noop.model.proto.NoopAst.*;
import scala.Enumeration;
import scala.collection.mutable.Buffer;
import scala.collection.mutable.ArrayBuffer;
@@ -311,7 +313,7 @@

expression returns [Expression exp]
: l=literal
- { $exp = $l.exp; }
+ { $exp = new ExpressionWrapper($l.exp); }
| d=dereference
{ $exp = $d.exp; }
| o=operatorExpression
@@ -369,13 +371,22 @@
}
;

-literal returns [Expression exp]
+literal returns [Expr exp]
: i=INT
- { $exp = new IntLiteralExpression(Integer.valueOf($i.text)); }
+ { $exp = Expr.newBuilder()
+ .setType(Expr.Type.INT_LITERAL)
+ .setIntLiteral(IntLiteral.newBuilder().setValue(Integer.valueOf($i.text)))
+ .build(); }
| s=StringLiteral
- { $exp = new StringLiteralExpression(stripQuotes($s.text)); }
+ { $exp = Expr.newBuilder()
+ .setType(Expr.Type.STRING_LITERAL)
+ .setStringLiteral(NoopAst.StringLiteral.newBuilder().setValue(stripQuotes($s.text)))
+ .build(); }
| b=('true' | 'false')
- { $exp = new BooleanLiteralExpression(Boolean.valueOf($b.text)); }
+ { $exp = Expr.newBuilder()
+ .setType(Expr.Type.BOOLEAN_LITERAL)
+ .setBooleanLiteral(NoopAst.BooleanLiteral.newBuilder().setValue(Boolean.valueOf($b.text)))
+ .build(); }
;

doc returns [String doc]
=======================================
--- /core/src/main/scala/noop/model/BooleanLiteralExpression.scala Thu Oct
8 10:25:57 2009
+++ /core/src/main/scala/noop/model/BooleanLiteralExpression.scala Fri Dec
18 09:39:36 2009
@@ -15,12 +15,16 @@
*/
package noop.model;

+import noop.model.proto.NoopAst.BooleanLiteral;
+
/**
* @author Erik Soe Sorensen (eri...@gmail.com)
* @author toc...@gmail.com (Jeremie Lenfant-Engelmann)
*/
-class BooleanLiteralExpression(val value: Boolean) extends Expression {
-
+class BooleanLiteralExpression(data: BooleanLiteral) extends Expression {
+
+ def value = data.getValue;
+
def accept(visitor: Visitor) = {
visitor.visit(this);
}
=======================================
--- /core/src/main/scala/noop/model/IntLiteralExpression.scala Thu Oct 8
10:25:57 2009
+++ /core/src/main/scala/noop/model/IntLiteralExpression.scala Fri Dec 18
09:39:36 2009
@@ -15,11 +15,15 @@
*/
package noop.model;

+import noop.model.proto.NoopAst.IntLiteral;
+
/**
* @author alex...@google.com (Alex Eagle)
* @author toc...@gmail.com (Jeremie Lenfant-Engelmann)
*/
-class IntLiteralExpression(val value: Int) extends Expression {
+class IntLiteralExpression(data: IntLiteral) extends Expression {
+
+ def value: Int = data.getValue;

def accept(visitor: Visitor) = {
visitor.visit(this);
=======================================
--- /core/src/main/scala/noop/model/StringLiteralExpression.scala Thu Oct
8 10:25:57 2009
+++ /core/src/main/scala/noop/model/StringLiteralExpression.scala Fri Dec
18 09:39:36 2009
@@ -15,11 +15,15 @@
*/
package noop.model;

+import noop.model.proto.NoopAst.StringLiteral;
+
/**
* @author alex...@google.com (Alex Eagle)
* @author toc...@gmail.com (Jeremie Lenfant-Engelmann)
*/
-class StringLiteralExpression(val value: String) extends Expression {
+class StringLiteralExpression(data: StringLiteral) extends Expression {
+
+ def value = data.getValue;

def accept(visitor: Visitor) = {
visitor.visit(this);
=======================================
--- /core/src/test/scala/noop/grammar/BindingSpec.scala Thu Dec 3 21:26:31
2009
+++ /core/src/test/scala/noop/grammar/BindingSpec.scala Fri Dec 18 09:39:36
2009
@@ -17,7 +17,7 @@


import org.scalatest.matchers.ShouldMatchers
-import noop.model.{BindingDefinition, StringLiteralExpression,
IdentifierExpression};
+import noop.model.{ExpressionWrapper, BindingDefinition,
StringLiteralExpression, IdentifierExpression}
import org.scalatest.Spec;

/**
@@ -104,8 +104,8 @@
method.block.anonymousBindings should have length(1);
val firstBinding = method.block.anonymousBindings.first;
firstBinding.noopType should be("String");
- firstBinding.boundTo.getClass() should
be(classOf[StringLiteralExpression]);
- firstBinding.boundTo.asInstanceOf[StringLiteralExpression].value
should be ("foo");
+ val typedExpression =
firstBinding.boundTo.asInstanceOf[ExpressionWrapper].getTypedExpression;
+ typedExpression.asInstanceOf[StringLiteralExpression].value should
be ("foo");
}
}

=======================================
--- /core/src/test/scala/noop/grammar/BlockSpec.scala Thu Oct 8 10:25:57
2009
+++ /core/src/test/scala/noop/grammar/BlockSpec.scala Fri Dec 18 09:39:36
2009
@@ -21,7 +21,7 @@
import org.scalatest.matchers.ShouldMatchers;
import org.scalatest.Spec;

-import model._;
+import noop.model._;

/**
* @author alex...@google.com (Alex Eagle)
@@ -42,8 +42,9 @@
block.statements(0).getClass() should be(classOf[ReturnExpression]);
val returnExpression =
block.statements(0).asInstanceOf[ReturnExpression];

- returnExpression.expr.getClass() should
be(classOf[IntLiteralExpression]);
- returnExpression.expr.asInstanceOf[IntLiteralExpression].value
should be(0);
+ val typedExpression =
returnExpression.expr.asInstanceOf[ExpressionWrapper].getTypedExpression
+ typedExpression.getClass should be(classOf[IntLiteralExpression]);
+ typedExpression.asInstanceOf[IntLiteralExpression].value should
be(0);
}

it("should allow chained property access on properties") {
@@ -157,8 +158,9 @@
methodInvocation.arguments should have length(2);
methodInvocation.arguments(0).getClass() should
be(classOf[IdentifierExpression]);

methodInvocation.arguments(0).asInstanceOf[IdentifierExpression].identifier
should be("c");
- methodInvocation.arguments(1).getClass() should
be(classOf[StringLiteralExpression]);
-
methodInvocation.arguments(1).asInstanceOf[StringLiteralExpression].value
should be ("d");
+ val typedExpression =
methodInvocation.arguments(1).asInstanceOf[ExpressionWrapper].getTypedExpression
+ typedExpression.getClass() should
be(classOf[StringLiteralExpression]);
+ typedExpression.asInstanceOf[StringLiteralExpression].value should
be ("d");
}
}
}
=======================================
--- /core/src/test/scala/noop/grammar/ExpressionsSpec.scala Thu Nov 12
22:16:59 2009
+++ /core/src/test/scala/noop/grammar/ExpressionsSpec.scala Fri Dec 18
09:39:36 2009
@@ -15,11 +15,10 @@
*/
package noop.grammar;

-import org.scalatest.matchers.ShouldMatchers;
+import org.scalatest.matchers.ShouldMatchers
+import noop.model.{ExpressionWrapper, OperatorExpression,
IntLiteralExpression, IdentifierDeclarationExpression};
import org.scalatest.Spec;

-import noop.model.{OperatorExpression, IntLiteralExpression,
IdentifierDeclarationExpression};
-
/**
* @author alex...@google.com (Alex Eagle)
* @author toc...@gmail.com (Jeremie Lenfant-Engelmann)
@@ -46,7 +45,8 @@
declaration.initialValue should be ('defined);
val expression1 =
declaration.initialValue.get().asInstanceOf[OperatorExpression];
expression1.operator should be ("+");
- expression1.left.asInstanceOf[IntLiteralExpression].value should be
(3);
+ val typedExpression =
expression1.left.asInstanceOf[ExpressionWrapper].getTypedExpression
+ typedExpression.asInstanceOf[IntLiteralExpression].value should be
(3);
val expression2 = expression1.asInstanceOf[OperatorExpression].right;
expression2.asInstanceOf[OperatorExpression].operator should be
("%");
}
=======================================
--- /core/src/test/scala/noop/grammar/LiteralsSpec.scala Thu Oct 8
10:25:57 2009
+++ /core/src/test/scala/noop/grammar/LiteralsSpec.scala Fri Dec 18
09:39:36 2009
@@ -15,11 +15,10 @@
*/
package noop.grammar;

-import org.scalatest.matchers.ShouldMatchers;
+import org.scalatest.matchers.ShouldMatchers
+import noop.model.{ExpressionWrapper, BooleanLiteralExpression,
IdentifierDeclarationExpression};
import org.scalatest.Spec;

-import model.{BooleanLiteralExpression, IdentifierDeclarationExpression};
-
/**
* @author alex...@google.com (Alex Eagle)
* @author toc...@gmail.com (Jeremie Lenfant-Engelmann)
@@ -61,8 +60,7 @@
.asInstanceOf[IdentifierDeclarationExpression];
statement.initialValue match {
case Some(b) => {
- b.getClass() should be (classOf[BooleanLiteralExpression]);
- b.asInstanceOf[BooleanLiteralExpression].value should be (true);
+
b.asInstanceOf[ExpressionWrapper].getTypedExpression.asInstanceOf[BooleanLiteralExpression].value
should be (true);
}
case None => fail();
}
=======================================
--- /core/src/test/scala/noop/grammar/LoopSpec.scala Thu Oct 8 10:25:57
2009
+++ /core/src/test/scala/noop/grammar/LoopSpec.scala Fri Dec 18 09:39:36
2009
@@ -15,11 +15,10 @@
*/
package noop.grammar;

-import org.scalatest.matchers.ShouldMatchers;
+import org.scalatest.matchers.ShouldMatchers
+import noop.model.{ExpressionWrapper, BooleanLiteralExpression, WhileLoop};
import org.scalatest.Spec;

-import model.{BooleanLiteralExpression, WhileLoop};
-
/**
* @author alex...@google.com (Alex Eagle)
*/
@@ -35,7 +34,8 @@
val block =
parser.buildTreeParser(parser.parseBlock(source)).block();
block.statements(0).getClass() should be (classOf[WhileLoop]);
val whileLoop = block.statements(0).asInstanceOf[WhileLoop];
-
whileLoop.continueCondition.asInstanceOf[BooleanLiteralExpression].value
should be(true);
+ val typedExpression =
whileLoop.continueCondition.asInstanceOf[ExpressionWrapper].getTypedExpression
+ typedExpression.asInstanceOf[BooleanLiteralExpression].value should
be(true);
}
}

=======================================
--- /interpreter/src/test/scala/noop/interpreter/ConditionalSpec.scala Thu
Dec 3 18:36:36 2009
+++ /interpreter/src/test/scala/noop/interpreter/ConditionalSpec.scala Fri
Dec 18 09:39:36 2009
@@ -20,18 +20,26 @@
import org.scalatest.matchers.ShouldMatchers;
import org.scalatest.Spec;

-import model.{Block, IntLiteralExpression, OperatorExpression, Visitor};
-import types.{IntegerFactory, NoopBoolean, NoopTypesModule};
+import noop.model.proto.NoopAst.IntLiteral;
+import noop.model.{Block, IntLiteralExpression, OperatorExpression,
Visitor};
+import noop.types.{IntegerFactory, NoopBoolean, NoopTypesModule};

/**
* @author toc...@gmail.com (Jeremie Lenfant-Engelmann)
*/
class ConditionalSpec extends Spec with ShouldMatchers {

+ val one = IntLiteral.newBuilder.setValue(1).build;
+ val two = IntLiteral.newBuilder.setValue(2).build;
+ val three = IntLiteral.newBuilder.setValue(3).build;
+
def createFixture = {
val injector = Guice.createInjector(new InterpreterModule(List()), new
NoopTypesModule());
val context = injector.getInstance(classOf[Context]);
val visitor = injector.getInstance(classOf[Visitor]);
+ val integerFactory = injector.getInstance(classOf[IntegerFactory]);
+
+ context.addRootFrame(integerFactory.create(1));
(injector, context, visitor);
}

@@ -39,11 +47,8 @@

it("should evaluate to true when equals is called and the values are
equal") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(1), "==",
- new IntLiteralExpression(1));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(one), "==",
+ new IntLiteralExpression(one));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -55,11 +60,8 @@

it("should evaluate to false when equals is called and the values are
not equal") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(1), "==",
- new IntLiteralExpression(2));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(one), "==",
+ new IntLiteralExpression(two));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -71,11 +73,8 @@

it("should evaluate to true when doesNotEqual is called and the values
are not equal") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(1), "!=",
- new IntLiteralExpression(2));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(one), "!=",
+ new IntLiteralExpression(two));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -87,11 +86,8 @@

it("should evaluate to false when doesNotEqual is called and the
values are equal") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(1), "!=",
- new IntLiteralExpression(1));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(one), "!=",
+ new IntLiteralExpression(one));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -103,11 +99,8 @@

it("should evaluate to true when greaterThan is called and the value
is greater than the other one") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(2), ">",
- new IntLiteralExpression(1));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(two), ">",
+ new IntLiteralExpression(one));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -119,11 +112,8 @@

it("should evaluate to false when greaterThan is called and the value
is less than the other one") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(1), ">",
- new IntLiteralExpression(2));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(one), ">",
+ new IntLiteralExpression(two));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -135,11 +125,8 @@

it("should evaluate to true when lesserThan is called and the value is
less than the other one") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(1), "<",
- new IntLiteralExpression(2));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(one), "<",
+ new IntLiteralExpression(two));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -151,11 +138,8 @@

it("should evaluate to false when lesserThan is called and the value
is greater than the other one") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(3), "<",
- new IntLiteralExpression(2));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(three), "<",
+ new IntLiteralExpression(two));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -167,11 +151,8 @@

it("should evaluate to true when greaterOrEqualThan is called and the
value is greater or equal than the other one") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(3), ">=",
- new IntLiteralExpression(2));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(three), ">=",
+ new IntLiteralExpression(two));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -183,11 +164,8 @@

it("should evaluate to false when greaterOrEqualThan is called and the
value is lesser than the other one") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(1), ">=",
- new IntLiteralExpression(2));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(one), ">=",
+ new IntLiteralExpression(two));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -199,11 +177,8 @@

it("should evaluate to true when lesserOrEqualThan is called and the
value is lesser or equal than the other one") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(1), "<=",
- new IntLiteralExpression(2));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(one), "<=",
+ new IntLiteralExpression(two));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
@@ -215,11 +190,8 @@

it("should evaluate to false when lesserOrEqualThan is called and the
value is greater than the other one") {
val (injector, context, visitor) = createFixture;
- val integerFactory = injector.getInstance(classOf[IntegerFactory]);
-
- context.addRootFrame(integerFactory.create(1));
- val conditional = new OperatorExpression(new
IntLiteralExpression(2), "<=",
- new IntLiteralExpression(1));
+ val conditional = new OperatorExpression(new
IntLiteralExpression(two), "<=",
+ new IntLiteralExpression(one));

conditional.accept(visitor);
context.stack.top.lastEvaluated should have size(1);
=======================================
--- /interpreter/src/test/scala/noop/interpreter/ControlStructureSpec.scala
Mon Nov 9 09:28:48 2009
+++ /interpreter/src/test/scala/noop/interpreter/ControlStructureSpec.scala
Fri Dec 18 09:39:36 2009
@@ -15,13 +15,14 @@
*/
package noop.interpreter;

-import inject.Injector
+import noop.model.proto.NoopAst.BooleanLiteral;
+import noop.model.proto.NoopAst.StringLiteral;
import org.scalatest.matchers.ShouldMatchers;
import org.scalatest.Spec;

-import model.{IdentifierExpression, AssignmentExpression, Block,
BooleanLiteralExpression, Expression,
+import noop.model.{IdentifierExpression, AssignmentExpression, Block,
BooleanLiteralExpression, Expression,
IdentifierDeclarationExpression, ReturnExpression,
StringLiteralExpression, Visitor, WhileLoop};
-import types.{NoopString};
+import noop.types.{NoopString};

/**
* @author alex...@google.com (Alex Eagle)
@@ -29,6 +30,8 @@
*/
class ControlStructureSpec extends Spec with ShouldMatchers with
GuiceInterpreterFixture {

+ val falseExpr = BooleanLiteral.newBuilder.setValue(false).build;
+
def interpreterFixture = {
val injector = fixture;
val block = new Block();
@@ -40,7 +43,7 @@

def accept(visitor: Visitor) = {
called += 1;
- new BooleanLiteralExpression(called <=
timesToReturnTrue).accept(visitor);
+ new
BooleanLiteralExpression(BooleanLiteral.newBuilder.setValue(called <=
timesToReturnTrue).build).accept(visitor);
}
}

@@ -62,7 +65,7 @@
val expression: MockExpression = new MockExpression();
block.statements += expression;

- val whileLoop = new WhileLoop(new BooleanLiteralExpression(false),
block);
+ val whileLoop = new WhileLoop(new
BooleanLiteralExpression(falseExpr), block);

whileLoop.accept(visitor);
expression.timesCalled should be(0);
@@ -92,7 +95,7 @@
val enclosingBlock = new Block();
enclosingBlock.statements += block;
enclosingBlock.statements += new AssignmentExpression(
- new IdentifierExpression("s"), new StringLiteralExpression("s"));
+ new IdentifierExpression("s"), new
StringLiteralExpression(StringLiteral.newBuilder.setValue("s").build));
new WhileLoop(new TrueThenFalseExpression(1),
enclosingBlock).accept(visitor);
}
}
@@ -101,7 +104,7 @@

it("should exit the current block and return the supplied value") {
val (context, block, visitor) = interpreterFixture;
- val returnMe = new ReturnExpression(new StringLiteralExpression("to
return"));
+ val returnMe = new ReturnExpression(new
StringLiteralExpression(StringLiteral.newBuilder.setValue("to
return").build));

block.statements += returnMe;
val dontRunMe = new MockExpression((c: Context) => fail());
=======================================
---
/interpreter/src/test/scala/noop/interpreter/IdentifierDeclarationSpec.scala
Mon Nov 9 09:28:48 2009
+++
/interpreter/src/test/scala/noop/interpreter/IdentifierDeclarationSpec.scala
Fri Dec 18 09:39:36 2009
@@ -15,12 +15,12 @@
*/
package noop.interpreter;

-import inject.Injector
-import model.{Visitor, StringLiteralExpression,
IdentifierDeclarationExpression}
+import noop.model.proto.NoopAst.StringLiteral;
+import noop.model.{Visitor, StringLiteralExpression,
IdentifierDeclarationExpression}
import org.scalatest.matchers.ShouldMatchers;
import org.scalatest.Spec;

-import types.{NoopString};
+import noop.types.{NoopString};

/**
* @author alex...@google.com (Alex Eagle)
@@ -37,7 +37,8 @@
it("should add an identifier to the map of identifiers on the current
stack frame") {
val identifierDeclaration = new
IdentifierDeclarationExpression("type", "myString");

- identifierDeclaration.initialValue = Some(new
StringLiteralExpression("hello world"));
+ identifierDeclaration.initialValue = Some(new
StringLiteralExpression(
+ StringLiteral.newBuilder.setValue("hello world").build));

val (context, visitor) = myFixture;

=======================================
--- /interpreter/src/test/scala/noop/interpreter/InterpreterSpec.scala Thu
Dec 3 21:26:31 2009
+++ /interpreter/src/test/scala/noop/interpreter/InterpreterSpec.scala Fri
Dec 18 09:39:36 2009
@@ -16,10 +16,9 @@
package noop.interpreter;


-import noop.inject.Injector
-import noop.types.{NoopObject, NoopTypesModule, NoopInteger}
-import noop.model.{ClassDefinition, ConcreteClassDefinition, Visitor,
IntLiteralExpression, OperatorExpression}
-import collection.mutable.Stack;
+import noop.types.{NoopTypesModule, NoopInteger};
+import noop.model.proto.NoopAst.IntLiteral;
+import noop.model.{Visitor, IntLiteralExpression, OperatorExpression}
import noop.grammar.Parser;

import com.google.inject.Guice;
@@ -44,7 +43,9 @@

it("should evaluate simple arithmetic") {
val (context, visitor) = createFixture;
- val expr = new OperatorExpression(new IntLiteralExpression(2), "+",
new IntLiteralExpression(3));
+ val expr = new OperatorExpression(
+ new
IntLiteralExpression(IntLiteral.newBuilder.setValue(2).build), "+",
+ new IntLiteralExpression(IntLiteral.newBuilder.setValue(3).build));

expr.accept(visitor);
val result = context.stack.top.lastEvaluated(0);
=======================================
--- /interpreter/src/test/scala/noop/interpreter/MethodInvocationSpec.scala
Fri Nov 13 11:55:04 2009
+++ /interpreter/src/test/scala/noop/interpreter/MethodInvocationSpec.scala
Fri Dec 18 09:39:36 2009
@@ -15,18 +15,21 @@
*/
package noop.interpreter;

-import inject.Injector
-import model._
+import noop.model.proto.NoopAst.IntLiteral;
+import noop.model.proto.NoopAst.StringLiteral;
+import noop.model._
import org.scalatest.matchers.ShouldMatchers;
import org.scalatest.Spec;

-import types.{NoopString, NoopObject};
+import noop.types.{NoopString, NoopObject};

/**
* @author alex...@google.com (Alex Eagle)
*/
class MethodInvocationSpec extends Spec with ShouldMatchers with
GuiceInterpreterFixture {

+ val aString = StringLiteral.newBuilder.setValue("aString").build;
+
def interpreterFixture = {
val injector = fixture;
(injector.getInstance(classOf[Context]),
injector.getInstance(classOf[Visitor]));
@@ -36,7 +39,7 @@

it("should throw an exception if the method doesn't exist on the
type") {
val (context, visitor) = interpreterFixture;
- val target = new StringLiteralExpression("aString");
+ val target = new StringLiteralExpression(aString);
val expr = new
MethodInvocationExpression(target, "noSuchMethodSorry", List());
val exception = intercept[NoSuchMethodException] (
expr.accept(visitor)
@@ -55,7 +58,7 @@
val myMethod = new Method("length", block, null);
myMethod.returnTypes += "Int";
context.classLoader.findClass("noop.String").methods += myMethod;
- val target = new StringLiteralExpression("aString");
+ val target = new StringLiteralExpression(aString);
val expr = new MethodInvocationExpression(target, "length", List());
expr.accept(visitor);
}
@@ -77,8 +80,9 @@

myMethod.parameters += new Parameter(paramName, "String");
context.classLoader.findClass("noop.String").methods += myMethod;
- val target = new StringLiteralExpression("string1");
- val expr = new MethodInvocationExpression(target, "plus", List(new
StringLiteralExpression(arg)));
+ val target = new StringLiteralExpression(aString);
+ val expr = new MethodInvocationExpression(target, "plus",
+ List(new
StringLiteralExpression(StringLiteral.newBuilder.setValue(arg).build)));
expr.accept(visitor);
}

@@ -89,7 +93,7 @@

myMethod.parameters += new Parameter("other", "String");
context.classLoader.findClass("noop.String").methods += myMethod;
- val target = new StringLiteralExpression("string1");
+ val target = new StringLiteralExpression(aString);
val expr = new MethodInvocationExpression(target, "plus", List());

intercept[RuntimeException] {
@@ -104,8 +108,9 @@

myMethod.parameters += new Parameter("other", "String");
context.classLoader.findClass("noop.String").methods += myMethod;
- val target = new StringLiteralExpression("string1");
- val expr = new MethodInvocationExpression(target, "plus", List(new
IntLiteralExpression(1)));
+ val target = new StringLiteralExpression(aString);
+ val expr = new MethodInvocationExpression(target, "plus",
+ List(new
IntLiteralExpression(IntLiteral.newBuilder.setValue(1).build)));

intercept[RuntimeException] {
expr.accept(visitor);
@@ -118,7 +123,7 @@
myMethod.returnTypes += "Void";

context.classLoader.findClass("noop.String").methods += myMethod;
- val target = new StringLiteralExpression("string1");
+ val target = new StringLiteralExpression(aString);
val expr = new MethodInvocationExpression(target, "plus", List());
val currentFrame = new Frame(null, null);

@@ -139,7 +144,7 @@
myMethod.parameters += new Parameter(paramName, "String");
context.classLoader.findClass("noop.String").methods += myMethod;

- val target = new StringLiteralExpression("string1");
+ val target = new StringLiteralExpression(aString);
val expr = new MethodInvocationExpression(target, "plus", List(new
MockExpression()));

intercept[RuntimeException] {
@@ -149,7 +154,7 @@

it("should be aware of the 'this' identifier and dispatch the method
on thisRef") {
val (context, visitor) = interpreterFixture;
- new StringLiteralExpression("hello").accept(visitor);
+ new StringLiteralExpression(aString).accept(visitor);
val thisRef: NoopObject = context.stack.top.lastEvaluated(0);

thisRef should not be (null);
=======================================
--- /interpreter/src/test/scala/noop/interpreter/ShouldExpressionSpec.scala
Mon Nov 9 09:28:48 2009
+++ /interpreter/src/test/scala/noop/interpreter/ShouldExpressionSpec.scala
Fri Dec 18 09:39:36 2009
@@ -15,8 +15,9 @@
*/
package noop.interpreter

-import interpreter.testing.{TestFailedException};
-import model.{ShouldExpression, Visitor, MethodInvocationExpression,
StringLiteralExpression};
+import noop.interpreter.testing.{TestFailedException};
+import noop.model.proto.NoopAst.StringLiteral;
+import noop.model.{ShouldExpression, Visitor, MethodInvocationExpression,
StringLiteralExpression};

import org.scalatest.matchers.ShouldMatchers;
import org.scalatest.Spec;
@@ -26,11 +27,14 @@
*/

class ShouldExpressionSpec extends Spec with ShouldMatchers with
GuiceInterpreterFixture {
+ val hello = StringLiteral.newBuilder.setValue("hello").build;
+ val goodbye = StringLiteral.newBuilder.setValue("goodbye").build;
+
describe("the 'should' operator") {

it("should be silent if the lefthand side matches an equals matcher") {
- val matcher = new MethodInvocationExpression(null, "equal", List(new
StringLiteralExpression("hello")));
- val shouldExpr = new ShouldExpression(new
StringLiteralExpression("hello"), matcher);
+ val matcher = new MethodInvocationExpression(null, "equal", List(new
StringLiteralExpression(hello)));
+ val shouldExpr = new ShouldExpression(new
StringLiteralExpression(hello), matcher);
val injector = fixture;
val visitor = injector.getInstance(classOf[Visitor]);
val context = injector.getInstance(classOf[Context]);
@@ -42,8 +46,8 @@
}

it("should throw a test failed exception if an equals matcher is not
satisfied") {
- val matcher = new MethodInvocationExpression(null, "equal", List(new
StringLiteralExpression("goodbye")));
- val shouldExpr = new ShouldExpression(new
StringLiteralExpression("hello"), matcher);
+ val matcher = new MethodInvocationExpression(null, "equal", List(new
StringLiteralExpression(goodbye)));
+ val shouldExpr = new ShouldExpression(new
StringLiteralExpression(hello), matcher);
val injector = fixture;
val visitor = injector.getInstance(classOf[Visitor]);

Reply all
Reply to author
Forward
0 new messages