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

Last change on this file since 353 was 346, checked in by wouter, 2 years ago

version 0.1 of automatic java to python translator. Can translate some simple programs, lot of work remains to be done

File size: 1.8 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 /**
22 *
23 * @param type
24 * @return true if given type is primitive type. Note that "void" is also
25 * supported as if it were a primitive type. This is because there
26 * is not a "void" class and we need a Stub for the void type
27 */
28 static public boolean isPrimitive(String type) {
29 return primitivetypes.contains(type);
30 }
31
32 /**
33 *
34 * @param primitive "int","char", etc
35 */
36 public StubPrimitive(String primitive) {
37 this.type = primitive;
38 getName(); // check if type is legla
39 }
40
41 @Override
42 public ExpressionLine call(MethodCallExpr callexp) {
43 throw new IllegalArgumentException(
44 "Not implemented " + this.getClass() + callexp);
45 }
46
47 @Override
48 public String getName() {
49 switch (type) {
50 case "int":
51 return "int";
52 case "boolean":
53 return "bool";
54 case "char":
55 return "str"; // should we?
56 case "long":
57 return "int";
58 case "float":
59 return "float";
60 case "double":
61 return "float";
62 case "short":
63 return "int";
64 case "void":
65 return "None"; // PEP-484: None is considered equivalent to
66 // type(None)
67
68 }
69 throw new IllegalArgumentException("Unknown type " + type);
70 }
71
72 @Override
73 public ExpressionLine field(FieldAccessExpr node) {
74 throw new IllegalArgumentException(
75 "primitive " + type + "has no field");
76 }
77
78 @Override
79 public String getImport() {
80 return null;
81 }
82
83}
Note: See TracBrowser for help on using the repository browser.