/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 *
 * association.cpp
 *
 * Author: pgess <v.melnychenko@xreate.org>
 * Created on August 12, 2017, 9:28 PM
 */
#include "xreatemanager.h"
#include "clasplayer.h"
#include <gtest/gtest.h>

using namespace xreate;
using namespace std;

TEST(Association, test1){
std::string controller=
R"Code(
     program(add).
)Code";

std::string script=
R"Code(
Annotation = type variant {
    Num:: int,
    String:: string,
    Func:: {name::string, arguments::[Annotation]}
}.

extractCmd  = function(program::Annotation):: Annotation; interpretation(force){
    switch variant(program)::Annotation
    case (Num){String("wrong expression")}
    case (String){String("wrong expression")}
    case (Func){program["arguments"][0]}
}

main= function:: int; entry{
    x= 5::int.
    y = 6::int.

    program = intrinsic query("program")[0]::Annotation.
    cmd = extractCmd(program)::Annotation; interpretation(force).

    answer = switch variant(cmd)::int
        case (Num) {0}
        case (String){0}
        case (Func){
            switch(cmd["name"])::int
            case("add"){x + y}
            case default {0}
        }.

    answer
}
)Code";

    std::unique_ptr<XreateManager> man(XreateManager::prepare(std::move(script)));
    man->clasp->addRawScript(move(controller));
    int (*main)() = (int (*)())man->run();
    int result = main();

    ASSERT_EQ(11, result);
}


//std::string script=
//R"Code(
//
//case context:: test1 {
//test= function::bool; registerTest {
//    x = 8:: int.
//
//    x == 8
//}}
//
//case context:: test2{
//test= function::bool{
//    x = 3::int.
//    y = 12::int.,
//
//    (x+y) <> 25
//}}
//
//runTests= function::bool{
//        tests = intrinsic query (registeredTests)::[Expression].
//
//        loop fold(tests->test::Expression, true->result):: bool {
//            shot = {
//                context:: make context (test)
//                test()
//            }
//
//            result and shot
//        }
//}
//
//main= function:: int; entry{
//        runTests()
//}
//)Code";
