Assertion failure fixed
This commit is contained in:
@@ -32,6 +32,7 @@ public class JavaDescriptorResolver {
|
|||||||
|
|
||||||
protected final Map<String, ClassDescriptor> classDescriptorCache = new HashMap<String, ClassDescriptor>();
|
protected final Map<String, ClassDescriptor> classDescriptorCache = new HashMap<String, ClassDescriptor>();
|
||||||
protected final Map<PsiTypeParameter, TypeParameterDescriptor> typeParameterDescriptorCache = Maps.newHashMap();
|
protected final Map<PsiTypeParameter, TypeParameterDescriptor> typeParameterDescriptorCache = Maps.newHashMap();
|
||||||
|
protected final Map<PsiMethod, FunctionDescriptor> methodDescriptorCache = Maps.newHashMap();
|
||||||
protected final Map<String, NamespaceDescriptor> namespaceDescriptorCache = new HashMap<String, NamespaceDescriptor>();
|
protected final Map<String, NamespaceDescriptor> namespaceDescriptorCache = new HashMap<String, NamespaceDescriptor>();
|
||||||
protected final JavaPsiFacade javaFacade;
|
protected final JavaPsiFacade javaFacade;
|
||||||
protected final GlobalSearchScope javaSearchScope;
|
protected final GlobalSearchScope javaSearchScope;
|
||||||
@@ -247,28 +248,34 @@ public class JavaDescriptorResolver {
|
|||||||
WritableFunctionGroup writableFunctionGroup = new WritableFunctionGroup(methodName);
|
WritableFunctionGroup writableFunctionGroup = new WritableFunctionGroup(methodName);
|
||||||
final Collection<HierarchicalMethodSignature> signatures = psiClass.getVisibleSignatures();
|
final Collection<HierarchicalMethodSignature> signatures = psiClass.getVisibleSignatures();
|
||||||
for (HierarchicalMethodSignature signature: signatures) {
|
for (HierarchicalMethodSignature signature: signatures) {
|
||||||
final PsiMethod method = signature.getMethod();
|
PsiMethod method = signature.getMethod();
|
||||||
if (method.hasModifierProperty(PsiModifier.STATIC) != staticMembers) {
|
if (method.hasModifierProperty(PsiModifier.STATIC) != staticMembers) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!methodName.equals(method.getName())) {
|
if (!methodName.equals(method.getName())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
final PsiParameter[] parameters = method.getParameterList().getParameters();
|
FunctionDescriptor functionDescriptor = methodDescriptorCache.get(method);
|
||||||
|
if (functionDescriptor != null) {
|
||||||
|
writableFunctionGroup.addFunction(functionDescriptor);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
FunctionDescriptorImpl functionDescriptor = new FunctionDescriptorImpl(
|
PsiParameter[] parameters = method.getParameterList().getParameters();
|
||||||
|
FunctionDescriptorImpl functionDescriptorImpl = new FunctionDescriptorImpl(
|
||||||
JavaDescriptorResolver.JAVA_ROOT,
|
JavaDescriptorResolver.JAVA_ROOT,
|
||||||
Collections.<Annotation>emptyList(), // TODO
|
Collections.<Annotation>emptyList(), // TODO
|
||||||
methodName
|
methodName
|
||||||
);
|
);
|
||||||
functionDescriptor.initialize(
|
functionDescriptorImpl.initialize(
|
||||||
null,
|
null,
|
||||||
resolveTypeParameters(method.getTypeParameters()),
|
resolveTypeParameters(method.getTypeParameters()),
|
||||||
semanticServices.getDescriptorResolver().resolveParameterDescriptors(functionDescriptor, parameters),
|
semanticServices.getDescriptorResolver().resolveParameterDescriptors(functionDescriptorImpl, parameters),
|
||||||
semanticServices.getTypeTransformer().transformToType(method.getReturnType())
|
semanticServices.getTypeTransformer().transformToType(method.getReturnType())
|
||||||
);
|
);
|
||||||
semanticServices.getTrace().recordDeclarationResolution(method, functionDescriptor);
|
semanticServices.getTrace().recordDeclarationResolution(method, functionDescriptorImpl);
|
||||||
writableFunctionGroup.addFunction(functionDescriptor);
|
writableFunctionGroup.addFunction(functionDescriptorImpl);
|
||||||
|
methodDescriptorCache.put(method, functionDescriptorImpl);
|
||||||
}
|
}
|
||||||
return writableFunctionGroup;
|
return writableFunctionGroup;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import java.*
|
||||||
|
import util.*
|
||||||
|
|
||||||
|
import java.io.*
|
||||||
|
|
||||||
|
fun takeFirst(expr: StringBuilder): Char {
|
||||||
|
val c = expr.charAt(0)
|
||||||
|
expr.deleteCharAt(0)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
fun evaluateArg(expr: AbstractStringBuilder, numbers: ArrayList<Int>): Int {
|
||||||
|
if (expr.length() == 0) throw new Exception("Syntax error: Character expected");
|
||||||
|
val c = takeFirst<error>(expr)</error>
|
||||||
|
if (c <error>>=</error> '0' && c <error><=</error> '9') {
|
||||||
|
val n = c - '0'
|
||||||
|
if (!numbers.contains(n)) throw new Exception("You used incorrect number: " + n)
|
||||||
|
numbers.remove(n)
|
||||||
|
return n \
|
||||||
|
}
|
||||||
|
throw new Exception("Syntax error: Unrecognized character " + c)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun evaluateAdd(expr: StringBuilder, numbers: ArrayList<Int>): Int {
|
||||||
|
val lhs = evaluateArg(expr, numbers)
|
||||||
|
if (expr.length() > 0) {
|
||||||
|
|
||||||
|
}
|
||||||
|
return lhs
|
||||||
|
}
|
||||||
|
|
||||||
|
fun evaluate(expr: StringBuilder, numbers: ArrayList<Int>): Int {
|
||||||
|
val lhs = evaluateAdd(expr, numbers)
|
||||||
|
if (expr.length() > 0) {
|
||||||
|
val c = expr.charAt(0)
|
||||||
|
expr.deleteCharAt(0)
|
||||||
|
}
|
||||||
|
return lhs
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
System.out?.println("24 game")
|
||||||
|
val numbers = new ArrayList<Int>(4)
|
||||||
|
val rnd = new Random();
|
||||||
|
val prompt = new StringBuilder()
|
||||||
|
for(val i in 0..3) {
|
||||||
|
val n = rnd.nextInt(9) + 1
|
||||||
|
numbers.add(n)
|
||||||
|
if (i > 0) prompt.append(" ");
|
||||||
|
prompt.append(n)
|
||||||
|
}
|
||||||
|
System.out?.println("Your numbers: " + prompt)
|
||||||
|
System.out?.println("Enter your expression:")
|
||||||
|
val reader = new BufferedReader(new InputStreamReader(System.`in`))
|
||||||
|
val expr = new StringBuilder(reader.readLine())
|
||||||
|
try {
|
||||||
|
val result = evaluate(expr, numbers)
|
||||||
|
if (result != 24)
|
||||||
|
System.out?.println("Sorry, that's " + result)
|
||||||
|
else
|
||||||
|
System.out?.println("You won!");
|
||||||
|
}
|
||||||
|
catch(e: Throwable) {
|
||||||
|
System.out?.println(e.getMessage())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user