Exemplo n.º 1
0
  private Namespace createNamespace(Element element) {
    Namespace namespace = IrFactory.eINSTANCE.createNamespace();
    String id = element.getAttribute("id");
    namespace.setId(id);
    doAnnotations(namespace, element);

    for (String s : Util.unpackQualifiedName(element.getAttribute("name"))) {
      namespace.getName().add(s);
    }

    addIrObject(id, namespace);

    List<Element> declarations = getChildren(element, "Decl");
    for (Element e : declarations) {
      Declaration var = createDeclaration(e);
      namespace.getDeclarations().add(var);
    }

    return namespace;
  }
Exemplo n.º 2
0
  private Declaration createDeclaration(Element element) {
    String kind = element.getAttribute("kind");
    if (kind.equals("Variable")) {
      Variable variable = IrFactory.eINSTANCE.createVariable();
      variable.setName(element.getAttribute("name"));
      String id = element.getAttribute("id");
      variable.setId(id);
      doAnnotations(variable, element);

      addIrObject(id, variable);

      variable.setScope((Scope) findIrObject(element.getAttribute("scope")));

      if (element.getAttribute("constant").equals("true")) {
        variable.setConstant(true);
      } else {
        variable.setConstant(false);
      }
      if (element.getAttribute("parameter").equals("true")) {
        variable.setParameter(true);
      } else {
        variable.setParameter(false);
      }

      Element typeElement = getChild(element, "Type");
      Type type = createType(typeElement);
      variable.setType(type);

      Element initialValueElement = getChild(element, "InitialValue");
      if (initialValueElement != null) {
        Expression initalValue = createExpression(getChild(initialValueElement, "Expr"));
        variable.setInitValue(initalValue);
      }

      return variable;
    } else if (kind.equals("VariableExternal")) {
      VariableExternal variableExternal = IrFactory.eINSTANCE.createVariableExternal();
      variableExternal.setName(element.getAttribute("name"));
      String id = element.getAttribute("id");
      variableExternal.setId(id);
      doAnnotations(variableExternal, element);

      addIrObject(id, variableExternal);

      variableExternal.setScope((Scope) findIrObject(element.getAttribute("scope")));
      Element typeElement = getChild(element, "Type");
      Type type = createType(typeElement);
      variableExternal.setType(type);

      return variableExternal;
    } else if (kind.equals("VariableImport")) {
      VariableImport variableImport = IrFactory.eINSTANCE.createVariableImport();
      variableImport.setName(element.getAttribute("name"));
      String id = element.getAttribute("id");
      variableImport.setId(id);
      doAnnotations(variableImport, element);

      addIrObject(id, variableImport);

      for (String s : Util.unpackQualifiedName(element.getAttribute("namespace"))) {
        variableImport.getNamespace().add(s);
      }

      return variableImport;
    } else if (kind.equals("TypeImport")) {
      TypeDeclarationImport typeImport = IrFactory.eINSTANCE.createTypeDeclarationImport();
      typeImport.setName(element.getAttribute("name"));
      String id = element.getAttribute("id");
      typeImport.setId(id);
      doAnnotations(typeImport, element);

      addIrObject(id, typeImport);

      for (String s : Util.unpackQualifiedName(element.getAttribute("namespace"))) {
        typeImport.getNamespace().add(s);
      }

      return typeImport;
    } else if (kind.equals("Forward")) {
      ForwardDeclaration forwardDeclaration = IrFactory.eINSTANCE.createForwardDeclaration();
      forwardDeclaration.setName(element.getAttribute("name"));
      String id = element.getAttribute("id");
      forwardDeclaration.setId(id);
      doAnnotations(forwardDeclaration, element);

      addIrObject(id, forwardDeclaration);

      forwardDeclaration.setScope((Scope) findIrObject(element.getAttribute("scope")));
      Element typeElement = getChild(element, "Type");
      Type type = createType(typeElement);
      forwardDeclaration.setType(type);

      // Since the declaration that the ForwardDeclaration
      // is pointing at does not yet exists. That value must
      // be set in a second pass after the whole actor/namespace
      // is evaluted. The id of the actual declaration is stored
      // temprary map.

      forwardDeclarationMap.put(forwardDeclaration, element.getAttribute("forward-id"));

      return forwardDeclaration;
    } else if (kind.equals("Type")) {
      TypeDeclaration typeDeclaration = IrFactory.eINSTANCE.createTypeDeclaration();
      String id = element.getAttribute("id");
      typeDeclaration.setId(id);
      doAnnotations(typeDeclaration, element);

      addIrObject(id, typeDeclaration);

      typeDeclaration.setName(element.getAttribute("name"));

      typeDeclaration.setScope((Scope) findIrObject(element.getAttribute("scope")));

      Element typeElement = getChild(element, "Type");
      Type type = createType(typeElement);
      typeDeclaration.setType(type);

      Element ctorElement = getChild(element, "TypeConstructor");
      TypeConstructor ctor = IrFactory.eINSTANCE.createTypeConstructor();
      ctor.setName(ctorElement.getAttribute("name"));
      ctor.setId(ctorElement.getAttribute("id"));
      doAnnotations(ctor, ctorElement);
      ctor.setScope((Scope) findIrObject(ctorElement.getAttribute("scope")));
      ctor.setTypedef(typeDeclaration);

      typeDeclaration.setConstructor(ctor);

      return typeDeclaration;
    }

    assert (false);
    return null;
  }
