|
|
@@ -1,20 +1,27 @@
|
|
|
package com.example.lambda.service.web;
|
|
|
|
|
|
-import com.example.lambda.service.interpreter.Ast;
|
|
|
+import com.example.lambda.service.interpreter.type.Abstract;
|
|
|
+import com.example.lambda.service.interpreter.type.Application;
|
|
|
+import com.example.lambda.service.interpreter.type.Ast;
|
|
|
import com.example.lambda.service.interpreter.InterpreterState;
|
|
|
import com.example.lambda.service.interpreter.LambdaInterpreter;
|
|
|
import com.example.lambda.service.interpreter.Result;
|
|
|
+import com.example.lambda.service.interpreter.type.Atom;
|
|
|
import com.example.lambda.service.web.entity.*;
|
|
|
+import com.example.lambda.service.web.type.IRType;
|
|
|
+import com.example.lambda.service.web.type.IType;
|
|
|
+import com.example.lambda.service.web.type.SourceType;
|
|
|
+import com.example.lambda.service.web.type.TargetType;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
public class LambdaService {
|
|
|
|
|
|
+ public static final String PATTERN_SOURCE_TYPE_UNKNOWN = "无法解析参数类型";
|
|
|
+
|
|
|
@Autowired
|
|
|
private LambdaInterpreter interpreter;
|
|
|
|
|
|
@@ -22,47 +29,79 @@ public class LambdaService {
|
|
|
return interpreter != null ? "Interpreter is available(" + interpreter + ")" : "Interpreter is unavailable";
|
|
|
}
|
|
|
|
|
|
+ private List<TypeInfo> createTypeInfos(IType[] types) {
|
|
|
+ List<TypeInfo> infos = new ArrayList<>();
|
|
|
+ for (IType type : types) {
|
|
|
+ infos.add(new TypeInfo(type));
|
|
|
+ }
|
|
|
+ return infos;
|
|
|
+ }
|
|
|
+
|
|
|
public ServiceInfo info() {
|
|
|
ServiceInfo info = new ServiceInfo();
|
|
|
- // pattern samples
|
|
|
- List<PatternData> patternSamples = new ArrayList<>();
|
|
|
- for (PatternTargetType target : PatternTargetType.values()) {
|
|
|
- PatternData sample = new PatternData();
|
|
|
- sample.setTarget(target);
|
|
|
- sample.setData("An lambda expression");
|
|
|
- patternSamples.add(sample);
|
|
|
- }
|
|
|
- // render samples
|
|
|
- List<RenderData> renderSamples = new ArrayList<>();
|
|
|
+ // add types
|
|
|
+ info.setSourceTypes(createTypeInfos(SourceType.values()));
|
|
|
+ info.setIrTypes(createTypeInfos(IRType.values()));
|
|
|
+ info.setTargetTypes(createTypeInfos(TargetType.values()));
|
|
|
+
|
|
|
+ // pattern options
|
|
|
+ List<TypeMap<SourceType, IRType>> patternOptions = new ArrayList<>();
|
|
|
+ patternOptions.add(new TypeMap<>(SourceType.PATTERN, IRType.ATOM));
|
|
|
+ patternOptions.add(new TypeMap<>(SourceType.PATTERN, IRType.AST));
|
|
|
+ info.setPatternOptions(patternOptions);
|
|
|
+
|
|
|
+ // render options
|
|
|
+ List<TypeMap<IRType, TargetType>> renderOptions = new ArrayList<>();
|
|
|
+ renderOptions.add(new TypeMap<>(IRType.ATOM, TargetType.ATOM));
|
|
|
+ renderOptions.add(new TypeMap<>(IRType.AST, TargetType.AST));
|
|
|
+ info.setRenderOptions(renderOptions);
|
|
|
|
|
|
- info.setPatternSamples(patternSamples);
|
|
|
- info.setRenderSamples(renderSamples);
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
- public ServiceResponse pattern(PatternTargetType type, String pattern) {
|
|
|
+ public ServiceResponse pattern(PatternParam param) {
|
|
|
+ if (param.getType() != SourceType.PATTERN) {
|
|
|
+ // 参数类型不存在
|
|
|
+ return ServiceResponse.error(PATTERN_SOURCE_TYPE_UNKNOWN);
|
|
|
+ }
|
|
|
+ String pattern = param.getData();
|
|
|
try {
|
|
|
- Result res = interpreter.solveSuccessStub(pattern);
|
|
|
+ Result res = interpreter.solve(pattern);
|
|
|
// Result res = interpreter.solveErrorStub(pattern);
|
|
|
if (res.getState() == InterpreterState.ERROR) {
|
|
|
+ // 解析失败
|
|
|
return ServiceResponse.error(res.getMsg());
|
|
|
}
|
|
|
- Object target = null;
|
|
|
- switch (type) {
|
|
|
+ switch (param.getTarget()) {
|
|
|
case ATOM:
|
|
|
- target = extractAtoms(res.getAst());
|
|
|
- break;
|
|
|
+ List<Object> atoms = extractAtoms(res.getAst());
|
|
|
+ return ServiceResponse.success(atoms);
|
|
|
+ case AST:
|
|
|
+ default:
|
|
|
+ return ServiceResponse.success(res.getAst());
|
|
|
}
|
|
|
- return ServiceResponse.success(res.getAst());
|
|
|
} catch (Exception e) {
|
|
|
return ServiceResponse.error("interpreter error");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private List<String> extractAtoms(Ast ast) {
|
|
|
- List<String> atoms = new ArrayList<>();
|
|
|
-
|
|
|
- return atoms;
|
|
|
+ private List<Object> extractAtoms(Ast ast) {
|
|
|
+ Set<Atom> atoms = new HashSet<>();
|
|
|
+ Queue<Ast> Q = new LinkedList<>();
|
|
|
+ Q.add(ast);
|
|
|
+ while (Q.size() > 0) {
|
|
|
+ Ast a = Q.poll();
|
|
|
+ if (a instanceof Atom) {
|
|
|
+ atoms.add((Atom) a);
|
|
|
+ } else if (a instanceof Application) {
|
|
|
+ Q.offer(((Application) a).getLeft());
|
|
|
+ Q.offer(((Application) a).getRight());
|
|
|
+ } else if (a instanceof Abstract) {
|
|
|
+ atoms.add(((Abstract) a).getParam());
|
|
|
+ Q.add(((Abstract) a).getSub());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new ArrayList<>(atoms);
|
|
|
}
|
|
|
|
|
|
}
|