Skip to content

Commit

Permalink
added constant resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 10, 2024
1 parent bc6ebb4 commit fab11ff
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
6 changes: 3 additions & 3 deletions sample.hexo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
> 01 02 03
> 'hello'
> 2x10
$ hello 'world'

> $hello
4 changes: 4 additions & 0 deletions src/compiler/rst/compilation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ impl CompilationContext {
pub(crate) fn has_constant(&self, name: &String) -> bool {
return self.constant_table.contains_key(name);
}

pub(crate) fn get_constant(&self, name: &String) -> Option<&ConstantBinding> {
return self.constant_table.get(name);
}
}
58 changes: 46 additions & 12 deletions src/compiler/rst/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use std::path::PathBuf;
use crate::compiler::cst::{CstAtom, CstEmitStatement, CstFile, CstFunctionStatement};
use clap::builder::Str;
use crate::compiler::cst::{CstAtom, CstAtomVec, CstEmitStatement, CstFile, CstFunctionStatement};
use crate::compiler::HexoCompiler;
use crate::compiler::rst::compilation_context::CompilationContext;
use crate::compiler::rst::compilation_context::{CompilationContext, ConstantBinding};
use crate::compiler::rst::node::HexoFile;
use crate::compiler::util::ByteBuffer;

#[derive(Debug)]
pub(crate) enum RstCompilerError {}
pub(crate) enum RstCompilerError {
UnresolvedConstant { name: String },
}

pub(crate) struct RstCompiler<'a> {
parent: &'a HexoCompiler,
}

impl RstCompiler<'_> {

pub(crate) fn new(parent: &HexoCompiler) -> RstCompiler {
RstCompiler {
parent: parent
Expand All @@ -38,28 +40,60 @@ impl RstCompiler<'_> {
let mut byte_buffer = ByteBuffer::new();

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

return Ok(byte_buffer);
}

fn build_bytes_into(context: &CompilationContext, statement: &CstEmitStatement, buffer: &mut ByteBuffer) {
for atom in &statement.atoms {
fn build_bytes_into(context: &CompilationContext, atoms: &CstAtomVec, buffer: &mut ByteBuffer) -> Result<(), RstCompilerError> {
for atom in atoms {
match atom {
CstAtom::Hex(byte) => buffer.push_byte(*byte),
CstAtom::String(string) => buffer.push_string(string.clone()),
CstAtom::Number(number) => buffer.push_u32_shrunk(*number),
CstAtom::Constant { .. } => {}
CstAtom::Hex(byte) =>
buffer.push_byte(*byte),
CstAtom::String(string) =>
buffer.push_string(string.clone()),
CstAtom::Number(number) =>
buffer.push_u32_shrunk(*number),
CstAtom::Constant { name } =>
{ Self::build_constant_into(context, &name, buffer)? }
CstAtom::Function { .. } => {}
}
}

Ok(())
}

fn build_constant_into(context: &CompilationContext, name: &String, buffer: &mut ByteBuffer) -> Result<(), RstCompilerError> {
let constant_binding = context.get_constant(name)
.ok_or(RstCompilerError::UnresolvedConstant { name: name.clone() })?;

buffer.push_byte_buffer(&constant_binding.byte_buffer);

Ok(())
}

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

Self::build_context_constants(&cst, &mut root_context)?;

return Ok(root_context);
}

fn build_context_constants(cst: &&CstFunctionStatement, root_context: &mut CompilationContext) -> Result<(), RstCompilerError> {
for constant in &cst.constants {
let mut buff = ByteBuffer::new();
Self::build_bytes_into(&root_context, &constant.atoms, &mut buff)?;
root_context.bind_constant(
ConstantBinding {
name: constant.name.clone(),
byte_buffer: buff,
}
)
}

Ok(())
}
}

2 changes: 1 addition & 1 deletion src/compiler/util/byte_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl ByteBuffer {
self.inner.extend(to_shrunk_bytes(num));
}

pub(crate) fn push_byte_buffer(&mut self, other: &mut ByteBuffer) {
pub(crate) fn push_byte_buffer(&mut self, other: &ByteBuffer) {
self.inner.extend(other.as_vec());
}

Expand Down

0 comments on commit fab11ff

Please sign in to comment.