Skip to content

Commit

Permalink
case _ of Vector.Vector_Data works again
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Aug 31, 2022
1 parent 0c5a44c commit aa17c2b
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Json

example_into =
json = Examples.json
format = (Vector.of (Book title=Text (Author name=Text year_of_birth=Number)))
format = (Vector.fill 1 (Book title=Text (Author name=Text year_of_birth=Number)))
json.into format
into : Any -> Any ! Marshalling_Error
into self type_descriptor =
Expand Down
16 changes: 1 addition & 15 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ polyglot java import org.enso.base.Array_Utils
new : Number -> (Number -> Any) -> Vector Any
new length constructor = @Builtin_Method "Vector.new_builtin"

## Creates a new vector of a single element.

Arguments:
- element: the element to be wrapper in a vector.

A vector allows to store an arbitrary number of elements in linear memory. It
is the recommended data structure for most applications.

> Example
Create a vector containing a single number 42.
Vector.of 42
of : Any -> Vector Any
of element = new 1 (_->element)

## ADVANCED

Converts an array into a vector.
Expand Down Expand Up @@ -134,7 +120,7 @@ type Vector a
A vector containing 50 elements, each being the number `42`, can be
created by:
Vector.fill length=50 item=42
Vector_Data storage
Vector_Data

to_array self = @Builtin_Method "Vector.to_array"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.oracle.truffle.api.profiles.ConditionProfile;
import org.enso.interpreter.runtime.callable.atom.Atom;
import org.enso.interpreter.runtime.callable.atom.AtomConstructor;
import org.enso.interpreter.runtime.data.Vector;

/** An implementation of the case expression specialised to working on constructors. */
@NodeInfo(shortName = "ConstructorMatch")
Expand All @@ -27,7 +28,10 @@ public abstract class ConstructorBranchNode extends BranchNode {
* @param branch the expression to be executed if (@code matcher} matches
* @return a node for matching in a case expression
*/
public static ConstructorBranchNode build(AtomConstructor matcher, RootCallTarget branch) {
public static BranchNode build(AtomConstructor matcher, RootCallTarget branch) {
if ("Standard.Builtins.Main.Vector_Data".equals(matcher.getQualifiedName().toString())) {
return ConstructorBranchNodeGen.VectorConstructorNodeGen.create(branch);
}
return ConstructorBranchNodeGen.create(matcher, branch);
}

Expand All @@ -40,4 +44,20 @@ void doAtom(VirtualFrame frame, Object state, Atom target) {

@Fallback
void doFallback(VirtualFrame frame, Object state, Object target) {}

@NodeInfo(shortName = "VectorConstructorMatch")
abstract static class VectorConstructorNode extends BranchNode {

VectorConstructorNode(RootCallTarget branch) {
super(branch);
}

@Specialization
void doVector(VirtualFrame frame, Object state, Vector target) {
accept(frame, state, new Object[0]);
}

@Fallback
void doFallback(VirtualFrame frame, Object state, Object target) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.frame.VirtualFrame;
import org.enso.interpreter.runtime.Context;

public class ObjectEqualityBranchNode extends BranchNode {
private final Object expected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ protected List<Cons> getDeclaredConstructors() {
return List.of(new Cons("Vector_Data"));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ public long getArraySize() throws UnsupportedMessageException {

@Builtin.Method(description = "Returns the length of this Vector.")
@Builtin.Specialize
@Builtin.WrapException(
from = UnsupportedMessageException.class,
to = PolyglotError.class
)
@Builtin.WrapException(from = UnsupportedMessageException.class, to = PolyglotError.class)
public long length(InteropLibrary interop) throws UnsupportedMessageException {
return interop.getArraySize(storage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.enso.interpreter.test;

import java.net.URI;
import java.nio.file.Paths;
import java.util.Map;
import org.enso.polyglot.RuntimeOptions;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.Language;
import org.graalvm.polyglot.Source;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;

public class VectorTest {
@Test
public void evaluation() throws Exception {
Engine eng = Engine.newBuilder()
.allowExperimentalOptions(true)
.option(
RuntimeOptions.LANGUAGE_HOME_OVERRIDE,
Paths.get("../../distribution/component").toFile().getAbsolutePath()
).build();
Context ctx = Context.newBuilder()
.engine(eng)
.allowIO(true)
.allowAllAccess(true)
.build();
final Map<String, Language> langs = ctx.getEngine().getLanguages();
assertNotNull("Enso found: " + langs, langs.get("enso"));

final URI facUri = new URI("memory://choose.enso");
final Source facSrc = Source.newBuilder("enso", """
import Standard.Base.Data.Vector
choose x = case x of
Vector.Vector_Data -> "is vector"
_ -> "nothing"
check = choose [1, 2, 3]
""", "choose.enso")
.uri(facUri)
.buildLiteral();

var module = ctx.eval(facSrc);
var res = module.invokeMember("eval_expression", "check");
assertEquals("is vector", res.asString());
}
}
4 changes: 2 additions & 2 deletions test/Tests/src/Data/Array_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ spec =
arr = make_enso_array (Vector.fill 1000 0)
text = arr.to_default_visualization_data
json = Json.parse text
as_vec = json.into (Vector.of Number)
as_vec = json.into (Vector.fill 1 Number)
as_vec.should_equal <| Vector.fill 100 0

Test.group "Polyglot Arrays" <|
Expand All @@ -59,7 +59,7 @@ spec =
arr = make_java_array (Vector.fill 1000 0)
text = arr.to_default_visualization_data
json = Json.parse text
as_vec = json.into (Vector.of Number)
as_vec = json.into (Vector.fill 1 Number)
as_vec.should_equal <| Vector.fill 100 0

main = Test.Suite.run_main spec
2 changes: 1 addition & 1 deletion test/Tests/src/Data/Json_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ spec =

json_string = (enso_project.data / "books.json").read_text
parsed = Json.parse json_string
domain = parsed.into (Vector.of (Book title=Text (Author name=Text year_of_birth=Number)))
domain = parsed.into (Vector.fill 1 (Book_Data title=Text (Author_Data name=Text year_of_birth=Number)))
domain.should_equal books

Test.group "JSON Serialization" <|
Expand Down
2 changes: 1 addition & 1 deletion test/Tests/src/Data/Vector_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ spec = Test.group "Vectors" <|
vec = Vector.fill 1000 0
text = vec.to_default_visualization_data
json = Json.parse text
as_vec = json.into (Vector.of Number)
as_vec = json.into (Vector.fill 1 Number)
as_vec.should_equal <| Vector.fill 100 0

Test.specify "should pad elements" <|
Expand Down

0 comments on commit aa17c2b

Please sign in to comment.