mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Добавлена функция std::try
This commit is contained in:
parent
23f61a64e5
commit
8f0f37956f
@ -253,3 +253,6 @@ println formatDate(d, newFormat("yyyy-MM-dd HH:mm:ss, EEEE"))
|
|||||||
println parseDate("2016/05/10", newFormat("yyyy/MM/dd"))
|
println parseDate("2016/05/10", newFormat("yyyy/MM/dd"))
|
||||||
println toTimestamp(d)
|
println toTimestamp(d)
|
||||||
println newDate(toTimestamp(d) - 100000)
|
println newDate(toTimestamp(d) - 100000)
|
||||||
|
|
||||||
|
try(def() = try + 2)
|
||||||
|
println try(def() = try(), def(type, message) = sprintf("Error handled:\ntype: %s\nmessage: %s", type, message))
|
28
src/com/annimon/ownlang/lib/modules/functions/std_try.java
Normal file
28
src/com/annimon/ownlang/lib/modules/functions/std_try.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package com.annimon.ownlang.lib.modules.functions;
|
||||||
|
|
||||||
|
import com.annimon.ownlang.exceptions.TypeException;
|
||||||
|
import com.annimon.ownlang.lib.*;
|
||||||
|
|
||||||
|
public final class std_try implements Function {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value execute(Value... args) {
|
||||||
|
Arguments.checkOrOr(1, 2, args.length);
|
||||||
|
if (args[0].type() != Types.FUNCTION) {
|
||||||
|
throw new TypeException(args[0].toString() + " is not a function");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return ((FunctionValue) args[0]).getValue().execute();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
if (args.length == 2 && args[1].type() == Types.FUNCTION) {
|
||||||
|
final String message = ex.getMessage();
|
||||||
|
final Function catchFunction = ((FunctionValue) args[1]).getValue();
|
||||||
|
return catchFunction.execute(
|
||||||
|
new StringValue(ex.getClass().getName()),
|
||||||
|
new StringValue(message == null ? "" : message));
|
||||||
|
}
|
||||||
|
return NumberValue.MINUS_ONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -22,6 +22,7 @@ public final class std implements Module {
|
|||||||
Functions.set("sleep", new std_sleep());
|
Functions.set("sleep", new std_sleep());
|
||||||
Functions.set("thread", new std_thread());
|
Functions.set("thread", new std_thread());
|
||||||
Functions.set("sync", new std_sync());
|
Functions.set("sync", new std_sync());
|
||||||
|
Functions.set("try", new std_try());
|
||||||
|
|
||||||
// String
|
// String
|
||||||
Functions.set("sprintf", new std_sprintf());
|
Functions.set("sprintf", new std_sprintf());
|
||||||
|
Loading…
Reference in New Issue
Block a user