radon-project / radon

The Radon Programming Language
https://radon-project.github.io
GNU General Public License v3.0
21 stars 2 forks source link

Added `raise` statement #135

Closed Vardan2009 closed 1 month ago

Vardan2009 commented 1 month ago

The syntax: raise errortype expression

Example: raise TypeError "Input must be integer"

Almas-Ali commented 1 month ago

The errortype should not be like a keyword rather should have to be like a function or class.

Example:

raise TypeError("Invalid type")

Follow #83 for details. Also make sure, all tests are passing.

Thanks for your contribution. Keep it up. 👍🏻

angelcaru commented 1 month ago

I don't think it will be possible to do it as a class because errors are not currently first-class citizens (try-catch gives a string). Also, the current implementation seems to allow any identifier as an error type, which I don't know if it's desirable (but the more I think about it the more I feel it's actually a good idea):

raise shfgsdjkfhg "shfgsdjkfhg moment"

(To be clear, the reason I'm mentioning that is because it wouldn't be possible to do that with classes either. At least not as conveniently)

Vardan2009 commented 1 month ago

The errortype should not be like a keyword rather should have to be like a function or class.

I did think doing it with classes, but I thought it will be hard because classes don't support inheritance (like class TypeError : BaseError).

angelcaru commented 1 month ago

Maybe it could be function syntax but not actually a function call? Like:

raise FooError("message")

That would be valid, but not actually calling any FooError function. Just as part of the syntax of raise

Vardan2009 commented 1 month ago

That seems like a good idea, but it will still allow any identifier to be an error type

angelcaru commented 1 month ago

but it will still allow any identifiere to be an error type

Which is, in my opinion, a Good Thing

Vardan2009 commented 1 month ago

I think so too

Vardan2009 commented 1 month ago

But I still think the idea with a function returning an error message is a better idea Something like:

fun ArgError(arg)
{
    return "Argument `"+arg+"` not found"
}

raise ArgError("arg1")
angelcaru commented 1 month ago

Oooh! Never thought about doing it like that! Thought you would need inheritance to make it work but I guess not. Good idea!

Vardan2009 commented 1 month ago

Now the syntax I mentioned above should work as expected, I also added a simple test to the tests

Almas-Ali commented 1 month ago

@Vardan2009 You need to test your local code before pushing to Github. Use python3.11 test.py full for testing everything. But in my opinion you just need to focus on mypy issue for now. So, mypy . for just checking Python codes.

Vardan2009 commented 1 month ago

Sorry, I was having some problems running the tests on my local machine, but I got it working now. I will try fixing these errors.

Almas-Ali commented 1 month ago

Sorry, I was having some problems running the tests on my local machine, but I got it working now. I will try fixing these errors.

Most important is to fix the mypy issue now.

Almas-Ali commented 1 month ago

Thanks, @Vardan2009 mypy is not complaining anymore. Also, thanks to @angelcaru for being so helpful.

Stay contributing everyone.

Vardan2009 commented 1 month ago

This was my first ever contribution to a project, thank you so much!

Almas-Ali commented 1 month ago

This was my first ever contribution to a project, thank you so much!

Your welcome.

Almas-Ali commented 1 month ago

@Vardan2009 You can join us here https://discord.gg/zJhwFzqxs7