Files
  • index.js
  • examples
  • colors.js
  • lexer.js
  • parser.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const fs = require('fs');
const rs = require('readline-sync');

const lexer = require('./lexer.js');
const parser = require('./parser.js');

const colors = require('./colors');

rs.setPrompt('');

console.log(colors.cyan, 'NOOBLANG by Caleb [Version 2.0]\n');
console.log(colors.yellow, 'Enter the .noob filename under examples/ or leave it blank for REPL mode');

const filename = rs.prompt();
if(filename) runFile("examples/" + filename);
else runPrompt();
console.log('');

function runFile(name) {
	fs.readFile(name, 'utf8', (err, data) => {
		if (err) return console.log(colors.red, err);
		let code = data.split('\n');
		for (let line of code) 
			parser.run(lexer.scan(line + '\n'));
		if (parser.isError()) return;
	});
}

function runPrompt() {
	while (true) {
		rs.setPrompt('>'.repeat(parser.layers * 2 + 1) + ' ');
		let result = parser.run(lexer.scan(rs.prompt() + '\n'), true);
		if (typeof result == 'string') result = '"' + result + '"';
		if (!parser.layers) console.log(colors.green, !parser.isError()? '=> ' + result + '\n': '');
	}
}

node v10.15.2 linux/amd64