Skip to content

Commit

Permalink
Merge pull request #20 from mtrberzi/real-typevalue
Browse files Browse the repository at this point in the history
Add primitive type for real numbers
  • Loading branch information
mtrberzi committed Sep 24, 2014
2 parents 508bc10 + deecdfc commit 17d6eb8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
nbproject/private/
.nb-gradle

# Eclipse project files
.classpath
.project
.settings

## generic files
*~
*.lock
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ targetCompatibility = 1.8

group = "org.manifold"
// name = "manifold-core"
version = '0.2.0-SNAPSHOT'
version = '0.3.0-SNAPSHOT'

jar {
manifest {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/manifold/compiler/RealTypeValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.manifold.compiler;

public class RealTypeValue extends TypeValue {
private static final RealTypeValue instance = new RealTypeValue();

private RealTypeValue() {

}

public static RealTypeValue getInstance() {
return instance;
}

public void accept(SchematicValueVisitor visitor) {
visitor.visit(this);
}
}
27 changes: 27 additions & 0 deletions src/main/java/org/manifold/compiler/RealValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.manifold.compiler;

public class RealValue extends Value {
private final Double val;
public RealValue(Double val){
super(RealTypeValue.getInstance());
this.val = val;
}

@Override
public boolean isElaborationtimeKnowable() {
return true;
}

@Override
public boolean isRuntimeKnowable() {
return false;
}

public void accept(SchematicValueVisitor visitor) {
visitor.visit(this);
}

public double toDouble() {
return val;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public interface SchematicValueVisitor {

void visit(ArrayValue arrayValue);

void visit(RealTypeValue realTypeValue);

void visit(RealValue realValue);

}
6 changes: 4 additions & 2 deletions src/main/java/org/manifold/compiler/middle/Schematic.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.manifold.compiler.NodeTypeValue;
import org.manifold.compiler.NodeValue;
import org.manifold.compiler.PortTypeValue;
import org.manifold.compiler.RealTypeValue;
import org.manifold.compiler.StringTypeValue;
import org.manifold.compiler.TypeValue;
import org.manifold.compiler.UndeclaredIdentifierException;
Expand Down Expand Up @@ -72,18 +73,19 @@ public Schematic(String name) {

/*
* Add "library standard" type definitions for basic types such as integer,
* string, and boolean. Every class in .intermediate.types should be
* represented in here.
* string, and boolean.
*/
private void populateDefaultType() {
TypeValue boolType = BooleanTypeValue.getInstance();
TypeValue intType = IntegerTypeValue.getInstance();
TypeValue stringType = StringTypeValue.getInstance();
TypeValue realType = RealTypeValue.getInstance();

try {
addUserDefinedType("Bool", boolType);
addUserDefinedType("Int", intType);
addUserDefinedType("String", stringType);
addUserDefinedType("Real", realType);
} catch (MultipleDefinitionException mde) {
// this should not actually be possible unless there is something wrong
// with the compiler itself
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/org/manifold/compiler/TestAtomicTypes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.manifold.compiler;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;

import org.junit.Test;

Expand All @@ -11,16 +13,19 @@ public void testEquality() {
BooleanTypeValue p1 = BooleanTypeValue.getInstance();
IntegerTypeValue p2 = IntegerTypeValue.getInstance();
StringTypeValue p3 = StringTypeValue.getInstance();
RealTypeValue p4 = RealTypeValue.getInstance();

// Primitive types are singletons.
assertEquals(p1, BooleanTypeValue.getInstance());
assertEquals(p2, IntegerTypeValue.getInstance());
assertEquals(p3, StringTypeValue.getInstance());
assertEquals(p4, RealTypeValue.getInstance());

// Two types are equal iff they are the same object
assertNotEquals(p1, p2);
assertNotEquals(p1, p3);
assertNotEquals(p2, p3);
assertNotEquals(p3, p4);

// Equality function doesn't fail for null.
assertFalse(p1.equals(null));
Expand Down

0 comments on commit 17d6eb8

Please sign in to comment.