Add nil keyword
This commit is contained in:
		| @@ -20,6 +20,14 @@ func (p Position) String() string { | ||||
| 	return fmt.Sprintf("%s: line %d, col %d", p.Name, p.Line, p.Col) | ||||
| } | ||||
|  | ||||
| type Nil struct { | ||||
| 	Position Position | ||||
| } | ||||
|  | ||||
| func (n Nil) Pos() Position { | ||||
| 	return n.Position | ||||
| } | ||||
|  | ||||
| type Tag struct { | ||||
| 	Name     Ident | ||||
| 	Params   []Node | ||||
|   | ||||
| @@ -16,6 +16,7 @@ type User struct { | ||||
| 	Name           string | ||||
| 	LoggedIn       bool | ||||
| 	IsAdmin        bool | ||||
| 	X              func() | ||||
| 	RegisteredTime time.Time | ||||
| } | ||||
|  | ||||
| @@ -24,6 +25,7 @@ var users = []User{ | ||||
| 		Name:           "Elara", | ||||
| 		LoggedIn:       true, | ||||
| 		IsAdmin:        true, | ||||
| 		X:              func() {}, | ||||
| 		RegisteredTime: time.Date(2023, time.January, 10, 10, 10, 10, 0, time.UTC), | ||||
| 	}, | ||||
| 	{ | ||||
|   | ||||
							
								
								
									
										11
									
								
								expr.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								expr.go
									
									
									
									
									
								
							| @@ -54,6 +54,17 @@ func (t *Template) performOp(a, b reflect.Value, op ast.Operator) (any, error) { | ||||
| 		default: | ||||
| 			return nil, ast.PosError(op, "the in operator can only be used on strings, arrays, and slices (got %s and %s)", a.Type(), b.Type()) | ||||
| 		} | ||||
| 	} else if !b.IsValid() { | ||||
| 		if op.Value != "==" { | ||||
| 			return nil, ast.PosError(op, "invalid operator for nil value (expected ==, got %s)", op.Value) | ||||
| 		} | ||||
|  | ||||
| 		switch a.Kind() { | ||||
| 		case reflect.Chan, reflect.Slice, reflect.Map, reflect.Func, reflect.Interface, reflect.Pointer: | ||||
| 			return a.IsNil(), nil | ||||
| 		default: | ||||
| 			return nil, ast.PosError(op, "values of type %s cannot be compared against nil", a.Type()) | ||||
| 		} | ||||
| 	} else if b.CanConvert(a.Type()) { | ||||
| 		b = b.Convert(a.Type()) | ||||
| 	} else { | ||||
|   | ||||
							
								
								
									
										606
									
								
								parser/parser.go
									
									
									
									
									
								
							
							
						
						
									
										606
									
								
								parser/parser.go
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -129,7 +129,7 @@ ParamList = '(' params:(Expr ( ',' _ Expr )* )? ')' { | ||||
|     return out, nil | ||||
| } | ||||
|  | ||||
| Value = not:"!"? node:(MethodCall / FieldAccess / Index / String / RawString / Float / Integer / Bool / FuncCall / VariableOr / Ident / ParenExpr / Array / Map) { | ||||
| Value = not:"!"? node:(Nil / MethodCall / FieldAccess / Index / String / RawString / Float / Integer / Bool / FuncCall / VariableOr / Ident / ParenExpr / Array / Map) { | ||||
|     return ast.Value{ | ||||
|         Node: node.(ast.Node), | ||||
|         Not:  not != nil, | ||||
| @@ -288,6 +288,10 @@ ArithmeticOp = ('+' / '-' / '/' / '*' / '%') { | ||||
|     }, nil | ||||
| } | ||||
|  | ||||
| Nil = "nil" { | ||||
|     return ast.Nil{Position: getPos(c)}, nil | ||||
| } | ||||
|  | ||||
| Text = . [^#]* { return ast.Text{Data: c.text, Position: getPos(c)}, nil } | ||||
|  | ||||
| _ "whitespace" ← [ \t\r\n]* | ||||
|   | ||||
		Reference in New Issue
	
	Block a user