Step-by-step guide to building your first language with The Guardian
Building a language with The Guardian is simple. You'll create a lexer, parser, and compiler that generates bytecode for The Guardian's VM.
The lexer tokenizes source code into tokens.
#include <guardian/parser/parser.hpp>
using namespace guardian::parser;
class MyLexer : public Lexer {
public:
MyLexer(const std::string& source) : Lexer(source) {}
std::vector tokenize() {
// Custom tokenization logic
return Lexer::tokenize();
}
};
The parser builds an AST from tokens.
class MyParser : public Parser {
public:
MyParser(const std::vector& tokens) : Parser(tokens) {}
std::unique_ptr parse() override {
// Custom parsing logic
return Parser::parse();
}
};
Use The Guardian's CodeGen to generate bytecode.
#include <guardian/vm/codegen.hpp>
vm::CodeGen codegen;
codegen.pushInt(42);
codegen.print();
codegen.halt();
auto bytecode = codegen.getBytecode();
#include <guardian/vm/vm.hpp>
vm::VM vm;
vm.load(bytecode);
vm.run();
Check out the complete Axiom language implementation:
git clone https://github.com/Taha95-dev/The-Guardian
cd The-Guardian/examples/axiom
cat compiler.cpp