dark_vm/lib.rs
1//! The DarkVM is a VM that can be targeted by any language.
2//! It is written for the Envious programming language, but may targeted by any language.
3//! The VM was created with zero-copy, speed, and simplicity in mind.
4//! This means that the implementation of the VM uses reference counted values, resembling Swift.
5//! Currently, the VM is highly experimental and may change, therefore writing programs in this language is not recommended.
6
7/// The Tokens module, which contains the Token struct and the TokenKind enum. These describe the various tokens that can be recognized.
8pub mod tokens;
9
10/// The Errors module, which contains the Error struct and the ErrorKind enum. These describe the various errors that could occur during the program execution.
11pub mod errors;
12
13/// The Utils module, which contains common utilities such as the stack and frames.
14pub mod utils;
15
16/// The Lexer module, which creates a vector of all of the tokens in the input. This input may come from either a file or a REPL.
17pub mod lexer;
18
19/// The Values module, which contains the Value struct and ValueKind enum. These describe the various values within the program.
20pub mod values;
21
22/// The Code module, which maintains the different values generated by the lexer.
23pub mod code;
24
25/// The VM module. This maintains most of the code for the behavior of different instructions and the behavior of the VM in general.
26pub mod vm;
27
28use lexer::Lexer;
29use vm::VM;
30
31/// Runs the VM, and produces either an error, or the final state of the VM after the operations.
32/// The errors produced can be found in the utils::error::ErrorKind enum.
33pub fn run(contents: &str) -> Result<String, String> {
34 let tokens = Lexer::default()
35 .lex(contents)
36 .map_err(|error| error.prettify(contents))?;
37 let mut vm = VM::new(tokens).map_err(|error| error.prettify(contents))?;
38 let result = vm.run().map_err(|error| error.prettify(contents))?;
39 if result.is_some() {
40 println!("{:#?}\n", result);
41 }
42
43 Ok(format!("{:#?}", vm))
44}