From e020fc42679e3720484be9ae0a779641ee5fea68 Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 18 Apr 2021 23:40:41 +0200 Subject: [PATCH] Implemented abool - Tidied up code --- Cargo.lock | 70 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/parser/mod.rs | 14 ++------- src/parser/utils.rs | 9 +++--- src/tokens.rs | 7 ----- src/variables.rs | 20 ++++++++++++- 6 files changed, 97 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3fcf7f8..05e63be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,7 @@ version = "0.1.0" dependencies = [ "clap", "logos", + "rand", ] [[package]] @@ -42,6 +43,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "2.33.3" @@ -63,6 +70,17 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.18" @@ -102,6 +120,12 @@ dependencies = [ "utf8-ranges", ] +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + [[package]] name = "proc-macro2" version = "1.0.26" @@ -120,6 +144,46 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + [[package]] name = "regex-syntax" version = "0.6.23" @@ -176,6 +240,12 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index a792b2e..9f8f743 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2018" [dependencies] clap="*" -logos = "0.12" \ No newline at end of file +logos = "0.12" +rand = "*" \ No newline at end of file diff --git a/src/parser/mod.rs b/src/parser/mod.rs index aeffcad..33a8409 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -3,21 +3,11 @@ mod utils; use item::Expr; -use crate::variables::Value; -use crate::{ - error::{Error, ErrorKind}, - tokens::Token, -}; +use crate::error::{Error, ErrorKind}; +use crate::tokens::Token; use logos::Logos; -#[derive(Debug)] -pub enum ParseError { - UnexpectedToken, - LexError, - UnexpectedEOF, -} - /// Parser structure / state machine pub struct Parser<'a> { lexer: logos::Lexer<'a, Token>, diff --git a/src/parser/utils.rs b/src/parser/utils.rs index 68098dd..bae92a3 100644 --- a/src/parser/utils.rs +++ b/src/parser/utils.rs @@ -1,9 +1,8 @@ -use crate::{ - error::{Error, ErrorKind}, - tokens::{Abool, Token}, -}; +use crate::error::{Error, ErrorKind}; +use crate::tokens::Token; +use crate::variables::Abool; -use super::{ParseError, Parser}; +use super::Parser; pub fn abool2num(abool: Abool) -> i32 { match abool { diff --git a/src/tokens.rs b/src/tokens.rs index 902c3db..ddfe26e 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -103,10 +103,3 @@ pub enum Token { #[error] Error, } - -#[derive(Debug, PartialEq)] -pub enum Abool { - Never = -1, - Sometimes = 0, - Always = 1, -} diff --git a/src/variables.rs b/src/variables.rs index 1c9963f..b3020a1 100644 --- a/src/variables.rs +++ b/src/variables.rs @@ -1,11 +1,29 @@ +use rand::Rng; use std::collections::HashMap; +#[derive(Debug, Clone, PartialEq)] +pub enum Abool { + Never = -1, + Sometimes = 0, + Always = 1, +} + +impl Into for Abool { + fn into(self) -> bool { + match self { + Abool::Never => false, + Abool::Always => true, + Abool::Sometimes => rand::thread_rng().gen(), + } + } +} + #[derive(Debug, Clone)] pub enum Value { Str(String), Int(i32), Bool(bool), - //TODO(Able): Add abool and other variable types + Abool(Abool), } #[derive(Debug)]