This repository was archived by the owner on Jul 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
126 lines (117 loc) · 3.9 KB
/
index.ts
File metadata and controls
126 lines (117 loc) · 3.9 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {argv, cwd, stdout} from 'process';
import {readFile, existsSync} from 'fs';
import {magentaBright, cyan} from 'chalk';
import {LongException} from './exception/error';
import {LongLexicalAnalyser} from './lexer';
import {LongCommand} from './command/command';
import {LongProject} from './project/project';
import {LongApplication} from './project/run';
import {LongProjectList} from './project/list';
/**
*
* @param {string} filename The name of the file
*
* Checks if the file exists, if yes, raed the file,
* else, throw a LongException and also create a new
* lexer object after reading the file
*
* @returns {undefined | LongException} undefine dor an error message
*/
export const createLongLexer = (filename: string) => {
const fileExists = existsSync(filename);
if (!fileExists) {
const fileNotFound = new LongException(
`Unable to find ${filename}`,
'Check for typos',
'FileNotFound'
);
return fileNotFound;
}
readFile(filename, (error: NodeJS.ErrnoException | null, data: Buffer) => {
if (error) {
const exception = new LongException(
'An Error occured while reading the file',
'Recheck the filename',
'ReadFile'
).evokeLongException();
return exception;
} else {
const fileReadData = data.toString();
const lexer = new LongLexicalAnalyser(fileReadData);
const tokens = lexer.createLexicalAnalyser();
const commands = new LongCommand(tokens);
process.exit();
}
});
};
class LongArgumentParser {
private arguments: Array<string>;
private length: number;
/**
* @constructor
* @param {string[]} argument The command line arguments to parse
*/
constructor(argument: Array<string>) {
this.arguments = argument.slice(2, argument.length);
this.length = this.arguments.length;
}
/**
* @public
*
* If there are no arguments, return undefined
* else,if the first argument ends with long(.long)
* file extension, read the file and create a lexer
*
* @returns {undefined}
*/
public parseLongArguments = () => {
if (this.length == 0) {
return undefined;
} else {
if (this.arguments[0].endsWith('.long')) {
const coderunner = createLongLexer(this.arguments[0]);
} else if (this.arguments[0] == 'help') {
const outputLinks = [
{
text: 'Github',
link: 'https://github.com/pranavbaburaj',
},
{
text: 'Discord',
link: 'https://discord.gg/vzcNRVrHR5',
},
{
text: 'Discussions',
link: 'https://github.com/pranavbaburaj/long/discussions',
},
{
text: 'Report a bug',
link: 'https://github.com/pranavbaburaj/long/issues',
},
];
console.log(magentaBright('Long Help Center \n'));
for (let index = 0; index < outputLinks.length; index++) {
const link = outputLinks[index];
console.log(cyan(`[${link.text}] (${link.link})`));
}
} else if (this.arguments[0] == 'new') {
const project = LongProject.createLongProject();
} else if (this.arguments[0] == 'run') {
const application = new LongApplication(cwd());
} else if (this.arguments[0] == 'list') {
const list = new LongProjectList();
} else if (this.arguments[0] == 'shell') {
// createLongInteractiveRepl((data) => {
// const lexer = new LongLexicalAnalyser(data);
// const tokens = lexer.createLexicalAnalyser();
// const commands = new LongCommand(tokens);
// stdout.write('\n');
// });
}
}
return undefined;
};
}
// The argument parser
const longArgumentParser = new LongArgumentParser(argv);
longArgumentParser.parseLongArguments();