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

Commit

Permalink
refactor(grammar): updated how return types are represented
Browse files Browse the repository at this point in the history
  • Loading branch information
marsninja committed Jan 22, 2024
1 parent 13080b8 commit 2467b2a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 64 deletions.
8 changes: 5 additions & 3 deletions jaclang/compiler/absyntree.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def __init__(
self.tag = tag
AstNode.__init__(self, kid=kid)


class SubNodeList(AstNode, Generic[T]):
"""SubNodeList node type for Jac Ast."""

Expand Down Expand Up @@ -677,7 +678,7 @@ class FuncSignature(AstSemStrNode):
def __init__(
self,
params: Optional[SubNodeList[ParamVar]],
return_type: Optional[SubTag[Expr]],
return_type: Optional[Expr],
kid: Sequence[AstNode],
semstr: Optional[String] = None,
) -> None:
Expand Down Expand Up @@ -711,7 +712,7 @@ def __init__(
self,
event: Token,
arch_tag_info: Optional[Expr],
return_type: Optional[SubTag[Expr]],
return_type: Optional[Expr],
kid: Sequence[AstNode],
semstr: Optional[String] = None,
) -> None:
Expand Down Expand Up @@ -761,7 +762,7 @@ def flat_name(self) -> str:
)


class ParamVar(AstSymbolNode, AstTypedVarNode,AstSemStrNode):
class ParamVar(AstSymbolNode, AstTypedVarNode, AstSemStrNode):
"""ParamVar node type for Jac Ast."""

