2017-11-18 02:55:38 +08:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
package parser
|
|
|
|
|
2017-08-24 08:03:42 +08:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
2015-01-31 09:27:36 +08:00
|
|
|
|
|
|
|
type Scope interface {
|
|
|
|
Get(name string) string
|
|
|
|
Set(name, value string)
|
2019-01-08 07:29:01 +08:00
|
|
|
Call(name string, args []string) []string
|
|
|
|
SetFunc(name string, f func([]string) []string)
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type scope struct {
|
|
|
|
variables map[string]string
|
2019-01-08 07:29:01 +08:00
|
|
|
functions map[string]func([]string) []string
|
2015-01-31 09:27:36 +08:00
|
|
|
parent Scope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scope) Get(name string) string {
|
|
|
|
if val, ok := s.variables[name]; ok {
|
|
|
|
return val
|
|
|
|
} else if s.parent != nil {
|
|
|
|
return s.parent.Get(name)
|
|
|
|
} else if val, ok := builtinScope[name]; ok {
|
|
|
|
return val
|
|
|
|
} else {
|
|
|
|
return "<'" + name + "' unset>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scope) Set(name, value string) {
|
|
|
|
s.variables[name] = value
|
|
|
|
}
|
|
|
|
|
2019-01-08 07:29:01 +08:00
|
|
|
func (s *scope) Call(name string, args []string) []string {
|
2015-01-31 09:27:36 +08:00
|
|
|
if f, ok := s.functions[name]; ok {
|
|
|
|
return f(args)
|
|
|
|
}
|
|
|
|
|
2019-01-08 07:29:01 +08:00
|
|
|
return []string{"<func:'" + name + "' unset>"}
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 07:29:01 +08:00
|
|
|
func (s *scope) SetFunc(name string, f func([]string) []string) {
|
2015-01-31 09:27:36 +08:00
|
|
|
s.functions[name] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewScope(parent Scope) Scope {
|
|
|
|
return &scope{
|
|
|
|
variables: make(map[string]string),
|
2019-01-08 07:29:01 +08:00
|
|
|
functions: make(map[string]func([]string) []string),
|
2015-01-31 09:27:36 +08:00
|
|
|
parent: parent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var builtinScope map[string]string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
builtinScope := make(map[string]string)
|
2018-02-21 18:10:29 +08:00
|
|
|
builtinScope[builtinDollar] = "$"
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 07:29:01 +08:00
|
|
|
func (v Variable) EvalFunction(scope Scope) ([]string, bool) {
|
2015-01-31 09:27:36 +08:00
|
|
|
f := v.Name.SplitN(" \t", 2)
|
|
|
|
if len(f) > 1 && f[0].Const() {
|
|
|
|
fname := f[0].Value(nil)
|
|
|
|
if isFunctionName(fname) {
|
|
|
|
args := f[1].Split(",")
|
|
|
|
argVals := make([]string, len(args))
|
|
|
|
for i, a := range args {
|
|
|
|
argVals[i] = a.Value(scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fname == "call" {
|
2015-04-04 07:51:45 +08:00
|
|
|
return scope.Call(argVals[0], argVals[1:]), true
|
2015-01-31 09:27:36 +08:00
|
|
|
} else {
|
2019-01-08 07:29:01 +08:00
|
|
|
return []string{"__builtin_func:" + fname + " " + strings.Join(argVals, " ")}, true
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 07:29:01 +08:00
|
|
|
return []string{""}, false
|
2015-04-04 07:51:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v Variable) Value(scope Scope) string {
|
|
|
|
if ret, ok := v.EvalFunction(scope); ok {
|
2019-01-08 07:29:01 +08:00
|
|
|
if len(ret) > 1 {
|
|
|
|
panic("Expected a single value, but instead got a list")
|
|
|
|
}
|
|
|
|
return ret[0]
|
2015-04-04 07:51:45 +08:00
|
|
|
}
|
2017-08-24 08:03:42 +08:00
|
|
|
if scope == nil {
|
|
|
|
panic("Cannot take the value of a variable in a nil scope")
|
|
|
|
}
|
2015-01-31 09:27:36 +08:00
|
|
|
return scope.Get(v.Name.Value(scope))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toVariable(ms *MakeString) (Variable, bool) {
|
|
|
|
if len(ms.Variables) == 1 && ms.Strings[0] == "" && ms.Strings[1] == "" {
|
|
|
|
return ms.Variables[0], true
|
|
|
|
}
|
|
|
|
return Variable{}, false
|
|
|
|
}
|