Skip to content
This repository has been archived by the owner on Nov 11, 2021. It is now read-only.

Add switch blocks #119

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Add switch blocks #119

wants to merge 12 commits into from

Conversation

mosswg
Copy link

@mosswg mosswg commented Oct 10, 2021

Added switch blocks using integers for comparison. This also adds the switch, case, and break keywords.

In compilation a list is created in the switch's operand. The last 2 elements contain the default case location and the end location. The other elements of the list are the case conditions.

In simulation mode the list is looped through to find the matching case. If no match if found it will jump to either the default case if it exists or the end of the switch block.

In compilation mode all the conditions are checked consecutively until a match is found. If no matching one is found there is a non conditional jump to either the default case or the end of the switch block.

If a case is found without anything on the stack it will be treated as the default case. Since only one default case is possible having more will cause a compile error. A default keyword could be easily added but I wanted to minimize the amount of new keywords added.

mosswg added 12 commits October 10, 2021 00:23
Added switch blocks using ints. This also adds the switch, case, and break keywords. In compilation a dictionary is created in the switch's operand. The None key contains a tuple with the location of the default case (-1 if none exists) and the location of the end of the switch block. The other elements of the dictionary are the case conditions. In simulation mode the dictonary is indexed to jump to the matching case. In compilation mode all the conditions are check consecutively until a matching one is found. If no matching one is found there is a non conditional jump to either the default case or the end of the switch block. If a case is found without anything on the stack it will be treated as the default case. Since only one default case is possible having more will cause a compile error. A default keyword could be easily added but I wanted to minimize the amount of new keywords added.
PyPy doesn't like dictionaries with defined types :(
Changed switch case storage from dictionary to list so changing to
self-hosting is easier.
Before I was creating a list then appending elements to it which made
mypy mad. Changed to creating a list with elements so no appending is
needed.
Removed ability to have case without condition as default and added
default keyword.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant