mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Улучшен вывод ошибок
This commit is contained in:
parent
748c7ee45e
commit
4eabeca2ac
@ -34,11 +34,13 @@ public final class UserDefinedFunction implements Function {
|
||||
final int size = values.length;
|
||||
final int requiredArgsCount = arguments.getRequiredArgumentsCount();
|
||||
if (size < requiredArgsCount) {
|
||||
throw new ArgumentsMismatchException(String.format("Arguments count mismatch. %d < %d", size, requiredArgsCount));
|
||||
throw new ArgumentsMismatchException(String.format(
|
||||
"Arguments count mismatch. Required %d, got %d", requiredArgsCount, size));
|
||||
}
|
||||
final int totalArgsCount = getArgsCount();
|
||||
if (size > totalArgsCount) {
|
||||
throw new ArgumentsMismatchException(String.format("Arguments count mismatch. %d > %d", size, totalArgsCount));
|
||||
throw new ArgumentsMismatchException(String.format(
|
||||
"Arguments count mismatch. Total %d, got %d", totalArgsCount, size));
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.annimon.ownlang.parser.ast;
|
||||
|
||||
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
|
||||
import com.annimon.ownlang.exceptions.TypeException;
|
||||
import com.annimon.ownlang.exceptions.VariableDoesNotExistsException;
|
||||
import com.annimon.ownlang.exceptions.UnknownFunctionException;
|
||||
import com.annimon.ownlang.lib.*;
|
||||
@ -40,9 +42,13 @@ public final class FunctionalExpression extends InterruptableNode implements Exp
|
||||
}
|
||||
final Function f = consumeFunction(functionExpr);
|
||||
CallStack.enter(functionExpr.toString(), f);
|
||||
final Value result = f.execute(values);
|
||||
try {
|
||||
return f.execute(values);
|
||||
} catch (ArgumentsMismatchException | TypeException | VariableDoesNotExistsException ex) {
|
||||
throw new RuntimeException(ex.getMessage() + " in function " + functionExpr, ex);
|
||||
} finally {
|
||||
CallStack.exit();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private Function consumeFunction(Expression expr) {
|
||||
@ -58,7 +64,9 @@ public final class FunctionalExpression extends InterruptableNode implements Exp
|
||||
}
|
||||
|
||||
private Function getFunction(String key) {
|
||||
if (Functions.isExists(key)) return Functions.get(key);
|
||||
if (Functions.isExists(key)) {
|
||||
return Functions.get(key);
|
||||
}
|
||||
if (Variables.isExists(key)) {
|
||||
final Value variable = Variables.get(key);
|
||||
if (variable.type() == Types.FUNCTION) {
|
||||
|
@ -36,7 +36,7 @@ public final class UseStatement extends InterruptableNode implements Statement {
|
||||
loadModule(value.asString());
|
||||
break;
|
||||
default:
|
||||
throw new TypeException("Array or string required");
|
||||
throw typeException(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ public final class UseStatement extends InterruptableNode implements Statement {
|
||||
final Module module = (Module) Class.forName(String.format(PACKAGE, name, name)).newInstance();
|
||||
module.init();
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
throw new RuntimeException("Unable to load module " + name, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,10 +61,15 @@ public final class UseStatement extends InterruptableNode implements Statement {
|
||||
loadConstants(value.asString());
|
||||
break;
|
||||
default:
|
||||
throw new TypeException("Array or string required");
|
||||
throw typeException(value);
|
||||
}
|
||||
}
|
||||
|
||||
private TypeException typeException(Value value) {
|
||||
return new TypeException("Array or string required in 'use' statement, " +
|
||||
"got " + Types.typeToString(value.type()) + " " + value);
|
||||
}
|
||||
|
||||
private void loadConstants(String moduleName) {
|
||||
try {
|
||||
final Class<?> moduleClass = Class.forName(String.format(PACKAGE, moduleName, moduleName));
|
||||
|
Loading…
Reference in New Issue
Block a user