Skip to content

Commit

Permalink
ast: use &Expr instead of ExprData as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 24, 2024
1 parent 1e3b096 commit 926e818
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 54 deletions.
39 changes: 20 additions & 19 deletions std/jule/ast/node.jule
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub type ExprData: any
// Expression.
pub struct Expr {
pub token: Token
pub range: bool // Packed in parentheses.

// Possible types:
// - &TupleExpr
Expand Down Expand Up @@ -221,7 +222,7 @@ impl LitExpr {
// Unsafe expression.
pub struct UnsafeExpr {
pub token: Token // Token of unsafe keyword.
pub expr: ExprData
pub expr: &Expr
}

// Co expression.
Expand All @@ -247,19 +248,19 @@ impl IdentExpr {
// Unary expression.
pub struct UnaryExpr {
pub op: Token
pub expr: ExprData
pub expr: &Expr
}

// Variadiced expression.
pub struct VariadicExpr {
pub token: Token
pub expr: ExprData
pub expr: &Expr
}

// Casting expression.
pub struct CastExpr {
pub kind: &TypeDecl
pub expr: ExprData
pub expr: &Expr
}

// Namespace identifier selection expression.
Expand All @@ -271,14 +272,14 @@ pub struct NsSelectionExpr {
// Object sub identifier selection expression.
pub struct SubIdentExpr {
pub is_self: bool // True if root selector is "self" keyword.
pub expr: ExprData // Selected object.
pub expr: &Expr // Selected object.
pub ident: Token // TOken of selected identifier.
}

// Binary operation.
pub struct BinopExpr {
pub left: ExprData
pub right: ExprData
pub left: &Expr
pub right: &Expr
pub op: Token
}

Expand Down Expand Up @@ -308,7 +309,7 @@ impl FnCallExpr {
// Field-Expression pair.
pub struct FieldExprPair {
pub field: Token // Field identifier token.
pub expr: ExprData
pub expr: &Expr
}

impl FieldExprPair {
Expand Down Expand Up @@ -339,16 +340,16 @@ impl BraceLit {

// Key-value pair expression.
pub struct KeyValPair {
pub key: ExprData
pub val: ExprData
pub key: &Expr
pub val: &Expr
pub colon: Token
}

// Slice initiating expression.
// Also represents array initiating expression.
pub struct SliceExpr {
pub token: Token
pub elems: []ExprData
pub elems: []&Expr
}

impl SliceExpr {
Expand All @@ -361,24 +362,24 @@ impl SliceExpr {
// Indexing expression.
pub struct IndexingExpr {
pub token: Token
pub expr: ExprData // Value expression to indexing.
pub index: ExprData // Index value expression.
pub expr: &Expr // Value expression to indexing.
pub index: &Expr // Index value expression.
}

// Slicing expression.
pub struct SlicingExpr {
pub token: Token
pub expr: ExprData // Value expression to slicing.
pub start: ExprData // Start index value expression.
pub to: ExprData // To index value expression.
pub expr: &Expr // Value expression to slicing.
pub start: &Expr // Start index value expression.
pub to: &Expr // To index value expression.
}

// Ternary expression.
pub struct TernaryExpr {
pub token: Token
pub condition: ExprData
pub true_expr: ExprData
pub false_expr: ExprData
pub condition: &Expr
pub true_expr: &Expr
pub false_expr: &Expr
}

// Generic type declaration.
Expand Down
46 changes: 24 additions & 22 deletions std/jule/parser/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl ExprBuilder {

ret &UnaryExpr{
op: op,
expr: self.build(tokens),
expr: self.build_from_tokens(tokens),
}
}

Expand All @@ -323,7 +323,7 @@ impl ExprBuilder {
}
ret &SubIdentExpr{
ident: ident_token,
expr: self.build(tokens),
expr: self.build_from_tokens(tokens),
}
}

Expand Down Expand Up @@ -383,7 +383,7 @@ impl ExprBuilder {
tokens = tokens[:tokens.len-1] // Remove variadic operator token.
ret &VariadicExpr{
token: token,
expr: self.build(tokens),
expr: self.build_from_tokens(tokens),
}
}

Expand All @@ -410,15 +410,17 @@ impl ExprBuilder {
ret nil
}

fn build_between_parentheses(mut self, mut tokens: []Token): ExprData {
fn build_between_parentheses(mut self, mut tokens: []Token): &Expr {
let token = tokens[0]
tokens = tokens[1 : tokens.len-1] // Remove parentheses.
if tokens.len == 0 {
self.push_err(token, LogMsg.MissingExpr)
self.push_suggestion(LogMsg.EmptyParentNotValid)
ret nil
}
ret self.build(tokens)
let mut expr = self.build_from_tokens(tokens)
expr.range = true
ret expr
}

fn try_build_cast(mut self, mut &tokens: []Token): &CastExpr {
Expand Down Expand Up @@ -486,7 +488,7 @@ impl ExprBuilder {
ret nil
}
expr_tokens = expr_tokens[:j]
cast.expr = self.build(expr_tokens)
cast.expr = self.build_from_tokens(expr_tokens)
}
}
ret cast
Expand Down Expand Up @@ -614,7 +616,7 @@ impl ExprBuilder {
}
ret &UnsafeExpr{
token: token,
expr: self.build_from_tokens(range_tokens).kind,
expr: self.build_from_tokens(range_tokens),
}
}

Expand Down Expand Up @@ -699,7 +701,7 @@ impl ExprBuilder {
field: tokens[0],
}
tokens = tokens[2:] // Remove field identifier and colon tokens.
pair.expr = self.build_from_tokens(tokens).kind
pair.expr = self.build_from_tokens(tokens)
ret pair
}

Expand Down Expand Up @@ -756,8 +758,8 @@ impl ExprBuilder {
if l != nil {
ret &KeyValPair{
colon: tokens[l.len],
key: self.build_from_tokens(l).kind,
val: self.build_from_tokens(r).kind,
key: self.build_from_tokens(l),
val: self.build_from_tokens(r),
}
}
ret self.build_from_tokens(tokens)
Expand Down Expand Up @@ -850,12 +852,12 @@ impl ExprBuilder {
ret slc
}

slc.elems = make([]ExprData, 0, parts.len)
slc.elems = make([]&Expr, 0, parts.len)
for (_, mut p) in parts {
if p.len == 0 {
continue
}
slc.elems = append(slc.elems, self.build_from_tokens(p).kind)
slc.elems = append(slc.elems, self.build_from_tokens(p))
}

ret slc
Expand All @@ -878,26 +880,26 @@ impl ExprBuilder {
}
ret &IndexingExpr{
token: error_token,
expr: expr.kind,
index: index.kind,
expr: expr,
index: index,
}
}

fn build_slicing(mut self, mut &expr_tokens: []Token, mut &slicing_tokens: []Token,
&colon: int, mut &error_token: Token): &SlicingExpr {
let mut slc = &SlicingExpr{
token: error_token,
expr: self.build_from_tokens(expr_tokens).kind,
expr: self.build_from_tokens(expr_tokens),
}

let mut start_expr_tokens = slicing_tokens[:colon]
if start_expr_tokens.len > 0 {
slc.start = self.build_from_tokens(start_expr_tokens).kind
slc.start = self.build_from_tokens(start_expr_tokens)
}

let mut to_expr_tokens = slicing_tokens[colon+1:]
if to_expr_tokens.len > 0 {
slc.to = self.build_from_tokens(to_expr_tokens).kind
slc.to = self.build_from_tokens(to_expr_tokens)
}

ret slc
Expand Down Expand Up @@ -941,7 +943,7 @@ impl ExprBuilder {
self.push_err(ter.token, LogMsg.MissingExpr)
}

ter.condition = self.build_kind(expr_tokens)
ter.condition = self.build_from_tokens(expr_tokens)

let mut i = expr_tokens.len
let mut true_expr = range(i, TokenKind.LBrace, TokenKind.RBrace, tokens)
Expand Down Expand Up @@ -980,8 +982,8 @@ impl ExprBuilder {
self.push_err(tokens[i], LogMsg.InvalidSyntax)
}

ter.true_expr = self.build_kind(true_expr)
ter.false_expr = self.build_kind(false_expr)
ter.true_expr = self.build_from_tokens(true_expr)
ter.false_expr = self.build_from_tokens(false_expr)

ret ter
}
Expand Down Expand Up @@ -1070,8 +1072,8 @@ impl ExprBuilder {
ret nil
}
ret &BinopExpr{
left: self.build(left_tokens),
right: self.build(right_tokens),
left: self.build_from_tokens(left_tokens),
right: self.build_from_tokens(right_tokens),
op: tokens[i],
}
}
Expand Down
22 changes: 12 additions & 10 deletions std/jule/sema/eval.jule
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,9 @@ impl Eval {
let mut filled = false

if s.elems.len == 2 {
match type s.elems[1] {
match type s.elems[1].kind {
| &VariadicExpr:
if (&VariadicExpr)(s.elems[1]).expr != nil {
if (&VariadicExpr)(s.elems[1].kind).expr != nil {
break
}
// Filled.
Expand Down Expand Up @@ -1054,11 +1054,11 @@ impl Eval {
}

fn push_generics_from_index_data(mut &self, mut &decl: &IdentTypeDecl, mut &i: &IndexingExpr): bool {
match type i.index {
match type i.index.kind {
| &UnaryExpr:
// Unary eval catches type declarations.
// Therefore use unary eval algorithm for eval related type declarations.
let mut d = self.eval_unary((&UnaryExpr)(i.index))
let mut d = self.eval_unary((&UnaryExpr)(i.index.kind))
if !d.decl {
self.push_err(i.token, LogMsg.InvalidSyntax)
ret false
Expand All @@ -1067,17 +1067,17 @@ impl Eval {
kind: d.kind,
})
| &TypeDecl:
decl.generics = append(decl.generics, (&TypeDecl)(i.index))
decl.generics = append(decl.generics, (&TypeDecl)(i.index.kind))
| &IdentExpr:
let mut expr = (&IdentExpr)(i.index)
let mut expr = (&IdentExpr)(i.index.kind)
decl.generics = append(decl.generics, &TypeDecl{
kind: &IdentTypeDecl{
token: expr.token,
ident: expr.ident,
}
})
| &TupleExpr:
for (_, mut expr) in (&TupleExpr)(i.index).expr {
for (_, mut expr) in (&TupleExpr)(i.index.kind).expr {
match type expr.kind {
| &TypeDecl:
decl.generics = append(decl.generics, (&TypeDecl)(expr.kind))
Expand All @@ -1101,16 +1101,16 @@ impl Eval {
fn eval_ident_decl_from_indexing(mut &self, mut &d: &Data, mut &i: &IndexingExpr): &Data {
let mut tdecl = &TypeDecl{}
let mut decl: &IdentTypeDecl = nil
match type i.expr {
match type i.expr.kind {
| &IdentExpr:
let mut expr = (&IdentExpr)(i.expr)
let mut expr = (&IdentExpr)(i.expr.kind)
decl = &IdentTypeDecl{
token: expr.token,
ident: expr.ident,
}
tdecl.kind = decl
| &NsSelectionExpr:
let mut expr = (&NsSelectionExpr)(i.expr)
let mut expr = (&NsSelectionExpr)(i.expr.kind)
decl = &IdentTypeDecl{
token: expr.ident,
ident: expr.ident.kind,
Expand Down Expand Up @@ -2964,6 +2964,8 @@ impl Eval {
fn eval_expr_kind(mut &self, mut kind: ExprData): &Data {
let mut d: &Data = nil
match type kind {
| &Expr:
d = self.eval_expr_kind((&Expr)(kind).kind)
| &LitExpr:
d = self.eval_lit((&LitExpr)(kind))
| &IdentExpr:
Expand Down
2 changes: 1 addition & 1 deletion std/jule/sema/scope.jule
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ impl ScopeChecker {
self.check_ret_multi_assign(a)
ret
| &UnsafeExpr:
match type (&UnsafeExpr)(a.right.kind).expr {
match type (&UnsafeExpr)(a.right.kind).expr.kind {
| &FnCallExpr:
self.check_ret_multi_assign(a)
ret
Expand Down
5 changes: 3 additions & 2 deletions std/jule/sema/type2.jule
Original file line number Diff line number Diff line change
Expand Up @@ -814,16 +814,17 @@ impl StructLitChecker {
match type expr {
| &KeyValPair:
let mut pair = (&KeyValPair)(expr)
match type pair.key {
match type pair.key.kind {
| &IdentExpr:
// Ok
break
|:
self.push_err(pair.colon, LogMsg.InvalidSyntax)
ok = false
continue
}
exprs[i] = &FieldExprPair{
field: (&IdentExpr)(pair.key).token,
field: pair.key.token,
expr: pair.val,
}
}
Expand Down

0 comments on commit 926e818

Please sign in to comment.