Skip to content

Commit

Permalink
added rst compiler structure
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 9, 2024
1 parent 948b467 commit 231435b
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 15 deletions.
2 changes: 1 addition & 1 deletion sample.hexo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$ hello_world 'hello world' 01
> 01 02 03
4 changes: 2 additions & 2 deletions src/compiler/compilation_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pub(crate) struct Compilation {

impl Compilation {

pub(crate) fn empty() -> Self {
pub(crate) fn from(content: Vec<u8>) -> Self {
Compilation {
content: Vec::new()
content
}
}
}
8 changes: 3 additions & 5 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ impl HexoCompiler {
pub(crate) fn compile<TSource: CompilerSource>(&self, source: &TSource) -> Result<Compilation, CompilerError> {
let cst = self.compile_cst(source)?;
let rst_compiler = RstCompiler::new(self);
let rst = rst_compiler.compile(&cst);

println!("{:#?}", cst);

return Ok(Compilation::empty());
let rst = rst_compiler.compile(&cst)
.map_err(|e| CompilerError::RST(e))?;
return Ok(Compilation::from(rst.emits.as_vec()));
}
}
35 changes: 35 additions & 0 deletions src/compiler/rst/compilation_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::collections::HashMap;
use std::path::PathBuf;
use crate::compiler::cst::CstFile;

use crate::compiler::util::ByteBuffer;

pub(crate) struct ConstantBinding {
pub(crate) name: String,
pub(crate) byte_buffer: ByteBuffer
}

pub(crate) struct CompilationContext {
self_path: PathBuf,
constant_table: HashMap<String, ConstantBinding>,
}

impl CompilationContext {
pub(crate) fn new(path: &PathBuf) -> CompilationContext {
return CompilationContext {
self_path: path.clone(),
constant_table: HashMap::new(),
};
}

pub(crate) fn bind_constant(&mut self, constant: ConstantBinding) {
self.constant_table.insert(
constant.name.clone(),
constant,
);
}

pub(crate) fn has_constant(&self, name: &String) -> bool {
return self.constant_table.contains_key(name);
}
}
34 changes: 31 additions & 3 deletions src/compiler/rst/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::compiler::cst::CstFile;
use std::path::PathBuf;
use crate::compiler::cst::{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 All @@ -10,13 +14,37 @@ pub(crate) struct RstCompiler<'a> {
}

impl RstCompiler<'_> {

pub(crate) fn new(parent: &HexoCompiler) -> RstCompiler {
RstCompiler {
parent: parent
}
}

pub(crate) fn compile(&self, cst: &CstFile) -> Result<HexoFile, RstCompilerError> {
return todo!();
let context = Self::build_context(&cst.path, &cst.main)?;

let bb = Self::build_bytes(&context, &cst.main.emits)?;

return Ok(
HexoFile {
path: cst.path.clone(),
context: context,
emits: bb,
}
);
}

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

return Ok(byte_buffer);
}
}

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

return Ok(root_context);
}
}

1 change: 1 addition & 0 deletions src/compiler/rst/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod node;
mod compiler;
mod compilation_context;

pub(crate) use compiler::{RstCompiler, RstCompilerError};
pub(crate) use node::*;
8 changes: 7 additions & 1 deletion src/compiler/rst/node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pub(crate) struct HexoFile {
use std::path::PathBuf;
use crate::compiler::rst::compilation_context::CompilationContext;
use crate::compiler::util::ByteBuffer;

pub(crate) struct HexoFile {
pub(crate) path: PathBuf,
pub(crate) context: CompilationContext,
pub(crate) emits: ByteBuffer,
}
5 changes: 3 additions & 2 deletions src/cst_legacy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::PathBuf;
use crate::compiler::ast::{AstNode, AstNodeType};
use crate::cst_legacy::CstParseError::NodeValueMissing;
use crate::encoding_legacy;
Expand Down Expand Up @@ -66,7 +67,7 @@ impl FromIterator<CstAtom> for CstAtomStrip {

#[derive(Debug)]
pub(crate) struct CstFile {
pub(crate) file_name: String,
pub(crate) file_name: PathBuf,
pub(crate) statements: Vec<CstStatement>,
}

Expand Down Expand Up @@ -172,7 +173,7 @@ pub(crate) fn parse_cst(ast_node: AstNode) -> Result<CstFile, CstParseError> {

return Ok(
CstFile {
file_name: "unknown".to_string(), // TODO remove unknown
file_name: PathBuf::from("unknown"), // TODO remove unknown
statements: ast_node
.children
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn new_compiler() {
PathBuf::from("sample.hexo"),
);

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

// list files in directory test cases
Expand Down

0 comments on commit 231435b

Please sign in to comment.