Exemple #1
0
Swallow::ScopedProgramPtr analyzeStatement(Swallow::SymbolRegistry& registry, Swallow::CompilerResults& compilerResults, const char* func, const wchar_t* str)
{
    using namespace Swallow;
    GlobalScope* global = registry.getGlobalScope();
    global->declareFunction(L"println", 0, L"Void", L"Int", NULL);
    global->declareFunction(L"println", 0, L"Void", L"String", NULL);
    global->declareFunction(L"print", 0, L"Void", L"String", NULL);
    global->declareFunction(L"assert", 0, L"Void", L"Bool", L"String", NULL);


    ScopedNodeFactory nodeFactory;
    Parser parser(&nodeFactory, &compilerResults);
    parser.setSourceFile(SourceFilePtr(new SourceFile(L"<code>", str)));
    ScopedProgramPtr ret = std::dynamic_pointer_cast<ScopedProgram>(parser.parse(str));
    ModulePtr module(new Module(L"test", registry.getGlobalScope()->getModuleType()));
    if(!ret)
        return ret;
    try
    {
        dumpAST(ret, L"Before transform AST:");
        OperatorResolver operatorResolver(&registry, &compilerResults);
        SemanticAnalyzer analyzer(&registry, &compilerResults, module);
        ret->accept(&operatorResolver);
        ret->accept(&analyzer);

        dumpAST(ret, L"Successed to transform AST< result:");
    }
    catch(const Abort&)
    {
        dumpAST(ret, L"Failed to transform AST< result:");
    }

    return ret;
}
Exemple #2
0
Swallow::ScopedProgramPtr analyzeStatement(Swallow::SymbolRegistry& registry, Swallow::CompilerResults& compilerResults, const char* func, const wchar_t* str)
{
    using namespace Swallow;

    FunctionOverloadedSymbolPtr println(new FunctionOverloadedSymbol());
    {
        TypePtr t_int = registry.lookupType(L"Int");
        TypePtr t_Void = registry.lookupType(L"Void");
        std::vector<Type::Parameter> parameters = {Type::Parameter(t_int)};
        TypePtr type = Type::newFunction(parameters, t_Void, false);
        FunctionSymbolPtr println_int(new FunctionSymbol(L"println", type, nullptr));
        println->add(println_int);
    }
    registry.getCurrentScope()->addSymbol(L"println", SymbolPtr(println));
    ScopedNodeFactory nodeFactory;
    Parser parser(&nodeFactory, &compilerResults);
    parser.setFileName(L"<file>");
    ScopedProgramPtr ret = std::dynamic_pointer_cast<ScopedProgram>(parser.parse(str));
//ScopedProgram* f;
    //cdumpCompilerResults(compilerResults);
//d.g();
    if(!ret)
        return ret;
    try
    {
        SemanticAnalyzer analyzer(&registry, &compilerResults);
        ret->accept(&analyzer);
    }
    catch(const Abort&)
    {
//ignore this
    }

    return ret;
}