Skip to content

Commit

Permalink
Merge pull request #140 from radon-project/builtin-radiation-tracebacks
Browse files Browse the repository at this point in the history
Added builtin radiation tracebacks.
  • Loading branch information
Almas-Ali authored May 26, 2024
2 parents ea42ac3 + 62ee361 commit 6b783f9
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 5 deletions.
6 changes: 5 additions & 1 deletion core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def string_with_arrows(text: str, pos_start: Position, pos_end: Position) -> str
col_start = pos_start.col if i == 0 else 0
col_end = pos_end.col if i == line_count - 1 else len(line) - 1

col_start_color = col_start + 1
col_start_color = col_start # + 1
col_end_color = col_end + 1

# Append to result
Expand Down Expand Up @@ -70,6 +70,10 @@ def set_pos(self, pos_start=None, pos_end=None):
"""Says it's gonna set the position, but actually does NOTHING"""
return self

def set_context(self, context=None):
"""Says it's gonna set the context, but actually does NOTHING"""
return self

def __repr__(self) -> str:
if self.details is not None:
return f"{self.error_name}: {self.details}"
Expand Down
1 change: 1 addition & 0 deletions core/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"array",
"colorlib",
"math",
"radiation",
"system",
"string",
"universe", # ester egg
Expand Down
7 changes: 7 additions & 0 deletions examples/radiation-raise.rn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import radiation

if 2 != 3 {
raise radiation.ValueError("2 != 3")
}

print(sdf)
19 changes: 19 additions & 0 deletions radon.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ def usage(program_name: str, stream: IO[str]) -> None:
f"Usage: {program_name} [--source | -s] [--command | -c] [source_file] [--version | -v] [--help | -h]",
file=stream,
)
print(
"""
Options and arguments:
--source | -s Run a source file
--command | -c Run a command
--version | -v Print the version
--help | -h Print this help message
If no arguments are provided, the program will run in shell mode.
Example:
radon --source source_file.rn
radon --command 'print("Hello, World!")'
radon --version
radon --help
The Radon Programming Language.
"""
)


def main(argv: list[str]) -> None:
Expand Down
40 changes: 40 additions & 0 deletions stdlib/radiation.rn
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# All radiation built-in errors are defined here.

fun ValueError(message = null) -> message
fun TypeError(message = null) -> message
fun IOError(message = null) -> message
fun MemoryError(message = null) -> message
fun KeyError(message = null) -> message
fun IndexError(message = null) -> message
fun NotImplementedError(message = null) -> message
fun AssertionError(message = null) -> message
fun SyntaxError(message = null) -> message
fun KeyboardInterrupt(message = null) -> message
fun StopIteration(message = null) -> message
fun DeprecationWarning(message = null) -> message
fun ReferenceError(message = null) -> message
fun BufferError(message = null) -> message
fun ArithmeticError(message = null) -> message
fun LogicalError(message = null) -> message
fun RuntimeError(message = null) -> message

# List of all errors
errors = [
ValueError,
TypeError,
IOError,
MemoryError,
KeyError,
IndexError,
NotImplementedError,
AssertionError,
SyntaxError,
KeyboardInterrupt,
StopIteration,
DeprecationWarning,
ReferenceError,
BufferError,
ArithmeticError,
LogicalError,
RuntimeError
]
3 changes: 2 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
#!/usr/bin/python3.12

import sys
import os
import subprocess
Expand Down
16 changes: 16 additions & 0 deletions tests/radiation-modules.rn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import radiation

errors = radiation.errors

# print(radiation.errors[0])

# print(errors[0]("ValueError: " + "This is a test error message"))

assert arr_len(errors) == 17, "Error length is not 17"

