EOS 2  1.1.0
Einfache Objektbasierte Sprache
MClass.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.vm;
2 
3 import java.util.Map.Entry;
4 
5 import de.lathanda.eos.vm.exceptions.TypeMissMatchException;
6 
7 import java.util.TreeMap;
8 
14 public class MClass implements MType {
15  private String id;
16  private TreeMap<String, MProcedure> methods;
17  private TreeMap<String, MType> properties;
18  private MType sup;
19  private boolean isAbstract;
20 
21  public MClass(String name) {
22  this(name, MJavaClass.BASE);
23  }
24 
25  public MClass(String id, MType sup) {
26  methods = new TreeMap<>();
27  properties = new TreeMap<>();
28  this.sup = sup;
29  this.id = id;
30  isAbstract = false;
31  }
32 
33  public void addMethod(String signature, MProcedure m) {
34  methods.put(signature, m);
35  }
36 
37  public MProcedure getMethod(String signature) {
38  return methods.get(signature.toString());
39  }
40 
41  public void addProperty(String signature, MType t) {
42  properties.put(signature, t);
43  }
44 
45  @Override
46  public Object checkAndCast(Object obj) {
47  if (obj instanceof MObject) {
48  MType objType = ((MObject) obj).getType();
49  while (objType instanceof MClass) {
50  if (objType == this) {
51  return obj;
52  }
53  objType = ((MClass) objType).sup;
54  }
55  throw new TypeMissMatchException(id, ((MObject) obj).getType().getID());
56  } else if (sup != null) {
57  return sup.checkAndCast(obj);
58  } else {
59  if (obj instanceof MObject) {
60  throw new TypeMissMatchException(id, ((MObject) obj).getType().getID());
61  } else {
62  throw new TypeMissMatchException(id, obj.getClass().toString());
63  }
64  }
65  }
66 
67  @Override
68  public String getID() {
69  return id;
70  }
71 
72  @Override
73  public boolean isAbstract() {
74  return isAbstract;
75  }
76 
77  @Override
78  public Object newInstance(Machine m) throws Exception {
79  return new MObject(this, m);
80  }
81 
82  @Override
83  public TreeMap<String, Variable> createProperties(Machine m) throws Exception {
84  TreeMap<String, Variable> prop = sup.createProperties(m);
85  for (Entry<String, MType> p : properties.entrySet()) {
86  String s = p.getKey();
87  prop.put(s, m.createInitVariable(p.getKey(), p.getValue()));
88  }
89  return prop;
90  }
91 
92  public Object createJavaObject(Machine m) throws Exception {
93  return sup.createJavaObject(m);
94  }
95 
96  public void setSuper(MType sup) {
97  this.sup = sup;
98  }
99 }
MProcedure getMethod(String signature)
Definition: MClass.java:37
void addProperty(String signature, MType t)
Definition: MClass.java:41
void setSuper(MType sup)
Definition: MClass.java:96
Object newInstance(Machine m)
Definition: MClass.java:78
MClass(String name)
Definition: MClass.java:21
MClass(String id, MType sup)
Definition: MClass.java:25
void addMethod(String signature, MProcedure m)
Definition: MClass.java:33
Object createJavaObject(Machine m)
Definition: MClass.java:92
TreeMap< String, Variable > createProperties(Machine m)
Definition: MClass.java:83
Object checkAndCast(Object obj)
Definition: MClass.java:46
static final MType BASE
Definition: MJavaClass.java:13
Object checkAndCast(Object obj)
Object createJavaObject(Machine m)
TreeMap< String, Variable > createProperties(Machine m)
Impressum