<commit_before>//
//  Interpreter.cpp
//  Addie
//
//  Created by <NAME> on 24/04/16.
//  Copyright © 2016 <NAME>, Starcounter AB.
//

#include "VM.hpp"

using namespace Addie::Internals;

// VM byte-codes are described here
// https://github.com/Starcounter-Jack/Addie/wiki

Continuation Interpreter::Interpret( Isolate* isolate, Continuation cont ) {
    
    Instruction* p = cont.PC;
    //        Instruction* start = p;
    VALUE* r = cont.frame->GetStartOfRegisters();
    
    std::cout << "Resuming execution at " << p << "\n";
    
    //        int regCount = cont.frame->Comp->SizeOfRegisters/sizeof(VALUE);
    
    Symbol sym;
    //        uintptr_t address;
    
    while (true) {
        
        
        Instruction i = (*p);
        
        // std::cout << "OPERATION!!! " << (int)i.OP;
        
        switch (i.OP) {
            case END:
                goto end;
                /*
                 case SET_REGISTER_WINDOW:
                 p++;
                 std::cout << "set-reg-win";
                 std::cout << "      (";
                 std::cout << (*((VALUE*)p)).Integer;
                 std::cout << ")";
                 address = (uintptr_t)(*((VALUE*)p)).Integer;
                 r = (VALUE*)address;
                 p = (Instruction*)(((VALUE*)p) + 1); // Move IP past the address
                 
                 for (int t=0;t<regCount;t++) {
                 std::cout << "R[";
                 std::cout << t;
                 std::cout << "]=";
                 std::cout << r[t].Integer;
                 std::cout << "\n";
                 }
                 
                 break;
                 */
                
            case EXIT_WITH_CONTINUATION:
                p++;
                //std::cout << "\nexit-with-continuation @(" << p << ")";
                goto end;
            case (CALL_0):
                //std::cout << "CALL-0";
                //std::cout << " Symbol=";
                //std::cout << r[i.A].Integer;
                //std::cout << "\n";
                p++;
                break;
            case (CALL_1):
                //std::cout << "CALL-1";
                sym = r[i.A].SymbolId;
                if (sym == SymPrint) {
                    std::cout << "\nPrinting " << r[0].Print() << "\n";
                    r[0].Print();
                }
                
                p++;
                
                break;
            case (JMP):
                p += i.A3;
                break;
            case (JMP_IF_TRUE):
                if (r[0].Integer) {
                    p += i.A3;
                }
                break;
            case (MOVE):
                std::cout << "MOVE";
                std::cout << "        (";
                std::cout << (int)p->A;
                std::cout << ",";
                std::cout << (int)p->B;
                std::cout << ")";
                p++;
                
                break;
            case (CALL_2):
                //                   std::cout << "CALL-2";
                
                sym = r[i.A].SymbolId;
                if (sym == SymPlus) {
                    r[0] = INTEGER(r[i.B].Integer + r[i.C].Integer);
                }
                
                p++;
                
                break;
            default:
                std::cout << "Unknown: " << isolate->GetStringFromSymbolId(i.OP) << "\n" ;
                p++;
                break;
                //                   throw std::runtime_error("Illegal OP code");
                //                   break;
        }
    }
end:
    //return cont->Checkpoint();<commit_msg>Added some temporary interpreter code to test<commit_after>//
//  Interpreter.cpp
//  Addie
//
//  Created by <NAME> on 24/04/16.
//  Copyright © 2016 <NAME>, Starcounter AB.
//

#include "VM.hpp"

using namespace Addie::Internals;

// VM byte-codes are described here
// https://github.com/Starcounter-Jack/Addie/wiki

Continuation Interpreter::Interpret( Isolate* isolate, Continuation cont ) {
    
    Instruction* p = cont.PC;
    //        Instruction* start = p;
    VALUE* r = cont.frame->GetStartOfRegisters();
    
    std::cout << "Resuming execution at " << p << "\n";
    
    //        int regCount = cont.frame->Comp->SizeOfRegisters/sizeof(VALUE);
    
    Symbol sym;
    int sum;
    std::string str;

    //        uintptr_t address;
    VALUE a1,a2,a3,a4;

    
    while (true) {
        
        
        Instruction i = (*p);
        
        // std::cout << "OPERATION!!! " << (int)i.OP;
        
        switch (i.OP) {
            case END:
                goto end;
                /*
                 case SET_REGISTER_WINDOW:
                 p++;
                 std::cout << "set-reg-win";
                 std::cout << "      (";
                 std::cout << (*((VALUE*)p)).Integer;
                 std::cout << ")";
                 address = (uintptr_t)(*((VALUE*)p)).Integer;
                 r = (VALUE*)address;
                 p = (Instruction*)(((VALUE*)p) + 1); // Move IP past the address
                 
                 for (int t=0;t<regCount;t++) {
                 std::cout << "R[";
                 std::cout << t;
                 std::cout << "]=";
                 std::cout << r[t].Integer;
                 std::cout << "\n";
                 }
                 
                 break;
                 */
                
            case EXIT_WITH_CONTINUATION:
                p++;
                //std::cout << "\nexit-with-continuation @(" << p << ")";
                goto end;
            case (SCALL_0):
                sym = r[i.B].SymbolId;
                // TODO!
                p++;
                break;
            case (SCALL_1):
                sym = r[i.B].SymbolId;
                a1 = r[i.C];
                // TODO!
                if (sym == SymPrint) {
                    std::cout << "\nPrinting " << a1.Print() << "\n";
                }
                p++;
                break;
            case (SCALL_2):
                //std::cout << "CALL-1";
                sym = r[i.B].SymbolId;
                a1 = r[i.C];
                p++;
                a2 = r[(*p).OP];
                // TODO!
                if (sym == SymPrint) {
                    std::cout << "\nPrinting " << a1.Print() << ", " << a2.Print() << "\n";
                }
                
                p++;
                
                break;
            case (JMP):
                p += i.A3;
                break;
            case (JMP_IF_TRUE):
                if (r[0].Integer) {
                    p += i.A3;
                }
                break;
            case (MOVE):
                std::cout << "MOVE";
                std::cout << "        (";
                std::cout << (int)p->A;
                std::cout << ",";
                std::cout << (int)p->B;
                std::cout << ")";
                p++;
                
                break;
            case (SCALL_3):
                
                sym = r[i.B].SymbolId;
                str = isolate->GetStringFromSymbolId(sym);
                a1 = r[i.C];
                p++;
                a2 = r[(*p).OP];
                a3 = r[(*p).A];
                if (sym == SymPlus) {
                    sum = a1.Integer + a2.Integer + a3.Integer;
                    r[i.A] = INTEGER(sum);
                }
                p++;
                break;
            case (SCALL_4):
                
                sym = r[i.B].SymbolId;
                str = isolate->GetStringFromSymbolId(sym);
                a1 = r[i.C];
                p++;
                a2 = r[(*p).OP];
                a3 = r[(*p).A];
                a4 = r[(*p).B];
                if (sym == SymPlus) {
                    sum = a1.Integer + a2.Integer + a3.Integer + a4.Integer;
                    r[i.A] = INTEGER(sum);
                }
                p++;
                break;
            case (DEREF):
                sym = r[i.B].SymbolId;
                std::cout << "Dereferencing r[" << + (unsigned int)i.B << "] (" << isolate->GetStringFromSymbolId(sym) << ")\n";
                if (str.compare("y")) {
                    r[i.A] = INTEGER(5);
                }
                p++;
                break;
            default:
                std::cout << "Unknown: " << isolate->GetStringFromSymbolId(i.OP) << "\n" ;
                p++;
                break;
                //                   throw std::runtime_error("Illegal OP code");
                //                   break;
        }
    }
end:
    //return cont->Checkpoint();
{'fraction_non_alphanumeric': 0.15244820002315082, 'fraction_numerical': 0.008102789674730871, 'mean_word_length': 0.8811234487263226, 'pattern_counts': {'":': 0, '<': 159, '<?xml version=': 0, '>': 24, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5406293', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2371, 'n_words': 464}