Skip to content

Commit

Permalink
Add array and map related commands, var and constant commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcsmith-net committed May 3, 2024
1 parent c3713a2 commit ee1064b
Show file tree
Hide file tree
Showing 8 changed files with 772 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import org.praxislive.core.Value;
import org.praxislive.core.types.PArray;
import org.praxislive.core.types.PNumber;
import org.praxislive.script.Command;
import org.praxislive.script.Env;
import org.praxislive.script.InlineCommand;
Expand All @@ -35,23 +36,118 @@
*/
class ArrayCmds {

private final static Array ARRAY = new Array();
private static final Array ARRAY = new Array();
private static final ArrayGet ARRAY_GET = new ArrayGet();
private static final ArrayJoin ARRAY_JOIN = new ArrayJoin();
private static final ArrayRange ARRAY_RANGE = new ArrayRange();
private static final ArraySize ARRAY_SIZE = new ArraySize();

private ArrayCmds() {
}

static void install(Map<String, Command> commands) {
commands.put("array", ARRAY);
commands.put("array-get", ARRAY_GET);
commands.put("array-join", ARRAY_JOIN);
commands.put("array-range", ARRAY_RANGE);
commands.put("array-size", ARRAY_SIZE);
}

private static class Array implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.isEmpty()) {
return List.of(PArray.EMPTY);
}
PArray ar = args.stream().collect(PArray.collector());
return List.of(ar);
}

}

private static class ArrayGet implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.size() != 2) {
throw new IllegalArgumentException("Incorrect number of arguments");
}

PArray array = PArray.from(args.get(0))
.orElseThrow(() -> new IllegalArgumentException("First argument is not an array"));

int index = PNumber
.from(args.get(1))
.orElseThrow(() -> new IllegalArgumentException("Second argument is not a number"))
.toIntValue();

return List.of(array.get(index));
}

}

private static class ArrayJoin implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
PArray result = args.stream()
.flatMap(v -> PArray.from(v).stream())
.flatMap(PArray::stream)
.collect(PArray.collector());
return List.of(result);
}

}

private static class ArrayRange implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.size() < 2 || args.size() > 3) {
throw new IllegalArgumentException("Incorrect number of arguments");
}

PArray array = PArray.from(args.get(0))
.orElseThrow(() -> new IllegalArgumentException("First argument is not an array"));

int from, to;
if (args.size() == 2) {
from = 0;
to = PNumber
.from(args.get(1))
.orElseThrow(() -> new IllegalArgumentException("Second argument is not a number"))
.toIntValue();
} else {
from = PNumber
.from(args.get(1))
.orElseThrow(() -> new IllegalArgumentException("Second argument is not a number"))
.toIntValue();
to = PNumber
.from(args.get(2))
.orElseThrow(() -> new IllegalArgumentException("Third argument is not a number"))
.toIntValue();
}

return List.of(PArray.of(array.asList().subList(from, to)));
}

}

private static class ArraySize implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.size() != 1) {
throw new IllegalArgumentException("Incorrect number of arguments");
}

PArray array = PArray.from(args.get(0))
.orElseThrow(() -> new IllegalArgumentException("Argument is not an array"));

return List.of(PNumber.of(array.size()));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@
class BaseCmds {

private static final System.Logger LOG = System.getLogger(BaseCmds.class.getName());

private static final Constant CONSTANT = new Constant();
private static final Set SET = new Set();
private static final Var VAR = new Var();
private static final Echo ECHO = new Echo();

private BaseCmds() {
}

static void install(Map<String, Command> commands) {
commands.put("constant", CONSTANT);
commands.put("set", SET);
commands.put("var", VAR);
commands.put("echo", ECHO);
}

Expand All @@ -72,6 +77,36 @@ public List<Value> process(Env context, Namespace namespace, List<Value> args) t
}
}

private static class Constant implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.size() != 2) {
throw new Exception();
}
String varName = args.get(0).toString();
Value val = args.get(1);
namespace.createConstant(varName, val);
return List.of(val);

}
}

