Skip to content

Commit

Permalink
Merge pull request #3 from Zelak312/dev
Browse files Browse the repository at this point in the history
V0.1.3
  • Loading branch information
Zelak312 authored Nov 6, 2022
2 parents d88b65f + ee06961 commit c2e7cba
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/rust-clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
- name: Install required cargo
run: cargo install clippy-sarif sarif-fmt

- name: Cache rust
uses: Swatinem/rust-cache@v2

- name: Run rust-clippy
run: cargo clippy
--all-features
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/rust_build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ jobs:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Cache rust
uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --verbose
2 changes: 1 addition & 1 deletion 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
@@ -1,6 +1,6 @@
[package]
name = "nora"
version = "0.1.1"
version = "0.1.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The interpreted block can have the following expressions

- [Ternary expression](#ternary-expression)
- [Math expression](#math-expression)
- [String concatenation](#string-operation)
- [String operation](#string-operation)
- [Identifiers](#identifiers)
- [String conversion](#string-conversion)
- [Number conversion](#number-conversion)
Expand Down Expand Up @@ -156,10 +156,20 @@ This will give 30 since the left is a number and it will convert to string to a
Example:

```
["test" - "t"]
["testes" - "te"]
```

This will give the ouput `es`
This will give the ouput `stes` which removes the first `te` found

## Multiple Subtraction

Example:

```
["testes" -- "te"]
```

This will give the ouput `ss` which removes all `te` found

# Identifiers

Expand Down
2 changes: 2 additions & 0 deletions src/ast/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl nodes::ExecutableNode for nodes::NodeBinaryOperator {
let inner_value = match self.operator {
TokenType::Addition => n.inner_value + rigth.inner_value,
TokenType::Subtraction => n.inner_value - rigth.inner_value,
TokenType::DoubleSubtraction => n.inner_value + rigth.inner_value,
TokenType::Multiplication => n.inner_value * rigth.inner_value,
TokenType::Division => n.inner_value / rigth.inner_value,
TokenType::Power => n.inner_value.powf(rigth.inner_value),
Expand All @@ -85,6 +86,7 @@ impl nodes::ExecutableNode for nodes::NodeBinaryOperator {
let inner_value = match self.operator {
TokenType::Addition => n.inner_value + &rigth.inner_value,
TokenType::Subtraction => n.sub(&rigth).inner_value,
TokenType::DoubleSubtraction => n.sub_multiple(&rigth).inner_value,
_ => panic!("Operation not found for string"),
};

Expand Down
9 changes: 5 additions & 4 deletions src/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ impl Parser {
&mut self,
) -> Result<Rc<dyn nodes::ExecutableNode>, Box<dyn Error>> {
let mut left = self.parse_binary_mul_div()?;
while let Ok(operator) = self
.base_parser
.expect_m(vec![TokenType::Addition, TokenType::Subtraction])
{
while let Ok(operator) = self.base_parser.expect_m(vec![
TokenType::Addition,
TokenType::Subtraction,
TokenType::DoubleSubtraction,
]) {
let right = self.parse_binary_mul_div()?;
left = Rc::new(nodes::NodeBinaryOperator {
operator: operator.r#type,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/types/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ impl TryInto<NBoolean> for NNumber {
} else if self.inner_value == 0.0 {
Ok(false)
} else {
Err(BasicError::new("834572bhjer".to_owned()))
Err(BasicError::new(
"Couldn't convert number to boolean".to_owned(),
))
}?;

Ok(NBoolean {
Expand Down
12 changes: 8 additions & 4 deletions src/lib/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ pub struct NString {

impl NString {
pub fn sub(&self, other: &NString) -> NString {
let mut new_string = self.inner_value.clone();
new_string.retain(|c| !other.inner_value.contains(c));
NString {
inner_value: new_string,
inner_value: self.inner_value.replacen(&other.inner_value, "", 1),
}
}

pub fn sub_multiple(&self, other: &NString) -> NString {
NString {
inner_value: self.inner_value.replace(&other.inner_value, ""),
}
}
}
Expand All @@ -25,7 +29,7 @@ impl TryInto<NNumber> for NString {
let result = self
.inner_value
.parse::<f64>()
.map_err(|_| BasicError::new("dwjdi".to_owned()))?;
.map_err(|_| BasicError::new("couldn't convert string to number".to_owned()))?;

Ok(NNumber {
inner_value: result,
Expand Down
4 changes: 4 additions & 0 deletions src/tokenizer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ impl Lexer {
'/' => Some(TokenType::Log),
_ => None,
},
TokenType::Subtraction => match next_c {
'-' => Some(TokenType::DoubleSubtraction),
_ => None,
},
_ => None,
};

Expand Down
1 change: 1 addition & 0 deletions src/tokenizer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum TokenType {
GreaterThanSign,
GreaterThanEqualSign,
QuestionMarkGreaterThan,
DoubleSubtraction,

KeyNumber,
KeyString,
Expand Down

0 comments on commit c2e7cba

Please sign in to comment.