From 8d6338282b3db397eea27cd7adae4489a66f0c13 Mon Sep 17 00:00:00 2001 From: iiPython Date: Sat, 9 Mar 2024 19:02:17 -0600 Subject: [PATCH] [docs] OK seriously, last docs change today --- docs/book.toml | 6 +++++ docs/documents/datatypes.md | 6 ++--- docs/documents/packages.md | 4 ++-- docs/tutorials/branching.md | 44 ++++++++++++++++++++++++----------- docs/tutorials/calculator.md | 24 +++++++++---------- docs/tutorials/hello-world.md | 8 +++---- docs/tutorials/user-input.md | 12 +++++----- src/xpp/core/datastore.py | 6 ++++- 8 files changed, 68 insertions(+), 42 deletions(-) diff --git a/docs/book.toml b/docs/book.toml index 932b79c..a2e9bd5 100644 --- a/docs/book.toml +++ b/docs/book.toml @@ -5,5 +5,11 @@ authors = ["iiPython"] language = "en" src = "./" +[build] +create-missing = false + [output.html] git-repository-url = "https://github.com/iiPythonx/xpp" + +[output.html.redirect] +"/README.html" = "/" diff --git a/docs/documents/datatypes.md b/docs/documents/datatypes.md index 02f8158..1323b2e 100644 --- a/docs/documents/datatypes.md +++ b/docs/documents/datatypes.md @@ -126,14 +126,14 @@ It can be compared using mathematical comparators or be modified using certain o Unlike Python, integers do allow leading `0`s to exist: -```xt +```xpp var myInteger 05 prt myInteger :: 5 ``` In vanilla x++, it can also be used as a boolean value, where `1` represents `true` and `0` represents `false`: -```xt +```xpp psh hamburgerIsEaten 1 if (hamburgerIsEaten == 1) "prt 'Someone ate my hamburger'" ``` @@ -225,7 +225,7 @@ Any non-string data type within a string interpolation will require the use of ` When being compared against using mathematical comparators, it is compared lexicographically: -```xt +```xpp if "abc" < "cba" "prt 'true'" ``` diff --git a/docs/documents/packages.md b/docs/documents/packages.md index e5b22e5..f96280a 100644 --- a/docs/documents/packages.md +++ b/docs/documents/packages.md @@ -26,7 +26,7 @@ Packages are x++ files written by other people that can be imported to your x++ project using the `imp` operator: -```xt +```xpp imp "examplePackage" ``` @@ -36,7 +36,7 @@ Imported packages are only available in that one specific file. When attempting You can use an imported package by referencing the package name and then followed by a dot and the section name like so: -```xt +```xpp imp "examplePackage" examplePackage.mySection diff --git a/docs/tutorials/branching.md b/docs/tutorials/branching.md index 758fa3f..b160f0c 100644 --- a/docs/tutorials/branching.md +++ b/docs/tutorials/branching.md @@ -43,7 +43,7 @@ Now let's put it together: ```xpp var myAge 20 -if (myAge == 20) "prt 'I am 20 years old'" "prt 'I am not 20 years old'" +if (myAge == 20) { prt "I am 20 years old" } { prt "I am not 20 years old" } ``` When you run the code, you should see it outputs `"I am 20 years old"`. Try changing the value of `myAge` and see what happens. @@ -52,7 +52,7 @@ The `else` branch is optional. If you only want to check if the expression is tr ```xpp var myInteger 5 -if (myInteger == 5) "prt 'My integer is 5'" +if (myInteger == 5) { prt "My integer is 5" } ``` With that knowledge, you can now output a special text if the user name matches yours: @@ -60,30 +60,46 @@ With that knowledge, you can now output a special text if the user name matches ```xpp :: main.xpp read "What is your name? " ?name -upr name read "What is your age? " ?age -int age -sub 2023 age ?birthYear -if (name == "BOB") "prt 'Welcome, Bob!'" -prt "Your name is $(name) and you were born in $(birthYear)" + +:: Keep a copy of their original string +var ogName name + +:: Calculate birth year by using the current year +int age :: Ensure it's an integer +sub 2024 age ?birthYear + +:: Convert the name to lowercase (so we can check it) +if ((lwr name) == "bob") { prt "Welcome, Bob!" } + +:: Show them their info +prt "Your name is $(ogName) and you were born in $(birthYear)." ``` There are many more comparators, such as the `greater than` (`>`) or `not equal` (`!=`). They work the same way as the `equal to` comparator. Now's your turn. Check if the user's age is equal to or above 16 and output `"You can also drive a car"` after you output their name and their birth year if it is true. -Did you get something like this: +Did you get something like this? ```xpp :: main.xpp read "What is your name? " ?name -upr name read "What is your age? " ?age -int age -sub 2023 age ?birthYear -if (name == "BOB") "prt 'Welcome, Bob!'" -prt "Your name is $(name) and you were born in $(birthYear)" -if (age >= 16) "prt 'You can also drive a car'" + +:: Keep a copy of their original string +var ogName name + +:: Calculate birth year by using the current year +int age :: Ensure it's an integer +sub 2024 age ?birthYear + +:: Convert the name to lowercase (so we can check it) +if ((lwr name) == "bob") { prt "Welcome, Bob!" } + +:: Show them their info +prt "Your name is $(ogName) and you were born in $(birthYear)." +if (age >= 16) { prt "You can also drive a car." } ``` In the next lesson, you will be learning how to make a calculator using x++. diff --git a/docs/tutorials/calculator.md b/docs/tutorials/calculator.md index 2410091..2bbfbfc 100644 --- a/docs/tutorials/calculator.md +++ b/docs/tutorials/calculator.md @@ -24,7 +24,7 @@ From the last three lessons, you learned how to print values into the terminal, Let's introduce the program and get some inputs from the user using the `prt` and `read` operators: -```xt +```xpp :: main.xpp prt "Welcome to the x++ calculator!" prt "-----" @@ -74,7 +74,7 @@ upr o Now we can use the `if` operator to check what operator the user selected and act accordingly. Currently, there are four types of mathematical operators: `add`, `sub`, `mul`, and `div`. Let's use them: -```xt +```xpp :: main.xpp prt "Welcome to the x++ calculator!" prt "-----" @@ -91,15 +91,15 @@ prt "-----" int a int b upr o -if (o == "A") "add a b ?c" -if (o == "S") "sub a b ?c" -if (o == "M") "mul a b ?c" -if (o == "D") "div a b ?c" +if (o == "A") { add a b ?c } \ + (o == "S") { sub a b ?c } \ + (o == "M") { mul a b ?c } \ + (o == "D") { div a b ?c } ``` Since you defined `c` as the answer to the equation, you can simply output it to the terminal. Using string interpolation, you can also use variables within your strings: -```xt +```xpp :: main.xpp prt "Welcome to the x++ calculator!" prt "-----" @@ -116,11 +116,11 @@ prt "-----" int a int b upr o -if (o == "A") "add a b ?c" -if (o == "S") "sub a b ?c" -if (o == "M") "mul a b ?c" -if (o == "D") "div a b ?c" -prt "The answer to that equation is $(c)" +if (o == "A") { add a b ?c } \ + (o == "S") { sub a b ?c } \ + (o == "M") { mul a b ?c } \ + (o == "D") { div a b ?c } +prt "The answer to that equation is $(c)." ``` Tada! You got a working calculator. How cool is that? diff --git a/docs/tutorials/hello-world.md b/docs/tutorials/hello-world.md index 74f820a..39164c8 100644 --- a/docs/tutorials/hello-world.md +++ b/docs/tutorials/hello-world.md @@ -60,7 +60,7 @@ Let's put that in your code! :: main.xpp var name "Bob" var age 20 -prt "My name is Bob and I am 20 years old" +prt "My name is Bob and I am 20 years old." ``` Your can put your variables into your string using `string interpolation`. String interpolation is the process of inserting another statement within a string. This is usually done so by wrapping them in `$()`. @@ -69,8 +69,8 @@ The statement you can wrap inside of `$()` can be any valid x++ syntax, however ```xpp var x 5 -prt "$(x) should be 5" -:: 5 should be 5 +prt "$(x) should be 5." +:: 5 should be 5. ``` Let's try it! @@ -79,7 +79,7 @@ Let's try it! :: main.xpp var name "Bob" var age 20 -prt "My name is $(name) and I am $(age) years old" +prt "My name is $(name) and I am $(age) years old." ``` You did it! You made your first ever x++ project. diff --git a/docs/tutorials/user-input.md b/docs/tutorials/user-input.md index 2e589e2..b856b78 100644 --- a/docs/tutorials/user-input.md +++ b/docs/tutorials/user-input.md @@ -28,7 +28,7 @@ So far, you should've gotten this in your `main.xpp` file: :: main.xpp var name "Bob" var age 20 -prt "My name is $(name) and I am $(age) years old" +prt "My name is $(name) and I am $(age) years old." ``` What if you want to ask what the user's name is instead of yours? You can use the `read` operator to get user input from the terminal. The `read` operator takes in two arguments, the `prompt` and the `output`. The prompt is what the user will see when you get a user input. You can think of it as asking a question. The output is the answer from the user input. @@ -41,11 +41,11 @@ read "What is your favorite color? " ?favoriteColor You can replace your `var` operators and use the `read` operators instead: -```xt +```xpp :: main.xpp read "What is your name? " ?name read "What is your age? " ?age -prt "Your name is $(name) and you are $(age) years old" +prt "Your name is $(name) and you are $(age) years old." ``` You can also make the name more standout from the rest of the string by making it all capitalized. You can uppercase all the letters in a string by using the `upr` operator: @@ -63,7 +63,7 @@ Let's try it: read "What is your name? " ?name upr name read "What is your age? " ?age -prt "Your name is $(name) and you are $(age) years old" +prt "Your name is $(name) and you are $(age) years old." ``` You can also use mathematical operators to calculate the user's birth year. By subtracting the user's age from the current year, you get their birth year. You can use the `sub` operator for this purpose: @@ -89,8 +89,8 @@ read "What is your name? " ?name upr name read "What is your age? " ?age int age -sub 2023 age ?birthYear -prt "Your name is $(name) and you were born in $(birthYear)" +sub 2024 age ?birthYear +prt "Your name is $(name) and you were born in $(birthYear)." ``` Now it will ask the user their name and age and output their name and birth year. Incredible isn't it? diff --git a/src/xpp/core/datastore.py b/src/xpp/core/datastore.py index 56fed7b..549eb36 100644 --- a/src/xpp/core/datastore.py +++ b/src/xpp/core/datastore.py @@ -91,7 +91,11 @@ def _parse(self) -> Any: elif token[1].isdigit() or token[1] in "+-": break - expr = expr.replace(token, str(self.mem.interpreter.execute(token[1:][:-1]))) + new_data = self.mem.interpreter.execute(token[1:][:-1]) + if isinstance(new_data, str): + new_data = f"\"{new_data}\"" + + expr = expr.replace(token, str(new_data)) return simple_eval(expr, names = self.mem.variables["scope"][self.last_stack.sid])