scope.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2017 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package parser
  15. import (
  16. "strings"
  17. )
  18. type Scope interface {
  19. Get(name string) string
  20. Set(name, value string)
  21. Call(name string, args []string) string
  22. SetFunc(name string, f func([]string) string)
  23. }
  24. type scope struct {
  25. variables map[string]string
  26. functions map[string]func([]string) string
  27. parent Scope
  28. }
  29. func (s *scope) Get(name string) string {
  30. if val, ok := s.variables[name]; ok {
  31. return val
  32. } else if s.parent != nil {
  33. return s.parent.Get(name)
  34. } else if val, ok := builtinScope[name]; ok {
  35. return val
  36. } else {
  37. return "<'" + name + "' unset>"
  38. }
  39. }
  40. func (s *scope) Set(name, value string) {
  41. s.variables[name] = value
  42. }
  43. func (s *scope) Call(name string, args []string) string {
  44. if f, ok := s.functions[name]; ok {
  45. return f(args)
  46. }
  47. return "<func:'" + name + "' unset>"
  48. }
  49. func (s *scope) SetFunc(name string, f func([]string) string) {
  50. s.functions[name] = f
  51. }
  52. func NewScope(parent Scope) Scope {
  53. return &scope{
  54. variables: make(map[string]string),
  55. functions: make(map[string]func([]string) string),
  56. parent: parent,
  57. }
  58. }
  59. var builtinScope map[string]string
  60. func init() {
  61. builtinScope := make(map[string]string)
  62. builtinScope["__builtin_dollar"] = "$"
  63. }
  64. func (v Variable) EvalFunction(scope Scope) (string, bool) {
  65. f := v.Name.SplitN(" \t", 2)
  66. if len(f) > 1 && f[0].Const() {
  67. fname := f[0].Value(nil)
  68. if isFunctionName(fname) {
  69. args := f[1].Split(",")
  70. argVals := make([]string, len(args))
  71. for i, a := range args {
  72. argVals[i] = a.Value(scope)
  73. }
  74. if fname == "call" {
  75. return scope.Call(argVals[0], argVals[1:]), true
  76. } else {
  77. return "__builtin_func:" + fname + " " + strings.Join(argVals, " "), true
  78. }
  79. }
  80. }
  81. return "", false
  82. }
  83. func (v Variable) Value(scope Scope) string {
  84. if ret, ok := v.EvalFunction(scope); ok {
  85. return ret
  86. }
  87. if scope == nil {
  88. panic("Cannot take the value of a variable in a nil scope")
  89. }
  90. return scope.Get(v.Name.Value(scope))
  91. }
  92. func toVariable(ms *MakeString) (Variable, bool) {
  93. if len(ms.Variables) == 1 && ms.Strings[0] == "" && ms.Strings[1] == "" {
  94. return ms.Variables[0], true
  95. }
  96. return Variable{}, false
  97. }