From 18e5e6b4f293a77a7140a0d275fd1dc2d24bed97 Mon Sep 17 00:00:00 2001 From: Kai Zhao Date: Mon, 2 Mar 2015 12:14:34 +0800 Subject: [PATCH] Change the reference variable: token_list and alloc to pointer --- libjsonnet.cpp | 6 +-- parser.cpp | 134 ++++++++++++++++++++++++------------------------- parser.h | 2 +- vm.cpp | 16 +++--- vm.h | 4 +- 5 files changed, 81 insertions(+), 81 deletions(-) diff --git a/libjsonnet.cpp b/libjsonnet.cpp index b1a245a8b..89337cf37 100644 --- a/libjsonnet.cpp +++ b/libjsonnet.cpp @@ -135,7 +135,7 @@ static char *jsonnet_evaluate_snippet_aux(JsonnetVm *vm, const char *filename, { try { Allocator alloc; - AST *expr = jsonnet_parse(alloc, filename, snippet); + AST *expr = jsonnet_parse(&alloc, filename, snippet); std::string json_str; std::map files; if (vm->debugAst) { @@ -143,12 +143,12 @@ static char *jsonnet_evaluate_snippet_aux(JsonnetVm *vm, const char *filename, } else { jsonnet_static_analysis(expr); if (multi) { - files = jsonnet_vm_execute_multi(alloc, expr, vm->extVars, vm->maxStack, + files = jsonnet_vm_execute_multi(&alloc, expr, vm->extVars, vm->maxStack, vm->gcMinObjects, vm->gcGrowthTrigger, vm->importCallback, vm->importCallbackContext, vm->stringOutput); } else { - json_str = jsonnet_vm_execute(alloc, expr, vm->extVars, vm->maxStack, + json_str = jsonnet_vm_execute(&alloc, expr, vm->extVars, vm->maxStack, vm->gcMinObjects, vm->gcGrowthTrigger, vm->importCallback, vm->importCallbackContext, vm->stringOutput); diff --git a/parser.cpp b/parser.cpp index f1bd7a787..34fede432 100644 --- a/parser.cpp +++ b/parser.cpp @@ -167,13 +167,13 @@ namespace { Token pop(void) { Token tok = peek(); - tokens.pop_front(); + tokens->pop_front(); return tok; } Token peek(void) { - Token tok = tokens.front(); + Token tok = tokens->front(); return tok; } @@ -193,12 +193,12 @@ namespace { return tok; } - std::list &tokens; - Allocator &alloc; + std::list *tokens; + Allocator *alloc; public: - Parser(std::list &tokens, Allocator &alloc) + Parser(std::list *tokens, Allocator *alloc) : tokens(tokens), alloc(alloc) { } @@ -263,7 +263,7 @@ namespace { void parseBind(Local::Binds &binds, unsigned obj_level) { Token var_id = popExpect(Token::IDENTIFIER); - auto *id = alloc.makeIdentifier(var_id.data); + auto *id = alloc->makeIdentifier(var_id.data); if (binds.find(id) != binds.end()) { throw StaticError(var_id.location, "Duplicate local var: " + var_id.data); @@ -274,7 +274,7 @@ namespace { auto params = parseIdentifierList("function parameter", obj_level); popExpect(Token::OPERATOR, "="); AST *body = parse(MAX_PRECEDENCE, obj_level); - init = alloc.make(span(var_id, body), params, body); + init = alloc->make(span(var_id, body), params, body); } else { popExpect(Token::OPERATOR, "="); init = parse(MAX_PRECEDENCE, obj_level); @@ -291,8 +291,8 @@ namespace { // Hidden variable to allow outer/top binding. if (obj_level == 0) { - const Identifier *hidden_var = alloc.makeIdentifier("$"); - let_binds[hidden_var] = alloc.make(LocationRange()); + const Identifier *hidden_var = alloc->makeIdentifier("$"); + let_binds[hidden_var] = alloc->make(LocationRange()); } bool got_comma = true; @@ -315,11 +315,11 @@ namespace { r = fields; } else { for (const auto &f : fields) { - AST *body = alloc.make(f.body->location, let_binds, f.body); + AST *body = alloc->make(f.body->location, let_binds, f.body); r.emplace_back(f.name, f.hide, body); } } - obj = alloc.make(span(tok, next), r); + obj = alloc->make(span(tok, next), r); return next; } else if (next.kind == Token::FOR) { if (fields.size() != 1) { @@ -334,7 +334,7 @@ namespace { Object::Field::Hide field_hide = fields.front().hide; AST *value = fields.front().body; if (let_binds.size() > 0) { - value = alloc.make(value->location, let_binds, value); + value = alloc->make(value->location, let_binds, value); } if (field_hide != Object::Field::INHERIT) { auto msg = "Object comprehensions cannot have hidden fields."; @@ -344,11 +344,11 @@ namespace { throw StaticError(next.location, "Unexpected comma before for."); } Token id_tok = popExpect(Token::IDENTIFIER); - const Identifier *id = alloc.makeIdentifier(id_tok.data); + const Identifier *id = alloc->makeIdentifier(id_tok.data); popExpect(Token::IN); AST *array = parse(MAX_PRECEDENCE, obj_level); Token last = popExpect(Token::BRACE_R); - obj = alloc.make(span(tok, last), field, value, id, array); + obj = alloc->make(span(tok, last), field, value, id, array); return last; } if (!got_comma) @@ -386,16 +386,16 @@ namespace { if (!literal_fields.insert(next.data).second) { throw StaticError(next.location, "Duplicate field: "+next.data); } - AST *field_expr = alloc.make(next.location, next.data); + AST *field_expr = alloc->make(next.location, next.data); AST *body = parse(MAX_PRECEDENCE, obj_level+1); if (is_method) { - body = alloc.make(body->location, params, body); + body = alloc->make(body->location, params, body); } if (plus_sugar) { - AST *f = alloc.make(plus_loc, next.data); - AST *super_f = alloc.make(plus_loc, alloc.make(LocationRange()), f); - body = alloc.make(body->location, super_f, BOP_PLUS, body); + AST *f = alloc->make(plus_loc, next.data); + AST *super_f = alloc->make(plus_loc, alloc->make(LocationRange()), f); + body = alloc->make(body->location, super_f, BOP_PLUS, body); } fields.emplace_back(field_expr, field_hide, body); } @@ -470,7 +470,7 @@ namespace { Token next = peek(); if (next.kind == Token::BRACKET_R) { pop(); - return alloc.make(span(tok, next), std::vector{}); + return alloc->make(span(tok, next), std::vector{}); } AST *first = parse(MAX_PRECEDENCE, obj_level); next = peek(); @@ -478,26 +478,26 @@ namespace { LocationRange l; pop(); Token id_token = popExpect(Token::IDENTIFIER); - const Identifier *id = alloc.makeIdentifier(id_token.data); + const Identifier *id = alloc->makeIdentifier(id_token.data); std::vector params = {id}; - AST *std = alloc.make(l, alloc.makeIdentifier("std")); - AST *map_func = alloc.make(first->location, params, first); + AST *std = alloc->make(l, alloc->makeIdentifier("std")); + AST *map_func = alloc->make(first->location, params, first); popExpect(Token::IN); AST *arr = parse(MAX_PRECEDENCE, obj_level); Token maybe_if = pop(); if (maybe_if.kind == Token::BRACKET_R) { - AST *map_str = alloc.make(l, "map"); - AST *map = alloc.make(l, std, map_str); + AST *map_str = alloc->make(l, "map"); + AST *map = alloc->make(l, std, map_str); std::vector args = {map_func, arr}; - return alloc.make(span(tok, maybe_if), map, args); + return alloc->make(span(tok, maybe_if), map, args); } else if (maybe_if.kind == Token::IF) { AST *cond = parse(MAX_PRECEDENCE, obj_level); Token last = popExpect(Token::BRACKET_R); - AST *filter_func = alloc.make(cond->location, params, cond); - AST *fmap_str = alloc.make(l, "filterMap"); - AST *fmap = alloc.make(l, std, fmap_str); + AST *filter_func = alloc->make(cond->location, params, cond); + AST *fmap_str = alloc->make(l, "filterMap"); + AST *fmap = alloc->make(l, std, fmap_str); std::vector args = {filter_func, map_func, arr}; - return alloc.make(span(tok, last), fmap, args); + return alloc->make(span(tok, last), fmap, args); } else { std::stringstream ss; ss << "Expected if or ] after for clause, got: " << maybe_if; @@ -525,7 +525,7 @@ namespace { } elements.push_back(parse(MAX_PRECEDENCE, obj_level)); } while (true); - return alloc.make(span(tok, next), elements); + return alloc->make(span(tok, next), elements); } } @@ -538,29 +538,29 @@ namespace { // Literals case Token::NUMBER: - return alloc.make(span(tok), strtod(tok.data.c_str(), nullptr)); + return alloc->make(span(tok), strtod(tok.data.c_str(), nullptr)); case Token::STRING: - return alloc.make(span(tok), tok.data); + return alloc->make(span(tok), tok.data); case Token::FALSE: - return alloc.make(span(tok), false); + return alloc->make(span(tok), false); case Token::TRUE: - return alloc.make(span(tok), true); + return alloc->make(span(tok), true); case Token::NULL_LIT: - return alloc.make(span(tok)); + return alloc->make(span(tok)); // Import case Token::IMPORT: { Token file = popExpect(Token::STRING); - return alloc.make(span(tok, file), file.data); + return alloc->make(span(tok, file), file.data); } case Token::IMPORTSTR: { Token file = popExpect(Token::STRING); - return alloc.make(span(tok, file), file.data); + return alloc->make(span(tok, file), file.data); } @@ -569,16 +569,16 @@ namespace { if (obj_level == 0) { throw StaticError(tok.location, "No top-level object found."); } - return alloc.make(span(tok), alloc.makeIdentifier("$")); + return alloc->make(span(tok), alloc->makeIdentifier("$")); case Token::IDENTIFIER: - return alloc.make(span(tok), alloc.makeIdentifier(tok.data)); + return alloc->make(span(tok), alloc->makeIdentifier(tok.data)); case Token::SELF: - return alloc.make(span(tok)); + return alloc->make(span(tok)); case Token::SUPER: - return alloc.make(span(tok)); + return alloc->make(span(tok)); } std::cerr << "INTERNAL ERROR: Unknown tok kind: " << tok.kind << std::endl; @@ -597,7 +597,7 @@ namespace { case Token::ERROR: { pop(); AST *expr = parse(MAX_PRECEDENCE, obj_level); - return alloc.make(span(begin, expr), expr); + return alloc->make(span(begin, expr), expr); } case Token::IF: { @@ -610,9 +610,9 @@ namespace { pop(); branch_false = parse(MAX_PRECEDENCE, obj_level); } else { - branch_false = alloc.make(span(begin, branch_true)); + branch_false = alloc->make(span(begin, branch_true)); } - return alloc.make(span(begin, branch_false), + return alloc->make(span(begin, branch_false), cond, branch_true, branch_false); } @@ -634,7 +634,7 @@ namespace { } params.push_back(p->id); } - return alloc.make(span(begin, body), params, body); + return alloc->make(span(begin, body), params, body); } else { std::stringstream ss; ss << "Expected ( but got " << next; @@ -656,7 +656,7 @@ namespace { if (delim.kind == Token::SEMICOLON) break; } while (true); AST *body = parse(MAX_PRECEDENCE, obj_level); - return alloc.make(span(begin, body), binds, body); + return alloc->make(span(begin, body), binds, body); } default: @@ -672,7 +672,7 @@ namespace { if (UNARY_PRECEDENCE == precedence) { Token op = pop(); AST *expr = parse(precedence, obj_level); - return alloc.make(span(op, expr), uop, expr); + return alloc->make(span(op, expr), uop, expr); } } @@ -720,32 +720,32 @@ namespace { if (op.kind == Token::BRACKET_L) { AST *index = parse(MAX_PRECEDENCE, obj_level); Token end = popExpect(Token::BRACKET_R); - lhs = alloc.make(span(begin, end), lhs, index); + lhs = alloc->make(span(begin, end), lhs, index); } else if (op.kind == Token::DOT) { Token field = popExpect(Token::IDENTIFIER); - AST *index = alloc.make(span(field), field.data); - lhs = alloc.make(span(begin, field), lhs, index); + AST *index = alloc->make(span(field), field.data); + lhs = alloc->make(span(begin, field), lhs, index); } else if (op.kind == Token::PAREN_L) { std::vector args; Token end = parseCommaList(args, Token::PAREN_R, "function argument", obj_level); - lhs = alloc.make(span(begin, end), lhs, args); + lhs = alloc->make(span(begin, end), lhs, args); } else if (op.kind == Token::BRACE_L) { AST *obj; Token end = parseObjectRemainder(obj, op, obj_level); - lhs = alloc.make(span(begin, end), lhs, BOP_PLUS, obj); + lhs = alloc->make(span(begin, end), lhs, BOP_PLUS, obj); } else if (op.data == "%") { AST *rhs = parse(precedence - 1, obj_level); LocationRange l; - AST *std = alloc.make(l, alloc.makeIdentifier("std")); - AST *mod_str = alloc.make(l, "mod"); - AST *f_mod = alloc.make(l, std, mod_str); + AST *std = alloc->make(l, alloc->makeIdentifier("std")); + AST *mod_str = alloc->make(l, "mod"); + AST *f_mod = alloc->make(l, std, mod_str); std::vector args = {lhs, rhs}; - lhs = alloc.make(span(begin, rhs), f_mod, args); + lhs = alloc->make(span(begin, rhs), f_mod, args); } else { // Logical / arithmetic binary operator. @@ -755,9 +755,9 @@ namespace { bop = BOP_MANIFEST_EQUAL; invert = true; } - lhs = alloc.make(span(begin, rhs), lhs, bop, rhs); + lhs = alloc->make(span(begin, rhs), lhs, bop, rhs); if (invert) { - lhs = alloc.make(lhs->location, UOP_NOT, lhs); + lhs = alloc->make(lhs->location, UOP_NOT, lhs); } } } @@ -804,13 +804,13 @@ BuiltinDecl jsonnet_builtin_decl(unsigned long builtin) return BuiltinDecl(); } -static AST *do_parse(Allocator &alloc, const std::string &file, const char *input) +static AST *do_parse(Allocator *alloc, const std::string &file, const char *input) { // Lex the input. auto token_list = jsonnet_lex(file, input); // Parse the input. - Parser parser(token_list, alloc); + Parser parser(&token_list, alloc); AST *expr = parser.parse(MAX_PRECEDENCE, 0); if (token_list.front().kind != Token::END_OF_FILE) { std::stringstream ss; @@ -825,7 +825,7 @@ static constexpr char STD_CODE[] = { #include "std.jsonnet.h" }; -AST *jsonnet_parse(Allocator &alloc, const std::string &file, const char *input) +AST *jsonnet_parse(Allocator *alloc, const std::string &file, const char *input) { // Parse the actual file. AST *expr = do_parse(alloc, file, input); @@ -842,14 +842,14 @@ AST *jsonnet_parse(Allocator &alloc, const std::string &file, const char *input) const auto &decl = jsonnet_builtin_decl(c); std::vector params; for (const auto &p : decl.params) - params.push_back(alloc.makeIdentifier(p)); - fields.emplace_back(alloc.make(l, decl.name), Object::Field::HIDDEN, - alloc.make(l, c, params)); + params.push_back(alloc->makeIdentifier(p)); + fields.emplace_back(alloc->make(l, decl.name), Object::Field::HIDDEN, + alloc->make(l, c, params)); } Local::Binds std_binds; - std_binds[alloc.makeIdentifier("std")] = std_obj; - AST *wrapped = alloc.make(expr->location, std_binds, expr); + std_binds[alloc->makeIdentifier("std")] = std_obj; + AST *wrapped = alloc->make(expr->location, std_binds, expr); return wrapped; } diff --git a/parser.h b/parser.h index 5089fa9d6..a37aa35eb 100644 --- a/parser.h +++ b/parser.h @@ -27,7 +27,7 @@ limitations under the License. * \param input The string to be tokenized & parsed. * \returns The parsed abstract syntax tree. */ -AST *jsonnet_parse(Allocator &alloc, const std::string &file, const char *input); +AST *jsonnet_parse(Allocator *alloc, const std::string &file, const char *input); /** Escapes a string for JSON output. */ diff --git a/vm.cpp b/vm.cpp index 841fcb316..b7512e93e 100644 --- a/vm.cpp +++ b/vm.cpp @@ -404,7 +404,7 @@ namespace { * * This is used at import time, and in a few other cases. */ - Allocator &alloc; + Allocator *alloc; /** Used to "name" thunks crated on the inside of an array. */ const Identifier *idArrayElement; @@ -718,11 +718,11 @@ namespace { * * \param loc The location range of the file to be executed. */ - Interpreter(Allocator &alloc, const StrMap &ext_vars, + Interpreter(Allocator *alloc, const StrMap &ext_vars, unsigned max_stack, double gc_min_objects, double gc_growth_trigger, JsonnetImportCallback *import_callback, void *import_callback_context) : heap(gc_min_objects, gc_growth_trigger), stack(max_stack), alloc(alloc), - idArrayElement(alloc.makeIdentifier("array_element")), externalVars(ext_vars), + idArrayElement(alloc->makeIdentifier("array_element")), externalVars(ext_vars), importCallback(import_callback), importCallbackContext(import_callback_context) { scratch = makeNull(); @@ -1805,7 +1805,7 @@ namespace { } const std::string &index_name = static_cast(scratch.v.h)->value; - auto *fid = alloc.makeIdentifier(index_name); + auto *fid = alloc->makeIdentifier(index_name); stack.pop(); ast_ = objectIndex(ast.location, obj, fid); goto recurse; @@ -1861,7 +1861,7 @@ namespace { throw makeError(ast.location, "Field name was not a string."); } const auto &fname = static_cast(scratch.v.h)->value; - const Identifier *fid = alloc.makeIdentifier(fname); + const Identifier *fid = alloc->makeIdentifier(fname); if (f.objectFields.find(fid) != f.objectFields.end()) { throw makeError(ast.location, "Duplicate field name: \"" + fname + "\""); @@ -1911,7 +1911,7 @@ namespace { throw makeError(ast.location, ss.str()); } const auto &fname = static_cast(scratch.v.h)->value; - const Identifier *fid = alloc.makeIdentifier(fname); + const Identifier *fid = alloc->makeIdentifier(fname); if (f.elements.find(fid) != f.elements.end()) { throw makeError(ast.location, "Duplicate field name: \"" + fname + "\""); @@ -2159,7 +2159,7 @@ namespace { } -std::string jsonnet_vm_execute(Allocator &alloc, const AST *ast, +std::string jsonnet_vm_execute(Allocator *alloc, const AST *ast, const StrMap &ext_vars, unsigned max_stack, double gc_min_objects, double gc_growth_trigger, @@ -2176,7 +2176,7 @@ std::string jsonnet_vm_execute(Allocator &alloc, const AST *ast, } } -StrMap jsonnet_vm_execute_multi(Allocator &alloc, const AST *ast, const StrMap &ext_vars, +StrMap jsonnet_vm_execute_multi(Allocator *alloc, const AST *ast, const StrMap &ext_vars, unsigned max_stack, double gc_min_objects, double gc_growth_trigger, JsonnetImportCallback *import_callback, void *ctx, bool string_output) diff --git a/vm.h b/vm.h index 8bc92d060..6974ad7ed 100644 --- a/vm.h +++ b/vm.h @@ -54,7 +54,7 @@ struct RuntimeError { * \throws RuntimeError reports runtime errors in the program. * \returns The JSON result in string form. */ -std::string jsonnet_vm_execute(Allocator &alloc, const AST *ast, +std::string jsonnet_vm_execute(Allocator *alloc, const AST *ast, const std::map &ext_vars, unsigned max_stack, double gc_min_objects, double gc_growth_trigger, @@ -77,7 +77,7 @@ std::string jsonnet_vm_execute(Allocator &alloc, const AST *ast, * \returns A mapping from filename to the JSON strings for that file. */ std::map jsonnet_vm_execute_multi( - Allocator &alloc, const AST *ast, const std::map &ext_vars, + Allocator *alloc, const AST *ast, const std::map &ext_vars, unsigned max_stack, double gc_min_objects, double gc_growth_trigger, JsonnetImportCallback *import_callback, void *import_callback_ctx, bool string_output);