File: expression.go

package info (click to toggle)
golang-github-leonelquinteros-gotext 1.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 404 kB
  • sloc: makefile: 4
file content (43 lines) | stat: -rw-r--r-- 846 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for full license information.
 */

package plurals

// Expression is a plurals expression. Eval evaluates the expression for
// a given n value. Use plurals.Compile to generate Expression instances.
type Expression interface {
	Eval(n uint32) int
}

type constValue struct {
	value int
}

func (c constValue) Eval(n uint32) int {
	return c.value
}

type test interface {
	test(n uint32) bool
}

type ternary struct {
	test      test
	trueExpr  Expression
	falseExpr Expression
}

func (t ternary) Eval(n uint32) int {
	if t.test.test(n) {
		if t.trueExpr == nil {
			return -1
		}
		return t.trueExpr.Eval(n)
	}
	if t.falseExpr == nil {
		return -1
	}
	return t.falseExpr.Eval(n)
}