Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

18b Logical operators #48

Merged
merged 10 commits into from
May 5, 2022
Merged

18b Logical operators #48

merged 10 commits into from
May 5, 2022

Conversation

ngjunsiang
Copy link
Contributor

Now that our interpreter can understand TRUE and FALSE, let's implement AND, OR, and NOT operators.

We start with scanning. First let's add the operators in builtin.py and have them recognised as operators:
https://github.com/nyjc-computing/pseudo/blob/4241f231018f605311a67ad762c9db3e4cf5e8cb/builtin.py#L71-L78
https://github.com/nyjc-computing/pseudo/blob/4241f231018f605311a67ad762c9db3e4cf5e8cb/builtin.py#L105-L119

Then enable scanning them:
https://github.com/nyjc-computing/pseudo/blob/4241f231018f605311a67ad762c9db3e4cf5e8cb/scanner.py#L98-L100

@ngjunsiang
Copy link
Contributor Author

ngjunsiang commented May 5, 2022

Operator precedence

NOT has the same precedence as unary -, so we parse it at the same line:
https://github.com/nyjc-computing/pseudo/blob/96d886e011e1e6c384f6c56d2546a9d81b7000cd/parser.py#L123-L124

AND and OR have lower precedence than <> and =; they have the lowest precedence of all the operators. So we add a new parser for them, between equality() and expression():
https://github.com/nyjc-computing/pseudo/blob/96d886e011e1e6c384f6c56d2546a9d81b7000cd/parser.py#L193-L207

@ngjunsiang
Copy link
Contributor Author

ngjunsiang commented May 5, 2022

Resolving logical operators

Next, we've got to resolve these new operators so that appropriate types are returned. First, the unary NOT:
https://github.com/nyjc-computing/pseudo/blob/160a9282da65ecaf1dc841f3284d7aa922f311c9/resolver.py#L52-L54

Then the binary AND & OR:
https://github.com/nyjc-computing/pseudo/blob/4deb5a00d98af85be24d26fc5e89ebc028e8549b/resolver.py#L59-L62

@ngjunsiang
Copy link
Contributor Author

Interpreting logical operators

Okay, nothing to change here; our operators take in the )type-checked) input, and return their output. But a slightly horrifying discovery:
https://github.com/nyjc-computing/pseudo/blob/4deb5a00d98af85be24d26fc5e89ebc028e8549b/interpreter.py#L29-L31

For unary -, we are passing a single argument to our sub operator, which only accepts two arguments 😱:
https://github.com/nyjc-computing/pseudo/blob/4deb5a00d98af85be24d26fc5e89ebc028e8549b/builtin.py#L44-L45

Fix - we overload sub to also handle a single argument, by returning its negative:
https://github.com/nyjc-computing/pseudo/blob/763213fc914a2b1cf2a44a2f64a21bc046432343/builtin.py#L44-L47

@ngjunsiang
Copy link
Contributor Author

And we have working logical operators 👍

@ngjunsiang ngjunsiang merged commit f90fd10 into main May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant