This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
forked from sass/libsass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bind.cpp
160 lines (151 loc) · 5.69 KB
/
bind.cpp
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
#include "bind.hpp"
#include "ast.hpp"
#include "context.hpp"
#include "eval.hpp"
#include <map>
#include <iostream>
#include <sstream>
#include "to_string.hpp"
namespace Sass {
using namespace std;
void bind(string callee, Parameters* ps, Arguments* as, Context& ctx, Env* env, Eval* eval)
{
map<string, Parameter*> param_map;
// Set up a map to ensure named arguments refer to actual parameters. Also
// eval each default value left-to-right, wrt env, populating env as we go.
for (size_t i = 0, L = ps->length(); i < L; ++i) {
Parameter* p = (*ps)[i];
param_map[p->name()] = p;
// if (p->default_value()) {
// env->current_frame()[p->name()] = p->default_value()->perform(eval->with(env));
// }
}
// plug in all args; if we have leftover params, deal with it later
size_t ip = 0, LP = ps->length();
size_t ia = 0, LA = as->length();
while (ia < LA) {
if (ip >= LP) {
stringstream msg;
msg << callee << " only takes " << LP << " arguments; "
<< "given " << LA;
error(msg.str(), as->path(), as->line());
}
Parameter* p = (*ps)[ip];
Argument* a = (*as)[ia];
if (a->is_rest_argument() && p->is_rest_parameter()) {
// rest param and rest arg -- just add one to the other
if (env->current_frame_has(p->name())) {
*static_cast<List*>(env->current_frame()[p->name()])
+= static_cast<List*>(a->value());
}
else {
env->current_frame()[p->name()] = a->value();
}
++ia;
++ip;
}
else if (p->is_rest_parameter()) {
List* arglist = 0;
if (!env->current_frame_has(p->name())) {
arglist = new (ctx.mem) List(p->path(),
p->line(),
0,
List::COMMA,
true);
env->current_frame()[p->name()] = arglist;
}
else {
arglist = static_cast<List*>(env->current_frame()[p->name()]);
}
*arglist << a->value(); // TODO: named args going into rest-param?
++ia;
}
else if (a->is_rest_argument()) {
// normal param and rest arg
if (env->current_frame_has(p->name())) {
stringstream msg;
msg << "parameter " << p->name()
<< " provided more than once in call to " << callee;
error(msg.str(), a->path(), a->line());
}
List* arglist = static_cast<List*>(a->value());
// if it's the last param, move the whole arglist into it
if (ip == LP-1) {
env->current_frame()[p->name()] = arglist;
++ia;
}
// otherwise move one of the rest args into the param and loop
else {
env->current_frame()[p->name()] = (*arglist)[0];
arglist->elements().erase(arglist->elements().begin());
}
++ip;
}
else if (a->name().empty()) {
// ordinal arg -- bind it to the next param
env->current_frame()[p->name()] = a->value();
++ip;
++ia;
}
else {
// named arg -- bind it to the appropriately named param
if (!param_map.count(a->name())) {
stringstream msg;
msg << callee << " has no parameter named " << a->name();
error(msg.str(), a->path(), a->line());
}
if (param_map[a->name()]->is_rest_parameter()) {
stringstream msg;
msg << "argument " << a->name() << " of " << callee
<< "cannot be used as named argument";
error(msg.str(), a->path(), a->line());
}
if (env->current_frame_has(a->name())) {
stringstream msg;
msg << "parameter " << p->name()
<< "provided more than once in call to " << callee;
error(msg.str(), a->path(), a->line());
}
env->current_frame()[a->name()] = a->value();
++ia;
}
}
// If we make it here, we're out of args but may have leftover params.
// That's only okay if they have default values, or were already bound by
// named arguments, or if it's a single rest-param.
for (size_t i = ip; i < LP; ++i) {
To_String to_string;
Parameter* leftover = (*ps)[i];
// cerr << "env for default params:" << endl;
// env->print();
// cerr << "********" << endl;
if (!env->current_frame_has(leftover->name())) {
if (leftover->is_rest_parameter()) {
env->current_frame()[leftover->name()] = new (ctx.mem) List(leftover->path(),
leftover->line(),
0,
List::COMMA,
true);
}
else if (leftover->default_value()) {
// make sure to eval the default value in the env that we've been populating
Env* old_env = eval->env;
Backtrace* old_bt = eval->backtrace;
Expression* dv = leftover->default_value()->perform(eval->with(env, eval->backtrace));
eval->env = old_env;
eval->backtrace = old_bt;
// dv->perform(&to_string);
env->current_frame()[leftover->name()] = dv;
}
else {
// param is unbound and has no default value -- error
stringstream msg;
msg << "required parameter " << leftover->name()
<< " is missing in call to " << callee;
error(msg.str(), as->path(), as->line());
}
}
}
return;
}
}