-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevlang.js
More file actions
134 lines (123 loc) · 4.01 KB
/
Copy pathdevlang.js
File metadata and controls
134 lines (123 loc) · 4.01 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
127
128
129
130
131
132
133
134
#!/usr/bin/env node
/**
* DevLang Compiler - A Simple Programming Language with Hindi Keywords
*
* DevLang is a simple programming language that uses Hindi keywords for better
* accessibility to Hindi speakers. It supports basic programming constructs
* like variables, conditionals, and I/O operations.
*
* Features:
* - Hindi keywords: bolo (print), dekho (if), puchoo (input), nhi_toh (else)
* - Variable assignment and arithmetic operations
* - Conditional statements with if-else
* - String and integer literals
* - Symbol table for variable management
* - Lexical analysis, parsing, and interpretation
*
* Usage:
* node devlang.js -f program.dev // Run from file
* node devlang.js -e "code here" // Run inline code
* node devlang.js --tokens -f file // Show tokens for debugging
* node devlang.js --async -f file // Enable real input
*
* Example DevLang Program:
* naam = "DevLang";
* bolo naam;
* age = 25;
* dekho(age > 18) {
* bolo "Adult";
* } nhi_toh {
* bolo "Minor";
* }
*
* Keywords:
* - bolo: Print/output statement
* - dekho: Conditional if statement
* - puchoo: Input function (prompt user)
* - nhi_toh: Else statement
*
* @version 1.0.0
* @license MIT
*/
const fs = require('fs');
const { Tokenizer } = require('./tokenizer');
const { Parser } = require('./parser');
const { Interpreter } = require('./interpreter');
/**
* Main function to run the DevLang interpreter
*/
async function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log('DevLang Compiler v1.0.0');
console.log('Usage: node devlang.js -f <file> OR node devlang.js -e "<code>"');
console.log(' node devlang.js --tokens -f <file> (show tokens)');
console.log(' node devlang.js --async -f <file> (enable real input)');
console.log('');
console.log('Example: node devlang.js -e "naam = \\"Hello\\"; bolo naam;"');
process.exit(1);
}
let code = '';
let isAsync = false;
// Check for async flag
if (args.includes('--async')) {
isAsync = true;
// Remove --async from args
const asyncIndex = args.indexOf('--async');
args.splice(asyncIndex, 1);
}
if (args[0] === '-f' && args[1]) {
try {
code = fs.readFileSync(args[1], 'utf8');
} catch (error) {
console.error(`Error reading file: ${error.message}`);
process.exit(1);
}
} else if (args[0] === '-e' && args[1]) {
code = args[1];
} else if (args[0] === '--tokens' && args[1] === '-f' && args[2]) {
try {
code = fs.readFileSync(args[2], 'utf8');
const tokenizer = new Tokenizer(code);
console.log('Tokens:');
tokenizer.getTokens().forEach(token => console.log(token.toString()));
return;
} catch (error) {
console.error(`Error reading file: ${error.message}`);
process.exit(1);
}
} else if (args[0] === '--tokens' && args[1] === '-e' && args[2]) {
code = args[2];
const tokenizer = new Tokenizer(code);
console.log('Tokens:');
tokenizer.getTokens().forEach(token => console.log(token.toString()));
return;
} else {
console.log('Invalid arguments. Use -f <file> or -e "<code>"');
process.exit(1);
}
try {
const interpreter = new Interpreter(code, isAsync);
if (isAsync) {
await interpreter.execute();
} else {
interpreter.executeSync();
}
} catch (error) {
console.error(`Execution error: ${error.message}`);
process.exit(1);
}
}
// Run main function when script is executed directly
if (require.main === module) {
main().catch(error => {
console.error(`Fatal error: ${error.message}`);
process.exit(1);
});
}
// Export modules for testing and external use
module.exports = {
Tokenizer,
Parser,
Interpreter
};