// CLI Runner untuk Iris Language
// Cara pakai: node run.js <file.iris>

const fs = require('fs');
const path = require('path');

// Definisikan consoleLog global sebelum load interpreter
global.consoleLog = (msg, type) => {
    const prefix = type === 'err' ? '[ERROR] ' : type === 'info' ? '[INFO] ' : '';
    console.log(prefix + msg);
};

// Load interpreter
const { Iris } = require('./iris-interpreter.js');

// Main
const file = process.argv[2];
if (!file) {
    console.error('Usage: node run.js <file.iris>');
    process.exit(1);
}

let source = fs.readFileSync(file, 'utf8');
// Pastikan ada newline di akhir untuk parser
if (!source.endsWith('\n')) source += '\n';

console.log('[IRIS ENGINE v1.0]');
console.log('File:', file);
console.log('---');

try {
    const basePath = path.dirname(path.resolve(file));
    const result = Iris.run(source, basePath);
    if (result !== undefined && result !== null) {
        console.log('---');
        console.log('Result:', result);
    }
} catch (e) {
    console.error('[ERROR]', e.message);
    process.exit(1);
}
