EOS 2  1.1.0
Einfache Objektbasierte Sprache
UserType.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.baseparser;
2 
3 import java.util.ArrayList;
4 import java.util.LinkedList;
5 import java.util.TreeMap;
6 
7 import de.lathanda.eos.baseparser.exceptions.CyclicStorageException;
8 import de.lathanda.eos.baseparser.exceptions.DoubleClassDeclarationException;
9 import de.lathanda.eos.vm.Command;
10 import de.lathanda.eos.vm.MClass;
11 import de.lathanda.eos.vm.MProcedure;
12 import de.lathanda.eos.vm.MType;
13 import de.lathanda.eos.vm.ReservedVariables;
14 import de.lathanda.eos.vm.commands.DeclareVariable;
15 import de.lathanda.eos.vm.commands.StoreVariable;
22 public class UserType extends Type {
23  private String supCls = null;
24  private TreeMap<String, Method> usermethods = new TreeMap<>();
25  private TreeMap<String, Property> userproperties = new TreeMap<>();
26  private boolean isAbstract = false;
27  private MClass mtype;
28  private String name;
32  private boolean checked = false;
33  private boolean marked = false;
34  private boolean isUndefined = true;
35 
36  public UserType(String name) {
37  super(name);
38  this.name = name;
39  mtype = new MClass(id);
40  }
41 
42  public void setSuperclass(String sup) {
43  this.supCls = sup;
44  }
45 
46  public void bind(Environment env) {
47  if (superType == null && supCls != null) {
48  superType = env.getProgram().getTypeByName(supCls);
49  if (superType instanceof UserType) {
50  ((UserType) superType).bind(env);
51  }
52  mtype.setSuper(superType.getMType());
53  }
54  for (Method s : usermethods.values()) {
55  s.createMethodType(env);
56  super.registerMethod(s.getMethodType(env));
57  }
58  }
59 
61  if (checked)
62  return;
63  checkCyclicStorageInternal();
64  }
65 
66  private void checkCyclicStorageInternal() throws CyclicStorageException {
67  if (marked) {
68  throw new CyclicStorageException(id);
69  }
70  marked = true;
71  checkCyclicStorageInternal(superType);
72  for (Property prop : userproperties.values()) {
73  checkCyclicStorageInternal(prop.getPropertyType());
74  }
75  checked = true;
76 
77  }
78 
79  private void checkCyclicStorageInternal(Type t) throws CyclicStorageException {
80  if (t != null && t instanceof UserType) {
81  ((UserType) t).checkCyclicStorage();
82  }
83  }
84 
85  @Override
86  public LinkedList<AutoCompleteInformation> getAutoCompletes() {
87  LinkedList<AutoCompleteInformation> aci = (supCls == null) ? new LinkedList<>() : superType.getAutoCompletes();
88  for (Property p : userproperties.values()) {
89  aci.addAll(p.getAutoCompletes());
90  }
91  for (Method m : usermethods.values()) {
92  aci.add(m.getAutoComplete());
93  }
94  return aci;
95  }
96 
97  @Override
98  public MType getMType() {
99  return mtype;
100  }
101 
102  @Override
103  public boolean isAbstract() {
104  return isAbstract;
105  }
106 
107  @Override
108  public boolean isUnknown() {
109  return isUndefined;
110  }
111 
112  public void addProperty(Property prop) {
113  for (String name : prop.getNames()) {
114  userproperties.put(name, prop);
115  }
116  }
117 
118  public void addMethod(Method meth) {
119  usermethods.put(meth.getName(), meth);
120  }
121 
122  public MClass getMClass() {
123  return mtype;
124  }
125 
126  public void compile() throws Exception {
127  // generate properties
128  for (Property p : userproperties.values()) {
129  for (String name : p.getNames()) {
130  mtype.addProperty(name, p.getPropertyType().getMType());
131  }
132  }
133  // generate methods
134  for (Method m : usermethods.values()) {
135  ArrayList<Command> methCmd = new ArrayList<>();
136  // store self
137  methCmd.add(new DeclareVariable(ReservedVariables.SELF, mtype));
138  methCmd.add(new StoreVariable(ReservedVariables.SELF));
139  // compile method
140  m.compile(methCmd, false);
141  MProcedure meth = new MProcedure(methCmd, false);
142  mtype.addMethod(Signatures.createVMMethodSignature(m.getMethodType()), meth);
143  }
144  }
145 
146  @Override
147  public MethodType getReadProperty(String name) {
148  if (userproperties.containsKey(name)) {
149  return new UserReadProperty(this, name, userproperties.get(name));
150  } else if (superType != null) {
151  return superType.getReadProperty(name);
152  } else {
153  return null;
154  }
155  }
156 
157  @Override
158  public MethodType getAssignProperty(String name) {
159  if (userproperties.containsKey(name)) {
160  return new UserAssignProperty(this, name, userproperties.get(name));
161  } else if (superType != null) {
162  return superType.getAssignProperty(name);
163  } else {
164  return null;
165  }
166  }
167 
168  public void define() {
169  if (isUndefined) {
170  isUndefined = false;
171  } else {
172  throw new DoubleClassDeclarationException(id);
173  }
174  }
175 
176  @Override
177  public TreeMap<String, MethodType> getAllReadProperties() {
178  TreeMap<String, MethodType> all = new TreeMap<>();
179  for (var entry : userproperties.entrySet()) {
180  all.put(entry.getKey(), new UserReadProperty(this, entry.getKey(), entry.getValue()));
181  }
182  return all;
183  }
184 
185  @Override
186  public TreeMap<String, MethodType> getAllAssignProperties() {
187  TreeMap<String, MethodType> all = new TreeMap<>();
188  for (var entry : userproperties.entrySet()) {
189  all.put(entry.getKey(), new UserAssignProperty(this, entry.getKey(), entry.getValue()));
190  }
191  return all;
192  }
193 
194  @Override
195  public LinkedList<PropertyViewer> getPropertyViewers(Object target) {
196  LinkedList<PropertyViewer> list = new LinkedList<>();
197  for (String name : userproperties.keySet()) {
198  list.add(new PropertyViewer.UserClassPropertyViewer(name, this));
199  }
200 
201  return list;
202  }
203 
204  public String getLabel() {
205  return name;
206  }
207 }
Type getTypeByName(String name)
Definition: Program.java:303
static String createVMMethodSignature(MethodType m)
Definition: Signatures.java:27
abstract MethodType getAssignProperty(String name)
LinkedList< AutoCompleteInformation > getAutoCompletes()
Definition: Type.java:184
abstract MethodType getReadProperty(String name)
LinkedList< AutoCompleteInformation > getAutoCompletes()
Definition: UserType.java:86
MethodType getReadProperty(String name)
Definition: UserType.java:147
MethodType getAssignProperty(String name)
Definition: UserType.java:158
void addProperty(Property prop)
Definition: UserType.java:112
TreeMap< String, MethodType > getAllAssignProperties()
Definition: UserType.java:186
TreeMap< String, MethodType > getAllReadProperties()
Definition: UserType.java:177
LinkedList< PropertyViewer > getPropertyViewers(Object target)
Definition: UserType.java:195
void bind(Environment env)
Definition: UserType.java:46
void addProperty(String signature, MType t)
Definition: MClass.java:41
void setSuper(MType sup)
Definition: MClass.java:96
void addMethod(String signature, MProcedure m)
Definition: MClass.java:33
Impressum