private static class Var implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.size() != 2) {
throw new Exception();
}
String varName = args.get(0).toString();
Value val = args.get(1);
namespace.createVariable(varName, val);
return List.of(val);

}
}

private static class Echo implements InlineCommand {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void install(Map<String, Command> commands) {
AtCmds.install(commands);
ConnectionCmds.install(commands);
FileCmds.install(commands);
MapCmds.install(commands);
ScriptCmds.install(commands);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2024 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License version 3
* along with this work; if not, see http://www.gnu.org/licenses/
*
*
* Please visit https://www.praxislive.org if you need additional information or
* have any questions.
*/
package org.praxislive.script.commands;

import java.util.List;
import java.util.Map;
import org.praxislive.core.Value;
import org.praxislive.core.types.PArray;
import org.praxislive.core.types.PMap;
import org.praxislive.core.types.PNumber;
import org.praxislive.core.types.PString;
import org.praxislive.script.Command;
import org.praxislive.script.Env;
import org.praxislive.script.InlineCommand;
import org.praxislive.script.Namespace;

/**
*
*/
class MapCmds {

private static final CreateMap MAP = new CreateMap();
private static final MapGet MAP_GET = new MapGet();
private static final MapKeys MAP_KEYS = new MapKeys();
private static final MapSize MAP_SIZE = new MapSize();

private MapCmds() {
}

static void install(Map<String, Command> commands) {
commands.put("map", MAP);
commands.put("map-get", MAP_GET);
commands.put("map-keys", MAP_KEYS);
commands.put("map-size", MAP_SIZE);
}

private static class CreateMap implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.isEmpty()) {
return List.of(PMap.EMPTY);
}

int size = args.size();
if (size % 2 != 0) {
throw new IllegalArgumentException("Map requires an even number of arguments");
}

var builder = PMap.builder();
for (int i = 0; i < size; i += 2) {
builder.put(args.get(i).toString(), args.get(i + 1));
}

return List.of(builder.build());
}

}

private static class MapGet implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.size() != 2) {
throw new IllegalArgumentException("Incorrect number of arguments");
}

PMap map = PMap.from(args.get(0))
.orElseThrow(() -> new IllegalArgumentException("Argument is not a map"));
String key = args.get(1).toString();

Value result = map.get(key);
if (result == null) {
throw new IllegalArgumentException("Unknown map key");
}
return List.of(result);
}

}

private static class MapKeys implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.size() != 1) {
throw new IllegalArgumentException("Incorrect number of arguments");
}

PMap map = PMap.from(args.get(0))
.orElseThrow(() -> new IllegalArgumentException("Argument is not a map"));

PArray result = map.keys().stream()
.map(PString::of)
.collect(PArray.collector());

return List.of(result);
}

}

private static class MapSize implements InlineCommand {

@Override
public List<Value> process(Env context, Namespace namespace, List<Value> args) throws Exception {
if (args.size() != 1) {
throw new IllegalArgumentException("Incorrect number of arguments");
}

PMap map = PMap.from(args.get(0))
.orElseThrow(() -> new IllegalArgumentException("Argument is not a map"));

return List.of(PNumber.of(map.size()));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ public void testEvalInline() throws Exception {
logTest("testEvalInline");
String script = """
eval --inline {
set X 42
var X 42
/hub.value $X
}
set X [echo $X $X]
/hub.value $X
eval {
set Y 84
var Y 84
/hub.value $Y
}
/hub.value $Y
Expand All @@ -143,7 +144,7 @@ public void testEvalInline() throws Exception {
hub.start();
hub.send("/script.eval", "/hub.result", script);

for (String expected : new String[]{"42", "42", "84"}) {
for (String expected : new String[]{"42", "4242", "84"}) {
Call call = hub.poll();
logCall("Value received", call);
assertEquals("/hub.value", call.to().toString());
Expand Down
Loading

0 comments on commit ee1064b

Please sign in to comment.