Add ternary expressions

This commit is contained in:
2023-10-30 08:37:59 -07:00
parent 9d44c8d02c
commit c1a2e56f4f
5 changed files with 352 additions and 186 deletions

View File

@@ -199,3 +199,14 @@ func (l Logical) Pos() Position {
func (l Logical) Op() string {
return l.Value
}
type Ternary struct {
Condition Node
IfTrue Node
Else Node
Position Position
}
func (t Ternary) Pos() Position {
return t.Position
}

File diff suppressed because it is too large Load Diff

View File

@@ -55,13 +55,15 @@ EndTag = "#!" name:Ident {
}, nil
}
ExprTag = "#(" expr:Expr ')' {
ExprTag = "#(" item:Item ')' {
return ast.ExprTag{
Value: expr.(ast.Node),
Value: item.(ast.Node),
Position: getPos(c),
}, nil
}
Item = Ternary / Expr
Expr = first:ExprSegment rest:(_ Logical _ ExprSegment)* {
restSlice := toAnySlice(rest)
if len(restSlice) == 0 {
@@ -100,7 +102,7 @@ ExprSegment = first:Value rest:(_ Operator _ Value)* {
return out, nil
}
ParamList = '(' params:(Expr ( ',' _ Expr )* )? ')' {
ParamList = '(' params:(Item ( ',' _ Item )* )? ')' {
paramSlice := toAnySlice(params)
if len(paramSlice) == 0 {
return []ast.Node{}, nil
@@ -121,6 +123,14 @@ Value = not:"!"? node:(MethodCall / FieldAccess / Index / String / RawString / F
}, nil
}
Ternary = cond:Expr _ '?' _ ifTrue:Value _ ':' _ elseVal:Value {
return ast.Ternary{
Condition: cond.(ast.Node),
IfTrue: ifTrue.(ast.Node),
Else: elseVal.(ast.Node),
}, nil
}
MethodCall = value:Value '.' name:Ident params:ParamList {
return ast.MethodCall{
Value: value.(ast.Node),