source: java2python/src/main/java/tudelft/utilities/j2p/t/StubPrimitive.java@ 363

Last change on this file since 363 was 362, checked in by wouter, 2 years ago

#110 transforming the Group/Block structure, getName now always returns ExpressionLine. Removed few TextBlocks in the translator.

File size: 2.1 KB
Line 
1package tudelft.utilities.j2p.t;
2
3import java.util.Arrays;
4import java.util.List;
5
6import com.github.javaparser.ast.expr.FieldAccessExpr;
7import com.github.javaparser.ast.expr.MethodCallExpr;
8
9import tudelft.utilities.j2p.formatting.ExpressionLine;
10
11/***
12 * Stub but for primitives (int, boolean, byte, char..)
13 */
14public class StubPrimitive implements J2P {
15
16 private static final List<String> primitivetypes = Arrays.asList("boolean",
17 "byte", "char", "float", "int", "long", "short", "double", "void");
18
19 private String type;
20
21 private ExpressionLine INTTYPE = new ExpressionLine("int");
22
23 private ExpressionLine BOOLTYPE = new ExpressionLine("bool");
24
25 private ExpressionLine CHARTYPE = new ExpressionLine("str");
26
27 private ExpressionLine FLOATTYPE = new ExpressionLine("float");
28
29 // PEP-484: None is considered equivalent to type(None)
30 private ExpressionLine VOIDTYPE = new ExpressionLine("None");
31
32 /**
33 *
34 * @param type
35 * @return true if given type is primitive type. Note that "void" is also
36 * supported as if it were a primitive type. This is because there
37 * is not a "void" class and we need a Stub for the void type
38 */
39 static public boolean isPrimitive(String type) {
40 return primitivetypes.contains(type);
41 }
42
43 /**
44 *
45 * @param primitive "int","char", etc
46 */
47 public StubPrimitive(String primitive) {
48 this.type = primitive;
49 getName(); // check if type is legla
50 }
51
52 @Override
53 public ExpressionLine call(MethodCallExpr callexp) {
54 throw new IllegalArgumentException(
55 "Not implemented " + this.getClass() + callexp);
56 }
57
58 @Override
59 public ExpressionLine getName() {
60 switch (type) {
61 case "int":
62 return INTTYPE;
63 case "boolean":
64 return BOOLTYPE;
65 case "char":
66 return CHARTYPE; // should we?
67 case "long":
68 return INTTYPE;
69 case "float":
70 return FLOATTYPE;
71 case "double":
72 return FLOATTYPE;
73 case "short":
74 return INTTYPE;
75 case "void":
76 return VOIDTYPE;
77
78 }
79 throw new IllegalArgumentException("Unknown type " + type);
80 }
81
82 @Override
83 public ExpressionLine field(FieldAccessExpr node) {
84 throw new IllegalArgumentException(
85 "primitive " + type + "has no field");
86 }
87
88}
Note: See TracBrowser for help on using the repository browser.