Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Added more operators, functions, examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnabRollin committed Oct 24, 2023
1 parent 4e9b962 commit 5630c50
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 25 deletions.
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.9.0] - 2023-10-24

### Added

- `!=`, `lazy=` and `lazy!=` operators
- `vars` function which prints all the variables
- `int` and `float` converter functions
- Guessing game example to `README.md`

### Changed

- Changed the example in `README.md`.

### Fixed

- Scope build-up bug has now been fixed

## [0.8.0] - 2023-10-21

### Added
Expand Down Expand Up @@ -94,7 +111,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Simple function and string parsing

[unreleased]: https://github.com/ArnabRollin/dwn/compare/v0.8.0...HEAD
[unreleased]: https://github.com/ArnabRollin/dwn/compare/v0.9.0...HEAD

[0.9.0]: https://github.com/ArnabRollin/dwn/compare/v0.8.0...v0.9.0

[0.8.0]: https://github.com/ArnabRollin/dwn/compare/v0.7.0...v0.8.0

Expand Down
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 = "dwn"
version = "0.8.0"
version = "0.9.0"
edition = "2021"

[dependencies]
Expand Down
50 changes: 33 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,47 @@

`dwn` is the interpreter for the Dawn programming language.

## Installation options

- [Go to book](https://arnabrollin.github.io/dwn-book)
- [Latest Release](https://github.com/ArnabRollin/dwn/release/latest)
- [Releases](https://arnabrollin.github.io/dwn/releases)

## Examples

`code.dwn`
### User Greeting

```dwn
let username = (ask "What is your name? ") ; Asking the user their name...
say "Hello!"
let name = (ask "What is your name? ")
say "Hello" username "!" ; Greet the user...
say "Hello" name "!"
```

let myname = "ExamplePerson"
### Guessing Game

say "My name is" myname "."
```dwn
let n = 1256
say "1 - 1 is" (1 - 1)
say "The sum of 123 and 678 is" (sum 123 678)
say "100 + 0.5 is" (100 + 0.5)
```
if (n == 1256) { ; test
say "Hello! Welcome to The Guesser"
}
forever {
let gs = (ask "Guess the number > ")
let g = (int gs)
if (g == n) {
say "Congratulations! The number was" n
break
}
```console
% dwn run code.dwn
if (g > n) {
say "Too big!"
}
What is your name? ArnabRollin
Hello ArnabRollin!
My name is ExamplePerson.
1 - 1 is 0
The sum of 123 and 678 is 801
100 + 0.5 is 100.5
if (g < n) {
say "Too small!"
}
}
```
150 changes: 148 additions & 2 deletions src/dwn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ lazy_static! {
"eq",
eq as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);
m.insert(
"ne",
ne as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);
m.insert(
"gt",
gt as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
Expand Down Expand Up @@ -113,6 +117,26 @@ lazy_static! {
"break",
break_ as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);
m.insert(
"lazy_eq",
lazy_eq as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);
m.insert(
"lazy_ne",
lazy_ne as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);
m.insert(
"int",
int as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);
m.insert(
"float",
float as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);
m.insert(
"vars",
vars as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);

RwLock::new(m)
};
Expand Down Expand Up @@ -254,12 +278,11 @@ fn run_scope(token: &Token, meta: &mut Metadata) -> Token {
}
}

*meta.scope -= 1;
let mut drop_vars: Vec<String> = vec![];
let mut variables = VARIABLES.write().unwrap();

for (k, v) in variables.clone() {
if v.scope == *meta.scope + 1 {
if v.scope == *meta.scope {
drop_vars.push(k);
}
}
Expand All @@ -268,6 +291,8 @@ fn run_scope(token: &Token, meta: &mut Metadata) -> Token {
variables.remove(&k);
}

*meta.scope -= 1;

return ret;
}
_ => {
Expand Down Expand Up @@ -681,6 +706,56 @@ fn eq(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let first = &args[0];
let second = &args[1];

if (first.ty == second.ty) && (first.val == second.val) {
return Ok(Token {
ty: TokenTypes::BOOL,
modifiers: vec![],
val: "true".to_string(),
});
}

return Ok(Token {
ty: TokenTypes::BOOL,
modifiers: vec![],
val: "false".to_string(),
});
}

fn ne(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let args = get_args(tokens, meta);

if args.len() < 2 {
return Err("(!=) Not enough arguments!".to_string());
}

let first = &args[0];
let second = &args[1];

if (first.ty == second.ty) && (first.val == second.val) {
return Ok(Token {
ty: TokenTypes::BOOL,
modifiers: vec![],
val: "false".to_string(),
});
}

return Ok(Token {
ty: TokenTypes::BOOL,
modifiers: vec![],
val: "true".to_string(),
});
}

fn lazy_eq(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let args = get_args(tokens, meta);

if args.len() < 2 {
return Err("(lazy=) Not enough arguments!".to_string());
}

let first = &args[0].val;
let second = &args[1].val;

if first == second {
return Ok(Token {
ty: TokenTypes::BOOL,
Expand All @@ -695,6 +770,32 @@ fn eq(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
val: "false".to_string(),
});
}

fn lazy_ne(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let args = get_args(tokens, meta);

if args.len() < 2 {
return Err("(lazy!=) Not enough arguments!".to_string());
}

let first = &args[0].val;
let second = &args[1].val;

if first == second {
return Ok(Token {
ty: TokenTypes::BOOL,
modifiers: vec![],
val: "false".to_string(),
});
}

return Ok(Token {
ty: TokenTypes::BOOL,
modifiers: vec![],
val: "true".to_string(),
});
}

fn gt(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let args = get_args(tokens, meta);

Expand Down Expand Up @@ -1148,3 +1249,48 @@ fn break_(_tokens: Vec<Token>, _meta: &mut Metadata) -> Result<Token, String> {
val: "break".to_string(),
});
}

fn int(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let args = get_args(tokens, meta);

let convertable = &args[0].val;
match convertable.parse::<i32>() {
Ok(int) => int,
Err(_) => return Err("(int) Could not convert value to integer".to_string()),
};

return Ok(Token {
ty: TokenTypes::INT,
modifiers: vec![],
val: convertable.to_string(),
});
}
fn float(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let args = get_args(tokens, meta);

let convertable = &args[0].val;
match convertable.parse::<f32>() {
Ok(int) => int,
Err(_) => return Err("(float) Could not convert value to float".to_string()),
};

return Ok(Token {
ty: TokenTypes::FLOAT,
modifiers: vec![],
val: convertable.to_string(),
});
}

fn vars(_tokens: Vec<Token>, _meta: &mut Metadata) -> Result<Token, String> {
let variables = VARIABLES.read().unwrap();

for (k, v) in variables.iter() {
println!("{}: <{:?}>{}", k, v.value.ty, v.value.val);
}

return Ok(Token {
ty: TokenTypes::NONE,
modifiers: vec![],
val: "None".to_string(),
});
}
Loading

0 comments on commit 5630c50

Please sign in to comment.