needsUnderscore = Arrays.asList("and", "or", "xor", "in");
+ if (needsUnderscore.contains(operator)) {
+ underscore = "_";
+ }
+ Expression self = (Expression) visit(left);
+ FunctionCall operatorCall =
+ new FunctionCall(position, new ResolvableIdentifier("operator" + underscore + operator),
+ Arrays.asList((Expression) visit(right)));
+ return new MemberAccess(position, self, operatorCall);
+ }
+
+ private CastExpression visitCastExpression(ExpressionContext ctx) {
+ return new CastExpression(position(ctx.getStart()), (Expression) visit(ctx.expr), new ResolvableIdentifier(
+ getText(ctx.ClassIdentifier())));
+ }
+
+ private IsExpression visitIsExpression(ExpressionContext ctx) {
+ return new IsExpression(position(ctx.getStart()), (Expression) visit(ctx.expr), new ResolvableIdentifier(
+ getText(ctx.ClassIdentifier())));
+ }
+
+ protected ASTNode aggregateResult(ASTNode aggregate, ASTNode nextResult) {
+ return nextResult == null ? aggregate : nextResult;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/ASTNode.java b/src/main/java/de/uni/bremen/monty/moco/ast/ASTNode.java
new file mode 100644
index 0000000..1ebb824
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/ASTNode.java
@@ -0,0 +1,77 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public interface ASTNode {
+
+ ASTNode getParentNode();
+
+ void setParentNode(ASTNode parentNode);
+
+ Position getPosition();
+
+ void setScope(Scope scope);
+
+ Scope getScope();
+
+ /** Visit this node using double-dispatch.
+ *
+ * This method is only called from within the BaseVisitor. Every actual subclass must implement this method with the
+ * following body:
+ *
+ *
+ * {@code visitor.visit(this);}
+ *
.
+ *
+ * @param visitor
+ * the visitor to visit this node */
+ void visit(BaseVisitor visitor);
+
+ /** Comfort method to visit all children.
+ *
+ * Can be used from any visitor if the traversal order does not matter. The BaseVisitor uses this method by default. @
+ * param visitor the visitor to viit this node
+ *
+ * @param visitor
+ * the visitor to visit this node */
+ void visitChildren(BaseVisitor visitor);
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/AccessModifier.java b/src/main/java/de/uni/bremen/monty/moco/ast/AccessModifier.java
new file mode 100644
index 0000000..2341483
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/AccessModifier.java
@@ -0,0 +1,62 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+public enum AccessModifier {
+
+ PUBLIC, PRIVATE, PROTECTED, PACKAGE;
+
+ public static AccessModifier stringToAccess(String AccessStr) {
+
+ switch (AccessStr) {
+
+ case "+":
+ return AccessModifier.PUBLIC;
+ case "-":
+ return AccessModifier.PRIVATE;
+ case "#":
+ return AccessModifier.PROTECTED;
+ case "~":
+ return AccessModifier.PACKAGE;
+ default:
+ return AccessModifier.PUBLIC;
+ }
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/AntlrAdapter.java b/src/main/java/de/uni/bremen/monty/moco/ast/AntlrAdapter.java
new file mode 100644
index 0000000..370340d
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/AntlrAdapter.java
@@ -0,0 +1,78 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+
+package de.uni.bremen.monty.moco.ast;
+
+import de.uni.bremen.monty.moco.antlr.MontyLexer;
+import de.uni.bremen.monty.moco.antlr.MontyParser;
+import de.uni.bremen.monty.moco.ast.declaration.ModuleDeclaration;
+import org.antlr.v4.runtime.ANTLRInputStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.SequenceInputStream;
+
+public class AntlrAdapter {
+
+ public MontyParser createParser(final InputStream file) throws IOException {
+
+ InputStream in = createInputStream(file);
+
+ // the additional line-break is needed because of our indentation rule
+ // and the fact that a statement should be terminated by a line break
+ ANTLRInputStream input = new ANTLRInputStream(in);
+ MontyLexer lexer = new MontyLexer(input);
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ return new MontyParser(tokens);
+ }
+
+ private InputStream createInputStream(InputStream file) {
+ return new SequenceInputStream(file, new ByteArrayInputStream("\n".getBytes()));
+ }
+
+ public ModuleDeclaration parse(InputStream file, String fileName) throws IOException {
+ MontyParser parser = createParser(file);
+
+ ASTBuilder astBuilder = new ASTBuilder(fileName);
+ ASTNode moduleNode = astBuilder.visit(parser.compilationUnit());
+ return (ModuleDeclaration) moduleNode;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/BasicASTNode.java b/src/main/java/de/uni/bremen/monty/moco/ast/BasicASTNode.java
new file mode 100644
index 0000000..74a5ce3
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/BasicASTNode.java
@@ -0,0 +1,109 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+/** Baseclass for every node in the AST. */
+public abstract class BasicASTNode implements ASTNode {
+
+ /** Sourcecode position of this node. */
+ private final Position position;
+
+ /** Parent node. */
+ private ASTNode parentNode;
+
+ /** Associated scope. */
+ private Scope scope;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node */
+ public BasicASTNode(Position position) {
+ this.position = position;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public String toString() {
+ return getClass().getSimpleName();
+ }
+
+ /** Get parent node.
+ *
+ * @return the parent node */
+ @Override
+ public ASTNode getParentNode() {
+ return parentNode;
+ }
+
+ /** Set parent node.
+ *
+ * @param parentNode
+ * the parent node */
+ @Override
+ public void setParentNode(ASTNode parentNode) {
+ this.parentNode = parentNode;
+ }
+
+ /** Get the sourcecode position.
+ *
+ * @return the position */
+ @Override
+ public Position getPosition() {
+ return position;
+ }
+
+ /** Set the associated scope.
+ *
+ * @param scope
+ * the associated scope */
+ @Override
+ public void setScope(Scope scope) {
+ this.scope = scope;
+ }
+
+ /** Get the accociated scope.
+ *
+ * @return the scope */
+ @Override
+ public Scope getScope() {
+ return scope;
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/Block.java b/src/main/java/de/uni/bremen/monty/moco/ast/Block.java
new file mode 100644
index 0000000..9948137
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/Block.java
@@ -0,0 +1,119 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+import java.util.*;
+
+import de.uni.bremen.monty.moco.ast.declaration.Declaration;
+import de.uni.bremen.monty.moco.ast.statement.Statement;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+/** This node represents a block.
+ *
+ * A block contains declarations and statements. The declarations must be processed first. */
+public class Block extends BasicASTNode {
+
+ /** The statements. */
+ private final List statements;
+
+ /** The declarations. */
+ private final List declarations;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node */
+ public Block(Position position) {
+ super(position);
+ statements = new ArrayList();
+ declarations = new ArrayList();
+ }
+
+ /** @return true if the block does not have any statements or declarations. */
+ public boolean isEmpty() {
+ return statements.isEmpty() && declarations.isEmpty();
+ }
+
+ /** Add a statement to this block.
+ *
+ * @param statement
+ * the statement to add */
+ public void addStatement(Statement statement) {
+ statements.add(statement);
+ }
+
+ /** Add a declaration to this block.
+ *
+ * @param declaration
+ * the declaration to add */
+ public void addDeclaration(Declaration declaration) {
+ declarations.add(declaration);
+ }
+
+ /** Get the statements of this block.
+ *
+ * @return the statements */
+ public List getStatements() {
+ return statements;
+ }
+
+ /** Get the declarations of this block.
+ *
+ * @return the declarations */
+ public List getDeclarations() {
+ return declarations;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ for (Declaration declaration : declarations) {
+ visitor.visitDoubleDispatched(declaration);
+ }
+ for (Statement statement : statements) {
+ visitor.visitDoubleDispatched(statement);
+ }
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/ClassScope.java b/src/main/java/de/uni/bremen/monty/moco/ast/ClassScope.java
new file mode 100644
index 0000000..69b256a
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/ClassScope.java
@@ -0,0 +1,159 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+import de.uni.bremen.monty.moco.ast.declaration.*;
+import de.uni.bremen.monty.moco.exception.*;
+import java.util.List;
+import java.util.ArrayList;
+
+/** A scope in which identifier are associated with declarations.
+ *
+ * To nest scopes or build a stack the parent scope is passed as an argument to the construtor. So you use it like this
+ *
+ *
+ *
+ * {@code
+ * // create a new scope and nest the old one
+ * currentScope = new ClassScope(currentScope);
+ * // do something
+ * // destroy this scope and use the old (nested) one
+ * currentScope = currentScope.getParentScope();
+ * }
+ *
+ *
+ * This special scope searches its associations, the parent classes in inheritance hierachy and only then the parent
+ * scope.
+ *
+ * Note: only single inheritance so far. */
+public class ClassScope extends Scope {
+
+ /** The parent class in inheritance hierachy. */
+ private List parentClassesScopes;
+
+ /** Constructor.
+ *
+ * @param parent
+ * the parent scope in nesting hierachy */
+ public ClassScope(Scope parent) {
+ super(parent);
+ this.parentClassesScopes = new ArrayList<>();
+ }
+
+ public void addParentClassScope(ClassScope scope) {
+ parentClassesScopes.add(scope);
+ }
+
+ /** Resolve an identifier in inherited scopes.
+ *
+ * @param identifier
+ * the identifier
+ * @return the declaration or null if nothing is found */
+ protected Declaration resolveMember(ResolvableIdentifier identifier) {
+ Declaration declaration = members.get(identifier);
+
+ if (declaration != null) {
+ return declaration;
+ }
+ for (ClassScope scope : parentClassesScopes) {
+ declaration = scope.resolveMember(identifier);
+ if (declaration != null) {
+ return declaration;
+ }
+ }
+ return null;
+ }
+
+ /** Resolve an identifier for list of overloaded procedures or functions in inherited scope.
+ *
+ * @param identifier
+ * the identifier to resolve
+ * @return the list of procedure declarations */
+ protected List resolveProcedureMember(ResolvableIdentifier identifier) {
+ List result = new ArrayList();
+
+ if (procedures.containsKey(identifier)) {
+ result.addAll(procedures.get(identifier));
+ }
+ for (ClassScope scope : parentClassesScopes) {
+ result.addAll(scope.resolveProcedureMember(identifier));
+ }
+ return result;
+ }
+
+ /** Resolve an identifier for a declaration
+ *
+ * It first searches its associations, the parent classes in inheritance hierachy and only then the parent scope.
+ *
+ * @param identifier
+ * the identifier to resolve
+ * @return the declaration or null if nothing is found */
+ @Override
+ public Declaration resolve(ResolvableIdentifier identifier) {
+ Declaration declaration = resolveMember(identifier);
+
+ if (declaration != null) {
+ return declaration;
+ }
+ return super.resolve(identifier);
+ }
+
+ /** Resolve an identifier for list of overloaded procedures or functions.
+ *
+ * It first searches its associations, the parent classes in inheritance hierachy and only then the parent scope.
+ *
+ * @param identifier
+ * the identifier to resolve
+ * @return the list of procedure declarations */
+ @Override
+ public List resolveProcedure(ResolvableIdentifier identifier) {
+ List result = new ArrayList();
+ result.addAll(resolveProcedureMember(identifier));
+ if (parent != null) {
+ try {
+ result.addAll(parent.resolveProcedure(identifier));
+ } catch (UnknownIdentifierException e) {
+ }
+ }
+ if (result.isEmpty()) {
+ throw new UnknownIdentifierException(identifier);
+ }
+ return result;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/CoreClasses.java b/src/main/java/de/uni/bremen/monty/moco/ast/CoreClasses.java
new file mode 100644
index 0000000..71ed950
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/CoreClasses.java
@@ -0,0 +1,102 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+
+package de.uni.bremen.monty.moco.ast;
+
+import de.uni.bremen.monty.moco.ast.declaration.ClassDeclaration;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Collection;
+import java.util.Collections;
+
+public class CoreClasses {
+
+ private static Map coreClasses = new HashMap();
+
+ static {
+ // TODO find name for void that is not a valid identifier
+ String[] classNames = new String[] { "Object", "Char", "String", "Int", "Float", "Bool", "Array", "__void" };
+ for (String name : classNames) {
+ CoreClasses.setCoreClass(name, new ClassDeclaration(new Position("Dummy_" + name, 0, 0), new Identifier(
+ name), Collections. emptyList(), new Block(
+ new Position("Dummy_" + name, 1, 0))));
+ }
+ }
+
+ public static Collection getAllCoreClasses() {
+ return coreClasses.values();
+ }
+
+ public static void setCoreClass(String name, ClassDeclaration classDeclaration) {
+ coreClasses.put(name, classDeclaration);
+ }
+
+ public static ClassDeclaration objectType() {
+ return coreClasses.get("Object");
+ }
+
+ public static ClassDeclaration charType() {
+ return coreClasses.get("Char");
+ }
+
+ public static ClassDeclaration stringType() {
+ return coreClasses.get("String");
+ }
+
+ public static ClassDeclaration intType() {
+ return coreClasses.get("Int");
+ }
+
+ public static ClassDeclaration floatType() {
+ return coreClasses.get("Float");
+ }
+
+ public static ClassDeclaration boolType() {
+ return coreClasses.get("Bool");
+ }
+
+ public static ClassDeclaration arrayType() {
+ return coreClasses.get("Array");
+ }
+
+ public static ClassDeclaration voidType() {
+ return coreClasses.get("__void");
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/Identifier.java b/src/main/java/de/uni/bremen/monty/moco/ast/Identifier.java
new file mode 100644
index 0000000..f3de556
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/Identifier.java
@@ -0,0 +1,93 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+/** An Identifier is the user defined name of a declaration.
+ *
+ * During context-analysis the identifier with an associated declaration is stored in a scope. */
+public class Identifier {
+
+ /** The name of the declaration. */
+ private final String symbol;
+
+ /** Constructor.
+ *
+ * @param symbol
+ * the name of the declaration */
+ public Identifier(String symbol) {
+ this.symbol = symbol;
+ }
+
+ /** Get the name of the declaration.
+ *
+ * @return the name */
+ public String getSymbol() {
+ return symbol;
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (this == other) {
+ return true;
+ } else if (other == null) {
+ return false;
+ }
+
+ if (other instanceof Identifier) {
+ Identifier that = (Identifier) other;
+ return getSymbol().equals(that.getSymbol());
+ }
+
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return symbol.hashCode();
+ }
+
+ @Override
+ public String toString() {
+ return symbol;
+ }
+
+ public static Identifier convert(ResolvableIdentifier identifier) {
+ return new Identifier(identifier.getSymbol());
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/Import.java b/src/main/java/de/uni/bremen/monty/moco/ast/Import.java
new file mode 100644
index 0000000..2ae0a3a
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/Import.java
@@ -0,0 +1,70 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+/** Represent the imported modules. */
+public class Import extends BasicASTNode {
+
+ /** The identifier of the importline */
+ private ResolvableIdentifier identifier;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node */
+ public Import(Position position, ResolvableIdentifier identifier) {
+ super(position);
+ this.identifier = identifier;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ // throw new NotYetImplementedException();
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/Package.java b/src/main/java/de/uni/bremen/monty/moco/ast/Package.java
new file mode 100644
index 0000000..3ccb27c
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/Package.java
@@ -0,0 +1,109 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+
+package de.uni.bremen.monty.moco.ast;
+
+import de.uni.bremen.monty.moco.ast.declaration.ModuleDeclaration;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Package extends BasicASTNode {
+
+ private Identifier name;
+
+ private List modules;
+ private List subPackages;
+ private boolean nativePackage = false;
+
+ /** Constructor.
+ *
+ * @param name */
+ public Package(Identifier name) {
+ super(new Position());
+ this.name = name;
+ modules = new ArrayList<>();
+ subPackages = new ArrayList<>();
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ for (Package subPackage : subPackages) {
+ visitor.visit(subPackage);
+ }
+ for (ModuleDeclaration module : modules) {
+ visitor.visit(module);
+ }
+ }
+
+ public void addModule(ModuleDeclaration module) {
+ modules.add(module);
+ }
+
+ public void addSubPackage(Package aPackage) {
+ subPackages.add(aPackage);
+ }
+
+ public List getModules() {
+ return modules;
+ }
+
+ public List getModulesRecursive() {
+ List allModules = new ArrayList<>();
+ allModules.addAll(modules);
+ for (Package subPackage : subPackages) {
+ allModules.addAll(subPackage.getModulesRecursive());
+ }
+ return allModules;
+ }
+
+ public boolean isNativePackage() {
+ return nativePackage;
+ }
+
+ public void setNativePackage(boolean nativePackage) {
+ this.nativePackage = nativePackage;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/PackageBuilder.java b/src/main/java/de/uni/bremen/monty/moco/ast/PackageBuilder.java
new file mode 100644
index 0000000..31615b9
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/PackageBuilder.java
@@ -0,0 +1,157 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+
+package de.uni.bremen.monty.moco.ast;
+
+import de.uni.bremen.monty.moco.ast.declaration.*;
+import de.uni.bremen.monty.moco.util.MontyFile;
+import de.uni.bremen.monty.moco.util.MontyJar;
+import de.uni.bremen.monty.moco.util.MontyResource;
+import de.uni.bremen.monty.moco.util.Params;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Collections;
+
+public class PackageBuilder {
+ private final AntlrAdapter antlrAdapter;
+ private Params params;
+
+ public PackageBuilder(Params params) {
+ this.params = params;
+ antlrAdapter = new AntlrAdapter();
+ }
+
+ public Package buildPackage() throws IOException {
+ Package basePackage = new Package(new Identifier(""));
+ String inputFile = params.getInputFile();
+ if (inputFile != null) {
+ basePackage.addSubPackage(createPackageFromSingleModule(inputFile));
+ } else {
+ basePackage.addSubPackage(createPackageFromSourceFolder(params));
+ }
+ addCoreLib(basePackage);
+ return basePackage;
+ }
+
+ private void addCoreLib(Package basePackage) throws IOException {
+ Package corePackage = createPackage(getCoreLibFolder());
+ corePackage.setNativePackage(true);
+ basePackage.addSubPackage(corePackage);
+
+ Block block = new Block(new Position());
+ ModuleDeclaration module =
+ new ModuleDeclaration(new Position(), new Identifier("CoreClasses"), block,
+ Collections. emptyList());
+ block.addDeclaration(CoreClasses.stringType());
+ block.addDeclaration(CoreClasses.arrayType());
+ block.addDeclaration(CoreClasses.voidType());
+ corePackage.addModule(module);
+ setCoreClasses(corePackage);
+ }
+
+ private void setCoreClasses(Package corePackage) {
+ for (ModuleDeclaration module : corePackage.getModulesRecursive()) {
+ for (Declaration declaration : module.getBlock().getDeclarations()) {
+ if (declaration instanceof ClassDeclaration) {
+ ClassDeclaration classDeclaration = (ClassDeclaration) declaration;
+ CoreClasses.setCoreClass(classDeclaration.getIdentifier().getSymbol(), classDeclaration);
+ }
+ }
+ }
+ }
+
+ private MontyResource getCoreLibFolder() {
+ try {
+ Class> aClass = PackageBuilder.class;
+ ClassLoader classLoader = aClass.getClassLoader();
+ URL testProgramFolder = classLoader.getResource("corelib/");
+ if (testProgramFolder.toString().startsWith("jar:")) {
+ return new MontyJar(testProgramFolder);
+ } else {
+ return new MontyFile(testProgramFolder.toURI());
+ }
+ } catch (URISyntaxException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private Package createPackageFromSourceFolder(Params params) throws IOException {
+ String baseFolder = params.getInputFolder();
+ MontyResource inputFolder = new MontyFile(baseFolder);
+ return createPackage(inputFolder);
+ }
+
+ private Package createPackageFromSingleModule(String inputFile) throws IOException {
+ MontyResource file = new MontyFile(inputFile);
+
+ Package mainPackage = new Package(new Identifier(""));
+ addModules(new MontyResource[] { file }, mainPackage);
+ return mainPackage;
+ }
+
+ private Package createPackage(MontyResource inputFolder) throws IOException {
+ MontyResource[] montyFiles = inputFolder.listSubModules();
+ return createPackage(inputFolder, montyFiles);
+ }
+
+ private Package createPackage(MontyResource inputFolder, MontyResource[] montyFiles) throws IOException {
+ Package mainPackage = new Package(new Identifier(inputFolder.getName()));
+ addModules(montyFiles, mainPackage);
+ addSubPackages(inputFolder, mainPackage);
+ return mainPackage;
+ }
+
+ private void addModules(MontyResource[] montyFiles, Package aPackage) throws IOException {
+ for (MontyResource file : montyFiles) {
+ ModuleDeclaration module = antlrAdapter.parse(file.toInputStream(), file.getName());
+ aPackage.addModule(module);
+ }
+ }
+
+ private void addSubPackages(MontyResource inputFolder, Package mainPackage) throws IOException {
+ MontyResource[] subPackages = inputFolder.listSubPackages();
+ for (MontyResource subPackage : subPackages) {
+ mainPackage.addSubPackage(createPackage(subPackage));
+ }
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/Position.java b/src/main/java/de/uni/bremen/monty/moco/ast/Position.java
new file mode 100644
index 0000000..48fe8c6
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/Position.java
@@ -0,0 +1,96 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+/** Represents a position in the source code. */
+public class Position {
+
+ /** The source file name. */
+ private String fileName = "";
+
+ /** The source file line number. */
+ private int lineNumber = 0;
+
+ /** The source file char number. */
+ private int charNumber = 0;
+
+ /** Default constructor. */
+ public Position() {
+
+ }
+
+ /** Constructor.
+ *
+ * @param fileName
+ * the source file name
+ * @param lineNumber
+ * the source file line number
+ * @param charNumber
+ * the source file char number */
+ public Position(String fileName, int lineNumber, int charNumber) {
+ this.fileName = fileName;
+ this.lineNumber = lineNumber;
+ this.charNumber = charNumber;
+ }
+
+ public String toString() {
+ return String.format("file: %s, line: %d, char: %d", fileName, lineNumber, charNumber);
+ }
+
+ /** Get the source file name.
+ *
+ * @return the source file name */
+ public String getFileName() {
+ return fileName;
+ }
+
+ /** Get the source file line number.
+ *
+ * @return the souce file line number */
+ public int getLineNumber() {
+ return lineNumber;
+ }
+
+ /** Get the source file char number.
+ *
+ * @return the source file char number */
+ public int getCharNumber() {
+ return charNumber;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/ResolvableIdentifier.java b/src/main/java/de/uni/bremen/monty/moco/ast/ResolvableIdentifier.java
new file mode 100644
index 0000000..bd43f14
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/ResolvableIdentifier.java
@@ -0,0 +1,57 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+/** A ResolvableIdentifier is similar to the Identifier a name of a declaration.
+ *
+ * During context-analysis this is resolved to a declaration using a scope. */
+public class ResolvableIdentifier extends Identifier {
+
+ /** Constructor.
+ *
+ * @param symbol
+ * the name of the declaration */
+ public ResolvableIdentifier(String symbol) {
+ super(symbol);
+ }
+
+ public static ResolvableIdentifier convert(Identifier identifier) {
+ return new ResolvableIdentifier(identifier.getSymbol());
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/Scope.java b/src/main/java/de/uni/bremen/monty/moco/ast/Scope.java
new file mode 100644
index 0000000..9ceb4e1
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/Scope.java
@@ -0,0 +1,202 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast;
+
+import java.util.*;
+
+import de.uni.bremen.monty.moco.ast.declaration.*;
+import de.uni.bremen.monty.moco.exception.*;
+
+/** A scope in which an identifier is associated with a declaration.
+ *
+ * To nest scopes or build a stack the parent scope is passed as an argument to the constructor. So you use it like
+ * this:
+ *
+ *
+ *
+ * {@code
+ * // create a new scope and nest the old one
+ * currentScope = new Scope(currentScope);
+ * // do something
+ * // destroy this scope and use the old (nested) one
+ * currentScope = currentScope.getParentScope();
+ * }
+ *
*/
+public class Scope {
+
+ /** The parent scope in nesting hierarchy. */
+ protected Scope parent;
+
+ /** The map to store the associations to procedure declarations. */
+ protected Map> procedures;
+
+ /** The map to store the remaining associations. */
+ protected Map members;
+
+ /** Constructor.
+ *
+ * @param parent
+ * the parent scope in nesting hierarchy */
+ public Scope(Scope parent) {
+ this.parent = parent;
+ procedures = new HashMap>();
+ members = new HashMap();
+ }
+
+ /** Get the parent scope in nesting hierarchy.
+ *
+ * This method acts as the 'pop()'-operation in the scope-stack analogy.
+ *
+ * @return the parent scope */
+ public Scope getParentScope() {
+ return parent;
+ }
+
+ /** Resolve an identifier for a declaration.
+ *
+ * First the declarations of this scope are searched. If the not successful the search continues recursively in the
+ * parent scope.
+ *
+ * @param identifier
+ * the identifier to resolve
+ * @return the declaration */
+ public Declaration resolve(ResolvableIdentifier identifier) {
+ Declaration declaration = members.get(identifier);
+
+ if (declaration != null) {
+ return declaration;
+ }
+ if (parent != null) {
+ return parent.resolve(identifier);
+ }
+ throw new UnknownIdentifierException(identifier);
+ }
+
+ /** Resolve an identifier for a type declaration.
+ *
+ * @param identifier
+ * the identifier to resolve
+ * @return the declaration */
+ public TypeDeclaration resolveType(ResolvableIdentifier identifier) {
+ try {
+ Declaration declaration = resolve(identifier);
+ if (declaration instanceof TypeDeclaration) {
+ return (TypeDeclaration) declaration;
+ }
+ throw new UnknownTypeException(identifier);
+ } catch (UnknownIdentifierException e) {
+ throw new UnknownTypeException(identifier);
+ }
+ }
+
+ /** Resolve an identifier for list of overloaded procedures or functions.
+ *
+ * @param identifier
+ * the identifier to resolve
+ * @return the list of procedure declarations */
+ public List resolveProcedure(ResolvableIdentifier identifier) {
+ List result = new ArrayList();
+
+ if (procedures.containsKey(identifier)) {
+ result.addAll(procedures.get(identifier));
+ }
+ if (parent != null) {
+ try {
+ result.addAll(parent.resolveProcedure(identifier));
+ } catch (UnknownIdentifierException e) {
+ }
+ }
+ if (result.isEmpty()) {
+ throw new UnknownIdentifierException(identifier);
+ }
+ return result;
+ }
+
+ /** Associate an identifier with a declaration.
+ *
+ * This method uses define(Identifier, ProcedureDeclaration) if the given declaration is a procedure or function
+ * declaration.
+ *
+ * @param identifier
+ * the identifier
+ * @param declaration
+ * the declaration
+ * @throws RedeclarationException
+ * if the identifier is already defined or this is invalid overloading */
+ public void define(Identifier identifier, Declaration declaration) throws RedeclarationException {
+ if (declaration instanceof ProcedureDeclaration) {
+ define(identifier, (ProcedureDeclaration) declaration);
+ } else if (members.get(identifier) != null) {
+ throw new RedeclarationException(declaration, identifier.getSymbol());
+ } else {
+ members.put(identifier, declaration);
+ }
+ }
+
+ /** Associate an identifier with a declaration.
+ *
+ * This differs from define(Identifier, Declaration) as this method uses the declaration's Identifier-attribute to
+ * call define(Identifier, Declaration)
+ *
+ * @param declaration
+ * the declaration
+ * @throws RedeclarationException
+ * if the identifier is already defined or this is invalid overloading */
+ public void define(Declaration declaration) throws RedeclarationException {
+ define(declaration.getIdentifier(), declaration);
+ }
+
+ /** Associate an identifier with a procedure or function declaration.
+ *
+ * This takes overloading into account and throws a RedeclarationException if the declaration is an instance of
+ * invalid overloading.
+ *
+ * @param identifier
+ * the identifier
+ * @param declaration
+ * the declaration
+ * @throws RedeclarationException
+ * if this is invalid overloading */
+ public void define(Identifier identifier, ProcedureDeclaration declaration) throws RedeclarationException {
+ if (!procedures.containsKey(identifier)) {
+ procedures.put(identifier, new ArrayList());
+ }
+ procedures.get(identifier).add(declaration);
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/declaration/ClassDeclaration.java b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/ClassDeclaration.java
new file mode 100644
index 0000000..75eda41
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/ClassDeclaration.java
@@ -0,0 +1,188 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.declaration;
+
+import de.uni.bremen.monty.moco.ast.Block;
+import de.uni.bremen.monty.moco.ast.Identifier;
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.ast.ResolvableIdentifier;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/** A ClassDeclaration represents the declaration of a class in the AST.
+ *
+ * A ClassDeclaration has a list of superclasses and a list of nested declarations. It can be used as a type. */
+public class ClassDeclaration extends TypeDeclaration {
+
+ /** Identifier of superclasses. */
+ private final List superClassIdentifiers = new ArrayList<>();
+
+ /** Superclasses. */
+ private final List superClassDeclarations = new ArrayList<>();
+
+ /** The generated default initializer to be called from every user defined initializer. */
+ private ProcedureDeclaration defaultInitializer;
+
+ /** Block with assignments */
+ private final Block block;
+
+ /** The virtal method table for this class */
+ private List virtualMethodTable = new ArrayList<>();
+
+ /** The last index for the attributes of this class. This counter starts at `1` as index 0 is reserved for a pointer
+ * to the vmt. */
+ private int lastAttributeIndex = 1;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param identifier
+ * the identifier
+ * @param superClasses
+ * a list of direct super-classes
+ * @param block
+ * the block */
+ public ClassDeclaration(Position position, Identifier identifier, List superClasses,
+ Block block) {
+ super(position, identifier);
+ this.block = block;
+ this.superClassIdentifiers.addAll(superClasses);
+ }
+
+ /** Get the list of declarations and assignments.
+ *
+ * @return the block with declarations and assignments */
+ public Block getBlock() {
+ return block;
+ }
+
+ /** Get the list of identifiers of direct superclasses
+ *
+ * @return the identifier of superclasses */
+ public List getSuperClassIdentifiers() {
+ return superClassIdentifiers;
+ }
+
+ /** Get the list of direct superclasses this class inherits from.
+ *
+ * @return the superclasses */
+ public List getSuperClassDeclarations() {
+ return superClassDeclarations;
+ }
+
+ /** Get a list of all the declarations of superclasses and this one. */
+ public List getSuperClassDeclarationsRecursive() {
+ List allSuperClassDeclarations = new ArrayList<>();
+ for (TypeDeclaration superClass : superClassDeclarations) {
+ if (superClass instanceof ClassDeclaration) {
+ allSuperClassDeclarations.addAll(((ClassDeclaration) superClass).getSuperClassDeclarationsRecursive());
+ }
+ }
+ allSuperClassDeclarations.add(this);
+ return allSuperClassDeclarations;
+ }
+
+ /** set the last attribute index.
+ *
+ * @param lastAttributeIndex
+ * the last attribute index */
+ public void setLastAttributeIndex(int lastAttributeIndex) {
+ this.lastAttributeIndex = lastAttributeIndex;
+ }
+
+ /** get the last attribute index
+ *
+ * @return the last attribute index */
+ public int getLastAttributeIndex() {
+ return lastAttributeIndex;
+ }
+
+ /** Get the VMT.
+ *
+ * @return the VMT */
+ public List getVirtualMethodTable() {
+ return virtualMethodTable;
+ }
+
+ /** Get the default initializer.
+ *
+ * @return the default initializer */
+ public ProcedureDeclaration getDefaultInitializer() {
+ return this.defaultInitializer;
+ }
+
+ /** Set the default initializer.
+ *
+ * @param defaultInitializer
+ * the new default initializer */
+ public void setDefaultInitializer(ProcedureDeclaration defaultInitializer) {
+ this.defaultInitializer = defaultInitializer;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ visitor.visitDoubleDispatched(block);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public boolean matchesType(TypeDeclaration other) {
+ if (super.matchesType(other)) {
+ return true;
+ }
+ if (other instanceof ClassDeclaration) {
+ for (TypeDeclaration parentClass : superClassDeclarations) {
+ if (parentClass.matchesType(other)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/declaration/Declaration.java b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/Declaration.java
new file mode 100644
index 0000000..3148ccc
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/Declaration.java
@@ -0,0 +1,99 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.declaration;
+
+import de.uni.bremen.monty.moco.ast.*;
+
+/** The baseclass of every declaration.
+ *
+ * A declaration has an identifier, the name under which this declaration is known. */
+public abstract class Declaration extends BasicASTNode {
+
+ /** The identifier. */
+ private final Identifier identifier;
+
+ /** The mangled identifier. */
+ private Identifier mangledIdentifier;
+
+ private AccessModifier access;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param identifier
+ * the identifier */
+ public Declaration(Position position, Identifier identifier) {
+ super(position);
+ this.identifier = identifier;
+ this.access = AccessModifier.PUBLIC;
+ }
+
+ public Declaration(Position position, Identifier identifier, AccessModifier access) {
+ super(position);
+ this.identifier = identifier;
+ this.access = access;
+ }
+
+ public void setAccessModifier(AccessModifier access) {
+
+ this.access = access;
+ }
+
+ /** Get the mangled Identifier.
+ *
+ * @return mangled Identifier */
+ public Identifier getMangledIdentifier() {
+ return mangledIdentifier;
+ }
+
+ /** Set the mangled Identifier.
+ *
+ * @param mangledIdentifier */
+ public void setMangledIdentifier(Identifier mangledIdentifier) {
+ this.mangledIdentifier = mangledIdentifier;
+ }
+
+ /** Get the identifier.
+ *
+ * @return the identifier */
+ public Identifier getIdentifier() {
+ return identifier;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/declaration/FunctionDeclaration.java b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/FunctionDeclaration.java
new file mode 100644
index 0000000..718d0c2
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/FunctionDeclaration.java
@@ -0,0 +1,148 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.declaration;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+import java.util.List;
+
+/** A FunctionDeclaration represents the declaration of a function in the AST.
+ *
+ * It can be used as a returnType. */
+public class FunctionDeclaration extends ProcedureDeclaration {
+
+ /** The return returnType. */
+ private ResolvableIdentifier returnTypeIdentifier;
+ private TypeDeclaration returnType;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param identifier
+ * the identifier
+ * @param body
+ * the body of this function
+ * @param parameter
+ * the parameter of this function
+ * @param returnTypeIdentifier
+ * the return returnType */
+ public FunctionDeclaration(Position position, Identifier identifier, Block body,
+ List parameter, ProcedureDeclaration.DeclarationType declarationType,
+ ResolvableIdentifier returnTypeIdentifier) {
+ super(position, identifier, body, parameter, declarationType);
+ this.returnTypeIdentifier = returnTypeIdentifier;
+ }
+
+ public FunctionDeclaration(Position position, Identifier identifier, Block body,
+ List parameter, ResolvableIdentifier returnTypeIdentifier) {
+ this(position, identifier, body, parameter, ProcedureDeclaration.DeclarationType.UNBOUND, returnTypeIdentifier);
+ }
+
+ /** Constructor
+ *
+ * @param position
+ * * Position of this node
+ * @param identifier
+ * the identifier
+ * @param body
+ * the body of this function
+ * @param parameter
+ * the parameter of this function
+ * @param returnType
+ * the return returnType */
+ public FunctionDeclaration(Position position, Identifier identifier, Block body,
+ List parameter, ClassDeclaration returnType) {
+ super(position, identifier, body, parameter);
+ this.returnType = returnType;
+ this.returnTypeIdentifier = ResolvableIdentifier.convert(returnType.getIdentifier());
+ }
+
+ /** get the return returnType.
+ *
+ * @return the return returnType */
+ public ResolvableIdentifier getReturnTypeIdentifier() {
+ return returnTypeIdentifier;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ super.visitChildren(visitor);
+ }
+
+ /** get the returnType.
+ *
+ * @return the returnType */
+ public TypeDeclaration getReturnType() {
+ return returnType;
+ }
+
+ /** set the returnType
+ *
+ * @param returnType */
+ public void setReturnType(TypeDeclaration returnType) {
+ this.returnType = returnType;
+ }
+
+ /** Check equality of two types taking into account the AST object hierachy.
+ *
+ *
+ * @param other
+ * the other TypeDeclaration to check against
+ * @return if equal */
+ @Override
+ public boolean matchesType(TypeDeclaration other) {
+ if (!super.matchesType(other)) {
+ return false;
+ }
+ if (!(other instanceof FunctionDeclaration)) {
+ return true;
+ }
+ FunctionDeclaration function = (FunctionDeclaration) other;
+ return returnType.matchesType(function.getReturnType());
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/declaration/ModuleDeclaration.java b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/ModuleDeclaration.java
new file mode 100644
index 0000000..688bf0a
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/ModuleDeclaration.java
@@ -0,0 +1,101 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.declaration;
+
+import java.util.List;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+/** A ModuleDeclaration represents the declaration of a module in the AST.
+ *
+ * It can be used as a type. */
+public class ModuleDeclaration extends TypeDeclaration {
+
+ /** The imports in this module. */
+ private final List imports;
+
+ /** The nested statements and declarations. */
+ private final Block block;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param identifier
+ * the identifier
+ * @param block
+ * the nested declarations and statements
+ * @param imports
+ * the imports in this module */
+ public ModuleDeclaration(Position position, Identifier identifier, Block block, List imports) {
+ super(position, identifier);
+ this.imports = imports;
+ this.block = block;
+ }
+
+ /** Get the body block.
+ *
+ * @return the block */
+ public Block getBlock() {
+ return block;
+ }
+
+ /** Get the list of imports.
+ *
+ * @return the imports */
+ public List getImports() {
+ return imports;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ for (Import imp : imports) {
+ visitor.visitDoubleDispatched(imp);
+ }
+ visitor.visitDoubleDispatched(block);
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/declaration/ProcedureDeclaration.java b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/ProcedureDeclaration.java
new file mode 100644
index 0000000..6a769d6
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/ProcedureDeclaration.java
@@ -0,0 +1,184 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.declaration;
+
+import java.util.List;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+/** A ProcedureDeclaration represents the declaration of a procedure in the AST.
+ *
+ * It can be used as a type. */
+public class ProcedureDeclaration extends TypeDeclaration {
+ public enum DeclarationType {
+ INITIALIZER, METHOD, UNBOUND
+ }
+
+ /** The declarations and statements within this declaration. */
+ private final Block body;
+
+ /** The parameters of this declaration. */
+ private final List parameter;
+
+ private DeclarationType declarationType;
+
+ /** Index of the procedure in the vmt if it is a procedure in the class struct */
+ private int vmtIndex;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param identifier
+ * the identifier
+ * @param body
+ * the body of this procedure
+ * @param parameter
+ * the parameter of this procedure */
+ public ProcedureDeclaration(Position position, Identifier identifier, Block body,
+ List parameter, DeclarationType declarationType) {
+ super(position, identifier);
+ this.body = body;
+ this.parameter = parameter;
+ this.declarationType = declarationType;
+ this.vmtIndex = -1;
+ }
+
+ public ProcedureDeclaration(Position position, Identifier identifier, Block body,
+ List parameter) {
+ this(position, identifier, body, parameter, DeclarationType.UNBOUND);
+ }
+
+ /** Get the body block.
+ *
+ * @return the body */
+ public Block getBody() {
+ return body;
+ }
+
+ /** Get the list of parameter.
+ *
+ * @return the paramter */
+ public List getParameter() {
+ return parameter;
+ }
+
+ /** set the declaration type */
+ public void setDeclarationType(DeclarationType type) {
+ this.declarationType = type;
+ }
+
+ /** get the declaration type
+ *
+ * @return the declaration type */
+ public DeclarationType getDeclarationType() {
+ return declarationType;
+ }
+
+ public boolean isInitializer() {
+ return declarationType == DeclarationType.INITIALIZER;
+ }
+
+ public boolean isMethod() {
+ return declarationType == DeclarationType.METHOD;
+ }
+
+ public boolean isUnbound() {
+ return declarationType == DeclarationType.UNBOUND;
+ }
+
+ public ClassDeclaration getDefiningClass() {
+ if (isMethod() || isInitializer()) {
+ return (ClassDeclaration) getParentNode().getParentNode();
+ }
+ return null;
+ }
+
+ /** Get the vmtIndex. */
+ public int getVMTIndex() {
+ return vmtIndex;
+ }
+
+ /** Set the vmtIndex. */
+ public void setVMTIndex(int vmtIndex) {
+ this.vmtIndex = vmtIndex;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ for (VariableDeclaration variableDeclaration : parameter) {
+ visitor.visitDoubleDispatched(variableDeclaration);
+ }
+ visitor.visitDoubleDispatched(body);
+ }
+
+ /** Check equality of two types taking into account the AST object hierachy.
+ *
+ *
+ * @param other
+ * the other TypeDeclaration to check against
+ * @return if equal */
+ @Override
+ public boolean matchesType(TypeDeclaration other) {
+ if (!super.matchesType(other)) {
+ return false;
+ }
+ if (!(other instanceof ProcedureDeclaration)) {
+ return false;
+ }
+ List otherParameter = ((ProcedureDeclaration) other).getParameter();
+ if (parameter.size() != otherParameter.size()) {
+ return false;
+ }
+ for (int i = 0; i < parameter.size(); i++) {
+ if (!parameter.get(i).getType().matchesType(otherParameter.get(i).getType())) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/declaration/TypeDeclaration.java b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/TypeDeclaration.java
new file mode 100644
index 0000000..235583e
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/TypeDeclaration.java
@@ -0,0 +1,67 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.declaration;
+
+import de.uni.bremen.monty.moco.ast.Identifier;
+import de.uni.bremen.monty.moco.ast.Position;
+
+/** The baseclass of every declaration that describes a new type.
+ *
+ * The TypeDeclarations are used as types for Expressions. */
+public abstract class TypeDeclaration extends Declaration {
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param identifier
+ * the identifier */
+ public TypeDeclaration(Position position, Identifier identifier) {
+ super(position, identifier);
+ }
+
+ /** Check equality of two types taking into account the AST object hierachy.
+ *
+ *
+ * @param other
+ * the other TypeDeclaration to check against
+ * @return if equal */
+ public boolean matchesType(TypeDeclaration other) {
+ return getIdentifier().equals(other.getIdentifier());
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/declaration/VariableDeclaration.java b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/VariableDeclaration.java
new file mode 100644
index 0000000..b5864a0
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/declaration/VariableDeclaration.java
@@ -0,0 +1,151 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.declaration;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class VariableDeclaration extends Declaration {
+ public enum DeclarationType {
+ VARIABLE, PARAMETER, ATTRIBUTE
+ }
+
+ private ResolvableIdentifier typeIdentifier;
+ private TypeDeclaration type;
+ private DeclarationType declarationType;
+ private boolean isGlobal;
+
+ /* Index of the variable if it is an attribute in the class struct */
+ private int attributeIndex;
+
+ public VariableDeclaration(Position position, Identifier identifier, ResolvableIdentifier typeIdentifier,
+ DeclarationType declarationType) {
+ this(position, identifier, typeIdentifier);
+ this.declarationType = declarationType;
+ }
+
+ public VariableDeclaration(Position position, Identifier identifier, ResolvableIdentifier typeIdentifier) {
+ super(position, identifier);
+ this.typeIdentifier = typeIdentifier;
+ this.declarationType = DeclarationType.VARIABLE;
+ attributeIndex = -1;
+ }
+
+ /** set the declaration type */
+ public void setDeclarationType(DeclarationType type) {
+ this.declarationType = type;
+ }
+
+ /** get the declaration type
+ *
+ * @return the declaration type */
+ public DeclarationType getDeclarationType() {
+ return declarationType;
+ }
+
+ public boolean isVariable() {
+ return declarationType == DeclarationType.VARIABLE;
+ }
+
+ public boolean isParameter() {
+ return declarationType == DeclarationType.PARAMETER;
+ }
+
+ public boolean isAttribute() {
+ return declarationType == DeclarationType.ATTRIBUTE;
+ }
+
+ /** get the identifier of the type.
+ *
+ * @return the type identifier */
+ public ResolvableIdentifier getTypeIdentifier() {
+ return typeIdentifier;
+ }
+
+ /** get the type.
+ *
+ * @return the type */
+ public TypeDeclaration getType() {
+ return type;
+ }
+
+ /** set the type
+ *
+ * @param type */
+ public void setType(TypeDeclaration type) {
+ this.type = type;
+ }
+
+ /** get if this variable is global.
+ *
+ * @return if global */
+ public boolean getIsGlobal() {
+ return isGlobal;
+ }
+
+ /** set if this variable is global.
+ *
+ * @param isGlobal
+ * if global */
+ public void setIsGlobal(boolean isGlobal) {
+ this.isGlobal = isGlobal;
+ }
+
+ /** Get the attributeIndex. */
+ public int getAttributeIndex() {
+ return attributeIndex;
+ }
+
+ /** Set the attributeIndex. */
+ public void setAttributeIndex(int attributeIndex) {
+ this.attributeIndex = attributeIndex;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/CastExpression.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/CastExpression.java
new file mode 100644
index 0000000..0c6f85a
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/CastExpression.java
@@ -0,0 +1,73 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.declaration.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class CastExpression extends Expression {
+
+ private Expression expression;
+ private ResolvableIdentifier castIdentifier;
+
+ public CastExpression(Position position, Expression expression, ResolvableIdentifier castIdentifier) {
+ super(position);
+ this.expression = expression;
+ this.castIdentifier = castIdentifier;
+ }
+
+ public ResolvableIdentifier getCastIdentifier() {
+ return castIdentifier;
+ }
+
+ public Expression getExpression() {
+ return expression;
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ visitor.visitDoubleDispatched(expression);
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/ConditionalExpression.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/ConditionalExpression.java
new file mode 100644
index 0000000..420a729
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/ConditionalExpression.java
@@ -0,0 +1,92 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class ConditionalExpression extends Expression {
+ private final Expression condition;
+ private final Expression thenExpression;
+ private final Expression elseExpression;
+
+ public ConditionalExpression(Position position, Expression condition, Expression thenExpression,
+ Expression elseExpression) {
+ super(position);
+ this.condition = condition;
+ this.thenExpression = thenExpression;
+ this.elseExpression = elseExpression;
+ }
+
+ /** get the condition.
+ *
+ * @return the condition */
+ public Expression getCondition() {
+ return condition;
+ }
+
+ /** get the expression of the then part.
+ *
+ * @return the then expression */
+ public Expression getThenExpression() {
+ return thenExpression;
+ }
+
+ /** get the expression of the else part.
+ *
+ * @return the else expression */
+ public Expression getElseExpression() {
+ return elseExpression;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ visitor.visitDoubleDispatched(condition);
+ visitor.visitDoubleDispatched(thenExpression);
+ visitor.visitDoubleDispatched(elseExpression);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/Expression.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/Expression.java
new file mode 100644
index 0000000..e5d8c92
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/Expression.java
@@ -0,0 +1,74 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.declaration.TypeDeclaration;
+
+/** The base class for every expression.
+ *
+ * An expression has a type which must be set by a visitor. */
+public abstract class Expression extends BasicASTNode {
+
+ /** The type. */
+ private TypeDeclaration type;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node */
+ public Expression(Position position) {
+ super(position);
+ }
+
+ /** Set the type.
+ *
+ * @param type
+ * the new type */
+ public void setType(TypeDeclaration type) {
+ this.type = type;
+ }
+
+ /** Get the type.
+ *
+ * @return the type */
+ public TypeDeclaration getType() {
+ return type;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/FunctionCall.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/FunctionCall.java
new file mode 100644
index 0000000..6f7ca2f
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/FunctionCall.java
@@ -0,0 +1,106 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression;
+
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.ast.ResolvableIdentifier;
+import de.uni.bremen.monty.moco.ast.Identifier;
+import de.uni.bremen.monty.moco.ast.declaration.ProcedureDeclaration;
+import de.uni.bremen.monty.moco.ast.statement.Statement;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+import java.util.List;
+
+public class FunctionCall extends Expression implements Statement {
+ private final ResolvableIdentifier identifier;
+ private final List arguments;
+ private ProcedureDeclaration declaration;
+
+ public FunctionCall(Position position, ResolvableIdentifier identifier, List arguments) {
+ super(position);
+ this.identifier = identifier;
+ this.arguments = arguments;
+ }
+
+ /** get the identifier.
+ *
+ * @return the identifier */
+ public ResolvableIdentifier getIdentifier() {
+ return identifier;
+ }
+
+ /** get the List of paramter
+ *
+ * @return the paramters */
+ public List getArguments() {
+ return arguments;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ for (Expression expression : arguments) {
+ visitor.visitDoubleDispatched(expression);
+ }
+ }
+
+ /** @return the declaration */
+ public ProcedureDeclaration getDeclaration() {
+ return declaration;
+ }
+
+ /** @param declaration
+ * the declaration to set */
+ public void setDeclaration(ProcedureDeclaration declaration) {
+ this.declaration = declaration;
+ }
+
+ /** Get mangled identifier
+ *
+ * @return the mangled identifier */
+ public Identifier getMangledIdentifier() {
+ return declaration.getMangledIdentifier();
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/IsExpression.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/IsExpression.java
new file mode 100644
index 0000000..024978d
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/IsExpression.java
@@ -0,0 +1,82 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.declaration.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class IsExpression extends Expression {
+
+ private Expression expression;
+ private ResolvableIdentifier isIdentifier;
+ private TypeDeclaration toType;
+
+ public IsExpression(Position position, Expression expression, ResolvableIdentifier isIdentifier) {
+ super(position);
+ this.expression = expression;
+ this.isIdentifier = isIdentifier;
+ }
+
+ public ResolvableIdentifier getIsIdentifier() {
+ return isIdentifier;
+ }
+
+ public Expression getExpression() {
+ return expression;
+ }
+
+ public void setToType(TypeDeclaration toType) {
+ this.toType = toType;
+ }
+
+ public TypeDeclaration getToType() {
+ return toType;
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ visitor.visitDoubleDispatched(expression);
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/MemberAccess.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/MemberAccess.java
new file mode 100644
index 0000000..b055f22
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/MemberAccess.java
@@ -0,0 +1,82 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression;
+
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.ast.statement.Statement;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class MemberAccess extends Expression implements Statement {
+ private final Expression left;
+ private final Expression right;
+
+ public MemberAccess(Position position, Expression left, Expression right) {
+ super(position);
+ this.left = left;
+ this.right = right;
+ }
+
+ /** get the left expression
+ *
+ * @return left */
+ public Expression getLeft() {
+ return left;
+ }
+
+ /** get the right expression
+ *
+ * @return right */
+ public Expression getRight() {
+ return right;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ visitor.visitDoubleDispatched(left);
+ visitor.visitDoubleDispatched(right);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/ParentExpression.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/ParentExpression.java
new file mode 100644
index 0000000..4cb5c22
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/ParentExpression.java
@@ -0,0 +1,75 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.declaration.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class ParentExpression extends Expression {
+
+ private ResolvableIdentifier parentIdentifier;
+ private ClassDeclaration selfType;
+
+ public ParentExpression(Position position, ResolvableIdentifier parentIdentifier) {
+ super(position);
+ this.parentIdentifier = parentIdentifier;
+ }
+
+ public ResolvableIdentifier getParentIdentifier() {
+ return parentIdentifier;
+ }
+
+ public ClassDeclaration getSelfType() {
+ return selfType;
+ }
+
+ public void setSelfType(ClassDeclaration selfType) {
+ this.selfType = selfType;
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/SelfExpression.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/SelfExpression.java
new file mode 100644
index 0000000..95a1885
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/SelfExpression.java
@@ -0,0 +1,58 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression;
+
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class SelfExpression extends Expression {
+
+ public SelfExpression(Position position) {
+ super(position);
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/VariableAccess.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/VariableAccess.java
new file mode 100644
index 0000000..8ea145d
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/VariableAccess.java
@@ -0,0 +1,107 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.declaration.Declaration;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+/** VariableAccess is an expression that references a local variable or a member of an object. */
+public class VariableAccess extends Expression {
+
+ /** Identifier of the variable to access. */
+ private final ResolvableIdentifier identifier;
+ private Declaration declaration;
+
+ /** Is this a L-value? */
+ private boolean lValue = false;
+
+ public VariableAccess(Position position, ResolvableIdentifier identifier) {
+ super(position);
+ this.identifier = identifier;
+ }
+
+ /** Get the identifier of the variable to access.
+ *
+ * @return the identifier */
+ public ResolvableIdentifier getIdentifier() {
+ return identifier;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ }
+
+ /** Mark this VariableAccess as a L-value. */
+ public void setLValue() {
+ lValue = true;
+ }
+
+ /** Is this a L-value?
+ *
+ * @return if L-value */
+ public boolean getLValue() {
+ return lValue;
+ }
+
+ /** @return the declaration */
+ public Declaration getDeclaration() {
+ return declaration;
+ }
+
+ /** @param declaration
+ * the declaration to set */
+ public void setDeclaration(Declaration declaration) {
+ this.declaration = declaration;
+ }
+
+ /** Get mangled identifier
+ *
+ * @return the mangled identifier */
+ public Identifier getMangledIdentifier() {
+ return declaration.getMangledIdentifier();
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/ArrayLiteral.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/ArrayLiteral.java
new file mode 100644
index 0000000..d41f185
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/ArrayLiteral.java
@@ -0,0 +1,78 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+
+package de.uni.bremen.monty.moco.ast.expression.literal;
+
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.ast.expression.Expression;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ArrayLiteral extends Expression {
+
+ private List entries;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param entries */
+ public ArrayLiteral(Position position, ArrayList entries) {
+ super(position);
+ this.entries = entries;
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ for (Expression entry : entries) {
+ entry.visit(visitor);
+ }
+ }
+
+ public List getEntries() {
+ return entries;
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/BooleanLiteral.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/BooleanLiteral.java
new file mode 100644
index 0000000..2121e34
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/BooleanLiteral.java
@@ -0,0 +1,56 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression.literal;
+
+import de.uni.bremen.monty.moco.ast.CoreClasses;
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class BooleanLiteral extends LiteralExpression {
+
+ public BooleanLiteral(Position position, Boolean value) {
+ super(position, value);
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/CharacterLiteral.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/CharacterLiteral.java
new file mode 100644
index 0000000..81f8a6e
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/CharacterLiteral.java
@@ -0,0 +1,56 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+
+package de.uni.bremen.monty.moco.ast.expression.literal;
+
+import de.uni.bremen.monty.moco.ast.CoreClasses;
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class CharacterLiteral extends LiteralExpression {
+ public CharacterLiteral(Position position, Character value) {
+ super(position, value);
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/FloatLiteral.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/FloatLiteral.java
new file mode 100644
index 0000000..86cac4b
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/FloatLiteral.java
@@ -0,0 +1,56 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression.literal;
+
+import de.uni.bremen.monty.moco.ast.CoreClasses;
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class FloatLiteral extends LiteralExpression {
+
+ public FloatLiteral(Position position, Float value) {
+ super(position, value);
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/IntegerLiteral.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/IntegerLiteral.java
new file mode 100644
index 0000000..8880f74
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/IntegerLiteral.java
@@ -0,0 +1,56 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression.literal;
+
+import de.uni.bremen.monty.moco.ast.CoreClasses;
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class IntegerLiteral extends LiteralExpression {
+
+ public IntegerLiteral(Position position, Integer value) {
+ super(position, value);
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/LiteralExpression.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/LiteralExpression.java
new file mode 100644
index 0000000..3e99bf4
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/LiteralExpression.java
@@ -0,0 +1,60 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression.literal;
+
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.ast.expression.Expression;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public abstract class LiteralExpression extends Expression {
+ protected T value;
+
+ public LiteralExpression(Position position, T value) {
+ super(position);
+ this.value = value;
+ }
+
+ public T getValue() {
+ return value;
+ }
+
+ @Override
+ public final void visitChildren(BaseVisitor visitor) {
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/StringLiteral.java b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/StringLiteral.java
new file mode 100644
index 0000000..d15f4d6
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/expression/literal/StringLiteral.java
@@ -0,0 +1,56 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.expression.literal;
+
+import de.uni.bremen.monty.moco.ast.CoreClasses;
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class StringLiteral extends LiteralExpression {
+ public StringLiteral(Position position, String value) {
+ super(position, value.replaceAll("\"", ""));
+ setType(CoreClasses.stringType());
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/Assignment.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/Assignment.java
new file mode 100644
index 0000000..bd6b4bb
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/Assignment.java
@@ -0,0 +1,94 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.expression.Expression;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class Assignment extends BasicASTNode implements Statement {
+
+ /** The left side. */
+ private final Expression left;
+
+ /** The right side. */
+ private final Expression right;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param left
+ * the left side
+ * @param right
+ * the right side */
+ public Assignment(Position position, Expression left, Expression right) {
+ super(position);
+ this.left = left;
+ this.right = right;
+ }
+
+ /** Get the left side.
+ *
+ * @return the left side */
+ public Expression getLeft() {
+ return left;
+ }
+
+ /** Get the right side.
+ *
+ * @return the right side */
+ public Expression getRight() {
+ return right;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ visitor.visitDoubleDispatched(left);
+ visitor.visitDoubleDispatched(right);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/BreakStatement.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/BreakStatement.java
new file mode 100644
index 0000000..a0ece7b
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/BreakStatement.java
@@ -0,0 +1,75 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class BreakStatement extends BasicASTNode implements Statement {
+
+ /** The loop to skip.* */
+ private WhileLoop loop;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node */
+ public BreakStatement(Position position) {
+ super(position);
+ }
+
+ public void setLoop(WhileLoop loop) {
+ this.loop = loop;
+ }
+
+ public WhileLoop getLoop() {
+ return loop;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/ConditionalStatement.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/ConditionalStatement.java
new file mode 100644
index 0000000..b8b34bd
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/ConditionalStatement.java
@@ -0,0 +1,103 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.expression.Expression;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class ConditionalStatement extends BasicASTNode implements Statement {
+
+ private final Expression condition;
+ private final Block thenBlock;
+ private final Block elseBlock;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param condition
+ * the condition
+ * @param thenBlock
+ * block if condition is true
+ * @param elseBlock
+ * block if condition is false */
+ public ConditionalStatement(Position position, Expression condition, Block thenBlock, Block elseBlock) {
+ super(position);
+ this.condition = condition;
+ this.thenBlock = thenBlock;
+ this.elseBlock = elseBlock;
+ }
+
+ /** get the condition
+ *
+ * @return the condition */
+ public Expression getCondition() {
+ return condition;
+ }
+
+ /** get the then block
+ *
+ * @return the then block */
+ public Block getThenBlock() {
+ return thenBlock;
+ }
+
+ /** get the else block
+ *
+ * @return the else block */
+ public Block getElseBlock() {
+ return elseBlock;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ visitor.visitDoubleDispatched(condition);
+ visitor.visitDoubleDispatched(thenBlock);
+ visitor.visitDoubleDispatched(elseBlock);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/ContinueStatement.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/ContinueStatement.java
new file mode 100644
index 0000000..71a5f2e
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/ContinueStatement.java
@@ -0,0 +1,66 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class ContinueStatement extends BasicASTNode implements Statement {
+
+ /** Constructor
+ *
+ * @param position
+ * Position of this node */
+ public ContinueStatement(Position position) {
+ super(position);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/RaiseStatement.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/RaiseStatement.java
new file mode 100644
index 0000000..be776c3
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/RaiseStatement.java
@@ -0,0 +1,63 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.BasicASTNode;
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.ast.expression.Expression;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class RaiseStatement extends BasicASTNode implements Statement {
+
+ private Expression expression;
+
+ public RaiseStatement(Position position, Expression expression) {
+ super(position);
+ this.expression = expression;
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ }
+
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/ReturnStatement.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/ReturnStatement.java
new file mode 100644
index 0000000..8ae4025
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/ReturnStatement.java
@@ -0,0 +1,80 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.expression.Expression;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class ReturnStatement extends BasicASTNode implements Statement {
+ private Expression parameter;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param parameter
+ * the expression to return */
+ public ReturnStatement(Position position, Expression parameter) {
+ super(position);
+ this.parameter = parameter;
+ }
+
+ /** get the paramter
+ *
+ * @return the paramter */
+ public Expression getParameter() {
+ return parameter;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ if (parameter != null) {
+ visitor.visitDoubleDispatched(parameter);
+ }
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/SkipStatement.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/SkipStatement.java
new file mode 100644
index 0000000..159852e
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/SkipStatement.java
@@ -0,0 +1,71 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.BasicASTNode;
+import de.uni.bremen.monty.moco.ast.Position;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class SkipStatement extends BasicASTNode implements Statement {
+
+ /** The loop to skip.* */
+ private WhileLoop loop;
+
+ public SkipStatement(Position position) {
+ super(position);
+ }
+
+ public void setLoop(WhileLoop loop) {
+ this.loop = loop;
+ }
+
+ public WhileLoop getLoop() {
+ return loop;
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+
+ }
+
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ }
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/Statement.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/Statement.java
new file mode 100644
index 0000000..fe6b4f3
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/Statement.java
@@ -0,0 +1,46 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.*;
+
+/** The baseclass for every statement. */
+public interface Statement extends ASTNode {
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/TryStatement.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/TryStatement.java
new file mode 100644
index 0000000..e13dd39
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/TryStatement.java
@@ -0,0 +1,80 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.declaration.VariableDeclaration;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class TryStatement extends BasicASTNode implements Statement {
+
+ VariableDeclaration handle;
+ Block tryBlock;
+ Block handleBlock;
+
+ public TryStatement(Position position, VariableDeclaration handle, Block tryBlock, Block handleBlock) {
+ super(position);
+ this.handle = handle;
+ this.tryBlock = tryBlock;
+ this.handleBlock = handleBlock;
+ }
+
+ public Block getTryBlock() {
+
+ return tryBlock;
+ }
+
+ public Block getHandleBlock() {
+
+ return handleBlock;
+ }
+
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ visitor.visitDoubleDispatched(tryBlock);
+ visitor.visitDoubleDispatched(handleBlock);
+
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/ast/statement/WhileLoop.java b/src/main/java/de/uni/bremen/monty/moco/ast/statement/WhileLoop.java
new file mode 100644
index 0000000..a0aaed9
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/ast/statement/WhileLoop.java
@@ -0,0 +1,91 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.ast.statement;
+
+import de.uni.bremen.monty.moco.ast.*;
+import de.uni.bremen.monty.moco.ast.expression.Expression;
+import de.uni.bremen.monty.moco.visitor.BaseVisitor;
+
+public class WhileLoop extends BasicASTNode implements Statement {
+
+ private final Expression condition;
+ private final Block body;
+
+ /** Constructor.
+ *
+ * @param position
+ * Position of this node
+ * @param condition
+ * the condition
+ * @param body
+ * loop-body */
+ public WhileLoop(Position position, Expression condition, Block body) {
+ super(position);
+ this.condition = condition;
+ this.body = body;
+ }
+
+ /** get the condition
+ *
+ * @return the condition */
+ public Expression getCondition() {
+ return condition;
+ }
+
+ /** get the body
+ *
+ * @return the body */
+ public Block getBody() {
+ return body;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visit(BaseVisitor visitor) {
+ visitor.visit(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void visitChildren(BaseVisitor visitor) {
+ visitor.visitDoubleDispatched(condition);
+ visitor.visitDoubleDispatched(body);
+ }
+
+}
diff --git a/src/main/java/de/uni/bremen/monty/moco/codegeneration/CodeGenerator.java b/src/main/java/de/uni/bremen/monty/moco/codegeneration/CodeGenerator.java
new file mode 100644
index 0000000..c120b66
--- /dev/null
+++ b/src/main/java/de/uni/bremen/monty/moco/codegeneration/CodeGenerator.java
@@ -0,0 +1,584 @@
+/*
+ * moco, the Monty Compiler
+ * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
+ *
+ * This file is part of moco, the Monty Compiler.
+ *
+ * moco is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 3.0 of the License, or (at your option) any later version.
+ *
+ * moco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * Linking this program and/or its accompanying libraries statically or
+ * dynamically with other modules is making a combined work based on this
+ * program. Thus, the terms and conditions of the GNU General Public License
+ * cover the whole combination.
+ *
+ * As a special exception, the copyright holders of moco give
+ * you permission to link this programm and/or its accompanying libraries
+ * with independent modules to produce an executable, regardless of the
+ * license terms of these independent modules, and to copy and distribute the
+ * resulting executable under terms of your choice, provided that you also meet,
+ * for each linked independent module, the terms and conditions of the
+ * license of that module.
+ *
+ * An independent module is a module which is not
+ * derived from or based on this program and/or its accompanying libraries.
+ * If you modify this library, you may extend this exception to your version of
+ * the program or library, but you are not obliged to do so. If you do not wish
+ * to do so, delete this exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library.
+ */
+package de.uni.bremen.monty.moco.codegeneration;
+
+import de.uni.bremen.monty.moco.ast.ASTNode;
+import de.uni.bremen.monty.moco.ast.CoreClasses;
+import de.uni.bremen.monty.moco.ast.declaration.*;
+import de.uni.bremen.monty.moco.codegeneration.context.CodeContext;
+import de.uni.bremen.monty.moco.codegeneration.context.CodeContext.LLVMFunctionAttribute;
+import de.uni.bremen.monty.moco.codegeneration.context.CodeContext.Linkage;
+import de.uni.bremen.monty.moco.codegeneration.context.Operations;
+import de.uni.bremen.monty.moco.codegeneration.identifier.FunctionSignature;
+import de.uni.bremen.monty.moco.codegeneration.identifier.LLVMIdentifier;
+import de.uni.bremen.monty.moco.codegeneration.identifier.LLVMIdentifierFactory;
+import de.uni.bremen.monty.moco.codegeneration.types.*;
+import de.uni.bremen.monty.moco.codegeneration.voodoo.BlackMagic;
+
+import java.util.*;
+
+import static de.uni.bremen.monty.moco.codegeneration.types.LLVMTypeFactory.*;
+
+/** This class should contain most of the logic for the CodeGeneration. Mainly methods are called from the
+ * CodeGenerationVisitor and have a Parameter 'CodeContext c'. On this argument LLVM instructions can be executed.
+ *
+ * It is an mapping layer between the CodeGenerationVisitor on one side and CodeContext on the other. The
+ * CodeGenerationVisitor is mainly influenced by the Monty-AST and the CodeContext only know LLVM instructions. So the
+ * CodeGenerator is the where most parts of the mapping between those two languages exists.
+ *
+ * An simple task of mapping is e.g. map from monty-Types (TypeDeclaration) to LLVM-Types(LLVMType) */
+public class CodeGenerator {
+ private final Operations operations;
+ private final BlackMagic blackMagic;
+ private final TypeConverter typeConverter;
+
+ /*
+ * Map an ASTNode to a label prefix.
+ */
+ protected HashMap node2label = new HashMap<>();
+
+ /*
+ * Map each label to its number of occurrences. We use this information to create unique labels in the resulting
+ * LLVM code.
+ */
+ protected HashMap label2occurrences = new HashMap<>();
+
+ private LLVMIdentifierFactory llvmIdentifierFactory;
+
+ public CodeGenerator(TypeConverter typeConverter, LLVMIdentifierFactory llvmIdentifierFactory) {
+ this.typeConverter = typeConverter;
+ this.llvmIdentifierFactory = llvmIdentifierFactory;
+ operations = new Operations(this, llvmIdentifierFactory);
+ blackMagic = new BlackMagic(operations);
+ initFormatStrings();
+ }
+
+ public void initFormatStrings() {
+ LLVMArrayType stringType = array(int8(), 3);
+ LLVMIdentifier> stringFormatIdent =
+ llvmIdentifierFactory.newGlobal(".stringFormat", stringType);
+ operations.setStringFormat(llvmIdentifierFactory.elementPointerTo(stringFormatIdent));
+ LLVMIdentifier> intFormatIdent =
+ llvmIdentifierFactory.newGlobal(".intFormat", stringType);
+ operations.setIntFormat(llvmIdentifierFactory.elementPointerTo(intFormatIdent));
+ LLVMIdentifier> floatFormatIdent =
+ llvmIdentifierFactory.newGlobal(".floatFormat", stringType);
+ operations.setFloatFormat(llvmIdentifierFactory.elementPointerTo(floatFormatIdent));
+ LLVMIdentifier> charFormatIdent =
+ llvmIdentifierFactory.newGlobal(".charFormat", stringType);
+ operations.setCharFormat(llvmIdentifierFactory.elementPointerTo(charFormatIdent));
+ }
+
+ private LLVMIdentifier> castIfNeeded(CodeContext c,
+ LLVMIdentifier> variable, LLVMPointer toType) {
+ if (!variable.getType().equals(toType)) {
+ LLVMIdentifier> castedVariable =
+ llvmIdentifierFactory.newLocal(toType, variable.needToBeResolved());
+ c.bitcast(castedVariable, variable);
+ return castedVariable;
+ }
+ return variable;
+ }
+
+ private LLVMIdentifier castIfNeeded(CodeContext c, LLVMIdentifier variable, T toType) {
+ if ((variable.getType() instanceof LLVMPointer) && (toType instanceof LLVMPointer)) {
+ return (LLVMIdentifier) (LLVMIdentifier>) castIfNeeded(
+ c,
+ (LLVMIdentifier>) (LLVMIdentifier>) variable,
+ (LLVMPointer) toType);
+ }
+ return variable;
+ }
+
+ private LLVMIdentifier resolveIfNeeded(CodeContext c, LLVMIdentifier addr) {
+ if (addr.needToBeResolved()) {
+ LLVMIdentifier> sourcePointer = llvmIdentifierFactory.pointerTo(addr);
+ LLVMIdentifier targetPointer = llvmIdentifierFactory.newLocal(addr.getType(), false);
+ return c.load(sourcePointer, targetPointer);
+ } else {
+ return addr;
+ }
+ }
+
+ private List> resolveArgumentsIfNeeded(CodeContext c,
+ List> arguments, List parameters) {
+ List> resolvedArguments = new ArrayList<>(arguments.size());
+ for (int i = 0; i < arguments.size(); i++) {
+ LLVMIdentifier resolvedArgument = resolveIfNeeded(c, (LLVMIdentifier) arguments.get(i));
+ LLVMType expectedType = mapToLLVMType(parameters.get(i));
+ resolvedArguments.add(castIfNeeded(c, resolvedArgument, expectedType));
+ }
+ return resolvedArguments;
+ }
+
+ private List> unboxArgumentsIfNeeded(CodeContext c,
+ List> arguments) {
+ List> unboxedArguments = new ArrayList<>(arguments.size());
+ for (LLVMIdentifier> llvmIdentifier : arguments) {
+ if (llvmIdentifier.getType().equals(mapToLLVMType(CoreClasses.intType()))) {
+ unboxedArguments.add(unboxType(c, (LLVMIdentifier) llvmIdentifier, int64()));
+ } else if (llvmIdentifier.getType().equals(mapToLLVMType(CoreClasses.boolType()))) {
+ unboxedArguments.add(unboxType(c, (LLVMIdentifier) llvmIdentifier, int1()));
+ } else if (llvmIdentifier.getType().equals(mapToLLVMType(CoreClasses.floatType()))) {
+ unboxedArguments.add(unboxType(c, (LLVMIdentifier) llvmIdentifier, double64()));
+ } else if (llvmIdentifier.getType().equals(mapToLLVMType(CoreClasses.charType()))) {
+ unboxedArguments.add(unboxType(c, (LLVMIdentifier) llvmIdentifier, int8()));
+ } else {
+ unboxedArguments.add(llvmIdentifier);
+ }
+ }
+ return unboxedArguments;
+ }
+
+ private LLVMIdentifier> addStringToDataField(CodeContext c, String value) {
+ int length = value.length() + 1;
+
+ LLVMArrayType type = array(int8(), length);
+ LLVMIdentifier> identifier = llvmIdentifierFactory.newGlobal(type);
+ String internValue = "c\"" + value + "\\00\";";
+ c.global(Linkage.priv, (LLVMIdentifier) (LLVMIdentifier>) identifier, true, internValue);
+ return identifier;
+ }
+
+ private LLVMIdentifier> getVMTPointer(CodeContext c,
+ LLVMIdentifier> selfReference, ClassDeclaration classDeclaration) {
+ LLVMPointer vmtType =
+ pointer((LLVMType) struct(classDeclaration.getMangledIdentifier().getSymbol() + "_vmt_type"));
+ LLVMIdentifier> vmtPointer = llvmIdentifierFactory.newLocal(vmtType, true);
+ c.getelementptr(
+ (LLVMIdentifier) (LLVMIdentifier>) vmtPointer,
+ selfReference,
+ llvmIdentifierFactory.constant(int32(), 0),
+ llvmIdentifierFactory.constant(int32(), 0));
+ return vmtPointer;
+ }
+
+ private LLVMIdentifier> getFunctionPointer(CodeContext c,
+ LLVMIdentifier> selfReference, ProcedureDeclaration declaration) {
+ LLVMIdentifier> vmtPointer =
+ getVMTPointer(c, selfReference, (ClassDeclaration) declaration.getParentNode().getParentNode());
+
+ LLVMPointer functionType = mapToLLVMType(declaration);
+ LLVMIdentifier> functionPointer = llvmIdentifierFactory.newLocal(functionType);
+ c.getelementptr(
+ functionPointer,
+ resolveIfNeeded(c, vmtPointer),
+ llvmIdentifierFactory.constant(int32(), 0),
+ llvmIdentifierFactory.constant(int32(), declaration.getVMTIndex()));
+ return functionPointer;
+ }
+
+ public void buildConstructor(CodeContext c, ClassDeclaration classDeclaration) {
+ List> llvmParameter = new ArrayList<>();
+ String constructorName = classDeclaration.getMangledIdentifier().getSymbol() + "_constructor";
+ addFunction(c, classDeclaration, llvmParameter, constructorName);
+
+ LLVMPointer selfType = mapToLLVMType(classDeclaration);
+ LLVMIdentifier> selfReference = llvmIdentifierFactory.newLocal(selfType, false);
+ malloc(c, selfReference);
+
+ LLVMIdentifier vmtPointer =
+ (LLVMIdentifier) (LLVMIdentifier>) getVMTPointer(c, selfReference, classDeclaration);
+ LLVMIdentifier vmtData =
+ llvmIdentifierFactory.newGlobal(
+ classDeclaration.getMangledIdentifier().getSymbol() + "_vmt_data",
+ vmtPointer.getType());
+ c.store(vmtData, llvmIdentifierFactory.pointerTo(vmtPointer));
+
+ returnValue(c, (LLVMIdentifier) selfReference, classDeclaration);
+ }
+
+ public LLVMIdentifier callConstructor(CodeContext c, ClassDeclaration classDeclaration) {
+ LLVMIdentifier result = llvmIdentifierFactory.newLocal(mapToLLVMType(classDeclaration), false);
+ LLVMIdentifier signature =
+ llvmIdentifierFactory.newGlobal(
+ classDeclaration.getMangledIdentifier().getSymbol() + "_constructor",
+ result.getType());
+ c.call(signature, result);
+ return result;
+ }
+
+ /** Create a unique label prefix and store it under an association with the given node. */
+ public String createLabelPrefix(String name, ASTNode node) {
+ if (!label2occurrences.containsKey(name)) {
+ label2occurrences.put(name, 0);
+ }
+ int id = label2occurrences.get(name);
+ String label = name + id;
+ label2occurrences.put(name, id + 1);
+ node2label.put(node, label);
+ return label;
+ }
+
+ /** Get the unique label prefix associated with the given node.
+ *
+ * This is a Map lookup so the error will be thrown if node does not exist as key */
+ public String getLabelPrefix(ASTNode node) {
+ return node2label.get(node);
+ }
+
+ public LLVMIdentifier declareGlobalVariable(CodeContext c, String name, TypeDeclaration type) {
+ LLVMType llvmType = mapToLLVMType(type);
+ LLVMIdentifier variable = llvmIdentifierFactory.newGlobal(name, llvmType);
+ c.global(Linkage.priv, variable, false);
+ return variable;
+ }
+
+ public LLVMIdentifier declareLocalVariable(CodeContext c, String name, TypeDeclaration type) {
+ LLVMType llvmType = mapToLLVMType(type);
+
+ LLVMIdentifier variable = llvmIdentifierFactory.newLocal(name, llvmType, true);
+ c.alloca(variable, llvmType);
+ return variable;
+ }
+
+ public LLVMIdentifier resolveLocalVarName(String name, TypeDeclaration type,
+ boolean resolvable) {
+ T llvmType = mapToLLVMType(type);
+ return llvmIdentifierFactory.newLocal(name, llvmType, resolvable);
+ }
+
+ public LLVMIdentifier resolveGlobalVarName(String name, TypeDeclaration type) {
+ T llvmType = mapToLLVMType(type);
+ return llvmIdentifierFactory.newGlobal(name, llvmType);
+ }
+
+ public void addMain(CodeContext active) {
+
+ FunctionSignature> mainFunction =
+ llvmIdentifierFactory.newFunction(
+ int32(),
+ "main",
+ Collections.> emptyList());
+
+ active.define(Arrays.asList(LLVMFunctionAttribute.ssp), mainFunction);
+ active.label("entry");
+ }
+
+ public void addFunction(CodeContext c, TypeDeclaration returnType,
+ List> llvmParameter, String name) {
+ LLVMType llvmReturnType = mapToLLVMType(returnType);
+ c.define(
+ new ArrayList(),
+ llvmIdentifierFactory.newFunction(llvmReturnType, name, llvmParameter));
+ c.label("entry");
+ }
+
+ public void addNativeFunction(CodeContext c, TypeDeclaration returnType,
+ List> llvmParameter, String name) {
+ addFunction(c, returnType, llvmParameter, name);
+
+ List> unboxedParameter = unboxArgumentsIfNeeded(c, llvmParameter);
+
+ LLVMIdentifier result =
+ (LLVMIdentifier) blackMagic.generateNativeFunction(c, name, unboxedParameter);
+ if (result != null) {
+ if (result.getType() instanceof LLVMPointer) {
+ returnValue(c, result, returnType);
+ } else {
+ returnValue(c, boxType(c, result, returnType), returnType);
+ }
+ } else {
+ returnValue(
+ c,
+ (LLVMIdentifier) (LLVMIdentifier>) llvmIdentifierFactory.voidId(),
+ CoreClasses.voidType());
+ }
+ }
+
+ public void returnMain(CodeContext c) {
+ c.ret(llvmIdentifierFactory.constant(int32(), 0));
+ }
+
+ public void returnValue(CodeContext c, LLVMIdentifier returnValue, TypeDeclaration expectedType) {
+ LLVMIdentifier resolved = resolveIfNeeded(c, returnValue);
+ LLVMIdentifier casted = castIfNeeded(c, resolved, mapToLLVMType(expectedType));
+ c.ret(casted);
+ }
+
+ public LLVMIdentifier> addConstantString(CodeContext constant, String value) {
+ LLVMIdentifier> nameOfDataField = addStringToDataField(constant, value);
+ LLVMIdentifier> stringAsCharPointer =
+ llvmIdentifierFactory.elementPointerTo(nameOfDataField);
+ return stringAsCharPointer;
+ }
+
+ public void exit(CodeContext c, int statusCode) {
+ LLVMIdentifier signature = llvmIdentifierFactory.newGlobal("exit", (LLVMType) voidType());
+ c.callVoid(signature, llvmIdentifierFactory.constant(int32(), statusCode));
+ }
+
+ /** Allocates heap memory for the given type and return a typed pointer. */
+ public LLVMIdentifier> malloc(CodeContext c,
+ LLVMIdentifier> result) {
+ return malloc(c, result, (LLVMPointer) result.getType());
+ }
+
+ /** Allocates heap memory for the given type and return a typed pointer. */
+ public LLVMIdentifier> malloc(CodeContext c,
+ LLVMIdentifier> result, LLVMPointer inputType) {
+
+ LLVMIdentifier> sizePtr = llvmIdentifierFactory.newLocal(inputType);
+ c.getelementptr(
+ sizePtr,
+ llvmIdentifierFactory.constantNull(inputType),
+ llvmIdentifierFactory.constant(int32(), 1));
+ LLVMIdentifier sizeInt = llvmIdentifierFactory.newLocal((LLVMType) int32());
+ c.ptrtoint(sizeInt, (LLVMIdentifier) sizePtr);
+
+ LLVMIdentifier> s = llvmIdentifierFactory.newGlobal("malloc", pointer(int8()));
+ LLVMIdentifier> mallocPtr = llvmIdentifierFactory.newLocal(s.getType());
+ c.call((LLVMIdentifier) (LLVMIdentifier>) s, mallocPtr, sizeInt);
+ c.bitcast((LLVMIdentifier) result, (LLVMIdentifier) mallocPtr);
+ return result;
+ }
+
+ public void assign(CodeContext c, LLVMIdentifier target, LLVMIdentifier source) {
+ source = resolveIfNeeded(c, source);
+ source = castIfNeeded(c, source, target.getType());
+ LLVMIdentifier> targetPointer = llvmIdentifierFactory.pointerTo(target);
+ c.store(source, targetPointer);
+ }
+
+ public LLVMIdentifier accessMember(CodeContext c, LLVMIdentifier> pointer,
+ int attributeOffset, TypeDeclaration type, boolean load) {
+
+ LLVMIdentifier result = llvmIdentifierFactory.newLocal(mapToLLVMType(type), load);
+ c.getelementptr(
+ result,
+ resolveIfNeeded(c, pointer),
+ llvmIdentifierFactory.constant(int32(), 0),
+ llvmIdentifierFactory.constant(int32(), attributeOffset));
+ return result;
+ }
+
+ public T mapToLLVMType(TypeDeclaration type) {
+ return typeConverter.mapToLLVMType(type);
+ }
+
+ public LLVMIdentifier> castClass(CodeContext c,
+ LLVMIdentifier> pointer, ClassDeclaration sourceType, ClassDeclaration resultType,
+ String labelPrefix) {
+
+ String successLabel = labelPrefix + ".success";
+ String failureLabel = labelPrefix + ".failure";
+
+ pointer = resolveIfNeeded(c, pointer);
+ LLVMIdentifier isaCmpResult = isClass(c, pointer, sourceType, resultType);
+
+ c.branch(isaCmpResult, successLabel, failureLabel);
+ c.label(failureLabel);
+ exit(c, 1);
+ c.branch(successLabel);
+ c.label(successLabel);
+ return castIfNeeded(c, pointer, (LLVMPointer) mapToLLVMType(resultType));
+ }
+
+ public LLVMIdentifier isClass(CodeContext c, LLVMIdentifier> pointer,
+ ClassDeclaration sourceType, ClassDeclaration resultType) {
+
+ pointer = resolveIfNeeded(c, pointer);
+ LLVMIdentifier> vmt = getVMTPointer(c, pointer, sourceType);
+ vmt = resolveIfNeeded(c, vmt);
+
+ LLVMType ctType = array(pointer(int8()), sourceType.getSuperClassDeclarationsRecursive().size() + 1);
+ LLVMIdentifier> ct = llvmIdentifierFactory.newLocal(pointer(ctType), true);
+ c.getelementptr(ct, vmt, llvmIdentifierFactory.constant(int32(), 0), llvmIdentifierFactory.constant(int32(), 0));
+ ct = resolveIfNeeded(c, ct);
+
+ LLVMType pointerArrayType = array(pointer(int8()), 0);
+ LLVMIdentifier> pointerArray = llvmIdentifierFactory.newLocal(pointer(pointerArrayType));
+ c.bitcast(pointerArray, ct);
+
+ LLVMType resultVMTType = struct(resultType.getMangledIdentifier().getSymbol() + "_vmt_type");
+ LLVMIdentifier> resultVMT =
+ llvmIdentifierFactory.newGlobal(
+ resultType.getMangledIdentifier().getSymbol() + "_vmt_data",
+ pointer(resultVMTType));
+ LLVMIdentifier> resultPointer =
+ llvmIdentifierFactory.newLocal(pointer((LLVMType) int8()));
+ c.bitcast(resultPointer, resultVMT);
+
+ LLVMIdentifier isaCmpResult = llvmIdentifierFactory.newLocal(int1(), false);
+ LLVMIdentifier isaSignature = llvmIdentifierFactory.newGlobal("vmt_isa_class", int1());
+ c.call((LLVMIdentifier) (LLVMIdentifier>) isaSignature, isaCmpResult, pointerArray, resultPointer);
+ return isaCmpResult;
+ }
+
+ public void checkArrayBounds(CodeContext c, LLVMIdentifier> array,
+ LLVMIdentifier index) {
+
+ List arrayTypeList = Arrays.asList(int64(), array(pointer(int8()), 0));
+ array = castIfNeeded(c, array, pointer(struct(arrayTypeList)));
+ LLVMIdentifier boundsCheckSignature =
+ llvmIdentifierFactory.newGlobal("array_bounds_check", voidType());
+ c.callVoid((LLVMIdentifier) (LLVMIdentifier>) boundsCheckSignature, array, index);
+ }
+
+ public LLVMIdentifier> call(CodeContext c, String functionName, TypeDeclaration returnType,
+ List> arguments, List parameters) {
+ LLVMType llvmReturnType = mapToLLVMType(returnType);
+ LLVMIdentifier functionSignature = llvmIdentifierFactory.newGlobal(functionName, llvmReturnType);
+ List> resolvedArguments = resolveArgumentsIfNeeded(c, arguments, parameters);
+ return c.call(
+ functionSignature,
+ llvmIdentifierFactory.newLocal(functionSignature.getType(), false),
+ resolvedArguments);
+ }
+
+ public void callVoid(CodeContext c, String functionName, List> arguments,
+ List parameters) {
+ List> resolvedArguments = resolveArgumentsIfNeeded(c, arguments, parameters);
+ c.callVoid(llvmIdentifierFactory.newGlobal(functionName, (LLVMType) voidType()), resolvedArguments);
+ }
+
+ public LLVMIdentifier> callMethod(CodeContext c, FunctionDeclaration declaration,
+ List> arguments, List parameters) {
+ List> resolvedArguments = resolveArgumentsIfNeeded(c, arguments, parameters);
+
+ LLVMIdentifier> functionPointer =
+ getFunctionPointer(c, (LLVMIdentifier>) resolvedArguments.get(0), declaration);
+ return c.call(
+ (LLVMIdentifier) (LLVMIdentifier>) resolveIfNeeded(c, functionPointer),
+ llvmIdentifierFactory.newLocal(mapToLLVMType(declaration.getReturnType()), false),
+ resolvedArguments);
+ }
+
+ public void callVoidMethod(CodeContext c, ProcedureDeclaration declaration, List> arguments,
+ List parameters) {
+ List> resolvedArguments = resolveArgumentsIfNeeded(c, arguments, parameters);
+
+ LLVMIdentifier> functionPointer =
+ getFunctionPointer(c, (LLVMIdentifier>) resolvedArguments.get(0), declaration);
+ c.callVoid(
+ (LLVMIdentifier