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

14a Reading from source #28

Merged
merged 3 commits into from
Apr 23, 2022
Merged

14a Reading from source #28

merged 3 commits into from
Apr 23, 2022

Conversation

ngjunsiang
Copy link
Contributor

Before we embark on File IO, it feels kind of strange to do so when our developers have to edit the source code of our main program to insert their code ... frankly it would be embarrassing to see this in real-world code!

So let's at least put something in place for us to read our source code from an external source file. For now, we'll just use main.pseudo as the filename of this source file (we don't support importing code from multiple source files anyway).

And let's read in the source code from that file at the start:
https://github.com/nyjc-computing/pseudo/blob/8d87a0983ec887b1e16d5fb4bac5fd6668bfd206/main.py#L12-L14

@ngjunsiang
Copy link
Contributor Author

ngjunsiang commented Apr 23, 2022

What if the user has a different file they want to use? It's common to pass that as the first argument to our interpreter as follows:

main.py myotherfile.pseudo

(Right now our file is called main.py. We'll give it a cooler name when we're packaging it.)

In python, we can extract these arguments passed to our program using sys.argv, which gives a list of arguments. The first argument is always the name of the file (in our case, main.py). So any filename passed to our program would be the second argument:

https://github.com/nyjc-computing/pseudo/blob/0be6bb23111255a1e9c9fd9087e70169bab771b9/main.py#L15-L18

The result of running it in a shell might look like this:

$ python main.py main.pseudo
5 + 1 is 6

@ngjunsiang
Copy link
Contributor Author

Now that's looking more respectable.

On to the next thing a proper compiler needs: error messages.

@ngjunsiang ngjunsiang merged commit 8320242 into main Apr 23, 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