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

A41 Feature: Add functions #30

Merged
merged 7 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/cli/cli_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl CLI {
if let Ok(tokens) = cc.tokenize() {
let mut meta = ParserMetadata::new(tokens, path, Some(code));
if let Ok(()) = block.parse(&mut meta) {
let mut meta = TranslateMetadata::new();
let mut meta = TranslateMetadata::new(&meta);
return block.translate(&mut meta);
}
return "[parsing err]".to_string()
Expand Down
22 changes: 15 additions & 7 deletions src/modules/block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use heraclitus_compiler::prelude::*;
use crate::{utils::{metadata::ParserMetadata, error::get_error_logger, TranslateMetadata}};
use crate::translate::module::TranslateModule;
use super::statement::st::Statement;
use super::statement::stmt::Statement;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Block {
statements: Vec<Statement>
}
Expand Down Expand Up @@ -35,7 +35,7 @@ impl SyntaxModule<ParserMetadata> for Block {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
meta.var_mem.push_scope();
meta.mem.push_scope();
while let Some(token) = meta.get_current_token() {
// Handle the end of line or command
if ["\n", ";"].contains(&token.word.as_str()) {
Expand All @@ -57,15 +57,23 @@ impl SyntaxModule<ParserMetadata> for Block {
}
self.statements.push(statemant);
}
meta.var_mem.pop_scope();
meta.mem.pop_scope();
Ok(())
}
}

impl TranslateModule for Block {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
self.statements.iter()
.map(|module| module.translate(meta))
.collect::<Vec<_>>().join(";\n")
meta.increase_indent();
let result = if self.is_empty() {
":".to_string()
}
else {
self.statements.iter()
.map(|module| meta.gen_indent() + &module.translate(meta))
.collect::<Vec<_>>().join(";\n")
};
meta.decrease_indent();
result
}
}
2 changes: 1 addition & 1 deletion src/modules/command/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;

use crate::modules::expression::literal::{parse_interpolated_region, translate_interpolated_region};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CommandExpr {
strings: Vec<String>,
interps: Vec<Expr>
Expand Down
2 changes: 1 addition & 1 deletion src/modules/command/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;

use crate::modules::expression::literal::{parse_interpolated_region, translate_interpolated_region};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CommandStatement {
strings: Vec<String>,
interps: Vec<Expr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::modules::expression::expr::Expr;
use crate::translate::module::TranslateModule;
use crate::utils::metadata::{ParserMetadata, TranslateMetadata};
use crate::modules::block::Block;
use crate::modules::statement::st::Statement;
use crate::modules::statement::stmt::Statement;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct IfChain {
cond_blocks: Vec<(Expr, Block)>,
false_block: Option<Box<Block>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@ use crate::translate::module::TranslateModule;
use crate::utils::error::get_warn_logger;
use crate::utils::metadata::{ParserMetadata, TranslateMetadata};
use crate::modules::block::Block;
use crate::modules::statement::st::{Statement, StatementType};
use crate::modules::statement::stmt::{Statement, StatementType};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct IfCondition {
expr: Box<Expr>,
true_block: Box<Block>,
false_block: Option<Box<Block>>,
}

impl IfCondition {
fn translate_block(&self, meta: &mut TranslateMetadata, block: &Block) -> String {
if block.is_empty() {
":\n".to_string()
} else {
block.translate(meta)
}
}

fn prevent_not_using_if_chain(&self, meta: &mut ParserMetadata, statement: &Statement, tok: Option<Token>) {
let is_not_if_chain = matches!(statement.value.as_ref().unwrap(), StatementType::IfCondition(_) | StatementType::IfChain(_));
if is_not_if_chain {
Expand Down Expand Up @@ -90,10 +82,10 @@ impl TranslateModule for IfCondition {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
let mut result = vec![];
result.push(format!("if [ {} != 0 ]; then", self.expr.translate(meta)));
result.push(self.translate_block(meta, &self.true_block));
result.push(self.true_block.translate(meta));
if let Some(false_block) = &self.false_block {
result.push("else".to_string());
result.push(self.translate_block(meta, false_block));
result.push(false_block.translate(meta));
}
result.push("fi".to_string());
result.join("\n")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::translate::module::TranslateModule;
use crate::utils::error::get_error_logger;
use crate::utils::metadata::{ParserMetadata, TranslateMetadata};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Ternary {
cond: Box<Expr>,
true_expr: Box<Expr>,
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Add {
left: Box<Expr>,
right: Box<Expr>,
Expand Down Expand Up @@ -36,7 +36,9 @@ impl SyntaxModule<ParserMetadata> for Add {
syntax(meta, &mut *self.right)?;
// If left and right are not of type Number
let error = "Add operation can only add numbers or text";
self.kind = expression_arms_of_type(meta, &*self.left, &*self.right, &[Type::Num, Type::Text], tok, error);
let l_type = self.left.get_type();
let r_type = self.right.get_type();
self.kind = expression_arms_of_type(meta, &l_type, &r_type, &[Type::Num, Type::Text], tok, error);
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{super::expr::Expr, parse_left_expr, expression_arms_of_same_type};
use crate::modules::{Type, Typed};


#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct And {
left: Box<Expr>,
right: Box<Expr>
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{super::expr::Expr, parse_left_expr, expression_arms_of_type};
use crate::modules::{Type, Typed};
use crate::translate::module::TranslateModule;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Div {
left: Box<Expr>,
right: Box<Expr>
Expand Down Expand Up @@ -32,7 +32,9 @@ impl SyntaxModule<ParserMetadata> for Div {
token(meta, "/")?;
syntax(meta, &mut *self.right)?;
let error = "Divide operation can only divide numbers";
expression_arms_of_type(meta, &*self.left, &*self.right, &[Type::Num], tok, error);
let l_type = self.left.get_type();
let r_type = self.right.get_type();
expression_arms_of_type(meta, &l_type, &r_type, &[Type::Num], tok, error);
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::strip_text_quotes;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_same_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Eq {
left: Box<Expr>,
right: Box<Expr>
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/ge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Ge {
left: Box<Expr>,
right: Box<Expr>
Expand Down Expand Up @@ -33,7 +33,9 @@ impl SyntaxModule<ParserMetadata> for Ge {
token(meta, ">=")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
expression_arms_of_type(meta, &*self.left, &*self.right, &[Type::Num], tok, error);
let l_type = self.left.get_type();
let r_type = self.right.get_type();
expression_arms_of_type(meta, &l_type, &r_type, &[Type::Num], tok, error);
Ok(())
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/gt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Gt {
left: Box<Expr>,
right: Box<Expr>
Expand Down Expand Up @@ -33,7 +33,9 @@ impl SyntaxModule<ParserMetadata> for Gt {
token(meta, ">")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
expression_arms_of_type(meta, &*self.left, &*self.right, &[Type::Num], tok, error);
let l_type = self.left.get_type();
let r_type = self.right.get_type();
expression_arms_of_type(meta, &l_type, &r_type, &[Type::Num], tok, error);
Ok(())
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/le.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Le {
left: Box<Expr>,
right: Box<Expr>
Expand Down Expand Up @@ -33,7 +33,9 @@ impl SyntaxModule<ParserMetadata> for Le {
token(meta, "<=")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
expression_arms_of_type(meta, &*self.left, &*self.right, &[Type::Num], tok, error);
let l_type = self.left.get_type();
let r_type = self.right.get_type();
expression_arms_of_type(meta, &l_type, &r_type, &[Type::Num], tok, error);
Ok(())
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/lt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Lt {
left: Box<Expr>,
right: Box<Expr>
Expand Down Expand Up @@ -33,7 +33,9 @@ impl SyntaxModule<ParserMetadata> for Lt {
token(meta, "<")?;
syntax(meta, &mut *self.right)?;
let error = "Cannot compare two values of different types";
expression_arms_of_type(meta, &*self.left, &*self.right, &[Type::Num], tok, error);
let l_type = self.left.get_type();
let r_type = self.right.get_type();
expression_arms_of_type(meta, &l_type, &r_type, &[Type::Num], tok, error);
Ok(())
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/modules/expression/binop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ pub mod le;
pub mod eq;
pub mod neq;

pub fn expression_arms_of_type<U, V>(meta: &mut ParserMetadata, left: &U, right: &V, kinds: &[Type], tok_pos: Option<Token>, message: &str) -> Type
where
U: Typed,
V: Typed,
{
if kinds.iter().all(|kind | ![left.get_type(), right.get_type()].iter().all(|item| item == kind)) {
pub fn expression_arms_of_type(meta: &mut ParserMetadata, left: &Type, right: &Type, kinds: &[Type], tok_pos: Option<Token>, message: &str) -> Type {
if kinds.iter().all(|kind | ![left, right].iter().all(|item| **item == *kind)) {
get_error_logger(meta, ErrorDetails::from_token_option(tok_pos))
.attach_message(message)
.show()
.exit()
}
left.get_type()
left.clone()
}

pub fn expression_arms_of_same_type(meta: &mut ParserMetadata, left: &Expr, right: &Expr, tok_pos: Option<Token>, message: &str) {
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/modulo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Modulo {
left: Box<Expr>,
right: Box<Expr>
Expand Down Expand Up @@ -33,7 +33,9 @@ impl SyntaxModule<ParserMetadata> for Modulo {
token(meta, "%")?;
syntax(meta, &mut *self.right)?;
let error = "Modulo operation can only be applied to numbers";
expression_arms_of_type(meta, &*self.left, &*self.right, &[Type::Num], tok, error);
let l_type = self.left.get_type();
let r_type = self.right.get_type();
expression_arms_of_type(meta, &l_type, &r_type, &[Type::Num], tok, error);
Ok(())
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Mul {
left: Box<Expr>,
right: Box<Expr>
Expand Down Expand Up @@ -33,7 +33,9 @@ impl SyntaxModule<ParserMetadata> for Mul {
token(meta, "*")?;
syntax(meta, &mut *self.right)?;
let error = "Multiply operation can only multiply numbers";
expression_arms_of_type(meta, &*self.left, &*self.right, &[Type::Num], tok, error);
let l_type = self.left.get_type();
let r_type = self.right.get_type();
expression_arms_of_type(meta, &l_type, &r_type, &[Type::Num], tok, error);
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/neq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::strip_text_quotes;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_same_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Neq {
left: Box<Expr>,
right: Box<Expr>
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_same_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Or {
left: Box<Expr>,
right: Box<Expr>
Expand Down
6 changes: 4 additions & 2 deletions src/modules/expression/binop/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;
use super::{super::expr::Expr, parse_left_expr, expression_arms_of_type};
use crate::modules::{Type, Typed};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Sub {
left: Box<Expr>,
right: Box<Expr>
Expand Down Expand Up @@ -33,7 +33,9 @@ impl SyntaxModule<ParserMetadata> for Sub {
token(meta, "-")?;
syntax(meta, &mut *self.right)?;
let error = "Substract operation can only substract numbers";
expression_arms_of_type(meta, &*self.left, &*self.right, &[Type::Num], tok, error);
let l_type = self.left.get_type();
let r_type = self.right.get_type();
expression_arms_of_type(meta, &l_type, &r_type, &[Type::Num], tok, error);
Ok(())
}
}
Expand Down
Loading