-
Notifications
You must be signed in to change notification settings - Fork 1
/
carrot
executable file
·55 lines (43 loc) · 1.3 KB
/
carrot
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env node
'use strict';
/*
* Carrot
* the simplistic string-based esolang made for code-golfing
* by Kritixi Lithos
*/
let fs = require("fs");
let Interpreter = require(__dirname+"/interpreter.js");
let Parser = require(__dirname+"/parser/parser.js");
let args = process.argv;
let usage =
`Usage: carrot [-d] -f <file> [input]
carrot [-d] <code> [input]
-d, --debug
output AST and tokenisation
-f, --file
take code and input from files rather than from arguments`;
let code;
let input;
var debugFlag = false;
var fileFlag = false;
if (args.length < 3) {
console.log(usage);
} else {
// remove first two arguments
args.shift();
args.shift();
// keeping track of flags
var flags = [];
var currArg = "";
while(/^-/.test(currArg=args.shift())) {
flags.push(currArg);
}
fileFlag = flags.includes("-f") || flags.includes("--file");
debugFlag = flags.includes("-d") || flags.includes("--debug");
// reading program and input files
code = fileFlag?fs.readFileSync(currArg).toString():currArg;
// optional input
input = args.length?fileFlag?fs.readFileSync(args.shift()).toString():args.shift():"";
var interpreter = new Interpreter(new Parser(code, false, debugFlag).parse(), new Parser(input, true, debugFlag).parse());
console.log(interpreter.run());
}