Create an your own interpreter for a custom programming language in java script, and…
- User
Create an your own interpreter for a custom programming language in java script, and build website for it to use it, make it Gen-Z slang
- Assistant
Create an your own interpreter for a custom programming language in java script, and build website for it to use it, make it Gen-Z slang
Yo, what's poppin'? Let's cook up a rad interpreter for our own custom programming language using JavaScript, and we'll slap together a slick website so everyone can vibe with it. We're gonna keep it 100 with some Gen-Z slang sprinkled in. Let's get it!
First off, we need a fresh name for our language. Let's call it "SLang" (short for "Slang Language"). It's dope, it's edgy, and it screams Gen-Z.
spill "Your message" to print text.drip <variable> = <value>.+, -, *, /.realTalk (condition) { ... } else { ... }.loop (times) { ... }.drip mood = "feelin' good"
spill "Yo, I'm " + mood
drip count = 3
loop (count) {
spill "This is lit!"
}
realTalk (count > 2) {
spill "Big moves!"
} else {
spill "Chillin'"
}
Now, let's craft the interpreter that can parse and execute SLang code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SLang Interpreter</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #1e1e1e;
color: #d4d4d4;
padding: 20px;
}
textarea {
width: 100%;
height: 200px;
background-color: #252526;
color: #d4d4d4;
border: none;
padding: 10px;
font-size: 16px;
}
button {
padding: 10px 20px;
background-color: #0e639c;
color: #fff;
border: none;
cursor: pointer;
}
#output {
background-color: #1e1e1e;
padding: 10px;
margin-top: 20px;
height: 150px;
overflow-y: auto;
border: 1px solid #333;
}
</style>
</head>
<body>
<h1>SLang Interpreter</h1>
<p>Type your SLang code below and hit Run:</p>
<textarea id="code" placeholder="Write your SLang code here..."></textarea><br><br>
<button onclick="runCode()">Run Code</button>
<h2>Output:</h2>
<div id="output"></div>
<script>
function runCode() {
const code = document.getElementById('code').value;
const output = document.getElementById('output');
output.innerHTML = ''; // Clear previous output
const lines = code.split('\\n');
let variables = {};
function parseLine(line, index) {
line = line.trim();
// Variable declaration
if (line.startsWith('drip')) {
const parts = line.match(/drip\\s+(\\w+)\\s*=\\s*(.+)/);
if (parts) {
const varName = parts[1];
const value = evaluateExpression(parts[2]);
variables[varName] = value;
}
}
// Print statement
else if (line.startsWith('spill')) {
const parts = line.match(/spill\\s+(.+)/);
if (parts) {
const message = evaluateExpression(parts[1]);
output.innerHTML += message + '<br>';
}
}
// Loop
else if (line.startsWith('loop')) {
const parts = line.match(/loop\\s*\\((.+)\\)/);
if (parts) {
const times = evaluateExpression(parts[1]);
const loopBody = getBlock(lines, index);
for (let i = 0; i < times; i++) {
executeBlock(loopBody);
}
return loopBody.length + 1; // Skip loop body lines
}
}
// Conditional
else if (line.startsWith('realTalk')) {
const parts = line.match(/realTalk\\s*\\((.+)\\)/);
if (parts) {
const condition = evaluateExpression(parts[1]);
const ifBody = getBlock(lines, index);
let elseBody = [];
let nextLineIndex = index + ifBody.length + 1;
if (lines[nextLineIndex] && lines[nextLineIndex].trim().startsWith('else')) {
elseBody = getBlock(lines, nextLineIndex);
}
if (condition) {
executeBlock(ifBody);
} else {
executeBlock(elseBody);
}
return ifBody.length + elseBody.length + (elseBody.length > 0 ? 2 : 1); // Skip if/else body lines
}
}
}
function evaluateExpression(exp) {
exp = exp.replace(/"(.*?)"/g, '`$1`'); // Convert to template literals
exp = exp.replace(/\\b(\\w+)\\b/g, function(match) {
if (variables.hasOwnProperty(match)) {
return JSON.stringify(variables[match]);
}
return match;
});
try {
return eval(exp);
} catch (e) {
console.error('Error evaluating expression:', exp, e);
return null;
}
}
function getBlock(lines, startIndex) {
let block = [];
for (let i = startIndex + 1; i < lines.length; i++) {
if (lines[i].trim() === '}') {
break;
}
block.push(lines[i]);
}
return block;
}
function executeBlock(blockLines) {
for (let i = 0; i < blockLines.length; i++) {
const skip = parseLine(blockLines[i], i);
if (skip) {
i += skip - 1;
}
}
}
// Main execution loop
for (let i = 0; i < lines.length; i++) {
const skip = parseLine(lines[i], i);
if (skip) {
i += skip - 1;
}
}
}
</script>
</body>
</html>
Save the above code into a file named index.html. This file combines HTML, CSS, and JavaScript to create a simple web page that runs the SLang interpreter.
index.html file in your favorite web browser.Copy the sample SLang code into the textarea:
drip mood = "feelin' good"
spill "Yo, I'm " + mood
drip count = 3
loop (count) {
spill "This is lit!"
}
realTalk (count > 2) {
spill "Big moves!"
} else {
spill "Chillin'"
}
Expected Output:
Yo, I'm feelin' good
This is lit!
This is lit!
This is lit!
Big moves!
Now you've got a basic interpreter that understands our Gen-Z inspired programming language. Feel free to expand it by adding more features or customizing the syntax even further. Here's some ideas:
That's a wrap! You've just built a custom interpreter in JavaScript for a lit programming language and set up a website for it. Time to share it with the squad and let them bask in its glory.
Keep it 100, and happy coding! ✌️
Not yet included in a published release.