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

Commit

Permalink
feat: all has variables are object unless explicitly static
Browse files Browse the repository at this point in the history
  • Loading branch information
marsninja committed Jan 29, 2024
1 parent fdb1590 commit 8b22aee
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 46 deletions.
34 changes: 16 additions & 18 deletions examples/reference/special_comprehensions.jac
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@
import:py random;

obj TestObj {
can <init> (){
self.x = random.randint(0,15);
self.y =random.randint(0,15);
self.z = random.randint(0,15);
has x: int = random.randint(0, 15),
y: int = random.randint(0, 15),
z: int = random.randint(0, 15);

can <post_init> {
print(<self>.x, <self>.y, <self>.z);
}
has x:int = random.randint(0,15),
y:int = random.randint(0,20),
z:int = random.randint(0,50);
}

with entry{
with entry {
random.seed(42);
apple = [];
for i=0 to i<10 by i+=1{
for i=0 to i<10 by i+=1 {
apple.append(TestObj());
}
#print(apple);
print (apple(= y <= 7));

print(apple(=y <= 7));
}

#assign comprehension

obj MyObj {
has apple:int=0, banana:int=0;
has apple: int = 0,
banana: int = 0;
}

with entry {
x=MyObj();
y=MyObj();

mvar = [x, y](* apple=5, banana=7);
print(mvar);
x = MyObj();
y = MyObj();
mvar = [x, y](*apple=5, banana=7);
print(mvar);
}
25 changes: 19 additions & 6 deletions jaclang/compiler/passes/main/pyast_gen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,24 +905,37 @@ def exit_has_var(self, node: ast.HasVar) -> None:
value=self.sync(
ast3.Name(id=Con.JAC_FEATURE.value, ctx=ast3.Load())
),
attr="has_container_default",
attr="has_instance_default",
ctx=ast3.Load(),
)
),
args=[],
keywords=[
self.sync(
ast3.keyword(
arg="container",
value=node.value.gen.py_ast,
arg="gen_func",
value=self.sync(
ast3.Lambda(
args=self.sync(
ast3.arguments(
posonlyargs=[],
args=[],
kwonlyargs=[],
vararg=None,
kwargs=None,
kw_defaults=[],
defaults=[],
)
),
body=node.value.gen.py_ast,
)
),
)
)
],
)
)
if node.value
and not is_class_var
and isinstance(node.value.gen.py_ast, (ast3.List, ast3.Dict))
if node.value and not is_class_var
else node.value.gen.py_ast
if node.value
else None,
Expand Down
4 changes: 2 additions & 2 deletions jaclang/plugin/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ def elvis(op1: Optional[T], op2: T) -> T:

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

@staticmethod
@hookimpl
Expand Down
4 changes: 2 additions & 2 deletions jaclang/plugin/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def elvis(op1: Optional[T], op2: T) -> T:
return JacFeature.pm.hook.elvis(op1=op1, op2=op2)

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

@staticmethod
def spawn_call(op1: Architype, op2: Architype) -> Architype:
Expand Down
2 changes: 1 addition & 1 deletion jaclang/plugin/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def elvis(op1: Optional[T], op2: T) -> T:

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

Expand Down
34 changes: 17 additions & 17 deletions support/jac-lang.org/docs/learn/jac_ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,55 +358,55 @@

## Atom
```yaml linenums="344"
--8<-- "jaclang/compiler/jac.lark:344:362"
--8<-- "jaclang/compiler/jac.lark:344:365"
```
--8<-- "examples/reference/atom.md"

## Collection values
```yaml linenums="365"
--8<-- "jaclang/compiler/jac.lark:365:386"
```yaml linenums="368"
--8<-- "jaclang/compiler/jac.lark:368:389"
```
--8<-- "examples/reference/collection_values.md"

## Tuples and Jac Tuples
```yaml linenums="389"
--8<-- "jaclang/compiler/jac.lark:389:396"
```yaml linenums="392"
--8<-- "jaclang/compiler/jac.lark:392:399"
```
--8<-- "examples/reference/tuples_and_jac_tuples.md"

## Data Spatial References
```yaml linenums="399"
--8<-- "jaclang/compiler/jac.lark:399:409"
```yaml linenums="402"
--8<-- "jaclang/compiler/jac.lark:402:412"
```
--8<-- "examples/reference/data_spatial_references.md"

## Special Comprehensions
```yaml linenums="412"
--8<-- "jaclang/compiler/jac.lark:412:415"
```yaml linenums="415"
--8<-- "jaclang/compiler/jac.lark:415:418"
```
--8<-- "examples/reference/special_comprehensions.md"

## Names and references
```yaml linenums="418"
--8<-- "jaclang/compiler/jac.lark:418:444"
```yaml linenums="421"
--8<-- "jaclang/compiler/jac.lark:421:447"
```
--8<-- "examples/reference/names_and_references.md"

## Builtin types
```yaml linenums="447"
--8<-- "jaclang/compiler/jac.lark:447:457"
```yaml linenums="450"
--8<-- "jaclang/compiler/jac.lark:450:460"
```
--8<-- "examples/reference/builtin_types.md"

## Lexer Tokens
```yaml linenums="460"
--8<-- "jaclang/compiler/jac.lark:460:628"
```yaml linenums="463"
--8<-- "jaclang/compiler/jac.lark:463:631"
```
--8<-- "examples/reference/lexer_tokens.md"

## f-string tokens
```yaml linenums="631"
--8<-- "jaclang/compiler/jac.lark:631:639"
```yaml linenums="634"
--8<-- "jaclang/compiler/jac.lark:634:644"
```
--8<-- "examples/reference/f_string_tokens.md"

0 comments on commit 8b22aee

Please sign in to comment.