Skip to content

Commit

Permalink
docs: add f-string example
Browse files Browse the repository at this point in the history
  • Loading branch information
xXenvy committed Aug 14, 2024
1 parent ac89006 commit 2845e1c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/examples/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,22 @@ This example demonstrates the use of logical operators `AND` and `OR` in Zynk.
println("Both c and d are true.");
}
}
main();

## Example 7: Formatted Strings (f-string)

This example demonstrates how to use f-strings to create formatted strings in Zynk.

def main() {
var name: string = "Alice";
var age: int = 30;
var height: float = 5.9;

// Using f-string to create a formatted string
var message: string = f"Name: {name}, Age: {age}, Height: {height}";

// Printing the formatted string
println(message);
println(f"{name} will be {age + 1} years old next year.");
}
main();
12 changes: 12 additions & 0 deletions tests/test_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,16 @@ TEST(EvaluatorTest, EvaluateFStringWithStringInterpolation) {
Evaluator evaluator;
evaluator.evaluate(program.get());
ASSERT_EQ(testing::internal::GetCapturedStdout(), "My name is Alice and I am 30 years old.\n");
}

TEST(EvaluatorTest, EvaluateFStringWithUndefinedVariable) {
const std::string code = "println(f\"{abc}\");";
Lexer lexer(code);
const std::vector<Token> tokens = lexer.tokenize();

Parser parser(tokens);
const auto program = parser.parse();

Evaluator evaluator;
ASSERT_THROW(evaluator.evaluate(program.get()), ZynkError);
}

0 comments on commit 2845e1c

Please sign in to comment.