Skip to content

Commit

Permalink
fix(objectionary#3084): combine malloc and memory
Browse files Browse the repository at this point in the history
maxonfjvipon committed Apr 23, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 91f6cba commit beae173
Showing 23 changed files with 819 additions and 1,160 deletions.
Original file line number Diff line number Diff line change
@@ -29,8 +29,10 @@
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.TrFast;
import com.yegor256.xsline.TrLambda;
import com.yegor256.xsline.Train;
import com.yegor256.xsline.Xsline;
import org.eolang.parser.StEoLogged;

/**
* Optimisation train of XLS`s.
@@ -50,18 +52,21 @@ public final class OptTrain implements Optimization {
* the hood in {@link TrClasspath}, is not thread-safe.
*/
static final Train<Shift> DEFAULT_TRAIN = new TrFast(
new TrClasspath<>(
new TrDefault<>(),
"/org/eolang/parser/optimize/globals-to-abstracts.xsl",
"/org/eolang/parser/optimize/remove-refs.xsl",
"/org/eolang/parser/optimize/abstracts-float-up.xsl",
"/org/eolang/parser/optimize/remove-levels.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/optimize/fix-missed-names.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/errors/broken-refs.xsl",
"/org/eolang/parser/set-locators.xsl"
).back(),
new TrLambda(
new TrClasspath<>(
new TrDefault<>(),
"/org/eolang/parser/optimize/globals-to-abstracts.xsl",
"/org/eolang/parser/optimize/remove-refs.xsl",
"/org/eolang/parser/optimize/abstracts-float-up.xsl",
"/org/eolang/parser/optimize/remove-levels.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/optimize/fix-missed-names.xsl",
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/errors/broken-refs.xsl",
"/org/eolang/parser/set-locators.xsl"
).back(),
StEoLogged::new
),
TrFast.class,
500L
);
2 changes: 1 addition & 1 deletion eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4
Original file line number Diff line number Diff line change
@@ -527,7 +527,7 @@ spacedArrow
// Does not contain elements in vertical notation
// Is used in happlicationArg, hmethodHead
scope
: LB (happlication | hanonym) RB
: LB (happlication | hanonym | onlyphi) RB
;

// Version
4 changes: 2 additions & 2 deletions eo-parser/src/main/java/org/eolang/parser/StEoLogged.java
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@
*
* @since 0.30
*/
final class StEoLogged implements Shift {
public final class StEoLogged implements Shift {

/**
* Origin shift.
@@ -50,7 +50,7 @@ final class StEoLogged implements Shift {
* Ctor.
* @param shift Origin shift
*/
StEoLogged(final Shift shift) {
public StEoLogged(final Shift shift) {
this(shift, message -> Logger.error(StEoLogged.class, message));
}

Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ tests:
- //o[@base='a']/o[@abstract and not(@name) and o[@abstract and @name='@' and o[@base='a' and @name='b']] and o[@name='c']]
- //o[@base='a']/o[@abstract and not(@name) and o[@base='d' and @name='@']]
- //o[@abstract and o[@base='z' and @name='@']]/following-sibling::o[@base='.x' and @method]
- //o[@base='m']/o[@abstract and not(@name) and o[@name='q' and not(@base)] and o[@base='q' and @name='@']]
eo: |
a > x
a.b > [i]
@@ -13,3 +14,4 @@ eo: |
d > []
z > []
.x
m (q > [q])
4 changes: 3 additions & 1 deletion eo-runtime/src/main/eo/org/eolang/go.eo
Original file line number Diff line number Diff line change
@@ -54,7 +54,9 @@
# ```
# Go to.
[] > go
(malloc 1).id.as-bytes > id
malloc.of > id!
8
m.put m.id > [m]

# To.
[body] > to
101 changes: 77 additions & 24 deletions eo-runtime/src/main/eo/org/eolang/malloc.eo
Original file line number Diff line number Diff line change
@@ -26,34 +26,87 @@
+rt jvm org.eolang:eo-runtime:0.0.0
+version 0.0.0

# Allocates a block in memory of `size` bytes.
# You can use it like:
# Storage of data in memory.
#
# There are two ways of usage `malloc`:
# 1. of size:
# ```
# malloc.of
# 8
# [m]
# m.put 10 > @
# ```
# Here, the first argument is a size of allocated block in memory, the second argument
# is the scope where memory block is available for reading and writing. When `malloc.of` is
# dataized it dataizes the scope, take the data from the block in memory, clears the block and
# returns the data. So there's no need for end-user to care about clearing memory after allocation.
#
# 2. for object
# ```
# (malloc 8).pointer > p
# seq
# *
# p.write 0 42 # write 8 bytes integer with offset 0
# p.read # read from memory, 42 will be returned
# p.free # free allocated block
# malloc.for
# "Hello world!"
# [m]
# m.put "Hello, Jeff!" > @
# ```
# Clearing the block is optional and is up to programmer.
[size] > malloc
@.pointer > pointer
# Here, the first argument is an object which will be dataized, then a block in memory of given data
# size is allocated and the data is written to the block. The second argument is the same scope as
# in the p.1.
#
# The void attribute in the scope object is memory-block object which provides API to write and read
# data to the memory.
# ```
# malloc.of
# 8 # allocate 8 bytes length block in memory
# [m]
# seq > @
# *
# m.write 2 "Hello" # write object "Hello" with offset 2
# m.read 3 4 # read 4 bytes from offset 3 -> "ello"
# m.put 42 # write object 42 with offset 0
# m.get # just get all the data from the memory block -> 42
# m.size # get size of the block
# m.id # get identifier of the block
# m.@ # the same as m.get
# ```
# Malloc.
[] > malloc
# Allocates block in memory for given `object`. After allocation the provided object is dataized
# and the data are written into memory.
[object scope] > for
(dataized object).as-bytes > bts
&.of > @
bts.size
[m]
seq > @
*
m.write 0 bts
scope m

# Allocates block in memory of given `size`. After allocation the `size` zero bytes bytes are
# written into memory.
[size scope] > of
# Allocates a memory block, initializes it, pass it to the `scope` and dataizes `scope` and
# at the end, even if the error is occurred, clears the block.
[] > @ /bytes

# Allocates a memory block in RAM and returns pointer to it.
[] > @ /memory-block-pointer
# Allocated block in memory that provides an API for writing and reading.
[id] > allocated
^.size > size
get > @

# Pointer to allocated block in memory.
# Here `id` is identifier of pointer, `size` is length of the block.
[id] > memory-block-pointer
$ > pointer
^.size > size
# Read `length` bytes with `offset` from the allocated block in memory.
[offset length] > read /bytes

# Read `length` bytes from `offset` from the block in memory by the pointer.
[offset length] > read /bytes
# Write `data` with `offset` to the allocated block in memory.
[offset data] > write /true

# Write `data` from `offset` to the block in memory by the pointer.
[offset data] > write /true
# Just get all the data from the allocated block in memory.
[] > get
^.read 0 ^.size > @

# Free the block in memory by the pointer.
[] > free /true
# Put `object` into the allocated block in memory. The `object` is supposed to be dataizable.
[object] > put
seq > @
*
^.write 0 object
^.get
69 changes: 0 additions & 69 deletions eo-runtime/src/main/eo/org/eolang/memory.eo

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -39,30 +39,32 @@
import org.eolang.XmirObject;

/**
* Malloc.pointer.read object.
* Malloc.of.allocated.read object.
* @since 0.36.0
* @checkstyle TypeNameCheck (5 lines)
*/
@Versionized
@XmirObject(oname = "malloc.pointer.read")
final class EOmalloc$EOmemory_block_pointer$EOread extends PhDefault implements Atom {
@XmirObject(oname = "malloc.of.allocated.read")
final class EOmalloc$EOof$EOallocated$EOread extends PhDefault implements Atom {
/**
* Ctor.
* @param sigma Sigma
*/
EOmalloc$EOmemory_block_pointer$EOread(final Phi sigma) {
EOmalloc$EOof$EOallocated$EOread(final Phi sigma) {
super(sigma);
this.add("offset", new AtVoid("offset"));
this.add("length", new AtVoid("length"));
}

@Override
public Phi lambda() throws Exception {
final Phi rho = this.take(Attr.RHO);
final int length = Math.toIntExact(new Param(this, "length").strong(Long.class));
return new Data.ToPhi(
Heaps.INSTANCE.read(
Math.toIntExact(new Param(this.take(Attr.RHO), "id").strong(Long.class)),
Math.toIntExact(new Param(rho, "id").strong(Long.class)),
Math.toIntExact(new Param(this, "offset").strong(Long.class)),
Math.toIntExact(new Param(this, "length").strong(Long.class))
length
)
);
}
Loading

0 comments on commit beae173

Please sign in to comment.