Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #177 from Jaseci-Labs/dev_11
Browse files Browse the repository at this point in the history
Dev 11
  • Loading branch information
marsninja authored Jan 27, 2024
2 parents d0c78f6 + 16a596d commit ef109b1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
39 changes: 34 additions & 5 deletions jaclang/compiler/passes/main/pyast_gen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,16 +867,18 @@ def exit_has_var(self, node: ast.HasVar) -> None:
"""Sub objects.
name: Name,
type_tag: SubTag[ExprType],
value: Optional[ExprType],
type_tag: SubTag[Expr],
value: Optional[Expr],
semstr: Optional[String] = None,
"""
annotation = node.type_tag.gen.py_ast if node.type_tag else None
if (
is_class_var = (
node.parent
and node.parent.parent
and isinstance(node.parent.parent, ast.ArchHas)
and node.parent.parent.is_static
):
)
if is_class_var:
self.needs_typing()
annotation = self.sync(
ast3.Subscript(
Expand All @@ -896,7 +898,34 @@ def exit_has_var(self, node: ast.HasVar) -> None:
ast3.AnnAssign(
target=node.name.gen.py_ast,
annotation=annotation,
value=node.value.gen.py_ast if node.value else None,
value=self.sync(
ast3.Call(
func=self.sync(
ast3.Attribute(
value=self.sync(
ast3.Name(id=Con.JAC_FEATURE.value, ctx=ast3.Load())
),
attr="has_container_default",
ctx=ast3.Load(),
)
),
args=[],
keywords=[
self.sync(
ast3.keyword(
arg="container",
value=node.value.gen.py_ast,
)
)
],
)
)
if node.value
and not is_class_var
and isinstance(node.value.gen.py_ast, (ast3.List, ast3.Dict))
else node.value.gen.py_ast
if node.value
else None,
simple=int(isinstance(node.name, ast.Name)),
)
)
Expand Down
8 changes: 7 additions & 1 deletion jaclang/plugin/default.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Jac Language Features."""
from __future__ import annotations

from dataclasses import dataclass
from dataclasses import dataclass, field
from functools import wraps
from typing import Any, Callable, Optional, Type

Expand Down Expand Up @@ -75,6 +75,12 @@ def elvis(op1: Optional[T], op2: T) -> T:
"""Jac's elvis operator feature."""
return ret if (ret := op1) is not None else op2

@staticmethod
@hookimpl
def has_container_default(container: list | dict) -> list[Any] | dict[Any, Any]:
"""Jac's has container default feature."""
return field(default_factory=lambda: container)

@staticmethod
@hookimpl
def spawn_call(op1: Architype, op2: Architype) -> Architype:
Expand Down
5 changes: 5 additions & 0 deletions jaclang/plugin/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def elvis(op1: Optional[T], op2: T) -> T:
"""Jac's elvis operator feature."""
return JacFeature.pm.hook.elvis(op1=op1, op2=op2)

@staticmethod
def has_container_default(container: list | dict) -> list[Any] | dict[Any, Any]:
"""Jac's has container default feature."""
return JacFeature.pm.hook.has_container_default(container=container)

@staticmethod
def spawn_call(op1: Architype, op2: Architype) -> Architype:
"""Jac's spawn operator feature."""
Expand Down
6 changes: 6 additions & 0 deletions jaclang/plugin/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def elvis(op1: Optional[T], op2: T) -> T:
"""Jac's elvis operator feature."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def has_container_default(container: list | dict) -> list[Any] | dict[Any, Any]:
"""Jac's has container default feature."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def spawn_call(op1: Architype, op2: Architype) -> Architype:
Expand Down
16 changes: 16 additions & 0 deletions jaclang/tests/fixtures/has_goodness.jac
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
obj MyObj {
has mylist: list[int] = [1, 2, 3],
mydict: dict[int, int] = {"a": 2, "b": 4};

static has mylist2: list[int] = [1, 2, 3];

can obj_print {
print("mylist: ", <self>.mylist);
print("mydict: ", <self>.mydict);
}
}

with entry {
myo = MyObj();
myo.obj_print();
}
11 changes: 11 additions & 0 deletions jaclang/tests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,14 @@ def test_deep_imports(self) -> None:
sys.stdout = sys.__stdout__
stdout_value = captured_output.getvalue()
self.assertEqual(stdout_value.split("\n")[0], "one level deeperslHello World!")

def test_has_lambda_goodness(self) -> None:
"""Test has lambda_goodness."""
construct.root._jac_.edges[construct.EdgeDir.OUT].clear()
captured_output = io.StringIO()
sys.stdout = captured_output
jac_import("has_goodness", self.fixture_abs_path("./"))
sys.stdout = sys.__stdout__
stdout_value = captured_output.getvalue()
self.assertEqual(stdout_value.split("\n")[0], "mylist: [1, 2, 3]")
self.assertEqual(stdout_value.split("\n")[1], "mydict: {'a': 2, 'b': 4}")

0 comments on commit ef109b1

Please sign in to comment.