mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Добавлена функция length
This commit is contained in:
parent
28c7b38025
commit
ca2462dff8
@ -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);
|
||||
}
|
||||
}
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user