Добавлена функция length

This commit is contained in:
Victor 2016-01-12 22:50:32 +02:00
parent 28c7b38025
commit ca2462dff8
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.annimon.ownlang.lib.modules.functions;
import com.annimon.ownlang.lib.*;
public final class std_length implements Function {
@Override
public Value execute(Value... args) {
if (args.length == 0) throw new RuntimeException("At least one arg expected");
final Value val = args[0];
int length;
switch (val.type()) {
case Types.ARRAY:
length = ((ArrayValue) val).size();
break;
case Types.MAP:
length = ((MapValue) val).size();
break;
case Types.STRING:
length = ((StringValue) val).length();
break;
case Types.FUNCTION:
final Function func = ((FunctionValue) val).getValue();
if (func instanceof UserDefinedFunction) {
length = ((UserDefinedFunction) func).getArgsCount();
} else {
length = 0;
}
break;
default:
length = 0;
}
return new NumberValue(length);
}
}

View File

@ -13,6 +13,7 @@ public final class std implements Module {
public void init() {
Functions.set("echo", new std_echo());
Functions.set("newarray", new std_newarray());
Functions.set("length", new std_length());
Functions.set("rand", new std_rand());
Functions.set("sleep", new std_sleep());
Functions.set("thread", new std_thread());