<commit_before>/* 
 * File:   scriptMain.cpp
 * Author: ghernan
 * 
 * Script engine main file. Contains 'evaluate' function, which runs an script
 * contained in a string.
 * 
 * Created on December 4, 2016, 10:31 PM
 */

#include "OS_support.h"
#include "scriptMain.h"
#include "jsParser.h"
#include "mvmCodegen.h"
#include "TinyJS_Functions.h"
#include "TinyJS_MathFunctions.h"
#include "mvmFunctions.h"
#include "semanticCheck.h"

using namespace std;

// Functions forward declarations.
//////////////////////////////////////////
CScriptToken parseFunctionArguments(Ref<JSFunction> function, CScriptToken token);

/**
 * Script evaluation function. Runs a script, and returns its result.
 *
 * @param script    Script code, in a C string.
 * @param globals   Global symbols
 * @return 
 */
Ref<JSValue> evaluate (const char* script, Ref<IScope> globals)
{
    CScriptToken    token (script);
    AstNodeList   statements;

    //Parsing loop
    token = token.next();
    while (!token.eof())
    {
        const ParseResult   parseRes = parseStatement (token);
        
        statements.push_back(parseRes.ast);
        token = parseRes.nextToken;
    }
    
    //Semantic check
    semanticCheck(statements);
    
    //Code generation.
    const Ref<MvmRoutine>    code = scriptCodegen(statements);
    
    //Execution
    return mvmExecute(code, globals);
}

/**
 * Creates the default global scope
 * @return 
 */
Ref<GlobalScope> createDefaultGlobals()
{
    auto    globals = GlobalScope::create();
    <commit_msg>BUGFIX: Script program crashed when entering an empty line (it triggered an assert)<commit_after>/* 
 * File:   scriptMain.cpp
 * Author: ghernan
 * 
 * Script engine main file. Contains 'evaluate' function, which runs an script
 * contained in a string.
 * 
 * Created on December 4, 2016, 10:31 PM
 */

#include "OS_support.h"
#include "scriptMain.h"
#include "jsParser.h"
#include "mvmCodegen.h"
#include "TinyJS_Functions.h"
#include "TinyJS_MathFunctions.h"
#include "mvmFunctions.h"
#include "semanticCheck.h"

using namespace std;

// Functions forward declarations.
//////////////////////////////////////////
CScriptToken parseFunctionArguments(Ref<JSFunction> function, CScriptToken token);

/**
 * Script evaluation function. Runs a script, and returns its result.
 *
 * @param script    Script code, in a C string.
 * @param globals   Global symbols
 * @return 
 */
Ref<JSValue> evaluate (const char* script, Ref<IScope> globals)
{
    CScriptToken    token (script);
    AstNodeList   statements;

    //Parsing loop
    token = token.next();
    while (!token.eof())
    {
        const ParseResult   parseRes = parseStatement (token);
        
        statements.push_back(parseRes.ast);
        token = parseRes.nextToken;
    }
    
    if (statements.empty())
        return undefined();
    
    //Semantic check
    semanticCheck(statements);
    
    //Code generation.
    const Ref<MvmRoutine>    code = scriptCodegen(statements);
    
    //Execution
    return mvmExecute(code, globals);
}

/**
 * Creates the default global scope
 * @return 
 */
Ref<GlobalScope> createDefaultGlobals()
{
    auto    globals = GlobalScope::create();
    
{'fraction_non_alphanumeric': 0.13310366426558096, 'fraction_numerical': 0.005637331663012841, 'mean_word_length': 2.8903775883069427, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3547398', 'n_tokens_mistral': 981, 'n_tokens_neox': 943, 'n_words': 270}