Skip to content

Commit

Permalink
added basic emits rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 10, 2024
1 parent 231435b commit d72a839
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 10 deletions.
2 changes: 2 additions & 0 deletions sample.hexo
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
> 01 02 03
> 'hello'
> 2x10
1 change: 1 addition & 0 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl HexoCompiler {
let rst_compiler = RstCompiler::new(self);
let rst = rst_compiler.compile(&cst)
.map_err(|e| CompilerError::RST(e))?;

return Ok(Compilation::from(rst.emits.as_vec()));
}
}
2 changes: 1 addition & 1 deletion src/compiler/cst/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) struct CstFile {
pub(crate) enum CstAtom {
Hex(u8),
String(String),
Number { value: u32 },
Number(u32),
Constant { name: String },
Function { name: String, params: Vec<CstActualParameter> },
}
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/cst/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,12 @@ fn parse_atom_base_num_into(node: &AstNode, buf: &mut Vec<CstAtom>) -> Result<()
let value_str = value.ok_or(CstParserError::MissingContent { node_type: AstNodeType::AtomBaseNumberValue })?;

buf.push(
CstAtom::Number {
value: u32::from_str_radix(value_str.as_str(), base_value)
CstAtom::Number(
u32::from_str_radix(value_str.as_str(), base_value)
.map_err(|_| CstParserError::MalformedNodeValue {
message: format!("can't parse number {}", value_str)
})?
}
)
);

return Ok(());
Expand Down
21 changes: 18 additions & 3 deletions src/compiler/rst/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::path::PathBuf;
use crate::compiler::cst::{CstEmitStatement, CstFile, CstFunctionStatement};
use crate::compiler::cst::{CstAtom, CstEmitStatement, CstFile, CstFunctionStatement};
use crate::compiler::HexoCompiler;
use crate::compiler::rst::compilation_context::CompilationContext;
use crate::compiler::rst::node::HexoFile;
use crate::compiler::util::ByteBuffer;
use crate::cst_legacy::CstAtom;

#[derive(Debug)]
pub(crate) enum RstCompilerError {}
Expand Down Expand Up @@ -36,11 +35,27 @@ impl RstCompiler<'_> {
}

fn build_bytes(context: &CompilationContext, emits: &Vec<CstEmitStatement>) -> Result<ByteBuffer, RstCompilerError> {
let byte_buffer = ByteBuffer::new();
let mut byte_buffer = ByteBuffer::new();

for emit in emits {
Self::build_bytes_into(context, emit, &mut byte_buffer)
}

return Ok(byte_buffer);
}

fn build_bytes_into(context: &CompilationContext, statement: &CstEmitStatement, buffer: &mut ByteBuffer) {
for atom in &statement.atoms {
match atom {
CstAtom::Hex(byte) => buffer.push_byte(*byte),
CstAtom::String(string) => buffer.push_string(string.clone()),
CstAtom::Number(number) => buffer.push_shrunk_u32(*number),
CstAtom::Constant { .. } => {}
CstAtom::Function { .. } => {}
}
}
}

fn build_context(file_path: &PathBuf, cst: &CstFunctionStatement) -> Result<CompilationContext, RstCompilerError> {
let root_context = CompilationContext::new(file_path);

Expand Down
8 changes: 5 additions & 3 deletions src/compiler/util/byte_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::compiler::util::encoding::to_shrunk_bytes;

pub(crate) struct ByteBuffer {
inner: Vec<u8>,
}
Expand All @@ -17,8 +19,8 @@ impl ByteBuffer {
self.inner.extend_from_slice(string.as_bytes());
}

pub(crate) fn push_u32(&mut self, num: u32) {
self.inner.extend_from_slice(num.to_be_bytes().as_slice());
pub(crate) fn push_shrunk_u32(&mut self, num: u32) {
self.inner.extend(to_shrunk_bytes(num));
}

pub(crate) fn push_byte_buffer(&mut self, other: &mut ByteBuffer) {
Expand Down Expand Up @@ -64,7 +66,7 @@ mod test {
#[test]
fn u32_push() {
let mut buffer = ByteBuffer::new();
buffer.push_u32(13);
buffer.push_shrunk_u32(13);

assert_eq!(buffer.len(), 4);

Expand Down
10 changes: 10 additions & 0 deletions src/compiler/util/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ pub(crate) fn decode_bytes_from_string(s: &String) -> Result<Vec<u8>, ()> {
})
.collect()
}

pub(crate) fn to_shrunk_bytes(value: u32) -> Vec<u8> {
let mut bytes = Vec::new();
let mut value = value;
while value > 0 {
bytes.push((value & 0xFF) as u8);
value >>= 8;
}
bytes
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ fn new_compiler() {
);

let compilation_result = compiler.compile(&source).unwrap();

println!("{:?}", compilation_result.content);
}

// list files in directory test cases
Expand Down

0 comments on commit d72a839

Please sign in to comment.