Program Listing for File Blackbird.h

Return to documentation for file (blackbird_cpp/Blackbird.h)

// Copyright 2019 Xanadu Quantum Technologies Inc.

// 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.
#define _USE_MATH_DEFINES

#include <string>
#include <iostream>
#include <cmath>
#include <complex>
#include <stdexcept>
#include <unordered_map>

#include "antlr4-runtime/antlr4-runtime.h"
#include "blackbirdLexer.h"
#include "blackbirdParser.h"
#include "blackbirdBaseVisitor.h"

#include "BlackbirdProgram.h"

namespace blackbird {

    template <class DstType, class SrcType>
    bool is_type(const SrcType* src)
    {
      return dynamic_cast<const DstType*>(src) != nullptr;
    }


    class  Visitor : public blackbirdBaseVisitor {
    private:
        std::string var_type;
        std::string var_name;
    public:
        std::unordered_map<std::string, std::complex<double>> complex_vars;
        std::unordered_map<std::string, double> float_vars;
        std::unordered_map<std::string, int> int_vars;
        std::unordered_map<std::string, std::string> str_vars;
        std::unordered_map<std::string, bool> bool_vars;

        std::unordered_map<std::string, complexmat> complexmat_vars;
        std::unordered_map<std::string, floatmat> floatmat_vars;
        std::unordered_map<std::string, intmat> intmat_vars;

        Program* program;

        template <class O>
        O* _create_operation(blackbirdParser::ArgumentsContext *ctx, intvec modes);

        antlrcpp::Any visitNumber(blackbirdParser::NumberContext *ctx);
        antlrcpp::Any visitExpressionvar(blackbirdParser::ExpressionvarContext *ctx);

        antlrcpp::Any visitArrayvar(blackbirdParser::ArrayvarContext *ctx);
        antlrcpp::Any visitArrayrow(blackbirdParser::ArrayrowContext*);

        antlrcpp::Any visitStatement(blackbirdParser::StatementContext *ctx);
        antlrcpp::Any visitProgram(blackbirdParser::ProgramContext *ctx);

        antlrcpp::Any visitStart(blackbirdParser::StartContext *ctx);
    };

    Program* parse(std::ifstream &stream);
    Program* parse(std::string &s_input);
}