lang: parser, funcs: Change the logical operators to OR, AND, NOT

This makes it easier to read for some, and easier to parse for us. This
also frees up more characters to use elsewhere.
This commit is contained in:
James Shubin
2023-10-09 16:41:03 -04:00
parent e8f11286dc
commit 05d570d250
12 changed files with 20 additions and 20 deletions

View File

@@ -14,7 +14,7 @@ $threshold = 1.5 # change me if you like
$h1 = $theload > $threshold $h1 = $theload > $threshold
$h2 = $theload{1} > $threshold $h2 = $theload{1} > $threshold
$h3 = $theload{2} > $threshold $h3 = $theload{2} > $threshold
$unload = $h1 || $h2 || $h3 $unload = $h1 or $h2 or $h3
virt "mgmt1" { virt "mgmt1" {
uri => "qemu:///session", uri => "qemu:///session",

View File

@@ -9,7 +9,7 @@ $mod0 = math.mod($now, 8) == 0
$mod1 = math.mod($now, 8) == 1 $mod1 = math.mod($now, 8) == 1
$mod2 = math.mod($now, 8) == 2 $mod2 = math.mod($now, 8) == 2
$mod3 = math.mod($now, 8) == 3 $mod3 = math.mod($now, 8) == 3
$mod = $mod0 || $mod1 || $mod2 || $mod3 $mod = $mod0 or $mod1 or $mod2 or $mod3
$fn = func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2 $fn = func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2
len($x) len($x)

View File

@@ -9,13 +9,13 @@ $mod0 = math.mod($now, 8) == 0
$mod1 = math.mod($now, 8) == 1 $mod1 = math.mod($now, 8) == 1
$mod2 = math.mod($now, 8) == 2 $mod2 = math.mod($now, 8) == 2
$mod3 = math.mod($now, 8) == 3 $mod3 = math.mod($now, 8) == 3
$moda = $mod0 || $mod1 || $mod2 || $mod3 $moda = $mod0 or $mod1 or $mod2 or $mod3
$mod4 = math.mod($now, 8) == 4 $mod4 = math.mod($now, 8) == 4
$mod5 = math.mod($now, 8) == 5 $mod5 = math.mod($now, 8) == 5
$mod6 = math.mod($now, 8) == 6 $mod6 = math.mod($now, 8) == 6
$mod7 = math.mod($now, 8) == 7 $mod7 = math.mod($now, 8) == 7
$modb = $mod4 || $mod5 || $mod6 || $mod7 $modb = $mod4 or $mod5 or $mod6 or $mod7
$fn = if $moda { $fn = if $moda {
func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2 func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2

View File

@@ -9,7 +9,7 @@ $s1 = template("Hello! It is now: {{ datetime_print . }}\n", $now)
$s2 = if $is_friday { $s2 = if $is_friday {
"It's friday!!! (don't break anything, read-only)" "It's friday!!! (don't break anything, read-only)"
} else { } else {
if $day == "saturday" || $day == "sunday" { if $day == "saturday" or $day == "sunday" {
"It's the weekend!" "It's the weekend!"
} else { } else {
fmt.printf("Unfortunately, it is %s. Go to work!", $day) fmt.printf("Unfortunately, it is %s. Go to work!", $day)

View File

@@ -8,7 +8,7 @@ $mod0 = math.mod($now, 8) == 0
$mod1 = math.mod($now, 8) == 1 $mod1 = math.mod($now, 8) == 1
$mod2 = math.mod($now, 8) == 2 $mod2 = math.mod($now, 8) == 2
$mod3 = math.mod($now, 8) == 3 $mod3 = math.mod($now, 8) == 3
$mod = $mod0 || $mod1 || $mod2 || $mod3 $mod = $mod0 or $mod1 or $mod2 or $mod3
file "/tmp/mgmt/" { file "/tmp/mgmt/" {
state => $const.res.file.state.exists, state => $const.res.file.state.exists,

View File

@@ -8,7 +8,7 @@ $mod0 = math.mod($now, 8) == 0
$mod1 = math.mod($now, 8) == 1 $mod1 = math.mod($now, 8) == 1
$mod2 = math.mod($now, 8) == 2 $mod2 = math.mod($now, 8) == 2
$mod3 = math.mod($now, 8) == 3 $mod3 = math.mod($now, 8) == 3
$mod = $mod0 || $mod1 || $mod2 || $mod3 $mod = $mod0 or $mod1 or $mod2 or $mod3
file "/tmp/mgmt/" { file "/tmp/mgmt/" {
state => $const.res.file.state.exists, state => $const.res.file.state.exists,

View File

@@ -8,7 +8,7 @@ $mod0 = math.mod($now, 8) == 0
$mod1 = math.mod($now, 8) == 1 $mod1 = math.mod($now, 8) == 1
$mod2 = math.mod($now, 8) == 2 $mod2 = math.mod($now, 8) == 2
$mod3 = math.mod($now, 8) == 3 $mod3 = math.mod($now, 8) == 3
$mod = $mod0 || $mod1 || $mod2 || $mod3 $mod = $mod0 or $mod1 or $mod2 or $mod3
file "/tmp/mgmt/" { file "/tmp/mgmt/" {
state => $const.res.file.state.exists, state => $const.res.file.state.exists,

View File

@@ -4,7 +4,7 @@ $ns = "estate"
$exchanged = world.kvlookup($ns) $exchanged = world.kvlookup($ns)
$state = maplookup($exchanged, $hostname, "default") $state = maplookup($exchanged, $hostname, "default")
if $state == "one" || $state == "default" { if $state == "one" or $state == "default" {
file "/tmp/mgmt/state" { file "/tmp/mgmt/state" {
state => $const.res.file.state.exists, state => $const.res.file.state.exists,

View File

@@ -295,7 +295,7 @@ func init() {
// logical and // logical and
// TODO: is there a way for the engine to have // TODO: is there a way for the engine to have
// short-circuit operators, and does it matter? // short-circuit operators, and does it matter?
RegisterOperator("&&", &types.FuncValue{ RegisterOperator("and", &types.FuncValue{
T: types.NewType("func(a bool, b bool) bool"), T: types.NewType("func(a bool, b bool) bool"),
V: func(input []types.Value) (types.Value, error) { V: func(input []types.Value) (types.Value, error) {
return &types.BoolValue{ return &types.BoolValue{
@@ -304,7 +304,7 @@ func init() {
}, },
}) })
// logical or // logical or
RegisterOperator("||", &types.FuncValue{ RegisterOperator("or", &types.FuncValue{
T: types.NewType("func(a bool, b bool) bool"), T: types.NewType("func(a bool, b bool) bool"),
V: func(input []types.Value) (types.Value, error) { V: func(input []types.Value) (types.Value, error) {
return &types.BoolValue{ return &types.BoolValue{
@@ -314,7 +314,7 @@ func init() {
}) })
// logical not (unary operator) // logical not (unary operator)
RegisterOperator("!", &types.FuncValue{ RegisterOperator("not", &types.FuncValue{
T: types.NewType("func(a bool) bool"), T: types.NewType("func(a bool) bool"),
V: func(input []types.Value) (types.Value, error) { V: func(input []types.Value) (types.Value, error) {
return &types.BoolValue{ return &types.BoolValue{

View File

@@ -6,7 +6,7 @@ $ns = "estate"
$exchanged = world.kvlookup($ns) $exchanged = world.kvlookup($ns)
$state = maplookup($exchanged, $hostname, "default") $state = maplookup($exchanged, $hostname, "default")
if $state == "one" || $state == "default" { if $state == "one" or $state == "default" {
file "/tmp/mgmt/state" { file "/tmp/mgmt/state" {
content => "state: one\n", content => "state: one\n",

View File

@@ -119,17 +119,17 @@
lval.str = yylex.Text() lval.str = yylex.Text()
return GTE return GTE
} }
/&&/ { /and/ {
yylex.pos(lval) // our pos yylex.pos(lval) // our pos
lval.str = yylex.Text() lval.str = yylex.Text()
return AND return AND
} }
/\|\|/ { /or/ {
yylex.pos(lval) // our pos yylex.pos(lval) // our pos
lval.str = yylex.Text() lval.str = yylex.Text()
return OR return OR
} }
/!/ { /not/ {
yylex.pos(lval) // our pos yylex.pos(lval) // our pos
lval.str = yylex.Text() lval.str = yylex.Text()
return NOT return NOT

View File

@@ -945,7 +945,7 @@ func TestLexParse0(t *testing.T) {
Name: funcs.OperatorFuncName, Name: funcs.OperatorFuncName,
Args: []interfaces.Expr{ Args: []interfaces.Expr{
&ast.ExprStr{ &ast.ExprStr{
V: "!", V: "not",
}, },
&ast.ExprInt{ &ast.ExprInt{
V: 3, V: 3,
@@ -966,7 +966,7 @@ func TestLexParse0(t *testing.T) {
name: "order of operations with not", name: "order of operations with not",
code: ` code: `
test "t1" { test "t1" {
boolptr => ! 3 > 4, # should parse, but not compile boolptr => not 3 > 4, # should parse, but not compile
} }
`, `,
fail: false, fail: false,
@@ -988,7 +988,7 @@ func TestLexParse0(t *testing.T) {
Name: funcs.OperatorFuncName, Name: funcs.OperatorFuncName,
Args: []interfaces.Expr{ Args: []interfaces.Expr{
&ast.ExprStr{ &ast.ExprStr{
V: "&&", V: "and",
}, },
&ast.ExprCall{ &ast.ExprCall{
Name: funcs.OperatorFuncName, Name: funcs.OperatorFuncName,
@@ -1018,7 +1018,7 @@ func TestLexParse0(t *testing.T) {
name: "order of operations logical", name: "order of operations logical",
code: ` code: `
test "t1" { test "t1" {
boolptr => 7 < 4 && true, # should be false boolptr => 7 < 4 and true, # should be false
} }
`, `,
fail: false, fail: false,