5-phase compiler pipeline: C++(lex)→Haskell(parse)→Rust(typecheck)→Lisp(optimize)→Go(emit) Components: - CompilerPhaseSpec + CompilerPhaseRegistry: phase descriptors sorted by order field - CompilerIRNode + IRNodePipeline: IR node kinds (Source→Token→AST→TypedAST→OptimizedAST→Bytecode) + queue - CompilerPhaseRouter: IRNodeKind→phaseId routing with optional fallback - CompilerPipelineRunner: chains PhaseTransform functions in order, supports runUpTo 26/26 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
93 lines
3.1 KiB
C++
93 lines
3.1 KiB
C++
// Step 1949: CompilerIRNode + IRNodePipeline
|
|
//
|
|
// t1: IRNodeKind enum values and irKindName
|
|
// t2: CompilerIRNode construction
|
|
// t3: IRNodePipeline push/pop/peek
|
|
// t4: IRNodePipeline drain
|
|
// t5: IRNodePipeline empty/size; underflow throws
|
|
|
|
#include "CompilerIRNode.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;}
|
|
|
|
void t1(){
|
|
T(IRNodeKind_and_irKindName);
|
|
C(ws::irKindName(ws::IRNodeKind::Source) == "Source", "Source");
|
|
C(ws::irKindName(ws::IRNodeKind::Token) == "Token", "Token");
|
|
C(ws::irKindName(ws::IRNodeKind::AST) == "AST", "AST");
|
|
C(ws::irKindName(ws::IRNodeKind::TypedAST) == "TypedAST", "TypedAST");
|
|
C(ws::irKindName(ws::IRNodeKind::OptimizedAST) == "OptimizedAST", "OptimizedAST");
|
|
C(ws::irKindName(ws::IRNodeKind::Bytecode) == "Bytecode", "Bytecode");
|
|
P();
|
|
}
|
|
|
|
void t2(){
|
|
T(CompilerIRNode_construction);
|
|
ws::CompilerIRNode n(ws::IRNodeKind::AST, "ast-payload", "unit1");
|
|
C(n.kind == ws::IRNodeKind::AST, "kind");
|
|
C(n.payload == "ast-payload", "payload");
|
|
C(n.sourceId == "unit1", "sourceId");
|
|
ws::CompilerIRNode def;
|
|
C(def.kind == ws::IRNodeKind::Source, "default kind Source");
|
|
P();
|
|
}
|
|
|
|
void t3(){
|
|
T(IRNodePipeline_push_pop_peek);
|
|
ws::IRNodePipeline q;
|
|
C(q.empty(), "empty initially");
|
|
q.push({ws::IRNodeKind::Source, "src"});
|
|
q.push({ws::IRNodeKind::Token, "tok"});
|
|
C(q.size() == 2, "size 2");
|
|
C(q.peek().kind == ws::IRNodeKind::Source, "peek Source");
|
|
auto n = q.pop();
|
|
C(n.kind == ws::IRNodeKind::Source, "pop Source");
|
|
C(q.peek().kind == ws::IRNodeKind::Token, "peek Token after pop");
|
|
P();
|
|
}
|
|
|
|
void t4(){
|
|
T(IRNodePipeline_drain);
|
|
ws::IRNodePipeline q;
|
|
q.push({ws::IRNodeKind::Source, "s"});
|
|
q.push({ws::IRNodeKind::Token, "t"});
|
|
q.push({ws::IRNodeKind::Bytecode,"b"});
|
|
auto drained = q.drain();
|
|
C(drained.size() == 3, "three drained");
|
|
C(q.empty(), "empty after drain");
|
|
C(drained[0].kind == ws::IRNodeKind::Source, "first Source");
|
|
C(drained[2].kind == ws::IRNodeKind::Bytecode,"last Bytecode");
|
|
P();
|
|
}
|
|
|
|
void t5(){
|
|
T(IRNodePipeline_empty_size_underflow);
|
|
ws::IRNodePipeline q;
|
|
C(q.empty(), "empty");
|
|
C(q.size() == 0, "size 0");
|
|
q.push({ws::IRNodeKind::AST, "x"});
|
|
C(!q.empty(), "not empty");
|
|
C(q.size() == 1, "size 1");
|
|
q.pop();
|
|
bool threw = false;
|
|
try { q.pop(); } catch (const std::underflow_error&) { threw = true; }
|
|
C(threw, "underflow on empty pop");
|
|
bool threw2 = false;
|
|
try { q.peek(); } catch (const std::underflow_error&) { threw2 = true; }
|
|
C(threw2, "underflow on empty peek");
|
|
P();
|
|
}
|
|
|
|
int main(){
|
|
std::cout << "Step 1949: CompilerIRNode + IRNodePipeline\n";
|
|
t1(); t2(); t3(); t4(); t5();
|
|
std::cout << "\n" << p << "/" << (p+f) << " passed\n";
|
|
return f > 0 ? 1 : 0;
|
|
}
|