Skip to content

Commit

Permalink
Merge pull request #3 from Ph0enixKM/A9
Browse files Browse the repository at this point in the history
A9 Feature: Add Block
  • Loading branch information
Ph0enixKM authored Jul 25, 2022
2 parents 3eb8a7b + 70857fd commit 37452df
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:#?}");
}
}
}
44 changes: 44 additions & 0 deletions src/modules/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use heraclitus_compiler::prelude::*;
use super::statement::Statement;

#[derive(Debug)]
pub struct Block {
statements: Vec<Statement>
}

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<DefaultMetadata> 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(())
}
}
3 changes: 2 additions & 1 deletion src/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod statement;
pub mod statement;
pub mod block;
4 changes: 3 additions & 1 deletion src/modules/statement.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use heraclitus_compiler::prelude::*;

#[derive(Debug)]
enum StatementType {}

struct Statement {
#[derive(Debug)]
pub struct Statement {
value: Option<StatementType>
}

Expand Down

0 comments on commit 37452df

Please sign in to comment.