{"size": 1425, "ext": "js", "max_stars_repo_path": "bin/picoc.js", "max_stars_repo_name": "harkal/picolang", "max_stars_repo_stars_event_min_datetime": "2020-07-18T16:32:47.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-12T17:05:09.000Z", "max_issues_repo_path": "bin/picoc.js", "max_issues_repo_name": "harkal/picolang", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "bin/picoc.js", "max_forks_repo_name": "harkal/picolang", "max_forks_repo_forks_event_min_datetime": "2020-11-22T16:06:36.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-22T16:06:36.000Z"}
{"max_stars_count": 5, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 20.3571428571, "max_line_length": 57, "alphanum_fraction": 0.6252631579}
#!/usr/bin/env node

const fs = require('fs');
const program = require('commander')
const { assemble } = require('../lib/picoasm')
const { compile_source } = require('../lib/picolang')

function compile(sources, cmd) {
	let source_code = ''
	
	sources.forEach(file=>{
		let sc = fs.readFileSync(file, 'utf-8')
		source_code += sc
	})

	let asm_code = ''
	try {
		asm_code = compile_source(source_code, sources[0], cmd)

		let outFile = cmd.output
		if (!outFile)outFile = 'out.asm'
	
		if (cmd.asm === true) {
			if (cmd.stdout) {
				console.log(asm_code)
			} else {
				var buf = new Buffer.from(asm_code)
				fs.writeFileSync(outFile, buf)
			}
			return
		}

		var asm = assemble(asm_code, 0)
		var buf = new Buffer.from(asm.code, 'binary')
		outFile = cmd.output
		if (!outFile)outFile = 'a.hex'
		if (cmd.stdout) {
			process.stdout.write(buf)
		} else {
			fs.writeFileSync(outFile, buf)
		}
	} catch (err) {
		console.log(err)
	}

}

function main() {
	program
		.version('0.0.1', '-v --version')
		.description('Compiler for the picolang')

	program
		.command('compile [sources...]', { isDefault: true })

		.option('-o, --output <output>', 'output file')
		.option('-a, --asm', 'output assembly')
		.option('-s, --stdout', 'emit code on stdout')

		.option('-O, --opt <level>', 'Optimization level', )

		.action(compile)

	program.parse(process.argv)    

    if (!program.args.length) program.help();
}

main()
