Skip to content

Commit

Permalink
Merge branch 'mistress' into real-language
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-extracts authored Nov 29, 2024
2 parents efc82a9 + ce065a8 commit f8b52d2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 3.3.0 : Colours
The REPL is now full of :sparkles: colours :sparkles:
Fix the re-assignment bug

# Version 3.2.0 : Differentiation #2
Differentiates all functions

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
name = "mini-calc"
version = "3.4.0-alpha"
license = "GPL-3.0-or-later"
description = "A fully-featured minimalistic configurable rust calculator"
homepage = "https://calc.nwa2coco.fr"
repository = "https://github.com/coco33920/calc"
description = "A Fully-Featured Configurable (mini) Rust Calculator"
homepage = "https://calc.charlotte-thomas.me"
repository = "https://github.com/vanilla-extract/calc"
readme = "README.md"
edition = "2021"
exclude = [
Expand Down
56 changes: 44 additions & 12 deletions src/interpreting/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,62 @@ pub fn interpret(
Parameters::Assign => match *(l.clone()) {
Ast::Call { name: n, lst: list } => {
if function.contains_key(&n) {
Parameters::Str("This function has already been set".to_string())
println!(
"{}",
ansi_term::Color::Red
.bold()
.paint("This function has already been set")
);
Parameters::Null
} else {
if n.as_str() != "" {
(function).insert(n.to_string(), (list, *r.clone()));
(function).insert(n.to_string(), (list.clone(), *r.clone()));
}
Parameters::Identifier(format!(
"@The function {} has been set",
n.clone()
))
println!(
"{}: {} = {}",
ansi_term::Color::Cyan.paint("fun"),
ansi_term::Color::RGB(255, 215, 0).paint(format!(
"{}",
Ast::Call {
name: n.clone(),
lst: list.clone()
}
)),
ansi_term::Color::RGB(255, 215, 0).paint(format!("{}", *r.clone()))
);
Parameters::Null
}
}
_ => {
let (a, b) = assign(param1.clone(), param2.clone());
let p1 = match *l.clone() {
Ast::Node { value, left, right } => {
match (value.clone(), *left, *right) {
(Parameters::Identifier(_), Ast::Nil, Ast::Nil) => {
value.clone()
}
_ => Parameters::Null,
}
}
_ => Parameters::Null,
};
let (a, b) = assign(p1, param2.clone());
if a != "".to_string() {
if ram.contains_key(&a) {
ram.remove(&a);
}
(ram).insert(a.clone(), b.clone());

return Parameters::Identifier(format!(
"@ {} = {}",
a.clone(),
b.clone().pretty_print(Some(ram), Some(function))
));
println!(
"{}: {} = {}",
ansi_term::Color::Cyan.paint("assign"),
ansi_term::Color::Yellow.paint(format!("{}", a.clone())),
ansi_term::Color::Yellow.paint(format!(
"{}",
b.clone().pretty_print(Some(ram), Some(function))
))
);

return Parameters::Null;
}
Parameters::Null
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ fn main() {
if result != Parameters::Null {
println!(
"{}",
result.pretty_print(Some(&mut ram), Some(&mut functions))
result.argument_print(Some(&mut ram), Some(&mut functions))
)
}
}
Expand Down
82 changes: 82 additions & 0 deletions src/parsing/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;
use std::fmt::{Display, Formatter};

use ansi_term::Color;

use crate::exact_math::rationals::Rationals;
use crate::lexing::token::{Operator, Token};
use crate::parsing::ast::Ast::{Nil, Node};
Expand Down Expand Up @@ -390,6 +392,86 @@ impl Parameters {
_ => format!("{self}"),
}
}
pub fn argument_print(
&self,
ram: Option<&mut HashMap<String, Parameters>>,
function: Option<&mut HashMap<String, (Vec<Ast>, Ast)>>,
) -> String {
match self.clone() {
Int(_) => format!(
"{}: {} = {}",
Color::Cyan.paint("val"),
Color::Green.paint("int"),
Color::Green.paint(self.pretty_print(ram, function))
),
Float(_) => format!(
"{}: {} = {}",
Color::Cyan.paint("val"),
Color::RGB(186, 214, 152).paint("float"),
Color::RGB(186, 214, 152).paint(self.pretty_print(ram, function))
),
Identifier(s) => {
if s.starts_with("@") {
self.pretty_print(ram, function)
} else {
format!(
"{}: {} = {}",
Color::Cyan.paint(format!("{}", s.clone())),
Color::Yellow.paint("ident"),
Color::Yellow.paint(self.pretty_print(ram, function))
)
}
}
Rational(_) => format!(
"{}: {} = {}",
Color::Cyan.paint("val"),
Color::RGB(237, 138, 35).paint("rational"),
Color::RGB(237, 138, 35).paint(self.pretty_print(ram, function)),
),
Bool(_) => format!(
"{}: {} = {}",
Color::Cyan.paint("val"),
Color::RGB(234, 144, 144).paint("bool"),
Color::RGB(234, 144, 144).paint(self.pretty_print(ram, function)),
),
InterpreterVector(_) => {
format!(
"{}: {} \n{}",
Color::Cyan.paint("val"),
Color::RGB(248, 204, 249).paint("matrix"),
Color::RGB(248, 204, 249).paint(self.pretty_print(ram, function))
)
}
Var(_, _, _) => {
format!(
"{}: {} = {}",
Color::Cyan.paint("val"),
Color::RGB(30, 154, 176).paint("var"),
Color::RGB(30, 154, 176).paint(self.pretty_print(ram, function))
)
}
Plus(_, _) | Mul(_, _) | Div(_, _) => {
format!(
"{}: {} = {}",
Color::Cyan.paint("val"),
Color::Red.paint("op"),
Color::Red.paint(self.pretty_print(ram, function))
)
}
Str(_) => {
format!(
"{}: {} = {}{}{}",
Color::Cyan.paint("val"),
Color::Blue.paint("string"),
Color::Blue.paint("\""),
Color::Blue.paint(self.pretty_print(ram, function)),
Color::Blue.paint("\"")
)
}

_ => self.pretty_print(ram, function),
}
}
}

pub fn token_to_parameter(token: Token) -> Parameters {
Expand Down

0 comments on commit f8b52d2

Please sign in to comment.