aboutsummaryrefslogtreecommitdiff
path: root/ast.go
blob: f30668aa1335c361a5a581b47af3ace326e913ec (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2015 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.

package kati

import (
	"bytes"
	"fmt"
	"strings"
)

type ast interface {
	eval(*Evaluator)
	show()
}

type astBase struct {
	filename string
	lineno   int
}

type assignAST struct {
	astBase
	lhs Value
	rhs Value
	op  string
	opt string // "override", "export"
}

func (ast *assignAST) eval(ev *Evaluator) {
	ev.evalAssign(ast)
}

func (ast *assignAST) evalRHS(ev *Evaluator, lhs string) Var {
	origin := "file"
	if ast.filename == bootstrapMakefileName {
		origin = "default"
	}
	if ast.opt == "override" {
		origin = "override"
	}
	// TODO(ukai): handle ast.opt == "export"
	switch ast.op {
	case ":=":
		switch v := ast.rhs.(type) {
		case literal:
			return &simpleVar{value: v.String(), origin: origin}
		case tmpval:
			return &simpleVar{value: v.String(), origin: origin}
		default:
			var buf bytes.Buffer
			v.Eval(&buf, ev)
			return &simpleVar{value: buf.String(), origin: origin}
		}
	case "=":
		return &recursiveVar{expr: ast.rhs, origin: origin}
	case "+=":
		prev := ev.LookupVarInCurrentScope(lhs)
		if !prev.IsDefined() {
			return &recursiveVar{expr: ast.rhs, origin: origin}
		}
		return prev.AppendVar(ev, ast.rhs)
	case "?=":
		prev := ev.LookupVarInCurrentScope(lhs)
		if prev.IsDefined() {
			return prev
		}
		return &recursiveVar{expr: ast.rhs, origin: origin}
	default:
		panic(fmt.Sprintf("unknown assign op: %q", ast.op))
	}
}

func (ast *assignAST) show() {
	Logf("%s %s %s %q", ast.opt, ast.lhs, ast.op, ast.rhs)
}

// maybeRuleAST is an ast for rule line.
// Note we cannot be sure what this is, until all variables in |expr|
// are expanded.
type maybeRuleAST struct {
	astBase
	expr      Value
	term      byte // Either ':', '=', or 0
	afterTerm []byte
}

func (ast *maybeRuleAST) eval(ev *Evaluator) {
	ev.evalMaybeRule(ast)
}

func (ast *maybeRuleAST) show() {
	Logf("%s", ast.expr)
}

type commandAST struct {
	astBase
	cmd string
}

func (ast *commandAST) eval(ev *Evaluator) {
	ev.evalCommand(ast)
}

func (ast *commandAST) show() {
	Logf("\t%s", strings.Replace(ast.cmd, "\n", `\n`, -1))
}

type includeAST struct {
	astBase
	expr string
	op   string
}

func (ast *includeAST) eval(ev *Evaluator) {
	ev.evalInclude(ast)
}

func (ast *includeAST) show() {
	Logf("include %s", ast.expr)
}

type ifAST struct {
	astBase
	op         string
	lhs        Value
	rhs        Value // Empty if |op| is ifdef or ifndef.
	trueStmts  []ast
	falseStmts []ast
}

func (ast *ifAST) eval(ev *Evaluator) {
	ev.evalIf(ast)
}

func (ast *ifAST) show() {
	// TODO
	Logf("if")
}

type exportAST struct {
	astBase
	expr   []byte
	export bool
}

func (ast *exportAST) eval(ev *Evaluator) {
	ev.evalExport(ast)
}

func (ast *exportAST) show() {
	// TODO
	Logf("export")
}