How shrehanrajsingh/sharna works
Project type and purpose
• This is a language‐runtime project: a Virtual Machine (SVM) accompanied by an assembler and a small standard library for a custom instruction set (“Sharna”).
• The assembler (in the /assembler folder) processes .sp assembly source into bytecode. The VM (vm.c, cpu/, ram/, rom/, inst/) loads and interprets that bytecode. The lib/std folder provides built-in routines linked into bytecode.
File structure
• Top-level CMakeLists.txt drives the build of:
• assembler/ – tokenizer, parser (quickparser), AST, codegen, symbol context.
• cpu/ – core execution loop and CPU state.
• ram/ and rom/ – memory modules.
• inst/ – one source file per instruction implementing the CPU’s decode/execute.
• lib/std/ – Sharna standard library routines in assembly (.sp).
• vm.c/.h – top-level VM harness that binds CPU, memory, I/O.
• tests/ – sample .sp programs and a C example showing VM usage.
• Configuration: CMakeLists.txt in root and tests folder.
Extract insights from the README
• Assembler examples in tests show usage.
• Standard library is extendable.
• A C compiler targeting SVM bytecode is envisioned but not yet present.
a. Main components
1) CLI / User Interface (driver)
2) Assembler
• Lexer/Tokenizer (tok.c)
• Parser/AST (quickparser.c + ast.c)
• Semantic Context (ctx.c)
• Code generator (codegen.c)
• Output: .svm bytecode
3) Virtual Machine (vm.c)
• Loader (reads bytecode, populates ROM and/or RAM)
• CPU core (cpu.c): fetch‐decode‐execute loop
• Memory modules: ROM (rom.c) and RAM (ram.c)
• Registers (registers.h)
• Instruction implementations under inst/
4) Standard Library (lib/std/*.sp)
5) Tests / Examples (tests/*.sp, test.c)
b. Relationships and interactions
• The user invokes the assembler CLI, passing one or more .sp files.
• Assembler produces a bytecode file.
• VM loader takes bytecode and loads it into ROM (and possibly expands into RAM).
• CPU fetches instructions from memory, decodes via inst/ modules, executes by manipulating registers and memory, and possibly dispatching interrupts (interrupt.c).
• Standard library routines are assembled and linked into the bytecode before execution.
• Tests drive both the assembler and VM in automated runs.
c. Architectural patterns / design principles
• Modular segmentation: each instruction as a separate module in inst/
• Layered design: front-end (assembler) vs. back-end (VM)
• Separation of concerns: parsing vs. codegen vs. execution vs. memory
• Clear interface boundaries: assembler API vs. VM API vs. stdlib
d. Key technologies / frameworks
• C language with modular .c/.h files
• CMake build system
• Custom JSON file for interrupts (INTERRUPTS.json) to drive instruction metadata
• Standard library in assembly (.sp) that the assembler consumes
Detailed capture of architecture
• Inst/*.c under “Instruction Set Implementation,” perhaps with a note “one file per opcode.”
• JSON file as a configuration input for instruction metadata.
• Tests hooking into both the assembler CLI and the VM API (test.c uses vm.h).