-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameterized_goals.awk
More file actions
142 lines (119 loc) · 3.9 KB
/
Copy pathparameterized_goals.awk
File metadata and controls
142 lines (119 loc) · 3.9 KB
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
BEGIN {
Goal["do_work"]
Goal["document_processed"]
Goal["document_downloaded"]
GoalParamsCnt["document_processed"] = 1
GoalParams ["document_processed",0] = "F"
GoalParamsCnt["document_downloaded"] = 1
GoalParams ["document_downloaded",0] = "F1"
DependencyCnt ["do_work"] = 2
Dependency ["do_work",0] = "document_processed"
DependencyArgsCnt ["do_work",0] = 1
DependencyArgs ["do_work",0,0] = "file1"
DependencyArgsType["do_work",0,0] = "string"
Dependency ["do_work",1] = "document_processed"
DependencyArgsCnt ["do_work",1] = 1
DependencyArgs ["do_work",1,0] = "file 2"
DependencyArgsType["do_work",1,0] = "string"
DependencyCnt ["document_processed"] = 2
Dependency ["document_processed",0] = "document_downloaded"
DependencyArgsCnt ["document_processed",0] = 1
DependencyArgs ["document_processed",0,0] = "F"
# DependencyArgs ["document_processed",0,0] = "F7" # check wrong arg
DependencyArgsType["document_processed",0,0] = "var"
Dependency ["document_processed",1] = "document_downloaded"
DependencyArgsCnt ["document_processed",1] = 1
DependencyArgs ["document_processed",1,0] = "file333"
DependencyArgsType["document_processed",1,0] = "string"
# === what we have ===
#
# - document_processed @params F
# - document_downloaded @args F
# - document_downloaded @args 'file333'
#
# - document_downloaded @params F1
#
# - do_work
# - document_processed @args 'file1'
# - document_processed @args 'file 2'
#
# === what we need ===
#
# - document_processed@file1
# - document_downloaded@file1
# - document_processed@'file 2'
# - document_downloaded@'file 2'
# - do_work
# - document_processed@file1
# - document_downloaded@file1
# - document_downloaded@file3
# - document_processed@'file 2'
# - document_downloaded@'file 2'
# - document_downloaded@file3
print "BEFORE:"
printDepsTree("do_work")
instantiate("do_work")
print "AFTER:"
printDepsTree("do_work")
}
function panic(s){ print s; exit 1 }
function indent(ind) {
printf "%" ind*2 "s", ""
}
function printDepsTree(goal,ind, i) {
if (!(goal in Goal)) { panic("unknown goal: " goal) }
indent(ind)
print quote2(goal)
for (i=0; i < DependencyCnt[goal]; i++) {
printDepsTree(Dependency[goal,i],ind+1)
}
}
function renderArgs(args, s,k) {
s = ""
for (k in args) {
s = s k "=>" args[k] " "
}
return s
}
#
# args: { F => "file1" }
#
function instantiate(goal,args,newArgs, i,j,depArg,depArgType,dep,goalNameInstantiated,argsCnt) { # -> goalNameInstantiated
# print ">instantiating " goal " { " renderArgs(args) "} ..."
if (!(goal in Goal)) { panic("unknown goal: " goal) }
Goal[goalNameInstantiated = instantiateGoalName(goal, args)]
DependencyCnt[goalNameInstantiated] = DependencyCnt[goal]
for (i=0; i < DependencyCnt[goal]; i++) {
dep = Dependency[goal,i]
if ((argsCnt = DependencyArgsCnt[goal,i]) != GoalParamsCnt[dep]) { panic("wrong args count") }
for (j=0; j < argsCnt; j++) {
depArg = DependencyArgs [goal,i,j]
depArgType = DependencyArgsType[goal,i,j]
newArgs[GoalParams[dep,j]] = \
depArgType == "string" ? \
depArg : \
depArgType == "var" ? \
(depArg in args ? args[depArg] : panic("wrong arg " depArg)) : \
panic("wrong depArgType: " depArgType)
}
Dependency[goalNameInstantiated,i] = instantiate(dep,newArgs)
}
return goalNameInstantiated
}
function instantiateGoalName(goal, args, i,res,cnt){
if ((cnt = GoalParamsCnt[goal]) == 0) { return goal }
res = goal
for (i=0; i < cnt; i++) {
res = res "@" args[GoalParams[goal,i]]
}
# print "@@ " res
return res
}
function quote2(s,force) {
if (index(s,"'")) {
gsub(/\\/,"\\\\",s)
gsub(/'/,"\\'",s)
return "$'" s "'"
} else
return force || s ~ /[^a-zA-Z0-9.,@_\/=+-]/ ? "'" s "'" : s
}