From 70857fd7066d23f46fa40e11ec6ce3cd92efd3c9 Mon Sep 17 00:00:00 2001 From: Ph0enixKM Date: Mon, 25 Jul 2022 22:19:04 +0200 Subject: [PATCH] feat: add block --- src/main.rs | 7 +++++++ src/modules/block.rs | 44 ++++++++++++++++++++++++++++++++++++++++ src/modules/mod.rs | 3 ++- src/modules/statement.rs | 4 +++- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/modules/block.rs diff --git a/src/main.rs b/src/main.rs index c9dbc352..9fe24c2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,13 +2,20 @@ mod modules; mod rules; use heraclitus_compiler::prelude::*; +use modules::block; fn main() { let code = "1 + 2"; let rules = rules::get_rules(); let mut cc = Compiler::new("Amber", rules); + let mut block = block::Block::new(); cc.load(code); if let Ok(tokens) = cc.tokenize() { println!("{tokens:?}"); + let path = Some(format!("/path/to/file")); + let mut meta = DefaultMetadata::new(tokens, path); + if let Ok(()) = block.parse(&mut meta) { + println!("{block:#?}"); + } } } diff --git a/src/modules/block.rs b/src/modules/block.rs new file mode 100644 index 00000000..71ad8bd6 --- /dev/null +++ b/src/modules/block.rs @@ -0,0 +1,44 @@ +use heraclitus_compiler::prelude::*; +use super::statement::Statement; + +#[derive(Debug)] +pub struct Block { + statements: Vec +} + +impl Block { + fn error(&mut self, meta: &mut DefaultMetadata, mut details: ErrorDetails) { + if let Some(path) = meta.path.clone() { + if let Ok(location) = details.get_pos_by_file(&path) { + Logger::new_err(path, location) + .attach_message("Undefined syntax") + .show() + .exit(); + } else { + println!("Couldn't load file '{}'", path); + } + } + } +} + +impl SyntaxModule for Block { + fn new() -> Self { + Block { + statements: vec![] + } + } + + fn parse(&mut self, meta: &mut DefaultMetadata) -> SyntaxResult { + loop { + if let None = meta.get_token_at(meta.get_index()) { + break; + } + let mut statemant = Statement::new(); + if let Err(details) = statemant.parse(meta) { + self.error(meta, details); + } + self.statements.push(statemant); + } + Ok(()) + } +} \ No newline at end of file diff --git a/src/modules/mod.rs b/src/modules/mod.rs index b5ea4a49..feb44168 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1 +1,2 @@ -mod statement; \ No newline at end of file +pub mod statement; +pub mod block; \ No newline at end of file diff --git a/src/modules/statement.rs b/src/modules/statement.rs index 61f26dcb..f0ca8e07 100644 --- a/src/modules/statement.rs +++ b/src/modules/statement.rs @@ -1,8 +1,10 @@ use heraclitus_compiler::prelude::*; +#[derive(Debug)] enum StatementType {} -struct Statement { +#[derive(Debug)] +pub struct Statement { value: Option }