Mastering Vexon: A Complete Guide to the New Language on the Block
Have you ever wanted a programming language that feels like a simplified JavaScript but comes with batteries included for file systems and networking right out of the box? Meet Vexon.
Vexon is a dynamically typed, interpreted language that runs on top of Node.js. It features a clean syntax, powerful built-in functions for I/O, and—best of all—it can compile your scripts into standalone .exe files.
In this guide, we’ll go from "Hello World" to building a fully functional CLI text editor.
1. Installation & Setup
Vexon doesn't require a complex installer. It consists of two JavaScript files:
vexon_core.js(The brain: lexer, parser, compiler, and VM).vexon_cli.js(The tool: runs code and compiles executables).
How to Install
- Ensure you have Node.js installed.
- Download
vexon_core.jsandvexon_cli.jsinto a folder. - That's it! You are ready to code.
To verify it works, create a file named hello.vx:
print("Hello, Vexon!");
Run it using the CLI:
node vexon_cli.js run hello.vx
2. The Basics: Syntax & Variables
Vexon syntax is very C-like (similar to JavaScript or C#), making it instantly familiar.
Variables
Use let to declare variables. Vexon is dynamic, so you don't need to specify types.
let name = "Vexon";
let version = 1.0;
let isAwesome = true;
Printing
Outputting text is simple with the built-in print function.
print("Welcome to " + name); // String concatenation
Arrays & Objects
Vexon supports complex data structures natively.
// Arrays
let colors = ["red", "green", "blue"];
print(colors[1]); // Output: green
// Objects
let user = {
name: "Alice",
id: 42
};
print(user.name); // Output: Alice
3. Control Flow
Logic in Vexon uses standard if, while, and for loops.
If / Else
let score = 85;
if (score >= 90) {
print("Grade: A");
} else if (score >= 80) {
print("Grade: B");
} else {
print("Grade: C");
}
Loops
While Loop:
let i = 0;
while (i < 5) {
print("Count: " + i);
i = i + 1;
}
For Loop:
Vexon uses a for..in style for iterating.
let nums = [10, 20, 30];
for n in nums {
print(n);
}
4. Functions
Functions are defined using the fn (or func/function) keyword.
fn add(a, b) {
return a + b;
}
let result = add(5, 10);
print(result); // 15
Important Note: Vexon functions handle return differently based on context. If you use the HALT opcode inside a function, it acts like a return, ensuring your whole program doesn't crash just because a function finished.
5. The "Standard Library" (Built-ins)
This is where Vexon shines. Unlike standard JavaScript which requires imports for file I/O, Vexon has these global functions ready to use.
File System Operations
read(path): Reads a file as a string.write(path, data): Overwrites a file with data.append(path, data): Appends data to a file.exists(path): Checks if a file exists (returns boolean).list(path): Lists files in a directory.
if (exists("data.txt")) {
let content = read("data.txt");
print(content);
} else {
write("data.txt", "Initial Content");
}
Array Manipulation
In Vexon, array methods are global functions, not methods on the object itself.
push(arr, value): Adds to the end.pop(arr): Removes from the end.len(item): Gets the length of an array or string.
let stack = [];
push(stack, 1);
push(stack, 2);
print(pop(stack)); // 2
print(len(stack)); // 1
Networking (Fetch)
Vexon has a built-in fetch function that handles HTTP requests. It returns an object with status, text, and json.
let response = fetch("https://jsonplaceholder.typicode.com/todos/1");
print(response.json.title);
Note: The Vexon VM handles the asynchronous nature of fetch automatically, so your code looks synchronous!
6. Let's Build a Project: A CLI Text Editor
Let's combine everything into a real tool: a command-line text editor.
Save this as editor.vx.
// A simple line-based text editor
let filename = "notes.txt";
let lines = [];
// Load existing file if present
if (exists(filename)) {
// We manually split by newline since .split isn't built-in
let content = read(filename);
let temp = "";
let i = 0;
while (i < len(content)) {
if (content[i] == "\n") {
push(lines, temp);
temp = "";
} else {
temp = temp + content[i];
}
i = i + 1;
}
push(lines, temp); // Push the last line
}
let running = true;
while (running) {
print("--- Editor: " + filename + " ---");
// Display lines
let idx = 0;
while (idx < len(lines)) {
print(idx + ": " + lines[idx]);
idx = idx + 1;
}
// Get user input
let cmd = input("Command (add/save/quit): ");
if (cmd == "quit") {
running = false;
} else if (cmd == "save") {
let output = "";
let j = 0;
while (j < len(lines)) {
output = output + lines[j] + "\n";
j = j + 1;
}
write(filename, output);
print("Saved!");
} else if (cmd == "add") {
let newLine = input("Enter text: ");
push(lines, newLine);
}
}
Run it:
node vexon_cli.js run editor.vx
7. Compiling to EXE
One of Vexon's coolest features is compiling your script into a standalone executable. This bundles the Vexon runtime and your code into a single file you can share.
Run the compile command:
node vexon_cli.js compile editor.vx
This will:
- Parse and compile your Vexon code to bytecode.
- Generate a JS runner wrapper.
- Use
pkg(a Node.js packager) to buildeditor.exe.
You now have a native Windows executable of your text editor!
Conclusion
Vexon is a capable language for scripting, tooling, and learning language design. Its "synchronous-style" async handling and built-in standard library make it incredibly fast to write CLI tools.
Next Step: Try extending the text editor to support deleting lines (pop or creating a new array) or adding a cat command to print other files using read. Happy coding! 🚀
No comments:
Post a Comment