try {
raise errors[1]("This is error message!")
}
catch as err {
print(err)
}
1 change: 1 addition & 0 deletions tests/radiation-modules.rn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code": 0, "stdout": "TypeError: This is error message!\n", "stderr": ""}
2 changes: 1 addition & 1 deletion tests/raise.rn.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"code": 1, "stdout": "test1\ntest2\n\u001b[38;5;208mRadiation (most recent call last):\n\u001b[0m File \u001b[38;5;117mtests/raise.rn\u001b[0m, line \u001b[38;5;117m7\u001b[0m\n\u001b[1m\u001b[31mArgError\u001b[0m: \u001b[38;5;203mArgument `arg1` not found\u001b[0m\n\nraise \u001b[1m\u001b[31mArgError(\"arg1\"\u001b[0m)\n \u001b[1m\u001b[31m^^^^^^^^^^^^^^^\u001b[0m\n", "stderr": ""}
{"code": 1, "stdout": "test1\ntest2\n\u001b[38;5;208mRadiation (most recent call last):\n\u001b[0m File \u001b[38;5;117mtests/raise.rn\u001b[0m, line \u001b[38;5;117m7\u001b[0m\n\u001b[1m\u001b[31mArgError\u001b[0m: \u001b[38;5;203mArgument `arg1` not found\u001b[0m\n\nraise\u001b[1m\u001b[31m ArgError(\"arg1\"\u001b[0m)\n \u001b[1m\u001b[31m^^^^^^^^^^^^^^^\u001b[0m\n", "stderr": ""}
2 changes: 1 addition & 1 deletion tests/raise2.rn.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"code": 1, "stdout": "\u001b[38;5;208mRadiation (most recent call last):\n\u001b[0m File \u001b[38;5;117mtests/raise2.rn\u001b[0m, line \u001b[38;5;117m4\u001b[0m\n\u001b[1m\u001b[31mSomeError\u001b[0m: \u001b[38;5;203mThe massage: AAAAAAAAAAAAAAAAA\u001b[0m\n\nraise \u001b[1m\u001b[31mraise2_lib.SomeError(\"AAAAAAAAAAAAAAAAA\"\u001b[0m)\n \u001b[1m\u001b[31m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m\n", "stderr": ""}
{"code": 1, "stdout": "\u001b[38;5;208mRadiation (most recent call last):\n\u001b[0m File \u001b[38;5;117mtests/raise2.rn\u001b[0m, line \u001b[38;5;117m4\u001b[0m\n\u001b[1m\u001b[31mSomeError\u001b[0m: \u001b[38;5;203mThe massage: AAAAAAAAAAAAAAAAA\u001b[0m\n\nraise\u001b[1m\u001b[31m raise2_lib.SomeError(\"AAAAAAAAAAAAAAAAA\"\u001b[0m)\n \u001b[1m\u001b[31m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m\n", "stderr": ""}
2 changes: 1 addition & 1 deletion tests/raise3.rn.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"code": 1, "stdout": "\u001b[38;5;208mRadiation (most recent call last):\n\u001b[0m File \u001b[38;5;117mtests/raise3.rn\u001b[0m, line \u001b[38;5;117m4\u001b[0m\n\u001b[1m\u001b[31mSomeError\u001b[0m: \u001b[38;5;203mThe massage: default massage\u001b[0m\n\nraise \u001b[1m\u001b[31mraise2_lib.SomeError\u001b[0m\n \u001b[1m\u001b[31m^^^^^^^^^^^^^^^^^^^^\u001b[0m\n", "stderr": ""}
{"code": 1, "stdout": "\u001b[38;5;208mRadiation (most recent call last):\n\u001b[0m File \u001b[38;5;117mtests/raise3.rn\u001b[0m, line \u001b[38;5;117m4\u001b[0m\n\u001b[1m\u001b[31mSomeError\u001b[0m: \u001b[38;5;203mThe massage: default massage\u001b[0m\n\nraise\u001b[1m\u001b[31m raise2_lib.SomeError\u001b[0m\n \u001b[1m\u001b[31m^^^^^^^^^^^^^^^^^^^^\u001b[0m\n", "stderr": ""}

0 comments on commit 6b783f9

Please sign in to comment.