Skip to content

Commit

Permalink
[docs] OK seriously, last docs change today
Browse files Browse the repository at this point in the history
  • Loading branch information
iiPythonx committed Mar 10, 2024
1 parent 8ed59da commit 8d63382
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 42 deletions.
6 changes: 6 additions & 0 deletions docs/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" = "/"
6 changes: 3 additions & 3 deletions docs/documents/datatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
```
Expand Down Expand Up @@ -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'"
```

Expand Down
4 changes: 2 additions & 2 deletions docs/documents/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

Expand All @@ -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
Expand Down
44 changes: 30 additions & 14 deletions docs/tutorials/branching.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -52,38 +52,54 @@ 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:

```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++.
Expand Down
24 changes: 12 additions & 12 deletions docs/tutorials/calculator.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "-----"
Expand Down Expand Up @@ -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 "-----"
Expand All @@ -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 "-----"
Expand All @@ -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?
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorials/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `$()`.
Expand All @@ -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!
Expand All @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions docs/tutorials/user-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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?
Expand Down
6 changes: 5 additions & 1 deletion src/xpp/core/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down

0 comments on commit 8d63382

Please sign in to comment.