Implemented abool

- Tidied up code
This commit is contained in:
Erin 2021-04-18 23:40:41 +02:00 committed by ondra05
parent 3e2dc5fba9
commit f7d1287fd5
6 changed files with 97 additions and 26 deletions

70
Cargo.lock generated
View file

@ -8,6 +8,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"logos", "logos",
"rand",
] ]
[[package]] [[package]]
@ -42,6 +43,12 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.33.3" version = "2.33.3"
@ -63,6 +70,17 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 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]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.18" version = "0.1.18"
@ -102,6 +120,12 @@ dependencies = [
"utf8-ranges", "utf8-ranges",
] ]
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.26" version = "1.0.26"
@ -120,6 +144,46 @@ dependencies = [
"proc-macro2", "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]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.23" version = "0.6.23"
@ -176,6 +240,12 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" 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]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View file

@ -8,4 +8,5 @@ edition = "2018"
[dependencies] [dependencies]
clap="*" clap="*"
logos = "0.12" logos = "0.12"
rand = "*"

View file

@ -3,21 +3,11 @@ mod utils;
use item::Expr; use item::Expr;
use crate::variables::Value; use crate::error::{Error, ErrorKind};
use crate::{ use crate::tokens::Token;
error::{Error, ErrorKind},
tokens::Token,
};
use logos::Logos; use logos::Logos;
#[derive(Debug)]
pub enum ParseError {
UnexpectedToken,
LexError,
UnexpectedEOF,
}
/// Parser structure / state machine /// Parser structure / state machine
pub struct Parser<'a> { pub struct Parser<'a> {
lexer: logos::Lexer<'a, Token>, lexer: logos::Lexer<'a, Token>,

View file

@ -1,9 +1,8 @@
use crate::{ use crate::error::{Error, ErrorKind};
error::{Error, ErrorKind}, use crate::tokens::Token;
tokens::{Abool, Token}, use crate::variables::Abool;
};
use super::{ParseError, Parser}; use super::Parser;
pub fn abool2num(abool: Abool) -> i32 { pub fn abool2num(abool: Abool) -> i32 {
match abool { match abool {

View file

@ -103,10 +103,3 @@ pub enum Token {
#[error] #[error]
Error, Error,
} }
#[derive(Debug, PartialEq)]
pub enum Abool {
Never = -1,
Sometimes = 0,
Always = 1,
}

View file

@ -1,11 +1,29 @@
use rand::Rng;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)]
pub enum Abool {
Never = -1,
Sometimes = 0,
Always = 1,
}
impl Into<bool> for Abool {
fn into(self) -> bool {
match self {
Abool::Never => false,
Abool::Always => true,
Abool::Sometimes => rand::thread_rng().gen(),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Value { pub enum Value {
Str(String), Str(String),
Int(i32), Int(i32),
Bool(bool), Bool(bool),
//TODO(Able): Add abool and other variable types Abool(Abool),
} }
#[derive(Debug)] #[derive(Debug)]