Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A7 Feature: Add Variables #10

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
heraclitus-compiler = "0.2.0"
heraclitus-compiler = "0.2.1"
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod modules;
mod rules;
mod parser;
mod utils;

use heraclitus_compiler::prelude::*;
use modules::block;
use parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;

fn main() {
let code = "not true";
let code = "let age = 12\nlet name = 'john'";
let rules = rules::get_rules();
let mut cc = Compiler::new("Amber", rules);
let mut block = block::Block::new();
Expand Down
25 changes: 8 additions & 17 deletions src/modules/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::process::exit;

use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::{metadata::ParserMetadata, error::get_error_logger};
use super::statement::statement::Statement;

#[derive(Debug)]
Expand All @@ -10,20 +8,11 @@ pub struct Block {
}

impl Block {
fn error(&mut self, meta: &mut ParserMetadata, 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 {
// TODO: Refactor this part of code
println!("ERROR at {:?}", details.position);
println!("Couldn't load file '{}'", path);
exit(1);
}
}
fn error(&mut self, meta: &mut ParserMetadata, details: ErrorDetails) {
get_error_logger(meta, details)
.attach_message("Undefined syntax")
.show()
.exit()
}
}

Expand All @@ -37,6 +26,7 @@ impl SyntaxModule<ParserMetadata> for Block {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
meta.var_mem.push_scope();
loop {
if let None = meta.get_token_at(meta.get_index()) {
break;
Expand All @@ -47,6 +37,7 @@ impl SyntaxModule<ParserMetadata> for Block {
}
self.statements.push(statemant);
}
meta.var_mem.pop_scope();
Ok(())
}
}
15 changes: 12 additions & 3 deletions src/modules/expression/binop/add.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Add {
left: Box<Expr>,
right: Box<Expr>
right: Box<Expr>,
kind: Type
}

impl Typed for Add {
fn get_type(&self) -> Type {
self.kind.clone()
}
}

impl SyntaxModule<ParserMetadata> for Add {
Expand All @@ -14,7 +22,8 @@ impl SyntaxModule<ParserMetadata> for Add {
fn new() -> Self {
Add {
left: Box::new(Expr::new()),
right: Box::new(Expr::new())
right: Box::new(Expr::new()),
kind: Type::Void
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/modules/expression/binop/and.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};


#[derive(Debug)]
pub struct And {
left: Box<Expr>,
right: Box<Expr>
}

impl Typed for And {
fn get_type(&self) -> Type {
Type::Bool
}
}

impl SyntaxModule<ParserMetadata> for And {
syntax_name!("And");

Expand Down
15 changes: 12 additions & 3 deletions src/modules/expression/binop/div.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Div {
left: Box<Expr>,
right: Box<Expr>
right: Box<Expr>,
kind: Type
}

impl Typed for Div {
fn get_type(&self) -> Type {
self.kind.clone()
}
}

impl SyntaxModule<ParserMetadata> for Div {
Expand All @@ -14,7 +22,8 @@ impl SyntaxModule<ParserMetadata> for Div {
fn new() -> Self {
Div {
left: Box::new(Expr::new()),
right: Box::new(Expr::new())
right: Box::new(Expr::new()),
kind: Type::Void
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/modules/expression/binop/eq.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Eq {
left: Box<Expr>,
right: Box<Expr>
}

impl Typed for Eq {
fn get_type(&self) -> Type {
Type::Bool
}
}

impl SyntaxModule<ParserMetadata> for Eq {
syntax_name!("Eq");

Expand Down
9 changes: 8 additions & 1 deletion src/modules/expression/binop/ge.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Ge {
left: Box<Expr>,
right: Box<Expr>
}

impl Typed for Ge {
fn get_type(&self) -> Type {
Type::Bool
}
}

impl SyntaxModule<ParserMetadata> for Ge {
syntax_name!("Ge");

Expand Down
9 changes: 8 additions & 1 deletion src/modules/expression/binop/gt.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Gt {
left: Box<Expr>,
right: Box<Expr>
}

impl Typed for Gt {
fn get_type(&self) -> Type {
Type::Bool
}
}

impl SyntaxModule<ParserMetadata> for Gt {
syntax_name!("Gt");

Expand Down
9 changes: 8 additions & 1 deletion src/modules/expression/binop/le.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Le {
left: Box<Expr>,
right: Box<Expr>
}

impl Typed for Le {
fn get_type(&self) -> Type {
Type::Bool
}
}

impl SyntaxModule<ParserMetadata> for Le {
syntax_name!("Le");

Expand Down
9 changes: 8 additions & 1 deletion src/modules/expression/binop/lt.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Lt {
left: Box<Expr>,
right: Box<Expr>
}

impl Typed for Lt {
fn get_type(&self) -> Type {
Type::Bool
}
}

impl SyntaxModule<ParserMetadata> for Lt {
syntax_name!("Lt");

Expand Down
4 changes: 2 additions & 2 deletions src/modules/expression/binop/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::super::expression::expr::Expr;

pub mod add;
Expand Down Expand Up @@ -34,7 +34,7 @@ impl Binop {
fn binop_left_cut(meta: &mut ParserMetadata, op: impl AsRef<str>) -> Result<usize, ErrorDetails> {
let old_index = meta.get_index();
let mut parenthesis = 0;
while let Some(token) = meta.get_token_at(meta.get_index()) {
while let Some(token) = meta.get_current_token() {
// If we were supposed to parse just a fraction
if let Some(border) = meta.binop_border {
if border <= meta.get_index() {
Expand Down
15 changes: 12 additions & 3 deletions src/modules/expression/binop/mul.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Mul {
left: Box<Expr>,
right: Box<Expr>
right: Box<Expr>,
kind: Type
}

impl Typed for Mul {
fn get_type(&self) -> Type {
self.kind.clone()
}
}

impl SyntaxModule<ParserMetadata> for Mul {
Expand All @@ -14,7 +22,8 @@ impl SyntaxModule<ParserMetadata> for Mul {
fn new() -> Self {
Mul {
left: Box::new(Expr::new()),
right: Box::new(Expr::new())
right: Box::new(Expr::new()),
kind: Type::Void
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/modules/expression/binop/neq.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Neq {
left: Box<Expr>,
right: Box<Expr>
}

impl Typed for Neq {
fn get_type(&self) -> Type {
Type::Bool
}
}

impl SyntaxModule<ParserMetadata> for Neq {
syntax_name!("Neq");

Expand Down
9 changes: 8 additions & 1 deletion src/modules/expression/binop/or.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use heraclitus_compiler::prelude::*;
use crate::parser::ParserMetadata;
use crate::utils::metadata::ParserMetadata;
use super::{super::expr::Expr, Binop};
use crate::modules::{Type, Typed};

#[derive(Debug)]
pub struct Or {
left: Box<Expr>,
right: Box<Expr>
}

impl Typed for Or {
fn get_type(&self) -> Type {
Type::Bool
}
}

impl SyntaxModule<ParserMetadata> for Or {
syntax_name!("Or");

Expand Down
Loading