Skip to content

Commit

Permalink
Perlito5 - java - Spark example - implement UnaryOperator, BinaryOper…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
fglock committed Nov 28, 2019
1 parent 39cd180 commit d342f8d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
@@ -1,5 +1,8 @@
Dev Version:

- Perlito Closure extends java.util.function.UnaryOperator, BinaryOperator:
sub ($) { ... } # UnaryOperator<PerlObject>
sub ($, $) { ... } # BinaryOperator<PerlObject>
-- fix: `__LINE__` returns the full path
-- fix: `a =>=> b =>` is not a syntax error
-- fix: `(,)` is a syntax error
Expand Down
10 changes: 10 additions & 0 deletions misc/Spark/README.md
Expand Up @@ -85,3 +85,13 @@ https://github.com/EclairJS/eclairjs-nashorn
https://stackoverflow.com/questions/49829959/apache-spark-implementation-in-nodejs-application


Extensions for Perlito + Spark
=============================

Perlito Closure extends java.util.function.UnaryOperator, BinaryOperator:

sub ($) { ... } # UnaryOperator<PerlObject>

sub ($, $) { ... } # BinaryOperator<PerlObject>


8 changes: 7 additions & 1 deletion src5/lib/Perlito5/Java/Emitter.pm
Expand Up @@ -2272,8 +2272,14 @@ package Perlito5::AST::Sub;
unshift @js_block, "int return_context = want;";
}

my $closure_type = "PlClosure"; # aka "Runnable"
if (defined($self->{sig})) {
$closure_type = "PlUnaryClosure" if $self->{sig} eq "$"; # aka "UnaryOperator"
$closure_type = "PlBinaryClosure" if $self->{sig} eq "$$"; # aka "BinaryOperator"
}
my @s = (
"new PlClosure(" . join( ", ", @closure_args ) . ") {",
"new $closure_type(" . join( ", ", @closure_args ) . ") {",
[
@perl_pos,
"public StackTraceElement firstLine() {",
Expand Down
33 changes: 32 additions & 1 deletion src5/lib/Perlito5/Java/Runtime.pm
Expand Up @@ -265,6 +265,7 @@ sub emit_java {
// use perlito5-lib.jar
import org.perlito.Perlito5.*;
import java.util.function.*;
import java.util.regex.Pattern;
import java.time.*;
import java.time.format.*;
Expand Down Expand Up @@ -391,6 +392,7 @@ import java.nio.ByteBuffer;
import static java.nio.file.attribute.PosixFilePermission.*;
import java.time.*;
import java.time.format.*;
import java.util.function.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -4552,6 +4554,35 @@ class PlRegexResult extends PlScalarImmutable {
return true;
}
}
class PlUnaryClosure extends PlClosure implements UnaryOperator<PlObject> {
public PlUnaryClosure(PlObject prototype, PlObject[] env, String pkg_name, boolean is_defined) {
super(prototype, env, pkg_name, is_defined);
}
public PlUnaryClosure(PlObject prototype, PlObject[] env, String pkg_name, boolean is_defined, PlClosure currentSub) {
// this is the constructor for do-BLOCK; currentSub points to the "sub" outside
super(prototype, env, pkg_name, is_defined, currentSub);
}
public PlObject apply(PlObject v1) {
// run as UnaryOperator
return this.apply(PlCx.SCALAR, new PlArray(v1));
}
}
class PlBinaryClosure extends PlClosure implements BinaryOperator<PlObject> {
public PlBinaryClosure(PlObject prototype, PlObject[] env, String pkg_name, boolean is_defined) {
super(prototype, env, pkg_name, is_defined);
}
public PlBinaryClosure(PlObject prototype, PlObject[] env, String pkg_name, boolean is_defined, PlClosure currentSub) {
// this is the constructor for do-BLOCK; currentSub points to the "sub" outside
super(prototype, env, pkg_name, is_defined, currentSub);
}
public PlObject apply(PlObject v1, PlObject v2) {
// run as BinaryOperator
return this.apply(PlCx.SCALAR, new PlArray(v1, v2));
}
}
class PlClosure extends PlReference implements Runnable {
public PlObject[] env; // new PlObject[]{ v1, v2, v3 }
public PlObject prototype; // '$$$'
Expand Down Expand Up @@ -4637,7 +4668,7 @@ class PlClosure extends PlReference implements Runnable {
return null;
}
public void run() {
// run as a thread
// run as Runnable - this is used to start a Thread
this.apply(PlCx.VOID, new PlArray());
}
Expand Down

0 comments on commit d342f8d

Please sign in to comment.