Skip to content

Commit

Permalink
Merge branch 'development' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erhanbaris committed Mar 2, 2022
2 parents 0f83820 + aa257b6 commit 507ed95
Show file tree
Hide file tree
Showing 61 changed files with 229 additions and 165 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "smartcalc"
description = "Text based calculator for peoples"
version = "1.0.3"
version = "1.0.4"
authors = ["Erhan BARIS <[email protected]>"]
edition = "2018"
license = "GPL-2.0"
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
10 changes: 8 additions & 2 deletions examples/console/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
extern crate smartcalc;
/*
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/

extern crate smartcalc;

fn main() {
use smartcalc::SmartCalc;
Expand All @@ -23,7 +29,7 @@ fn main() {

app.set_decimal_seperator(locale.decimal().to_string());
app.set_thousand_separator(locale.separator().to_string());
app.set_timezone("GMT+1".to_string()).unwrap();
app.set_timezone(timezone).unwrap();

let language = "en".to_string();
let results = app.execute(language, test_data);
Expand Down
21 changes: 20 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,27 @@ $1k | $1,000.00
1M + 1k | 1.001.000
```

### Number Type Conversion
Programmers can convert numbers from one type to another type.
Here is the supported types:

- Octal
- Hexadecimal
- Decimal
- Binary

To use all number types except hexa, you must use the type's name. Since the word hexadecimal is long, hex word can be used.

```
10 hex | 0xA
10 hex to octal | 0o12
0x12 to binary | 0b10010
data = 10 hex | 0xA
data decimal | 10
```

### Variable definition
You can store information in variable and use it again in formulas.
You can store information in variable and use it again in formulas.
```
gross salary = 100k dkk | 100.000,00 kr.
net salary = gross salary - %40 | 60.000,00 kr.
Expand Down
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand All @@ -20,7 +20,7 @@ use crate::tokinizer::TokenInfo;
use crate::tokinizer::TokenInfoStatus;
use crate::tokinizer::Tokinizer;
use crate::tools::parse_timezone;
use crate::types::TokenType;
use crate::types::{TokenType, NumberType};
use crate::types::{SmartCalcAstType, VariableInfo};
use crate::formatter::format_result;
use crate::worker::tools::read_currency;
Expand Down Expand Up @@ -285,7 +285,7 @@ impl SmartCalc {
let mut operator_required = false;

if let TokenType::Operator(_) = tokens[index].deref() {
tokens.insert(index, Rc::new(TokenType::Number(0.0)));
tokens.insert(index, Rc::new(TokenType::Number(0.0, NumberType::Decimal)));
}

while index < tokens.len() {
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/date.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down Expand Up @@ -169,7 +169,7 @@ fn date_test() {
let config = SmartCalcConfig::default();
let session = RefCell::new(Session::default());

assert_eq!(DateItem(NaiveDate::from_ymd(2020, 1, 1), config.get_time_offset()).print(&config, &session), "1 Jan 2020 UTC".to_string());
assert_eq!(DateItem(NaiveDate::from_ymd(2020, 1, 1), config.get_time_offset()).print(&config, &session), "1 Jan 2020".to_string());

let left = DateItem(NaiveDate::from_ymd(2020, 1, 1), config.get_time_offset());
let right = DateItem(NaiveDate::from_ymd(2020, 1, 1), config.get_time_offset());
Expand All @@ -182,5 +182,5 @@ fn date_test() {
let result = left.calculate(&config, true, &right, OperationType::Add);

assert!(result.is_some());
assert_eq!(result.unwrap().print(&config, &session), "21 Jan 2020 UTC".to_string());
assert_eq!(result.unwrap().print(&config, &session), "21 Jan 2020".to_string());
}
2 changes: 1 addition & 1 deletion src/compiler/date_time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/duration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/money.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand All @@ -13,7 +13,7 @@ use alloc::sync::Arc;
use core::ops::Deref;
use crate::app::Session;
use crate::config::SmartCalcConfig;
use crate::types::{CurrencyInfo, TokenType};
use crate::types::{CurrencyInfo, TokenType, NumberType};

use super::number::NumberItem;
use super::{DataItem, OperationType, UnaryType};
Expand Down Expand Up @@ -79,7 +79,7 @@ impl DataItem for MoneyItem {
OperationType::Div => {
let div_result = do_divition(left, right);
match is_other_money {
true => return Some(Arc::new(NumberItem(div_result))),
true => return Some(Arc::new(NumberItem(div_result, NumberType::Decimal))),
false => div_result
}
},
Expand Down
24 changes: 16 additions & 8 deletions src/compiler/number.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/

use core::any::{Any, TypeId};
use core::cell::RefCell;
use alloc::format;
use alloc::string::{ToString, String};
use alloc::sync::Arc;
use crate::app::Session;
use crate::config::SmartCalcConfig;
use crate::types::TokenType;
use crate::types::{TokenType, NumberType};
use super::percent::PercentItem;
use super::{DataItem, OperationType, UnaryType};
use crate::formatter::format_number;
use crate::tools::do_divition;

#[derive(Debug)]

pub struct NumberItem(pub f64);
pub struct NumberItem(pub f64, pub NumberType);
impl DataItem for NumberItem {
fn as_token_type(&self) -> TokenType {
TokenType::Number(self.0)
TokenType::Number(self.0, self.1)
}
fn is_same<'a>(&self, other: &'a dyn Any) -> bool {
match other.downcast_ref::<f64>() {
Expand Down Expand Up @@ -53,17 +54,24 @@ impl DataItem for NumberItem {
OperationType::Mul => left * right,
OperationType::Sub => left - right
};
Some(Arc::new(NumberItem(result)))
Some(Arc::new(NumberItem(result, self.1)))
}
fn get_number(&self, _: &dyn DataItem) -> f64 { self.0 }
fn get_underlying_number(&self) -> f64 { self.0 }
fn type_name(&self) -> &'static str { "NUMBER" }
fn type_id(&self) -> TypeId { TypeId::of::<NumberItem>() }
fn print(&self, config: &SmartCalcConfig, _: &RefCell<Session>) -> String { format_number(self.0, config.thousand_separator.to_string(), config.decimal_seperator.to_string(), 2, true, true) }
fn print(&self, config: &SmartCalcConfig, _: &RefCell<Session>) -> String {
match self.1 {
NumberType::Decimal => format_number(self.0, config.thousand_separator.to_string(), config.decimal_seperator.to_string(), 2, true, true),
NumberType::Binary => format!("{:#b}", self.0 as i32),
NumberType::Octal => format!("{:#o}", self.0 as i32),
NumberType::Hexadecimal => format!("{:#X}", self.0 as i32)
}
}
fn unary(&self, unary: UnaryType) -> Arc<dyn DataItem> {
match unary {
UnaryType::Minus => Arc::new(Self(-1.0 * self.0)),
UnaryType::Plus => Arc::new(Self(self.0))
UnaryType::Minus => Arc::new(Self(-1.0 * self.0, self.1)),
UnaryType::Plus => Arc::new(Self(self.0, self.1))
}
}
}
2 changes: 1 addition & 1 deletion src/compiler/percent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
8 changes: 5 additions & 3 deletions src/formatter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down Expand Up @@ -143,12 +143,14 @@ fn format_result_test() {
use crate::compiler::number::NumberItem;
use crate::compiler::time::TimeItem;
use crate::config::SmartCalcConfig;
use crate::types::NumberType;
let config = SmartCalcConfig::default();

let session = RefCell::new(Session::default());
session.borrow_mut().set_language("en".to_string());
assert_eq!(NumberItem(123456.123456789).print(&config, &session), "123.456,12".to_string());
assert_eq!(NumberItem(1.123456789).print(&config, &session), "1,12".to_string());
assert_eq!(NumberItem(123456.123456789, NumberType::Decimal).print(&config, &session), "123.456,12".to_string());
assert_eq!(NumberItem(1.123456789, NumberType::Decimal).print(&config, &session), "1,12".to_string());
assert_eq!(NumberItem(2.0, NumberType::Hexadecimal).print(&config, &session), "0x2".to_string());

assert_eq!(format_result(&config, &session, Rc::new(SmartCalcAstType::Item(Arc::new(TimeItem(chrono::Utc::today().and_hms(11, 30, 0).naive_utc(), config.get_time_offset()))))), "11:30:00 UTC".to_string());
assert_eq!(format_result(&config, &session, Rc::new(SmartCalcAstType::Item(Arc::new(TimeItem(chrono::Utc::today().and_hms(0, 0, 0).naive_utc(), config.get_time_offset()))))), "00:00:00 UTC".to_string());
Expand Down
11 changes: 5 additions & 6 deletions src/json/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@
"format": {
"date": {
"full_date_time": "{day} {month_short} {year} {hour_pad}:{minute_pad}:{second_pad} {timezone}",
"full_date": "{day} {month_short} {year} {timezone}",
"full_date": "{day} {month_short} {year}",
"current_year_with_time": "{day} {month_long} {hour_pad}:{minute_pad}:{second_pad} {timezone}",
"current_year": "{day} {month_long} {timezone}"
"current_year": "{day} {month_long}"
},
"duration": [
{
Expand Down Expand Up @@ -493,9 +493,8 @@
"number_type_group": [
"hex",
"hexadecimal",
"oct",
"decimal",
"octal",
"bin",
"binary"
],
"duration_group": [
Expand Down Expand Up @@ -569,8 +568,8 @@
"number_type_convert": {
"samples": [],
"rules": [
"{NUMBER:number} {GROUP:type:conversion_group} {TEXT_OR_MONTH:type}",
"{NUMBER:number} {GROUP:type:number_type_group}"
"{NUMBER:number} {GROUP:type:conversion_group} {GROUP:conversion_type:number_type_group}",
"{NUMBER:number} {GROUP:conversion_type:number_type_group}"
]
},
"memory_convert": {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/logger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/assignment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/binary.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/syntax/primative.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down Expand Up @@ -68,7 +68,7 @@ impl PrimativeParser {
return Ok(SmartCalcAstType::None);
},
TokenType::Money(price, currency) => Ok(SmartCalcAstType::Item(Arc::new(MoneyItem(*price, currency.clone())))),
TokenType::Number(double) => Ok(SmartCalcAstType::Item(Arc::new(NumberItem(*double)))),
TokenType::Number(double, number_type) => Ok(SmartCalcAstType::Item(Arc::new(NumberItem(*double, *number_type)))),
TokenType::Memory(memory, memory_type) => Ok(SmartCalcAstType::Item(Arc::new(MemoryItem(*memory, memory_type.clone())))),
TokenType::Field(field_type) => Ok(SmartCalcAstType::Field(field_type.clone())),
TokenType::Percent(percent) => Ok(SmartCalcAstType::Item(Arc::new(PercentItem(*percent)))),
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/statement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/syntax/unary.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down Expand Up @@ -41,7 +41,7 @@ impl UnaryParser {
};

match token.deref() {
TokenType::Number(double) => return Ok(SmartCalcAstType::Item(Arc::new(NumberItem(double * opt)))),
TokenType::Number(double, number_type) => return Ok(SmartCalcAstType::Item(Arc::new(NumberItem(double * opt, *number_type)))),
TokenType::Variable(variable) => return Ok(SmartCalcAstType::PrefixUnary(operator, Rc::new(SmartCalcAstType::Variable(variable.clone())))),
TokenType::Percent(percent) => return Ok(SmartCalcAstType::PrefixUnary(operator, Rc::new(SmartCalcAstType::Item(Arc::new(PercentItem(*percent)))))),
TokenType::Money(money, currency) => return Ok(SmartCalcAstType::PrefixUnary(operator, Rc::new(SmartCalcAstType::PrefixUnary(operator, Rc::new(SmartCalcAstType::Item(Arc::new(MoneyItem(*money, currency.clone())))))))),
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* smartcalc v1.0.3
* smartcalc v1.0.4
* Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
* Licensed under the GNU General Public License v2.0.
*/
Expand Down
Loading

0 comments on commit 507ed95

Please sign in to comment.