Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subnode list removed from list #1566

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions jac/jaclang/compiler/absyntree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2943,7 +2943,7 @@ class ListVal(AtomExpr):

def __init__(
self,
values: Optional[SubNodeList[Expr]],
values: list[Expr],
kid: Sequence[AstNode],
) -> None:
"""Initialize value node."""
Expand All @@ -2955,13 +2955,14 @@ def __init__(
def normalize(self, deep: bool = False) -> bool:
"""Normalize ast node."""
res = True
if deep:
res = self.values.normalize(deep) if self.values else res
new_kid: list[AstNode] = [
self.gen_token(Tok.LSQUARE),
]
if self.values:
new_kid.append(self.values)
for expr in self.values:
res = expr.normalize(deep) and res
new_kid: list[AstNode] = []
new_kid.append(self.gen_token(Tok.LSQUARE))
for idx, expr in enumerate(self.values):
if idx != 0:
new_kid.append(self.gen_token(Tok.COMMA))
new_kid.append(expr)
new_kid.append(self.gen_token(Tok.RSQUARE))
self.set_kids(nodes=new_kid)
return res
Expand Down Expand Up @@ -3410,6 +3411,7 @@ def __init__(

def normalize(self, deep: bool = True) -> bool:
"""Normalize ast node."""
res = True
if deep:
res = self.target.normalize(deep)
res = res and (not self.params or self.params.normalize(deep))
Expand Down
19 changes: 9 additions & 10 deletions jac/jaclang/compiler/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import keyword
import logging
import os
from typing import Callable, TypeAlias, TypeVar
from typing import Callable, TypeAlias, TypeVar, cast

import jaclang.compiler.absyntree as ast
from jaclang.compiler import jac_lark as jl # type: ignore
Expand Down Expand Up @@ -2203,20 +2203,19 @@ def index_slice(self, kid: list[ast.AstNode]) -> ast.IndexSlice:
if len(kid) == 1:
index = kid[0]
if isinstance(index, ast.ListVal):
if not index.values:
if len(index.values) == 0:
raise self.ice()
if len(index.values.items) == 1:
expr = index.values.items[0] if index.values else None
if len(index.values) == 1:
expr = index.values[0]
else:
sublist = ast.SubNodeList[ast.Expr | ast.KWPair](
items=[*index.values.items], delim=Tok.COMMA, kid=index.kid
items=[*index.values], delim=Tok.COMMA, kid=index.kid[1:-1]
)
expr = ast.TupleVal(values=sublist, kid=[sublist])
kid = [expr]
return ast.IndexSlice(
slices=[ast.IndexSlice.Slice(start=expr, stop=None, step=None)],
is_range=False,
kid=kid,
kid=[index.kid[0], expr, index.kid[-1]],
)
else:
raise self.ice()
Expand Down Expand Up @@ -2417,13 +2416,13 @@ def list_val(self, kid: list[ast.AstNode]) -> ast.ListVal:
"""
if len(kid) == 2:
return ast.ListVal(
values=None,
values=[],
kid=kid,
)
elif isinstance(kid[1], ast.SubNodeList):
return ast.ListVal(
values=kid[1],
kid=kid,
values=cast(list[ast.Expr], kid[1].items),
kid=[kid[0], *kid[1].kid, kid[2]],
)
else:
raise self.ice()
Expand Down
2 changes: 1 addition & 1 deletion jac/jaclang/compiler/passes/main/pyast_gen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2730,7 +2730,7 @@ def exit_list_val(self, node: ast.ListVal) -> None:
node.gen.py_ast = [
self.sync(
ast3.List(
elts=node.values.gen.py_ast if node.values else [],
elts=[expr.gen.py_ast[0] for expr in node.values],
ctx=node.py_ctx_func(),
)
)
Expand Down
25 changes: 12 additions & 13 deletions jac/jaclang/compiler/passes/main/pyast_load_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import ast as py_ast
import os
from typing import Optional, Sequence, TypeAlias, TypeVar
from typing import Optional, Sequence, TypeAlias, TypeVar, cast

# from icecream import ic

Expand Down Expand Up @@ -1610,20 +1610,19 @@ class List(expr):
ctx: expr_context
"""
elts = [self.convert(elt) for elt in node.elts]
valid_elts = [elt for elt in elts if isinstance(elt, ast.Expr)]
if len(valid_elts) != len(elts):
if not all(isinstance(elt, ast.Expr) for elt in elts):
raise self.ice("Length mismatch in list elements")
l_square = self.operator(Tok.LSQUARE, "[")
r_square = self.operator(Tok.RSQUARE, "]")
# self.operator() should be called self.token() imo.
kid: list[ast.AstNode] = []
kid.append(self.operator(Tok.LSQUARE, "["))
for idx, expr in enumerate(elts):
if idx != 0:
kid.append(self.operator(Tok.COMMA, ","))
kid.append(expr)
kid.append(self.operator(Tok.RSQUARE, "]"))
return ast.ListVal(
values=(
ast.SubNodeList[ast.Expr](
items=valid_elts, delim=Tok.COMMA, kid=valid_elts
)
if valid_elts
else None
),
kid=[*valid_elts] if valid_elts else [l_square, r_square],
values=cast(list[ast.Expr], elts),
kid=kid,
)

def proc_list_comp(self, node: py_ast.ListComp) -> ast.ListCompr:
Expand Down
2 changes: 1 addition & 1 deletion jac/jaclang/compiler/passes/main/pyout_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def enter_module(self, node: ast.Module) -> None:

def gen_python(self, node: ast.Module, out_path: str) -> None:
"""Generate Python."""
with open(out_path, "w") as f:
with open(out_path, "w", encoding="utf-8") as f:
f.write(node.gen.py)

def dump_bytecode(self, node: ast.Module, mod_path: str, out_path: str) -> None:
Expand Down
36 changes: 12 additions & 24 deletions jac/jaclang/compiler/passes/tool/jac_formatter_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -1650,31 +1650,19 @@ def exit_list_val(self, node: ast.ListVal) -> None:

values: Optional[SubNodeList[ExprType]],
"""
line_break_needed = False
indented = False
for i in node.kid:
if isinstance(i, ast.SubNodeList):
line_break_needed = self.is_line_break_needed(i.gen.jac, 88)
if line_break_needed:
self.emit_ln(node, "")
self.indent_level += 1
indented = True
for j in i.kid:
if j.gen.jac == (","):
self.indent_level -= 1
self.emit(node, f"{j.gen.jac}\n")
self.indent_level += 1
else:
self.emit(node, f"{j.gen.jac}")
else:
self.emit(node, f"{i.gen.jac}")
if indented:
self.indent_level -= 1
self.emit(node, "\n")
else:
self.emit(node, i.gen.jac)
values_codgen = ", ".join([expr.gen.jac for expr in node.values])
if self.is_line_break_needed(values_codgen, 88):
self.emit(node, "[")
self.emit_ln(node, "")
self.indent_level += 1
for expr in node.values:
self.emit(node, f"{expr.gen.jac},\n")
self.indent_level -= 1
self.emit(node, "]")
else:
self.emit(node, f"[{values_codgen}]")

def exit_set_val(self, node: ast.ListVal) -> None:
def exit_set_val(self, node: ast.SetVal) -> None:
"""Sub objects.

values: list[ExprType],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ JokeList {
{
"joke": "Which knight invented King Arthur's Round Table?",
"punchline": "Sir Cumference."
}
},
];

can 'Generate a Joke with a Punchline'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ glob examples: 'Examples for Picking Odd Word out': list[dict] = [
"OPTIONS": ["Spain", "France", "German", "England", "Singapore"],
"REASONING": "Spain is a country, France is a country, German is a language, ...",
"RESULT": "German"
}
},
];

