Skip to content

Commit

Permalink
Added constants to functionality: pi, i, e, and phi, as well as fixed…
Browse files Browse the repository at this point in the history
… a bug with regex replacement and an edge case with character parsing
  • Loading branch information
varphi-online committed Aug 11, 2024
1 parent c27c999 commit 5b32467
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 16 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ A tool to visualize and modify functions that act in both the real and complex p
TODO:

- Add a is_complex function to ast
- constants like PI, e, phi etc...
- Function customization
- Dynamic sliders
- Dynamic/Playable sliders
- Mouse hints/point data/text to screen
- Optimize parser

Expand Down Expand Up @@ -40,7 +39,7 @@ A non-exhaustive checklist on what is needed to complete this project:
- [x] Being able to select the coordinate space of the output for different views
- [ ] Multiple views that move simultaneously of the output data
(calculate once, render n-times)
- [ ] Better input, n-many input boxes, all formatted with KaTeX live rendering
- [x] Better input, n-many input boxes

Optional:

Expand All @@ -55,8 +54,8 @@ I learn any new paridigms or design patterns that I want/need to impl. I will
add them here:

- [x] Debug "mode" to only compile console_log!'s when told.
- [ ] Canvas modification via wasm
- [ ] canvas compositing and instantiation for each function.
- [x] Canvas modification via wasm
- [x] canvas compositing and instantiation for each function.

---

Expand Down
9 changes: 4 additions & 5 deletions pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ A tool to visualize and modify functions that act in both the real and complex p
TODO:

- Add a is_complex function to ast
- constants like PI, e, phi etc...
- Function customization
- Dynamic sliders
- Dynamic/Playable sliders
- Mouse hints/point data/text to screen
- Optimize parser

Expand Down Expand Up @@ -40,7 +39,7 @@ A non-exhaustive checklist on what is needed to complete this project:
- [x] Being able to select the coordinate space of the output for different views
- [ ] Multiple views that move simultaneously of the output data
(calculate once, render n-times)
- [ ] Better input, n-many input boxes, all formatted with KaTeX live rendering
- [x] Better input, n-many input boxes

Optional:

Expand All @@ -55,8 +54,8 @@ I learn any new paridigms or design patterns that I want/need to impl. I will
add them here:

- [x] Debug "mode" to only compile console_log!'s when told.
- [ ] Canvas modification via wasm
- [ ] canvas compositing and instantiation for each function.
- [x] Canvas modification via wasm
- [x] canvas compositing and instantiation for each function.

---

Expand Down
Binary file modified pkg/wgraphcal_bg.wasm
Binary file not shown.
3 changes: 3 additions & 0 deletions src/parser/evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use structs::{abstract_syntax_tree, operator::Operator};

use crate::util::clog;

use super::*;

pub fn string_to_ast_str(input: String) -> String {
Expand All @@ -11,6 +13,7 @@ pub fn string_to_ast_str(input: String) -> String {

pub fn string_to_operator(input: String) -> Option<Operator> {
let lexemes = lexer::scan(input);
clog!("Scanned lexemes: {:?}",lexemes);
let tokens = tokenizer::tokenize(lexemes);
let mut abstract_tree = abstract_syntax_tree::AbstractSyntaxTree::default();
abstract_tree.from_shunting_yard(tokens)
Expand Down
2 changes: 1 addition & 1 deletion src/parser/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn is_numeric(char: &str) -> bool {
}

pub fn is_operational(char: &str) -> bool {
"+-*/^()".contains(char)
"+ - * / ^ ( )".contains(char)
}

pub fn is_variable_id(input: &str) -> bool {
Expand Down
23 changes: 22 additions & 1 deletion src/parser/structs/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::{arity::Arities, op_vec::OpVec, token::Token, value::Value};
use crate::util::clog;
use num_complex::{c64, Complex64};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{collections::HashMap, f64::consts::{E, PI}};
use lazy_static::lazy_static;
use std::fmt;

#[derive(Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -35,6 +36,12 @@ impl Operator {
}
}

pub fn from_value(val_to_add: Value) -> Operator {
let mut out: Operator = Operator::from_token(Token::Num);
out.values = val_to_add;
out
}

pub fn from_token(type_of_token: Token) -> Operator {
let arity: Arities = match type_of_token {
Token::END | Token::SENTINEL | Token::Null | Token::OpenPar | Token::ClosePar => {
Expand Down Expand Up @@ -140,6 +147,8 @@ impl Operator {
Token::ID => {
if let Some(mapped_var) = map.get(&self.symbol) {
mapped_var.clone()
} else if let Some(mapped_var) = CONSTANTS.get(&self.symbol){
mapped_var.clone()
} else {
self.clone()
}
Expand Down Expand Up @@ -248,3 +257,15 @@ impl fmt::Debug for Operator {
}
}
}

lazy_static!{
pub static ref CONSTANTS: HashMap<String,Operator> = {
let mut out = HashMap::new();
out.insert("i".to_string(), Operator::from_value(Value::Number(Complex64::i())));
out.insert("pi".to_string(), Operator::from_value(Value::Number(Complex64::new(PI, 0.0))));
out.insert("e".to_string(), Operator::from_value(Value::Number(Complex64::new(E, 0.0))));
out.insert("phi".to_string(), Operator::from_value(Value::Number(Complex64::new(
1.618033988749894848204586834365, 0.0))));
out
};
}
4 changes: 0 additions & 4 deletions src/parser/structs/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,4 @@ lazy_static! {
out.insert(")".to_string(), Token::ClosePar);
out
};
static ref SYMBOLS: HashMap<Token, String> = OPERATORS
.iter()
.map(|(k, v)| (v.clone(), k.clone()))
.collect();
}
2 changes: 2 additions & 0 deletions src/parser/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use super::structs::{

pub fn tokenize(lexemes: Vec<String>) -> OpVec {
let mut out: OpVec = to_tokens(lexemes);
clog!("Basic token stream: {}",out);
out = apply_partial_grammar(out);
clog!("Partial Grammar: {}",out);
out
}

Expand Down

0 comments on commit 5b32467

Please sign in to comment.