-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add hashes namespace for inbuilt libs and http methods
- Loading branch information
1 parent
3f67989
commit 9256822
Showing
10 changed files
with
252 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
fun map (callable, arr) { | ||
var length_ = len(arr); | ||
var buffer = array(); | ||
for (var i = 0; i < length_; i = i + 1) { | ||
buffer.append(callable(arr.index(i))); | ||
class generics { | ||
map (callable, arr) { | ||
var length_ = len(arr); | ||
var buffer = array(); | ||
for (var i = 0; i < length_; i = i + 1) { | ||
buffer.append(callable(arr.get(i))); | ||
} | ||
return buffer; | ||
} | ||
return buffer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import dataclasses | ||
import typing as t | ||
|
||
from src.exceptions import PyLoxAttributeError | ||
from src.utils.expr import Literal | ||
|
||
from .callables import LoxCallable | ||
from .types import LoxContainer | ||
|
||
if t.TYPE_CHECKING: | ||
from src.interpreter.interpreter import Interpreter | ||
from src.lexer.tokens import Token | ||
|
||
|
||
__all__: tuple[str, ...] = ("LoxHash",) | ||
|
||
# pyright: reportIncompatibleVariableOverride=false | ||
|
||
|
||
@dataclasses.dataclass | ||
class HashCallable(LoxCallable): | ||
parent: "LoxHash" | ||
token: "Token" | ||
|
||
@property | ||
def arity(self) -> int: | ||
raise NotImplementedError | ||
|
||
def __call__(self, interpreter: "Interpreter", arguments: list[t.Any], /) -> t.Any: | ||
raise NotImplementedError | ||
|
||
|
||
@dataclasses.dataclass | ||
class Get(HashCallable): | ||
parent: "LoxHash" | ||
token: "Token" | ||
|
||
@property | ||
def arity(self) -> int: | ||
return 1 | ||
|
||
def __call__(self, interpreter: "Interpreter", arguments: list[t.Any], /) -> t.Any: | ||
return self.parent.fields.get(arguments[0], Literal(None)) | ||
|
||
|
||
@dataclasses.dataclass | ||
class Set(HashCallable): | ||
parent: "LoxHash" | ||
token: "Token" | ||
|
||
@property | ||
def arity(self) -> int: | ||
return 2 | ||
|
||
def __call__(self, interpreter: "Interpreter", arguments: list[t.Any], /) -> t.Any: | ||
self.parent.fields[arguments[0]] = arguments[1] | ||
return Literal(None) | ||
|
||
|
||
class LoxHash(LoxContainer): | ||
parent: t.Type["LoxHash"] | ||
fields: dict[t.Any, t.Any] | ||
methods: dict[str, t.Type[HashCallable]] = {"get": Get, "set": Set} | ||
|
||
def __init__(self) -> None: | ||
self.parent = LoxHash | ||
self.fields = {} | ||
|
||
def __repr__(self) -> str: | ||
return f"{self.__class__.__name__}({self.fields})" | ||
|
||
def __str__(self) -> str: | ||
return str(self.fields) | ||
|
||
def __len__(self) -> int: | ||
return len(self.fields) | ||
|
||
def get(self, name: "Token", /) -> t.Any: | ||
try: | ||
return self.methods[name.lexeme](self, name) # type: ignore | ||
except KeyError: | ||
raise PyLoxAttributeError(f"Undefined property '{name.lexeme}'.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import dataclasses | ||
import typing as t | ||
|
||
import requests | ||
|
||
from src.exceptions import PyLoxAttributeError, PyLoxRuntimeError | ||
|
||
from .callables import LoxCallable | ||
from .types import LoxContainer | ||
|
||
if t.TYPE_CHECKING: | ||
from src.interpreter.interpreter import Interpreter | ||
from src.lexer.tokens import Token | ||
|
||
|
||
__all__: tuple[str, ...] = ("LoxRequest",) | ||
|
||
|
||
# pyright: reportIncompatibleVariableOverride=false | ||
|
||
|
||
@dataclasses.dataclass | ||
class RequestCallable(LoxCallable): | ||
parent: "LoxRequest" | ||
token: "Token" | ||
|
||
@property | ||
def arity(self) -> int: | ||
raise NotImplementedError | ||
|
||
def __call__(self, interpreter: "Interpreter", arguments: list[t.Any], /) -> t.Any: | ||
raise NotImplementedError | ||
|
||
|
||
class LoxRequestModel(LoxContainer): | ||
parent: t.Type["LoxRequestModel"] | ||
|
||
def __init__(self, fields: requests.models.Response, /) -> None: | ||
self.parent = LoxRequestModel | ||
try: | ||
self.fields = fields.json() | ||
except ValueError: | ||
self.fields = {} | ||
for key in dir(fields): | ||
if not key.startswith("_") and not callable(getattr(fields, key)): | ||
self.fields[key] = getattr(fields, key) | ||
|
||
|
||
@dataclasses.dataclass | ||
class Get(RequestCallable): | ||
parent: "LoxRequest" | ||
token: "Token" | ||
|
||
@property | ||
def arity(self) -> int: | ||
return 1 | ||
|
||
def __call__(self, interpreter: "Interpreter", arguments: list[t.Any], /) -> LoxRequestModel: | ||
try: | ||
return LoxRequestModel(requests.get(str(arguments[0]))) | ||
except requests.exceptions.RequestException as e: | ||
raise PyLoxRuntimeError(interpreter.error(self.token, f"Request failed: {e}")) | ||
|
||
|
||
class LoxRequest(LoxContainer): | ||
parent: t.Type["LoxRequest"] | ||
fields: t.Optional[dict[str, t.Any]] = None | ||
_methods: dict[str, t.Type[RequestCallable]] = { | ||
"get": Get, | ||
} | ||
|
||
def __init__(self, /) -> None: | ||
self.parent = LoxRequest | ||
|
||
def get(self, name: "Token", /) -> t.Any: | ||
try: | ||
return self._methods[name.lexeme](self, name) # type: ignore | ||
except KeyError: | ||
raise PyLoxAttributeError(f"Attribute {name.lexeme} not found.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
print 2 ^ 3; | ||
try { | ||
print 5 /; | ||
} catch (e) { | ||
print "Caught exception:"; | ||
print e; | ||
} finally { | ||
print "Finally"; | ||
var d = hash(); | ||
for (var i = 0; i < 10; i = i + 1) { | ||
d.set(str(i), i); | ||
} | ||
print d; | ||
var z = d.get("0"); | ||
print z; |