can 'Pick only the Odd word out'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ QA {
{
"Thought": "High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.",
"Action": "Finish: 1,800 to 7,000 ft"
}
},
]
}
},
];

can 'Get next Thought and Action. Action should always startswith "Search" or "Finish"'
Expand Down
10 changes: 9 additions & 1 deletion jac/jaclang/compiler/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,22 @@ def fix(item: ast.TupleVal | ast.ListVal | ast.UnaryExpr) -> None:
if isinstance(item, ast.UnaryExpr):
if isinstance(item.operand, ast.AstSymbolNode):
item.operand.name_spec.py_ctx_func = ast3.Store
elif isinstance(item, (ast.TupleVal, ast.ListVal)):
elif isinstance(item, ast.TupleVal):
for i in item.values.items if item.values else []:
if isinstance(i, ast.AstSymbolNode):
i.name_spec.py_ctx_func = ast3.Store
elif isinstance(i, ast.AtomTrailer):
self.chain_def_insert(i.as_attr_list)
if isinstance(i, (ast.TupleVal, ast.ListVal, ast.UnaryExpr)):
fix(i)
elif isinstance(item, ast.ListVal):
for expr in item.values:
if isinstance(expr, ast.AstSymbolNode):
expr.name_spec.py_ctx_func = ast3.Store
elif isinstance(expr, ast.AtomTrailer):
self.chain_def_insert(expr.as_attr_list)
if isinstance(expr, (ast.TupleVal, ast.ListVal, ast.UnaryExpr)):
fix(expr)

fix(node)

Expand Down