Skip to content

Commit

Permalink
Added events.
Browse files Browse the repository at this point in the history
  • Loading branch information
DevReaper0 committed Nov 7, 2022
1 parent 8f1bb16 commit ff2e141
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
.git/
.vs/
venv/
.idea/
__pycache__/
**/__pycache__/
build/
dist/
*.bat
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Better interfacing with python code from ParaCode
- An entire interfacing module for ParaCode/Rust communication to maintain parity and compatibility with the Rust port (Python interfacing probably won't be added to the port)
- A complete requests HTTP module
- A unit tests module
- A more advanced unit tests module
- A better icon

## [2.1.0] - CURRENTLY UNRELEASED
Expand All @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Complete regex support
- Some basic exceptions
- A reflection module
- A basic unit tests module
- An events system
- Enums

### Changed
Expand Down
12 changes: 12 additions & 0 deletions examples/events.para
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "std/types/events/exceptionevent.para";

print(EventHandler.handlers);
let handler = EventHandler.new();
print(EventHandler.handlers);
print(handler.handled_events);
func m(handled_event, event) { print("EVENT TRIGGERED: " + handled_event.to_str() + " " + event.to_str()); }
handler.handle(ExceptionEvent, m);
print(handler.handled_events);

let a = [];
print(a[1]); // Will trigger an out-of-bounds error.
2 changes: 1 addition & 1 deletion examples/exceptions.para
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
try {
// Comment below line, change the exception to MultipleDefinitionError, TypeError, ArgumentError, MacroExpansionError, or Exception
// Try commenting out the below line or changing the exception type
MultipleDefinitionError.new("AIEVN").raise();
print("TRY");
}
Expand Down
3 changes: 3 additions & 0 deletions std/def.para
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ import "std/types/exceptions/notimplementederror.para";
import "std/types/exceptions/lookuperror.para";
import "std/types/exceptions/arraylookuperror.para";
import "std/types/exceptions/dictlookuperror.para";

import "std/types/events/eventhandler.para";
import "std/types/events/event.para";
22 changes: 22 additions & 0 deletions std/experimental/assert.para
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
let FailedAssertionError = Exception.extend({
name = 'FailedAssertionError'

instance = {
func raise(self) {
// TODO: Add the line number
return raise(self, "Assertion Failed");
}
}

func __construct__(self, _message) {
self.message = _message;
}
});

// TODO: Integrate into ParaCode so the condition and line number will be accessible and printable directly
func assert(condition, message) {
if !condition {
FailedAssertionError.new(message).raise();
}
return condition;
}
27 changes: 27 additions & 0 deletions std/experimental/testing.para
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let Test = Type.extend({
name = 'Test'

instance = {
statement,
expected_result

func test(self) {
import "std/experimental/assert.para";

assert(self.is_truthy(), "Test Failed!");
}

func is_truthy(self) {
return self.statement == self.expected_result;
}

func __bool__(self) {
return self.is_truthy();
}
}

func __construct__(self, statement, expected_result) {
self.statement = statement;
self.expected_result = expected_result;
}
});
16 changes: 16 additions & 0 deletions std/types/events/event.para
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let Event = Type.extend({
name = 'Event'

instance = {
func trigger(self) {
return EventHandler.trigger(self);
}
}

func __construct__(self) {
}

func handle(self, handler, method) {
return handler.handle(self, method);
}
});
50 changes: 50 additions & 0 deletions std/types/events/eventhandler.para
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
let EventHandler = Type.extend({
name = 'EventHandler'

handlers = []

instance = {
handled_events = []

func handle(self, event, method=null) {
if method == null && event.type() == HandledEvent {
self.handled_events.append(event);
return event;
}
else {
let handled = HandledEvent.new(event, method);
self.handled_events.append(handled);
return handled;
}
}
}

func __construct__(self) {
EventHandler.handlers.append(self);
return self;
}

func trigger(self, event) {
for handler in EventHandler.handlers {
for handled in handler.handled_events {
handled.method(event);
}
}
}
});

let HandledEvent = Type.extend({
name = 'HandledEvent'

instance = {
event_type
method
}

func __construct__(self, event_type, method) {
self.event_type = event_type;
self.method = method;

return self;
}
});
21 changes: 21 additions & 0 deletions std/types/events/exceptionevent.para
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let ExceptionEvent = Event.extend({
name = 'ExceptionEvent'

instance = {
name
exception

func trigger(self) {
return EventHandler.trigger(self);
}
}

func __construct__(self, name, exception) {
self.name = name;
self.exception = exception;
}

func handle(self, handler, method) {
return handler.handle(self, method);
}
});
16 changes: 9 additions & 7 deletions std/types/exceptions/exception.para
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
func raise(exception, name="") {
import "std/types/events/exceptionevent.para";

let classnames = [];
let current = exception.type();
while current != Exception {
while current != Type {
classnames.append(current.to_str());
current = current.type();
}
classnames.append("Exception");
if name != "" {
return __intern_exception_raise__(name, exception.message, classnames, exception);
}
else {
return __intern_exception_raise__(exception.type().name, exception.message, classnames, exception);
if name == "" {
name = exception.type().name;
}

ExceptionEvent.new(name, exception).trigger();

return __intern_exception_raise__(name, exception.message, classnames, exception);
}

let Exception = Type.extend({
Expand Down
1 change: 1 addition & 0 deletions std/types/str.para
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ let Str = Array.extend({
func center(self, width, fillchar=" ") {
return __intern_str_center__(self._value, width, fillchar);
}

func color(_, num) {
return __intern_get_color__(num) + self;
}
Expand Down
11 changes: 11 additions & 0 deletions std/util/langinfo.para
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let LangInfo = Type.extend({
name = "LangInfo"

func get_version() {
return "2.1.0";
}

func get_extension_language() {
return "Python";
}
});

0 comments on commit ff2e141

Please sign in to comment.