Exemplo n.º 3
0
  private Type createType(Element element) {
    String kind = element.getAttribute("kind");

    if (kind.equals("int")) {
      TypeInt type = TypeSystem.createTypeInt();
      List<Element> expressions = getChildren(element, "Expr");
      if (expressions.size() > 0) {
        Expression size = createExpression(expressions.get(0));
        type.setSize(size);
      }
      return type;
    } else if (kind.equals("uint")) {
      TypeUint type = TypeSystem.createTypeUint();
      List<Element> expressions = getChildren(element, "Expr");
      if (expressions.size() > 0) {
        Expression size = createExpression(expressions.get(0));
        type.setSize(size);
      }
      return type;
    } else if (kind.equals("bool")) {
      return TypeSystem.createTypeBool();
    } else if (kind.equals("float")) {
      return TypeSystem.createTypeFloat();
    } else if (kind.equals("string")) {
      return TypeSystem.createTypeString();
    } else if (kind.equals("List")) {
      Expression size = null;
      Element sizeElement = getChild(element, "Expr");
      if (sizeElement != null) {
        size = createExpression(sizeElement);
      }
      List<Element> types = getChildren(element, "Type");
      Type type = createType(types.get(0));
      return TypeSystem.createTypeList(size, type);
    } else if (kind.equals("user")) {
      Declaration typeDeclaration =
          (Declaration) findIrObject(element.getAttribute("type-declaration-id"));
      return TypeSystem.createTypeUser(typeDeclaration);
    } else if (kind.equals("record")) {
      TypeRecord typeRecord = IrFactory.eINSTANCE.createTypeRecord();
      String id = element.getAttribute("id");
      typeRecord.setId(id);
      doAnnotations(typeRecord, element);

      List<Element> members = getChildren(element, "Decl");
      for (Element member : members) {
        Variable m = (Variable) createDeclaration(member);
        typeRecord.getMembers().add(m);
      }

      addIrObject(id, typeRecord);

      return typeRecord;
    } else if (kind.equals("lambda")) {
      TypeLambda type = IrFactory.eINSTANCE.createTypeLambda();
      Element input = getChild(element, "Input");
      for (Element e : getChildren(input, "Type")) {
        type.getInputTypes().add(createType(e));
      }
      Element output = getChild(element, "Output");
      if (output != null) {
        type.setOutputType(createType(getChild(output, "Type")));
      }
      return type;
    } else if (kind.equals("proc")) {
      TypeProc type = IrFactory.eINSTANCE.createTypeProc();
      Element input = getChild(element, "Input");
      for (Element e : getChildren(input, "Type")) {
        type.getInputTypes().add(createType(e));
      }
      Element output = getChild(element, "Output");
      if (output != null) {
        for (Element e : getChildren(input, "Type")) {
          type.getOutputTypes().add(createType(e));
        }
      }
      return type;
    } else if (kind.equals("actor")) {
      String name = element.getAttribute("name");
      String namespace = element.getAttribute("namespace");
      return TypeSystem.createTypeActor(name, Util.unpackQualifiedName(namespace));
    } else if (kind.equals("undef")) {
      return TypeSystem.createTypeUndef();
    }

    assert (false);
    return null;
  }