Skip to content

Commit 4e11cf4

Browse files
committed
documentation
1 parent 7ba616c commit 4e11cf4

File tree

19 files changed

+130
-4
lines changed

19 files changed

+130
-4
lines changed

zenlang/src/ast/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! ast
2+
//!
3+
//! Contains all ast nodes
14
pub mod array;
25
pub mod array_assign;
36
pub mod array_index;

zenlang/src/ast/node.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@ use alloc::boxed::*;
33
use alloc::string::String;
44
use alloc::vec::*;
55

6+
/// Trait implemented by all ast nodes
67
pub trait Compile {
8+
/// Get the children vector
79
fn get_children(&mut self) -> Option<&mut Vec<Box<dyn Compile>>>;
810

11+
/// Disable pushing for the current node
12+
///
13+
/// This is needed in these cases, where with pushing would just flood the stack with unused data:
14+
/// ```
15+
/// fn main {
16+
/// 123;
17+
/// }
18+
/// ```
19+
/// This would push 123 on the stack with nothing using it, disable_push prevents this
920
fn disable_push(&mut self) {}
1021

22+
/// Compiles the current node and it's children recursively
1123
fn compile_all(&mut self, compiler: &mut Compiler) -> Result<(), String> {
1224
if let Err(e) = self.compile(compiler) {
1325
return Err(e);
@@ -25,5 +37,7 @@ pub trait Compile {
2537
}
2638
Ok(())
2739
}
40+
41+
/// Compiles the current node
2842
fn compile(&mut self, compiler: &mut Compiler) -> Result<(), String>;
2943
}

zenlang/src/compiler.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Compiler
2+
//!
3+
//! Compiles ZenLang into a module
4+
15
use crate::ast::node::Compile;
26
use crate::module::Module;
37
use crate::parser::*;
@@ -25,10 +29,12 @@ impl<'a> Compiler<'_> {
2529
return inst;
2630
}
2731

32+
/// Get the compile module
2833
pub fn get_module(&mut self) -> &mut Module {
2934
return &mut self.module;
3035
}
3136

37+
/// Compile everything
3238
pub fn compile(&mut self) -> Result<(), String> {
3339
self.warnings.clear();
3440
if let Err(e) = self.parser.parse() {

zenlang/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//! ZenLang
2+
//!
3+
//! Primary programming language of ZenOS
4+
//! ```
5+
//! fn main {
6+
//! println("Hello from ZenLang");
7+
//! return null;
8+
//! }
9+
//! ```
110
#![no_std]
211
pub mod ast;
312
pub mod compiler;

zenlang/src/module.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! Module
2+
//!
3+
//! Contains ZenLang module structs
14
use crate::opcode::Opcode;
25
use ::serde::{Deserialize, Serialize};
36
use alloc::string::String;
@@ -7,10 +10,16 @@ use bincode::config::Configuration;
710
use bincode::error::DecodeError;
811
use bincode::*;
912

13+
/// ModuleFunction
14+
///
15+
/// Contains information about a module function
1016
#[derive(Encode, Decode, Serialize, Deserialize, Debug)]
1117
pub struct ModuleFunction {
18+
/// Function name
1219
pub name: String,
20+
/// Function address relative to the module offset
1321
pub addr: u32,
22+
/// Argument count
1423
pub args_count: u64,
1524
}
1625

@@ -24,9 +33,14 @@ impl ModuleFunction {
2433
}
2534
}
2635

36+
/// Module
37+
///
38+
/// Contains module information (code)
2739
#[derive(Encode, Decode, Debug)]
2840
pub struct Module {
41+
/// Opcodes of the module (entire code)
2942
pub opcodes: Vec<Opcode>,
43+
/// Function informations
3044
pub functions: Vec<ModuleFunction>,
3145
}
3246

@@ -38,12 +52,14 @@ impl Module {
3852
};
3953
}
4054

55+
/// Compiles the module into bytes vector (Serializes)
4156
pub fn compile(&self) -> Result<Vec<u8>, bincode::error::EncodeError> {
4257
let cfg = bincode::config::standard();
4358
let bytes = bincode::encode_to_vec(self, cfg);
4459
return bytes;
4560
}
4661

62+
/// Load the module from bytes vector (Deserializes)
4763
pub fn load(&mut self, bytes: Vec<u8>) -> Result<(), DecodeError> {
4864
let cfg = bincode::config::standard();
4965
match bincode::decode_from_slice::<Module, Configuration>(&bytes, cfg) {
@@ -57,6 +73,7 @@ impl Module {
5773
}
5874
}
5975

76+
/// Get an opcode at a certain address
6077
pub fn get_opcode(&self, addr: u32) -> &Opcode {
6178
return &self.opcodes[addr as usize];
6279
}

zenlang/src/opcode.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
//! Opcode
2+
//!
3+
//! What the virtual machine executes
14
use alloc::string::*;
25
use alloc::vec::*;
36
use bincode::*;
47

8+
/// Opcode
59
#[derive(Encode, Decode, Debug)]
610
pub enum Opcode {
711
Call(),

zenlang/src/parser/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! Parser
2+
//!
3+
//! Contains a parser for generating AST
14
mod parser;
25
mod parser_expr;
36
mod parser_if_chain;

zenlang/src/parser/parser.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl<'a> Parser<'_> {
2020
};
2121
}
2222

23+
/// Get the token precedence from a given token
2324
pub(crate) fn get_token_precedence(&mut self, token: &Token) -> Option<i32> {
2425
match *token {
2526
Token::Operator(op) => {
@@ -64,20 +65,30 @@ impl<'a> Parser<'_> {
6465
}
6566
}
6667

68+
/// Steps a token once
69+
///
70+
/// Saves the next token to the self.current_token
6771
pub(crate) fn next(&mut self) -> Token {
6872
let token = self.tokenizer.next();
6973
self.current_token = token.clone();
7074
return token;
7175
}
7276

77+
/// Formats an error message like this:
78+
///
79+
/// 1: error text
7380
pub(crate) fn error(&self, text: &str) -> String {
7481
return format!("{}: {}", self.tokenizer.get_line(), text);
7582
}
7683

84+
/// Formats an error message like this:
85+
///
86+
/// 1: error text
7787
pub(crate) fn error_str(&self, text: String) -> String {
7888
return format!("{}: {}", self.tokenizer.get_line(), text);
7989
}
8090

91+
/// Parses a code block
8192
pub(crate) fn parse_block(&mut self) -> Result<Vec<Box<dyn node::Compile>>, String> {
8293
let mut vec: Vec<Box<dyn node::Compile>> = Vec::new();
8394

@@ -104,6 +115,7 @@ impl<'a> Parser<'_> {
104115
Ok(vec)
105116
}
106117

118+
/// Parses function
107119
pub(crate) fn parse_function(&mut self) -> Result<(), String> {
108120
let token = self.next();
109121
if let Token::Identifier(name) = token {
@@ -141,6 +153,7 @@ impl<'a> Parser<'_> {
141153
Ok(())
142154
}
143155

156+
/// Parse everything to self.root
144157
pub fn parse(&mut self) -> Result<(), String> {
145158
self.root = root::AstRoot::new();
146159

zenlang/src/parser/parser_expr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use alloc::format;
66
use alloc::string::*;
77

88
impl<'a> Parser<'_> {
9+
/// Parses an expression
10+
///
11+
/// * `min_prec` - Minimal precedence, 0 if just started parsing
12+
/// * `step_token` - Whether parse_expression should step a token once
913
pub(crate) fn parse_expression(
1014
&mut self,
1115
min_prec: i32,

zenlang/src/parser/parser_if_chain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use alloc::boxed::*;
55
use alloc::string::*;
66

77
impl<'a> Parser<'_> {
8+
/// Parses an if chain
89
pub(crate) fn parse_if_chain(&mut self) -> Result<Box<dyn node::Compile>, String> {
910
let mut chain = if_chain::AstIfChain::new();
1011
loop {

0 commit comments

Comments
 (0)