UNPKG

hindiscript-lang

Version:

HindiScript – A tiny Hindi-first toy language that runs .hs files. Created by Atikin Verse.

85 lines (69 loc) 2.59 kB
import fs from 'node:fs'; import path from 'node:path'; import process from 'node:process'; import { translateHindiToInternal } from './translator.js'; import { runInSandbox } from './sandbox.js'; import { makeRuntime } from './runtime.js'; import { color, exitWith } from './utils.js'; export async function runCLI() { const argv = process.argv.slice(2); if (argv.length === 0 || argv.includes('--help') || argv.includes('-h')) { printHelp(); process.exit(0); } const showAst = argv.includes('--show-internal'); const showCode = argv.includes('--show-code'); const idxFile = argv.findIndex(a => a.endsWith('.hs')); if (idxFile === -1) { exitWith(1, `${color.red('Error:')} Please provide a .hs file.\nTry: hindiscript examples/hello.hs`); } const file = argv[idxFile]; const abs = path.resolve(process.cwd(), file); if (!fs.existsSync(abs)) exitWith(1, `${color.red('Error:')} File not found: ${file}`); const src = fs.readFileSync(abs, 'utf8'); try { const { code, meta } = translateHindiToInternal(src, { filename: file }); if (showAst || showCode) { console.log(color.dim('\n---[ INTERNAL CODE VIEW ]---')); console.log(code); console.log(color.dim('---------------------------\n')); } const output = []; const runtime = makeRuntime({ print: (...args) => { const line = args.map(v => formatValue(v)).join(' '); output.push(line); console.log(line); } }); await runInSandbox(code, { runtime, filename: file }); // If user defined मुख्य(), call it by default if (typeof runtime.__globals.__main === 'function') { await runtime.__globals.__main(); } } catch (err) { console.error(color.red('Runtime Error:')); console.error(err && err.stack ? err.stack : err); process.exit(1); } } function formatValue(v) { if (typeof v === 'string') return v; try { return JSON.stringify(v); } catch { return String(v); } } function printHelp() { console.log(` ${color.cyan('HindiScript')} – run Hindi .hs files directly ${color.bold('Usage:')} hindiscript <file.hs> [options] ${color.bold('Options:')} --show-code Show internal translated code (for learning) --show-internal Same as --show-code -h, --help Show this help ${color.bold('Example:')} hindiscript examples/hello.hs hindiscript examples/control_flow.hs hindiscript examples/functions.hs hindiscript --show-code examples/hello.hs `); }