tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

ts-compiler.mjs (2417B)


      1 // Matching compiler observation boundaries on the site's current TS evaluator.
      2 import assert from 'node:assert/strict';
      3 import {readFile} from 'node:fs/promises';
      4 import {createHash} from 'node:crypto';
      5 import {pathToFileURL} from 'node:url';
      6 import path from 'node:path';
      7 const [site,id]=process.argv.slice(2);
      8 const load=name=>import(pathToFileURL(path.resolve(site,'dist',name+'.js')));
      9 const {Graph}=await load('graph');
     10 const {decodeBundle,loadBundle,COMPILER_BUNDLE_LIMIT}=await load('bundle');
     11 const {contractEnvironment}=await load('contracts');
     12 const {COMPILER_EXPORT,COMPILER_COLLECTION,sourceLiteral,compiledDefinitions}=await load('compiler');
     13 // Fixed comparison-harness guard, not a site execution setting.
     14 const COMPILER_BUDGET={nodes:6_000_000,fuel:Infinity,stack:100_000};
     15 const start=performance.now();
     16 const vectors=JSON.parse(await readFile(path.join(site,'test/compiler-conformance.json')));
     17 const descriptor=JSON.parse(await readFile(path.join(site,'compiler.json')));
     18 assert.equal(vectors.compilerBundleSha256,descriptor.hash);
     19 const bytes=await readFile(path.join(site,'bundles',descriptor.hash+'.arboricx'));
     20 assert.equal(createHash('sha256').update(bytes).digest('hex'),descriptor.hash);
     21 const c=vectors.cases[Number(id)];
     22 assert.notEqual(c.mode,'io');
     23 const g=new Graph(COMPILER_BUDGET,COMPILER_COLLECTION);
     24 const exports=loadBundle(g,decodeBundle(bytes,COMPILER_BUNDLE_LIMIT));
     25 const env=contractEnvironment(g,exports),source=sourceLiteral(g,c.source);
     26 const loaded=performance.now();
     27 const result=g.apply(exports.get(COMPILER_EXPORT),[env,source]);
     28 let defs,diagnostic;
     29 try{defs=compiledDefinitions(g,result);}catch(e){diagnostic=e.message;}
     30 const named=performance.now();
     31 if(c.diagnostic)assert.equal(diagnostic,c.diagnostic);
     32 else{
     33   assert.equal(diagnostic,undefined);
     34   assert.deepEqual([...defs.keys()],Object.keys(c.definitions));
     35   for(const [name,expected] of Object.entries(c.definitions))assert.equal(g.toTernary(defs.get(name),50000),expected);
     36 }
     37 const complete=performance.now();
     38 if(!c.diagnostic)assert.equal(g.toTernary(g.apply(defs.get(c.export),c.arguments.map(s=>g.fromTernary(s))),50000),c.result);
     39 const executed=performance.now();
     40 console.log(JSON.stringify({load_s:(loaded-start)/1000,compile_names_s:(named-loaded)/1000,
     41   compile_complete_s:(complete-loaded)/1000,run_s:(executed-complete)/1000,
     42   graph:g.stats,node_max_rss_kib:process.resourceUsage().maxRSS}));