Skip to content

Commit

Permalink
0.9.513
Browse files Browse the repository at this point in the history
- Evaluation
  • Loading branch information
RobbyV2 committed Dec 7, 2024
1 parent 0699b93 commit 09b5b6c
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 8 deletions.
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
Expand Up @@ -2,7 +2,7 @@

[package]
name = "fplc"
version = "0.9.512"
version = "0.9.513"
edition = "2021"
description = "A pseudolang interpreter written in Rust"
repository = "https://github.com/PseudoLang-Software-Foundation/Pseudolang"
Expand Down
4 changes: 2 additions & 2 deletions installer/pseudolang.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

!define MUI_ICON "Pseudolang-Logo.ico"

Name "PseudoLang Installer v0.9.512"
Name "PseudoLang Installer v0.9.513"
InstallDir "$PROGRAMFILES\PseudoLang\"
OutFile "../release/installer/pseudolang-setup-x64.exe"
BrandingText "(c) 2024 PseudoLang Software Foundation"
Expand Down Expand Up @@ -33,7 +33,7 @@ Section ""
WriteRegStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$INSTDIR;$R0"

WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Pseudolang" "DisplayName" "Pseudolang"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Pseudolang" "DisplayVersion" "0.9.512"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Pseudolang" "DisplayVersion" "0.9.513"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Pseudolang" "Publisher" "Pseudolang Software Foundation"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Pseudolang" "DisplayIcon" "$INSTDIR\Pseudolang-Logo.ico"

Expand Down
3 changes: 1 addition & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div align="center">
<p>
<img src="https://github.com/PseudoLang-Software-Foundation/Pseudolang/actions/workflows/build.yml/badge.svg" alt="Build and Test Pseudolang">
<img src="https://img.shields.io/badge/Version-0.9.512-green" alt="Version">
<img src="https://img.shields.io/badge/Version-0.9.513-green" alt="Version">
<a href="https://nightly.link/PseudoLang-Software-Foundation/Pseudolang/workflows/build/main"><img src="https://img.shields.io/badge/Nightly-Releases-purple" alt="Nightly Releases"></a>
</p>
</div>
Expand Down Expand Up @@ -60,7 +60,6 @@ The file `src/tests/mod.rs` also contains various unit tests (examples of code)
<summary>Functionality</summary>

- [ ] Dictionaries
- [ ] Evaluation
- [ ] Better error handling (line, column)
- [ ] Networking
- [ ] File IO
Expand Down
13 changes: 13 additions & 0 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,19 @@ fn evaluate_node(
}
}
}
AstNode::Eval(expr) => {
let expr_val = evaluate_node(expr, Rc::clone(&env), debug)?;
if let Value::String(s) = expr_val {
let mut lexer = crate::lexer::Lexer::new(&s);
let tokens = lexer.tokenize();
let mut parser = crate::parser::Parser::new(tokens);
let ast = parser.parse_expression(debug)?;

evaluate_node(&ast, Rc::clone(&env), debug)
} else {
Err("EVAL requires a string argument".to_string())
}
}
_ => Err(format!("Unimplemented node type: {:?}", node)),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub enum Token {

Null,
NaN,
Eval,
}

pub struct Lexer<'a> {
Expand Down Expand Up @@ -432,6 +433,7 @@ impl<'a> Lexer<'a> {
"SORT" => Some(Token::Sort),
"TRY" => Some(Token::Try),
"CATCH" => Some(Token::Catch),
"EVAL" => Some(Token::Eval),
_ => Some(Token::Identifier(identifier)),
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub enum AstNode {
error_var: Option<String>,
catch_block: Box<AstNode>,
},
Eval(Box<AstNode>),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -393,7 +394,7 @@ impl Parser {
}
}

fn parse_expression(&mut self, debug: bool) -> Result<AstNode, String> {
pub fn parse_expression(&mut self, debug: bool) -> Result<AstNode, String> {
if self.match_token(&Token::Sort) {
if !self.match_token(&Token::OpenParen) {
return Err("Expected '(' after SORT".to_string());
Expand Down Expand Up @@ -684,6 +685,17 @@ impl Parser {
}
Ok(AstNode::Input(prompt))
}
Some(Token::Eval) => {
self.advance();
if !self.match_token(&Token::OpenParen) {
return Err("Expected '(' after EVAL".to_string());
}
let expr = self.parse_expression(debug)?;
if !self.match_token(&Token::CloseParen) {
return Err("Expected ')' after EVAL expression".to_string());
}
Ok(AstNode::Eval(Box::new(expr)))
}
_ => match self.advance() {
Some(Token::Integer(n)) => Ok(AstNode::Integer(n)),
Some(Token::Float(f)) => Ok(AstNode::Float(f)),
Expand Down
43 changes: 43 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2436,4 +2436,47 @@ DISPLAY(arr)"#,
"NAN",
);
}

#[test]
fn test_eval() {
assert_output(r#"DISPLAY(EVAL("1 + 2"))"#, "3");
assert_output(r#"DISPLAY(EVAL("2 * (3 + 4)"))"#, "14");
assert_output(r#"DISPLAY(EVAL("10 / 2"))"#, "5");
assert_output(r#"DISPLAY(EVAL("7 MOD 3"))"#, "1");

assert_output(
r#"
x <- 5
DISPLAY(EVAL("x * 2"))
DISPLAY(EVAL("x * (x + 1)"))
"#,
"10\n30",
);

assert_output(
r#"
nums <- [1, 3, 5]
DISPLAY(EVAL("nums[1] + nums[2]"))
"#,
"4",
);

assert_output(
r#"
x <- 10
y <- 20
DISPLAY(EVAL("x < y"))
DISPLAY(EVAL("x = 10"))
DISPLAY(EVAL("(x > 5) AND (y < 30)"))
"#,
"true\ntrue\ntrue",
);

assert_output(r#"DISPLAY(EVAL("1.5 + 2.3"))"#, "3.8");
assert_output(r#"DISPLAY(EVAL("3.0 * (4.5 - 2.5)"))"#, "6");

assert!(run_test(r#"DISPLAY(EVAL("1 + "))"#).is_err());
assert!(run_test(r#"DISPLAY(EVAL("1 / 0"))"#).is_err());
assert!(run_test(r#"DISPLAY(EVAL("invalid"))"#).is_err());
}
}
2 changes: 1 addition & 1 deletion wapm.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[package]
name = "pseudolang/fplc"
version = "0.9.512"
version = "0.9.513"
description = "A pseudolang interpreter written in Rust"
license = "MIT"
readme = "readme.md"
Expand Down

0 comments on commit 09b5b6c

Please sign in to comment.