Skip to content

Instantly share code, notes, and snippets.

@cborac
Created April 22, 2020 13:42
Show Gist options
  • Save cborac/ef66014d61541da07e0af7bb76b3fccc to your computer and use it in GitHub Desktop.
Save cborac/ef66014d61541da07e0af7bb76b3fccc to your computer and use it in GitHub Desktop.
const fs = require("fs")
const dictionary = ["if", "print"]
const allowedAscii = [13]
const bjargs = []
fs.readdir(`./`, (err, files) => {
if (err) throw err;
files.forEach(file => {
if (file.endsWith(".bjarg")) bjargs.push(file)
})
if (bjargs.length === 0) throw new Error("Couldn't find a bjarg file.")
if (bjargs.length < 1) {
if (bjargs.includes("main.bjarg")) {
execute(fs.readFileSync(`./main.bjarg`))
} else throw new Error("Found multiple bjargs. Rename one of them 'main.bjarg'")
} else {
execute(fs.readFileSync(`./${bjargs[0]}`))
}
})
function execute(file) {
const lines = {}
/**
* @type {Array}
*/
const arrayOflines = file.toString().split("\n")
for (let i = 0; i < arrayOflines.length; i++) {
lines[i] = arrayOflines[i];
}
for (let atLine in lines) {
let line = lines[atLine]
if (atLine != arrayOflines.length - 1) lines[atLine] = line.substring(0, line.length - 1)
const lineBefore = lines[atLine - 1]
if (lineBefore === undefined) executeLine(line)
else {
const syntax = lineBefore.split(" ")[0]
const value = lineBefore.split(" ")[1]
if (syntax === "if") {
if (value !== "no") {
executeLine(line)
}
} else {
executeLine(line)
}
}
}
}
function executeLine(line) {
const cmd = line.split(" ")[0]
const content = line.split(" ").slice(1).join(" ")
if (!dictionary.includes(cmd)) {
if (!allowedAscii.includes(cmd.charCodeAt(0))) {
throw new Error(`Unknown statement '${cmd}'`)
}
}
if (line.startsWith("print")) console.log(content)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment