125 lines
5.3 KiB
C++
125 lines
5.3 KiB
C++
|
|
// Step 1952: Sprint 284 Integration — poly-compiler
|
||
|
|
//
|
||
|
|
// 5-phase pipeline: C++(lex)→Haskell(parse)→Rust(typecheck)→Lisp(optimize)→Go(emit)
|
||
|
|
//
|
||
|
|
// t1: registry has 5 phases sorted by order; languages correct
|
||
|
|
// t2: router maps all 5 IRNodeKinds to correct phases
|
||
|
|
// t3: pipeline runner runAll produces Bytecode from Source
|
||
|
|
// t4: runUpTo("typecheck") produces TypedAST
|
||
|
|
// t5: Sprint284IntegrationSummary reports 5 steps
|
||
|
|
|
||
|
|
#include "CompilerPhaseSpec.h"
|
||
|
|
#include "CompilerIRNode.h"
|
||
|
|
#include "CompilerPhaseRouter.h"
|
||
|
|
#include "CompilerPipelineRunner.h"
|
||
|
|
#include "Sprint284IntegrationSummary.h"
|
||
|
|
#include <iostream>
|
||
|
|
|
||
|
|
namespace ws = whetstone;
|
||
|
|
static int p=0,f=0;
|
||
|
|
#define T(n) { std::cout<<" "<<#n<<"... "; }
|
||
|
|
#define P() { std::cout<<"PASS\n"; ++p; }
|
||
|
|
#define F(m) { std::cout<<"FAIL: "<<m<<"\n"; ++f; }
|
||
|
|
#define C(c,m) if(!(c)){F(m);return;}
|
||
|
|
|
||
|
|
static ws::CompilerPhaseRegistry makeRegistry() {
|
||
|
|
ws::CompilerPhaseRegistry r;
|
||
|
|
r.registerPhase({"lex", "C++", "Lexing", 1, "Source", "Token"});
|
||
|
|
r.registerPhase({"parse", "Haskell", "Parsing", 2, "Token", "AST"});
|
||
|
|
r.registerPhase({"typecheck","Rust", "Type checking",3, "AST", "TypedAST"});
|
||
|
|
r.registerPhase({"optimize", "Lisp", "Optimization", 4, "TypedAST", "OptimizedAST"});
|
||
|
|
r.registerPhase({"emit", "Go", "Code emit", 5, "OptimizedAST", "Bytecode"});
|
||
|
|
return r;
|
||
|
|
}
|
||
|
|
|
||
|
|
static ws::CompilerPhaseRouter makeRouter() {
|
||
|
|
ws::CompilerPhaseRouter r;
|
||
|
|
r.registerRoute(ws::IRNodeKind::Source, "lex");
|
||
|
|
r.registerRoute(ws::IRNodeKind::Token, "parse");
|
||
|
|
r.registerRoute(ws::IRNodeKind::AST, "typecheck");
|
||
|
|
r.registerRoute(ws::IRNodeKind::TypedAST, "optimize");
|
||
|
|
r.registerRoute(ws::IRNodeKind::OptimizedAST, "emit");
|
||
|
|
return r;
|
||
|
|
}
|
||
|
|
|
||
|
|
static ws::CompilerPipelineRunner makeRunner() {
|
||
|
|
ws::CompilerPipelineRunner r;
|
||
|
|
r.addPhase({"lex", "C++", "",1,"",""}, [](const ws::CompilerIRNode& n){
|
||
|
|
return ws::CompilerIRNode{ws::IRNodeKind::Token, "tok:" +n.payload, n.sourceId};});
|
||
|
|
r.addPhase({"parse", "Haskell", "",2,"",""}, [](const ws::CompilerIRNode& n){
|
||
|
|
return ws::CompilerIRNode{ws::IRNodeKind::AST, "ast:" +n.payload, n.sourceId};});
|
||
|
|
r.addPhase({"typecheck","Rust", "",3,"",""}, [](const ws::CompilerIRNode& n){
|
||
|
|
return ws::CompilerIRNode{ws::IRNodeKind::TypedAST, "typed:" +n.payload, n.sourceId};});
|
||
|
|
r.addPhase({"optimize", "Lisp", "",4,"",""}, [](const ws::CompilerIRNode& n){
|
||
|
|
return ws::CompilerIRNode{ws::IRNodeKind::OptimizedAST, "opt:" +n.payload, n.sourceId};});
|
||
|
|
r.addPhase({"emit", "Go", "",5,"",""}, [](const ws::CompilerIRNode& n){
|
||
|
|
return ws::CompilerIRNode{ws::IRNodeKind::Bytecode, "bc:" +n.payload, n.sourceId};});
|
||
|
|
return r;
|
||
|
|
}
|
||
|
|
|
||
|
|
void t1(){
|
||
|
|
T(registry_5_phases_sorted_languages);
|
||
|
|
auto reg = makeRegistry();
|
||
|
|
C(reg.phaseCount() == 5, "five phases");
|
||
|
|
auto sorted = reg.phaseIdsSorted();
|
||
|
|
C(sorted[0] == "lex", "lex first");
|
||
|
|
C(sorted[1] == "parse", "parse second");
|
||
|
|
C(sorted[2] == "typecheck","typecheck third");
|
||
|
|
C(sorted[3] == "optimize", "optimize fourth");
|
||
|
|
C(sorted[4] == "emit", "emit fifth");
|
||
|
|
C(reg.getPhase("lex").language == "C++", "C++ lex");
|
||
|
|
C(reg.getPhase("parse").language == "Haskell", "Haskell parse");
|
||
|
|
C(reg.getPhase("typecheck").language== "Rust", "Rust typecheck");
|
||
|
|
C(reg.getPhase("optimize").language == "Lisp", "Lisp optimize");
|
||
|
|
C(reg.getPhase("emit").language == "Go", "Go emit");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
void t2(){
|
||
|
|
T(router_maps_5_kinds_to_phases);
|
||
|
|
auto router = makeRouter();
|
||
|
|
C(router.routePhase(ws::IRNodeKind::Source) == "lex", "Source→lex");
|
||
|
|
C(router.routePhase(ws::IRNodeKind::Token) == "parse", "Token→parse");
|
||
|
|
C(router.routePhase(ws::IRNodeKind::AST) == "typecheck", "AST→typecheck");
|
||
|
|
C(router.routePhase(ws::IRNodeKind::TypedAST) == "optimize", "TypedAST→optimize");
|
||
|
|
C(router.routePhase(ws::IRNodeKind::OptimizedAST) == "emit", "OptimizedAST→emit");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
void t3(){
|
||
|
|
T(runner_runAll_produces_Bytecode);
|
||
|
|
auto runner = makeRunner();
|
||
|
|
ws::CompilerIRNode src{ws::IRNodeKind::Source, "program", "main.cpp"};
|
||
|
|
auto result = runner.runAll(src);
|
||
|
|
C(result.kind == ws::IRNodeKind::Bytecode, "Bytecode kind");
|
||
|
|
C(result.payload == "bc:opt:typed:ast:tok:program", "full chain payload");
|
||
|
|
C(result.sourceId == "main.cpp", "sourceId preserved");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
void t4(){
|
||
|
|
T(runner_runUpTo_typecheck_produces_TypedAST);
|
||
|
|
auto runner = makeRunner();
|
||
|
|
ws::CompilerIRNode src{ws::IRNodeKind::Source, "fn", "x.cpp"};
|
||
|
|
auto result = runner.runUpTo(src, "typecheck");
|
||
|
|
C(result.kind == ws::IRNodeKind::TypedAST, "TypedAST kind");
|
||
|
|
C(result.payload == "typed:ast:tok:fn", "partial chain payload");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
void t5(){
|
||
|
|
T(sprint284_integration_summary);
|
||
|
|
ws::Sprint284IntegrationSummary s;
|
||
|
|
C(s.stepsCompleted == 5, "5 steps");
|
||
|
|
C(s.success, "success");
|
||
|
|
C(s.sprintName() == "Sprint 284: poly-compiler", "name");
|
||
|
|
P();
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(){
|
||
|
|
std::cout << "Step 1952: Sprint 284 Integration\n";
|
||
|
|
t1(); t2(); t3(); t4(); t5();
|
||
|
|
std::cout << "\n" << p << "/" << (p+f) << " passed\n";
|
||
|
|
return f > 0 ? 1 : 0;
|
||
|
|
}
|