/
condition.go
76 lines (63 loc) · 2.64 KB
/
condition.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transpiler
import (
"github.com/Lakshmi-Priya-Ramisetty/healthcare-data-harmonization/mapping_language/parser" /* copybara-comment: parser */
mpb "github.com/Lakshmi-Priya-Ramisetty/healthcare-data-harmonization/mapping_engine/proto" /* copybara-comment: mapping_go_proto */
)
func (t *transpiler) VisitConditionBlock(ctx *parser.ConditionBlockContext) interface{} {
// Condition block is a composite of an If and optionally an else, with corresponding blocks.
ctx.IfBlock().Accept(t)
if ctx.ElseBlock() != nil {
ctx.ElseBlock().Accept(t)
}
// IfBlock below pushes a condition (with its awareness of the condition subtree).
t.conditionStackTop().pop()
// Since this is a block of mappings that get added to the environment, there is nothing to
// return.
return nil
}
func (t *transpiler) VisitIfBlock(ctx *parser.IfBlockContext) interface{} {
// Transpile and add the condition to the stack, and process the block as usual.
t.conditionStackTop().push(ctx.Condition().Accept(t).(*mpb.ValueSource))
ctx.Block().Accept(t)
// Since this is a block of mappings that get added to the environment, there is nothing to
// return.
return nil
}
// not returns a new ValueSource, inverting the output of the given by passing it through _Not.
func not(vs *mpb.ValueSource) *mpb.ValueSource {
return &mpb.ValueSource{
Source: &mpb.ValueSource_ProjectedValue{
ProjectedValue: vs,
},
Projector: "$Not",
}
}
func (t *transpiler) VisitElseBlock(ctx *parser.ElseBlockContext) interface{} {
// Else uses the condition in If, but inverted.
t.conditionStackTop().push(not(t.conditionStackTop().pop()))
ctx.Block().Accept(t)
// Since this is a block of mappings that get added to the environment, there is nothing to
// return.
return nil
}
func (t *transpiler) VisitInlineCondition(ctx *parser.InlineConditionContext) interface{} {
// No-op here, just visit the Condition child.
return ctx.Condition().Accept(t)
}
func (t *transpiler) VisitCondition(ctx *parser.ConditionContext) interface{} {
// No-op here, just visit the Expression child.
return ctx.Expression().Accept(t)
}