private Expression createExpression(Element element) { String kind = element.getAttribute("kind"); if (kind.equals("Unary")) { UnaryExpression expr = IrFactory.eINSTANCE.createUnaryExpression(); expr.setId(element.getAttribute("id")); doAnnotations(expr, element); expr.setContext((Scope) findIrObject(element.getAttribute("context-scope"))); expr.setOperator(UtilIR.unmarshall(element.getAttribute("operator"))); expr.setOperand(createExpression(getChild(element, "Expr"))); // expr.setType(createType(getChild(element, "Type"))); return expr; } else if (kind.equals("Binary")) { BinaryExpression expr = IrFactory.eINSTANCE.createBinaryExpression(); expr.setId(element.getAttribute("id")); doAnnotations(expr, element); expr.setContext((Scope) findIrObject(element.getAttribute("context-scope"))); expr.setOperator(UtilIR.unmarshall(element.getAttribute("operator"))); List<Element> operands = getChildren(element, "Expr"); expr.setOperand1(createExpression(operands.get(0))); expr.setOperand2(createExpression(operands.get(1))); // expr.setType(createType(getChild(element, "Type"))); return expr; } else if (kind.equals("Call")) { FunctionCall expr = IrFactory.eINSTANCE.createFunctionCall(); expr.setId(element.getAttribute("id")); doAnnotations(expr, element); expr.setContext((Scope) findIrObject(element.getAttribute("context-scope"))); expr.setFunction(createExpression(getChild(element, "Expr"))); List<Element> args = getChildren(getChild(element, "Args"), "Expr"); for (Element arg : args) { expr.getParameters().add(createExpression(arg)); } // expr.setType(createType(getChild(element, "Type"))); return expr; } else if (kind.equals("Construction")) { TypeConstructorCall expr = IrFactory.eINSTANCE.createTypeConstructorCall(); expr.setId(element.getAttribute("id")); doAnnotations(expr, element); expr.setName(element.getAttribute("name")); expr.setContext((Scope) findIrObject(element.getAttribute("context-scope"))); expr.setTypedef((Declaration) findIrObject(element.getAttribute("typedef-id"))); List<Element> args = getChildren(getChild(element, "Args"), "Expr"); for (Element arg : args) { expr.getParameters().add(createExpression(arg)); } // expr.setType(createType(getChild(element, "Type"))); return expr; } else if (kind.equals("If")) { IfExpression expr = IrFactory.eINSTANCE.createIfExpression(); expr.setId(element.getAttribute("id")); doAnnotations(expr, element); expr.setContext((Scope) findIrObject(element.getAttribute("context-scope"))); List<Element> exprs = getChildren(element, "Expr"); expr.setCondition(createExpression(exprs.get(0))); expr.setThenExpression(createExpression(exprs.get(1))); expr.setElseExpression(createExpression(exprs.get(2))); // expr.setType(createType(getChild(element, "Type"))); return expr; } else if (kind.equals("Var")) { VariableExpression expr = IrFactory.eINSTANCE.createVariableExpression(); expr.setId(element.getAttribute("id")); doAnnotations(expr, element); Element child = getChild(element, "Type"); if (child != null) { expr.setType(createType(child)); } expr.setContext((Scope) findIrObject(element.getAttribute("context-scope"))); expr.setVariable((Declaration) findIrObject(element.getAttribute("decl-id"))); Element indices = getChild(element, "Indices"); if (indices != null) { for (Element index : getChildren(indices, "Expr")) { expr.getIndex().add(createExpression(index)); } } Element members = getChild(element, "Members"); if (members != null) { for (Element member : getChildren(members, "Member")) { expr.getMember().add(createMember(member)); } } return expr; } else if (kind.equals("Lambda")) { LambdaExpression expr = IrFactory.eINSTANCE.createLambdaExpression(); String id = element.getAttribute("id"); expr.setId(id); doAnnotations(expr, element); addIrObject(id, expr); expr.setContext((Scope) findIrObject(element.getAttribute("context-scope"))); expr.setOuter((Scope) findIrObject(element.getAttribute("outer-scope"))); expr.setType(createType(getChild(element, "Type"))); List<Element> declarations = getChildren(element, "Decl"); for (Element e : declarations) { Declaration var = (Declaration) createDeclaration(e); expr.getDeclarations().add(var); if (var instanceof Variable && ((Variable) var).isParameter()) expr.getParameters().add((Variable) var); } expr.setBody(createExpression(getChild(element, "Expr"))); return expr; } else if (kind.equals("Proc")) { ProcExpression expr = IrFactory.eINSTANCE.createProcExpression(); String id = element.getAttribute("id"); expr.setId(id); doAnnotations(expr, element); addIrObject(id, expr); expr.setContext((Scope) findIrObject(element.getAttribute("context-scope"))); expr.setOuter((Scope) findIrObject(element.getAttribute("outer-scope"))); expr.setType(createType(getChild(element, "Type"))); List<Element> declarations = getChildren(element, "Decl"); for (Element e : declarations) { Declaration var = (Declaration) createDeclaration(e); expr.getDeclarations().add(var); if (var instanceof Variable && ((Variable) var).isParameter()) expr.getParameters().add((Variable) var); } expr.setBody((Block) createStatement(getChild(element, "Stmt"))); return expr; } else if (kind.equals("List")) { ListExpression expr = IrFactory.eINSTANCE.createListExpression(); expr.setId(element.getAttribute("id")); doAnnotations(expr, element); expr.setContext((Scope) findIrObject(element.getAttribute("context-scope"))); List<Element> generators = getChildren(element, "Generator"); for (Element g : generators) { expr.getGenerators().add(createGenerator(g)); } List<Element> exprs = getChildren(element, "Expr"); for (Element e : exprs) { expr.getExpressions().add(createExpression(e)); } // expr.setType(createType(getChild(element, "Type"))); return expr; } else if (kind.equals("literal-integer")) { IntegerLiteral expr = IrFactory.eINSTANCE.createIntegerLiteral(); String value = element.getAttribute("value"); expr.setValue(Long.parseLong(value)); expr.setType(TypeSystem.createTypeInt()); return expr; } else if (kind.equals("literal-float")) { FloatLiteral expr = IrFactory.eINSTANCE.createFloatLiteral(); String value = element.getAttribute("value"); expr.setValue(Double.parseDouble(value)); expr.setType(TypeSystem.createTypeFloat()); return expr; } else if (kind.equals("literal-bool")) { BooleanLiteral expr = IrFactory.eINSTANCE.createBooleanLiteral(); String value = element.getAttribute("value"); expr.setValue(Boolean.parseBoolean(value)); expr.setType(TypeSystem.createTypeBool()); return expr; } else if (kind.equals("literal-string")) { StringLiteral expr = IrFactory.eINSTANCE.createStringLiteral(); String value = element.getAttribute("value"); expr.setValue(value); expr.setType(TypeSystem.createTypeString()); return expr; } assert (false); return null; }
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; }