def __init__(
Expand All @@ -787,6 +788,7 @@ def __init__(
AstTypedVarNode.__init__(self, type_tag=type_tag)
AstSemStrNode.__init__(self, semstr=semstr)


class ArchHas(AstAccessNode, AstDocNode, ArchBlockStmt):
"""HasStmt node type for Jac Ast."""

Expand Down
9 changes: 4 additions & 5 deletions jaclang/compiler/jac.lark
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,13 @@ ability: decorators? ability_def
| decorators? KW_ASYNC ability_decl
| decorators? ability_decl

ability_decl: KW_STATIC? KW_CAN access_tag? STRING? any_ref (func_decl | event_clause) (code_block | SEMI)
ability_decl: KW_STATIC? KW_CAN access_tag? STRING? any_ref (func_decl | event_clause) (code_block | SEMI)
ability_def: arch_to_abil_chain (func_decl | event_clause) code_block
abstract_ability: KW_STATIC? KW_CAN access_tag? STRING? any_ref (func_decl | event_clause) KW_ABSTRACT SEMI
event_clause: KW_WITH expression? (KW_EXIT | KW_ENTRY) return_type_tag?
func_decl: (LPAREN func_decl_params? RPAREN)? return_type_tag?
event_clause: KW_WITH expression? (KW_EXIT | KW_ENTRY) (RETURN_HINT STRING? expression)?
func_decl: (LPAREN func_decl_params? RPAREN)? (RETURN_HINT STRING? expression)?
func_decl_params: (param_var COMMA)* param_var
param_var: (STAR_POW | STAR_MUL)? STRING? NAME type_tag (EQ expression)?
return_type_tag: RETURN_HINT STRING? expression

// Global variables
global_var: (KW_LET | KW_GLOBAL) access_tag? assignment_list SEMI
Expand Down Expand Up @@ -262,7 +261,7 @@ aug_op: RSHIFT_EQ
expression: pipe (KW_IF expression KW_ELSE expression)? | lambda_expr

// Lambda expressions
lambda_expr: KW_WITH func_decl_params? return_type_tag? KW_CAN expression
lambda_expr: KW_WITH func_decl_params? (RETURN_HINT expression)? KW_CAN expression

// Pipe expressions
pipe: (pipe PIPE_FWD)? pipe_back
Expand Down
99 changes: 43 additions & 56 deletions jaclang/compiler/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,28 +701,17 @@ def ability(self, kid: list[ast.AstNode]) -> ast.Ability | ast.AbilityDef:
def ability_decl(self, kid: list[ast.AstNode]) -> ast.Ability:
"""Grammar rule.
ability_decl: KW_STATIC? KW_CAN access_tag? STRING? any_ref (func_decl | event_clause) (code_block | SEMI)
ability_decl: KW_STATIC? KW_CAN access_tag? STRING? any_ref (func_decl | event_clause) (code_block | SEMI)
"""
chomp = [*kid]
is_static = (
isinstance(chomp[0], ast.Token) and chomp[0].name == Tok.KW_STATIC
)
chomp = chomp[2:] if is_static else chomp[1:]
access = chomp[0] if isinstance(chomp[0], ast.SubTag) else None
semstr = (
chomp[1]
if access and isinstance(chomp[1], ast.String)
else chomp[0]
if access or isinstance(chomp[0], ast.String)
else None
)
chomp = (
chomp[2:]
if access and semstr
else chomp[1:]
if access or semstr
else chomp
)
chomp = chomp[1:] if access else chomp
semstr = chomp[0] if isinstance(chomp[0], ast.String) else None
chomp = chomp[1:] if semstr else chomp
name = chomp[0]
chomp = chomp[1:]
is_func = isinstance(chomp[0], ast.FuncSignature)
Expand Down Expand Up @@ -784,9 +773,9 @@ def abstract_ability(self, kid: list[ast.AstNode]) -> ast.Ability:
access = chomp[0] if isinstance(chomp[0], ast.SubTag) else None
semstr = (
chomp[1]
if access and isinstance(chomp[1], ast.string)
if access and isinstance(chomp[1], ast.String)
else chomp[0]
if access or isinstance(chomp[1], ast.string)
if access or isinstance(chomp[1], ast.String)
else None
)
chomp = (
Expand Down Expand Up @@ -824,14 +813,16 @@ def abstract_ability(self, kid: list[ast.AstNode]) -> ast.Ability:
def event_clause(self, kid: list[ast.AstNode]) -> ast.EventSignature:
"""Grammar rule.
event_clause: KW_WITH expression? (KW_EXIT | KW_ENTRY) return_type_tag?
event_clause: KW_WITH expression? (KW_EXIT | KW_ENTRY) (RETURN_HINT STRING? expression)?
"""
type_specs = kid[1] if isinstance(kid[1], ast.Expr) else None
return_spec = kid[-1] if isinstance(kid[-1], ast.SubTag) else None
semstr = return_spec.kid[1] if return_spec else None
return_spec = kid[-1] if isinstance(kid[-1], ast.Expr) else None
semstr = (
kid[-2] if return_spec and isinstance(kid[-2], ast.String) else None
)
event = kid[2] if type_specs else kid[1]
if isinstance(event, ast.Token) and (
isinstance(return_spec, ast.SubTag) or return_spec is None
isinstance(return_spec, ast.Expr) or return_spec is None
):
return self.nu(
ast.EventSignature(
Expand All @@ -848,17 +839,19 @@ def event_clause(self, kid: list[ast.AstNode]) -> ast.EventSignature:
def func_decl(self, kid: list[ast.AstNode]) -> ast.FuncSignature:
"""Grammar rule.
func_decl: (LPAREN func_decl_params? RPAREN)? return_type_tag?
func_decl: (LPAREN func_decl_params? RPAREN)? (RETURN_HINT STRING? expression)?
"""
params = (
kid[1] if len(kid) > 1 and isinstance(kid[1], ast.SubNodeList) else None
)
return_spec = (
kid[-1] if len(kid) and isinstance(kid[-1], ast.SubTag) else None
kid[-1] if len(kid) and isinstance(kid[-1], ast.Expr) else None
)
semstr = (
kid[-2] if return_spec and isinstance(kid[-2], ast.String) else None
)
semstr= return_spec.kid[1] if return_spec else None
if (isinstance(params, ast.SubNodeList) or params is None) and (
isinstance(return_spec, ast.SubTag) or return_spec is None
isinstance(return_spec, ast.Expr) or return_spec is None
):
return self.nu(
ast.FuncSignature(
Expand Down Expand Up @@ -891,18 +884,24 @@ def param_var(self, kid: list[ast.AstNode]) -> ast.ParamVar:
"""
star = (
kid[0]
if isinstance(kid[0], ast.Token) and kid[0].name != Tok.NAME
and not isinstance(kid[0], ast.String)
if isinstance(kid[0], ast.Token)
and kid[0].name != Tok.NAME
and not isinstance(kid[0], ast.String)
else None
)
semstr = (
kid[1]
if (star and isinstance(kid[1], ast.String))
else kid[0]
if isinstance(kid[0], ast.String)
else None
)
semstr =(
kid[1]
if (star and isinstance(kid[1], ast.String))
else kid[0] if isinstance(kid[0], ast.String)
else None
)
name = kid[2] if (star and semstr) else kid[1] if (star or semstr) else kid[0]
type_tag = kid[3] if (star and semstr) else kid[2] if (star or semstr) else kid[1]
name = (
kid[2] if (star and semstr) else kid[1] if (star or semstr) else kid[0]
)
type_tag = (
kid[3] if (star and semstr) else kid[2] if (star or semstr) else kid[1]
)
value = kid[-1] if isinstance(kid[-1], ast.Expr) else None
if isinstance(name, ast.Name) and isinstance(type_tag, ast.SubTag):
return self.nu(
Expand Down Expand Up @@ -1046,24 +1045,6 @@ def type_tag(self, kid: list[ast.AstNode]) -> ast.SubTag[ast.Expr]:
else:
raise self.ice()

def return_type_tag(self, kid: list[ast.AstNode]) -> ast.SubTag[ast.Expr]:
"""Grammar rule.
return_type_tag: RETURN_HINT STRING? expression
"""
semstr = kid[1] if isinstance(kid[1], ast.String) else None
tag = kid[2] if semstr else kid[1]
# chomp = [item for item in kid if item is not semstr]
if isinstance(tag, ast.Expr):
return self.nu(
ast.SubTag[ast.Expr](
tag=tag,
kid=kid,
)
)
else:
raise self.ice()

def builtin_type(self, kid: list[ast.AstNode]) -> ast.Token:
"""Grammar rule.
Expand Down Expand Up @@ -1792,13 +1773,19 @@ def binary_expr_unwind(self, kid: list[ast.AstNode]) -> ast.Expr:
def lambda_expr(self, kid: list[ast.AstNode]) -> ast.LambdaExpr:
"""Grammar rule.
lamda_expr: KW_WITH func_decl_params? return_type_tag? KW_CAN expression
lamda_expr: KW_WITH func_decl_params? (RETURN_HINT expression)? KW_CAN expression
"""
chomp = [*kid][1:]
params = chomp[0] if isinstance(chomp[0], ast.SubNodeList) else None
chomp = chomp[1:] if params else chomp
return_type = chomp[0] if isinstance(chomp[0], ast.SubTag) else None
chomp = chomp[1:] if return_type else chomp
return_type = (
chomp[1]
if isinstance(chomp[0], ast.Token)
and chomp[0].name == Tok.RETURN_HINT
and isinstance(chomp[1], ast.Expr)
else None
)
chomp = chomp[2:] if return_type else chomp
chomp = chomp[1:]
sig_kid: list[ast.AstNode] = []
if params:
Expand Down

0 comments on commit 2467b2a

Please sign in to comment.