All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.protelis.lang.interpreter.impl.ConditionalSideEffect Maven / Gradle / Ivy

There is a newer version: 17.6.0
Show newest version
/*
 * Copyright (C) 2021, Danilo Pianini and contributors listed in the project's build.gradle.kts or pom.xml file.
 *
 * This file is part of Protelis, and is distributed under the terms of the GNU General Public License,
 * with a linking exception, as described in the file LICENSE.txt in this project's top directory.
 */
package org.protelis.lang.interpreter.impl;

import org.protelis.lang.datatype.Unit;
import org.protelis.lang.interpreter.ProtelisAST;
import org.protelis.lang.interpreter.util.Bytecode;
import org.protelis.lang.loading.Metadata;
import org.protelis.vm.ExecutionContext;

import javax.annotation.Nonnull;

import static org.protelis.lang.interpreter.util.Bytecode.IF_THEN;

/**
 * Branch with side effects, returns {@link Unit}.
 *
 */
public final class ConditionalSideEffect extends AbstractProtelisAST {

    private static final long serialVersionUID = 1L;

    /**
     * @param metadata
     *            A {@link Metadata} object containing information about the code that generated this AST node.
     * @param cond
     *            condition
     * @param then
     *            branch to execute if condition is true (erase otherwise)
     */
    public ConditionalSideEffect(
            @Nonnull final Metadata metadata,
            @Nonnull final ProtelisAST cond,
            @Nonnull final ProtelisAST then) {
        super(metadata, cond, then);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Unit evaluate(final ExecutionContext context) {
        if (condition().eval(context)) {
            context.runInNewStackFrame(IF_THEN.getCode(), then()::eval);
        }
        return Unit.UNIT;
    }

    @Override
    public Bytecode getBytecode() {
        return Bytecode.IF;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "if (" + stringFor(condition()) + ") { " + stringFor(then()) + '}';
    }

    @SuppressWarnings("unchecked")
    private ProtelisAST condition() {
        return (ProtelisAST) getBranch(0);
    }

    private ProtelisAST then() {
        return getBranch(1);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy