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

com.pogofish.jadt.samples.ast.Usage Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
/*
Copyright 2012 James Iry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package com.pogofish.jadt.samples.ast;

// START SNIPPET: imports

import static com.pogofish.jadt.samples.ast.data.Arg.*;
import static com.pogofish.jadt.samples.ast.data.Expression.*;
import static com.pogofish.jadt.samples.ast.data.Function.*;
import static com.pogofish.jadt.samples.ast.data.Statement.*;
import static com.pogofish.jadt.samples.ast.data.Type.*;
import static java.util.Arrays.asList;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
// END SNIPPET: imports

import com.pogofish.jadt.samples.ast.data.*;

/**
 * This class shows how to use the code generated by the sample ast.  It is marked up with START SNIPPET and END SNIPPET boundaries to support
 * /jADT/src/site/apt/index.apt
 */
public class Usage {
    public Function sampleFunction() {
        // START SNIPPET: sampleFunction
        Function sampleFunction = _Function(_Int(), "addTwo",
                asList(_Arg(_Int(), "x"), _Arg(_Int(), "y")),
                asList(_Return(_Add(_Variable("x"), _Variable("y")))));
        // END SNIPPET: sampleFunction
        
        return sampleFunction;
    }

    // START SNIPPET: expressionLiterals
    public Set expressionLiterals(Expression expression) {
        return expression.match(new Expression.MatchBlock>() {
            @Override
            public Set _case(Add x) {
                final Set results = expressionLiterals(x.left);
                results.addAll(expressionLiterals(x.right));
                return results;
            }

            @Override
            public Set _case(Variable x) {
                return new HashSet();
            }

            @Override
            public Set _case(IntLiteral x) {
                return new HashSet(asList(x.value));
            }

            @Override
            public Set _case(LongLiteral x) {
                return new HashSet();
            }
        });
    }
    // END SNIPPET: expressionLiterals

    // START SNIPPET: hasReturn
    public boolean hasReturn(List statements) {
        boolean hasReturn = false;
        for (Statement statement : statements) {
            hasReturn = hasReturn || statement.match(new Statement.MatchBlockWithDefault() {
                @Override
                public Boolean _case(Return x) {
                    return true;
                }

                @Override
                public Boolean _default(Statement x) {
                    return false;
                }
            });
        }
        return hasReturn;
    }
    // END SNIPPET: hasReturn
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy