Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#2211): memory behaves as bytes #2430

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion eo-runtime/src/main/eo/org/eolang/memory.eo
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@
+version 0.0.0

# Storage of data in memory.
[] > memory /?
# @todo #2211:30min We can implement memory in EO instead of Java and let it use
# ram object. Let's not forget to update the "Origins of Objects" paper.
[] > memory /bytes
4 changes: 3 additions & 1 deletion eo-runtime/src/main/java/EOorg/EOeolang/AtMemoized.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
package EOorg.EOeolang;

import org.eolang.Attr;
import org.eolang.Data;
import org.eolang.ExFailure;
import org.eolang.Param;
import org.eolang.Phi;

/**
Expand Down Expand Up @@ -81,7 +83,7 @@ public Phi get() {

@Override
public void put(final Phi phi) {
this.object = phi;
this.object = new Data.ToPhi(new Param(phi, "Δ").asBytes().take());
}

@Override
Expand Down
15 changes: 5 additions & 10 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOmemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

import org.eolang.AtComposite;
import org.eolang.AtFree;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.Attr;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.XmirObject;
Expand Down Expand Up @@ -60,7 +59,7 @@ public EOmemory(final Phi sigma) {
* @since 1.0
*/
@XmirObject(oname = "memory.write")
private final class Write extends PhDefault {
private static final class Write extends PhDefault {
/**
* Ctor.
* @param sigma Sigma
Expand All @@ -73,13 +72,9 @@ private final class Write extends PhDefault {
new AtComposite(
this,
rho -> {
final Phi phi = new Data.ToPhi(
new Dataized(
rho.attr("x").get()
).take()
);
rho.attr("σ").get().attr("enclosure").put(phi);
return phi;
final Attr enclosure = rho.attr("σ").get().attr("enclosure");
enclosure.put(rho.attr("x").get());
return enclosure.get();
}
)
);
Expand Down
23 changes: 14 additions & 9 deletions eo-runtime/src/main/java/org/eolang/Param.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@

package org.eolang;

import java.math.BigInteger;
import java.nio.ByteBuffer;

/**
* Param of an object (convenient retrieval mechanism).
*
* <p>The job of the utility object is to help our EO objects retrieve
* attributes from other objects and from their own \rho (owners). On top of
* retrieval this object also does simple type checking. When an attribute
* retrieval, this object also does simple type checking. When an attribute
* is expected to be of some type, we use {@link #strong(Class)}. This method
* will throw a runtime exception if types don't match. If just a simple
* retrieval without type checking is necessary, just use the method
Expand Down Expand Up @@ -111,14 +108,22 @@ public Object weak() {
public Bytes asBytes() {
final Object ret = this.weak();
final Bytes res;
if (Long.class.isInstance(ret)) {
if (ret instanceof Long) {
res = new BytesOf((long) ret);
} else if (Character.class.isInstance(ret)) {
res = new BytesOf((char) ret);
} else if (Double.class.isInstance(ret)) {
} else if (ret instanceof Double) {
res = new BytesOf((double) ret);
} else if (byte[].class.isInstance(ret)) {
} else if (ret instanceof Character) {
res = new BytesOf((char) ret);
} else if (ret instanceof String) {
res = new BytesOf((String) ret);
} else if (ret instanceof byte[]) {
res = new BytesOf((byte[]) ret);
} else if (ret instanceof Boolean) {
final byte[] bytes = new byte[1];
if ((boolean) ret) {
bytes[0] = 1;
}
res = new BytesOf(bytes);
} else {
throw new ExFailure(
String.format(
Expand Down
50 changes: 30 additions & 20 deletions eo-runtime/src/test/eo/org/eolang/bool-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,41 @@

[] > iterates-over-simple-counter
memory 0 > x
assert-that > @
assert-that > res
and.
eq.
x.write 5
5
eq.
11
while.
x.lt 10
x.as-int.lt 10
[i]
^.^.^ > x
x.write (x.plus 1) > @
x.write (x.as-int.plus 1) > @
$.equal-to TRUE
nop > @

[] > prints-nice-formulas
memory 0 > x
assert-that > @
seq
x.write 2
while.
x.lt 6
x.as-int.lt 6
[i]
^.^.^ > x
seq > @
stdout
sprintf "%dx%d = %d\n" x x ((number x).pow 2)
x.write (x.plus 1)
sprintf
"%dx%d = %d\n"
x.as-int
x.as-int
pow.
number
x.as-int
2
x.write
x.as-int.plus 1
TRUE
$.equal-to TRUE

Expand All @@ -97,7 +105,7 @@
[] > and-short-circuiting
memory 0 > mFirst
memory 0 > mThird
assert-that > @
assert-that > res
and.
not.
and.
Expand All @@ -113,6 +121,7 @@
mFirst.eq 1
mThird.eq 3
$.equal-to TRUE
nop > @

# tests that bool.or stops calculations if its i'th
# object is true (including the base object)
Expand Down Expand Up @@ -157,51 +166,52 @@

[] > complex-bool-expression-in-while
memory 0 > m
assert-that > @
assert-that > res
seq
m.write 5
while.
eq.
m.gt 0
m.as-int.gt 0
TRUE
[i]
seq > @
m.write (m.minus 1)
stdout (sprintf "%d\n" m)
m.write (m.as-int.minus 1)
stdout (sprintf "%d\n" m.as-int)
TRUE
$.equal-to TRUE
nop > @

[] > last-while-dataization-object
memory 0 > x
assert-that > @
while.
x.lt 2
x.as-int.lt 2
[i]
seq > @
x.write (x.plus 1)
x
x.write (x.as-int.plus 1)
x.as-int
$.equal-to 3

[] > while-without-last-dataization
memory 0 > x
assert-that > @
seq
while.
x.lt 2
x.as-int.lt 2
[i]
x.write (x.plus 1) > @
x.write (x.as-int.plus 1) > @
.@
.<
x
x.as-int
$.equal-to 2

[] > last-while-dataization-object-with-false-condition
memory 3 > x
assert-that > @
while.
x.lt 1
x.as-int.lt 1
[i]
seq > @
x.write (x.plus 1)
x.write (x.as-int.plus 1)
x
$.equal-to FALSE
8 changes: 4 additions & 4 deletions eo-runtime/src/test/eo/org/eolang/cage-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@
[] > @
[] > @
seq > @
ma.write (ma.plus 1)
ma
ma.write (ma.as-int.plus 1)
ma.as-int
memory 0 > mb
cage b > cb
[] > b
[] > @
[] > @
[] > @
seq > z
mb.write (mb.plus 1)
mb
mb.write (mb.as-int.plus 1)
mb.as-int
assert-that > @
seq
ca
Expand Down
8 changes: 4 additions & 4 deletions eo-runtime/src/test/eo/org/eolang/goto-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
goto
[g]
seq > @
i.write (i.plus 1)
i.write (i.as-int.plus 1)
if.
i.lt 10
i.as-int.lt 10
g.backward
TRUE
stdout "Finished!"
Expand All @@ -54,9 +54,9 @@
goto
[g]
seq > @
i.write (i.plus 1)
i.write (i.as-int.plus 1)
if.
i.lt 10
i.as-int.lt 10
g.backward
TRUE
.@
Expand Down
9 changes: 6 additions & 3 deletions eo-runtime/src/test/eo/org/eolang/heap-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
heap 1024 > h!
h.malloc 64 > p1!
h.malloc 32 > p2!
assert-that > @
assert-that > res
eq.
seq
QQ.io.stdout
Expand All @@ -57,22 +57,24 @@
p1
p2
$.equal-to FALSE
nop > @

[] > mallocs-do-not-overlap
heap 1024 > h!
h.malloc 64 > p1!
h.malloc 32 > p2!
assert-that > @
assert-that > res
or.
p2.gte
p1.plus 64
p2.lte
p1.minus 32
$.equal-to TRUE
nop > @

[] > malloc-return-error
heap 2 > h!
assert-that > @
assert-that > res
try
[]
h.malloc > @
Expand All @@ -82,6 +84,7 @@
nop
$.equal-to
"Allocation failed: bad alloc (not enough memory in the heap)"
nop > @

[] > write-and-read-without-error
heap 1024 > h
Expand Down
4 changes: 2 additions & 2 deletions eo-runtime/src/test/eo/org/eolang/memory-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
assert-that > @
seq
m.write 1
m.write (m.plus 5)
m.write (m.as-int.plus 5)
m
$.equal-to 6

Expand All @@ -71,7 +71,7 @@
stdout
sprintf
"%d"
a.x
a.x.as-int

[] > writes-into-two-memory-objects
memory 0 > a
Expand Down
Loading