ajout de Sheebr + support de shutdown + auto install kernel
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
if(process.argv.length >= 3){
|
||||
console.log(process.argv[3])
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
const fs = require('fs')
|
||||
|
||||
let path = process.argv[3] ?? '.'
|
||||
let optionsArray = process.argv.slice(4) ?? []
|
||||
let options = []
|
||||
for(let option of optionsArray){
|
||||
if(option.startwith("--")) options.push(option.slice)
|
||||
else if(options.startwith("-")) options.push += option.slice(1).split('').map(el => {return '-' + el})
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(path);
|
||||
const nonHiddenFiles = files.filter(file => file[0] != '.')
|
||||
|
||||
console.log(options.includes('-a') ? files.join(' ') : nonHiddenFiles.join(' '))
|
||||
@@ -0,0 +1,6 @@
|
||||
const fs = require('fs')
|
||||
if(process.argv.length >= 3){
|
||||
fs.mkdir(process.argv[3], { recursive: true }, (err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
console.log(process.cwd())
|
||||
@@ -0,0 +1,60 @@
|
||||
const { Lexer } = require('../lib/sheebr/lexer')
|
||||
const { getline } = require('../lib/sheebr/utils')
|
||||
const fs = require('fs')
|
||||
const environment = require('../lib/sheebr/environment')
|
||||
const readline = require('readline').createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
terminal: false
|
||||
});
|
||||
const readlineSync = require('../lib/sheebr/readLineSync')
|
||||
let execSync = require('child_process').execSync
|
||||
|
||||
|
||||
let current_dir = process.cwd()
|
||||
|
||||
start()
|
||||
|
||||
async function start(){
|
||||
while(true){
|
||||
input = await getline("sheebr> ");
|
||||
if(input == "exit") break;
|
||||
else evaluate(input);
|
||||
}
|
||||
}
|
||||
|
||||
function evaluate(input){
|
||||
let lexer = new Lexer(input)
|
||||
args = lexer.scanTokens()
|
||||
|
||||
if(args[0] == "cd"){
|
||||
if(args.length != 2){
|
||||
console.log("Wrong number of arguments"); return;}
|
||||
if(!fs.existsSync(args[1])){console.log("Folder does not exist"); return;}
|
||||
current_dir = args[1][0] == "/" ? args[1] : current_dir + '/' + args[1];
|
||||
console.log(current_dir)
|
||||
return;
|
||||
}
|
||||
// check for all folders in path
|
||||
|
||||
let found = false;
|
||||
for(let path of environment.path){
|
||||
let tobreak = false;
|
||||
const files = fs.readdirSync(path);
|
||||
if(files.includes(`${args[0]}.js`)){
|
||||
found = true;
|
||||
script = execSync(`node ${path + '/' + args[0]}.js ${args.map(arg => {return arg.includes(' ') ? `"${arg}"` : arg}).join(' ')}`, {cwd: current_dir});
|
||||
process.stdout.write(script)
|
||||
tobreak = true;
|
||||
}
|
||||
|
||||
if(tobreak) break;
|
||||
}
|
||||
if(!found){
|
||||
console.log("Program not found")
|
||||
}
|
||||
let program = args[0]
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
const fs = require('fs')
|
||||
if(process.argv.length >= 3){
|
||||
fs.openSync(process.argv[3], 'w');
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
let environment = {
|
||||
path: ["/sheebr", "/usr/bin"]
|
||||
}
|
||||
|
||||
module.exports = environment
|
||||
@@ -0,0 +1,57 @@
|
||||
const { isAlpha } = require('./utils')
|
||||
|
||||
|
||||
class Lexer{
|
||||
constructor(source){
|
||||
this.tokens = [];
|
||||
this.current = 0;
|
||||
this.start = 0;
|
||||
this.source = source
|
||||
}
|
||||
|
||||
scanTokens(){
|
||||
while(this.current < this.source.length){
|
||||
this.start = this.current
|
||||
this.scanToken()
|
||||
|
||||
}
|
||||
return this.tokens;
|
||||
}
|
||||
|
||||
scanToken(){
|
||||
|
||||
if(this.source[this.current] == "\""){
|
||||
this.string()
|
||||
}
|
||||
else{
|
||||
let content = ""
|
||||
while(this.source[this.current] != ' ' && this.source[this.current] != '\n' && this.current < this.source.length ){
|
||||
content += this.source[this.current];
|
||||
this.current++;
|
||||
|
||||
}
|
||||
this.tokens.push(content)
|
||||
}
|
||||
this.current++;
|
||||
|
||||
}
|
||||
|
||||
string(){
|
||||
this.current++;
|
||||
let content = ""
|
||||
while(this.source[this.current] != "\"" && this.current < this.source.length){
|
||||
content += this.source[this.current];
|
||||
this.current++;
|
||||
}
|
||||
this.tokens.push(content)
|
||||
}
|
||||
|
||||
addToken(tokenType, value=""){
|
||||
this.tokens.push(Token(tokenType, value))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
Lexer
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,31 @@
|
||||
async function getline(str) {
|
||||
process.stdout.write(str)
|
||||
process.stdin.resume();
|
||||
process.stdin.setEncoding('utf8')
|
||||
let line = ""
|
||||
return new Promise((res, rej) => {
|
||||
process.stdin.on('data', function(chunk) {
|
||||
|
||||
line += chunk
|
||||
if (chunk.toString('utf-8').endsWith('\n')) {
|
||||
process.stdin.pause()
|
||||
res(line.substring(0, line.length -1))
|
||||
}
|
||||
})
|
||||
process.stdin.on('error', function(err) {
|
||||
rej(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function isAlpha(str){
|
||||
return /^[a-zA-Z]*$/.test(str);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
isAlpha, getline
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user