* Copyright (C) 2012, 2013
* Wolfgang Bumiller
* Dale Weiler
- *
+ *
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
#define PARSER_HT_SIZE 128
#define TYPEDEF_HT_SIZE 16
-typedef struct {
+typedef struct parser_s {
lex_file *lex;
int tok;
ast_value **imm_vector;
size_t translated;
+ ht ht_imm_string;
+
/* must be deleted first, they reference immediates and values */
ast_value **accessors;
static ast_value* parser_const_string(parser_t *parser, const char *str, bool dotranslate)
{
- size_t i;
+ size_t hash = util_hthash(parser->ht_imm_string, str);
ast_value *out;
+ if ( (out = util_htgeth(parser->ht_imm_string, str, hash)) ) {
+ if (dotranslate && out->name[0] == '#') {
+ char name[32];
+ snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
+ ast_value_set_name(out, name);
+ }
+ return out;
+ }
+ /*
for (i = 0; i < vec_size(parser->imm_string); ++i) {
if (!strcmp(parser->imm_string[i]->constval.vstring, str))
return parser->imm_string[i];
}
+ */
if (dotranslate) {
char name[32];
snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
out->hasvalue = true;
out->constval.vstring = parser_strdup(str);
vec_push(parser->imm_string, out);
+ util_htseth(parser->ht_imm_string, str, hash, out);
return out;
}
} else {
var = intrin_func(parser, parser_tokval(parser));
}
-
+
if (!var) {
char *correct = NULL;
size_t i;
on_error:
if (argcounter)
mem_d(argcounter);
+ if (varparam)
+ ast_delete(varparam);
ast_delete(var);
for (i = 0; i < vec_size(params); ++i)
ast_delete(params[i]);
bool cleanvar = true;
bool wasarray = false;
- ast_member *me[3];
+ ast_member *me[3] = { NULL, NULL, NULL };
if (!localblock && is_static)
parseerror(parser, "`static` qualifier is not supported in global scope");
/*
* store the vstring back to var for alias and
* deprecation messages.
- */
+ */
if (var->expression.flags & AST_FLAG_DEPRECATED ||
var->expression.flags & AST_FLAG_ALIAS)
var->desc = vstring;
ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
if (!parser_check_qualifiers(parser, var, proto)) {
retval = false;
- if (proto->desc)
+ if (proto->desc)
mem_d(proto->desc);
proto = NULL;
goto cleanup;
/*
* add alias to aliases table and to corrector
* so corrections can apply for aliases as well.
- */
+ */
util_htset(parser->aliases, var->name, find);
/*
* add to corrector so corrections can work
* even for aliases too.
- */
+ */
correct_add (
vec_last(parser->correct_variables),
&vec_last(parser->correct_variables_score),
/*
* add to corrector so corrections can work
* even for aliases too.
- */
+ */
correct_add (
vec_last(parser->correct_variables),
&vec_last(parser->correct_variables_score),
code_crc = crc;
}
-static parser_t *parser;
-
-bool parser_init()
+parser_t *parser_create()
{
+ parser_t *parser;
lex_ctx empty_ctx;
size_t i;
parser = (parser_t*)mem_a(sizeof(parser_t));
if (!parser)
- return false;
+ return NULL;
memset(parser, 0, sizeof(*parser));
if (!parser->assign_op) {
printf("internal error: initializing parser: failed to find assign operator\n");
mem_d(parser);
- return false;
+ return NULL;
}
vec_push(parser->variables, parser->htfields = util_htnew(PARSER_HT_SIZE));
parser->aliases = util_htnew(PARSER_HT_SIZE);
+ parser->ht_imm_string = util_htnew(512);
+
/* corrector */
vec_push(parser->correct_variables, correct_trie_new());
vec_push(parser->correct_variables_score, NULL);
} else {
parser->reserved_version = NULL;
}
- return true;
+
+ return parser;
}
-bool parser_compile()
+bool parser_compile(parser_t *parser)
{
/* initial lexer/parser state */
parser->lex->flags.noops = true;
return !compile_errors;
}
-bool parser_compile_file(const char *filename)
+bool parser_compile_file(parser_t *parser, const char *filename)
{
parser->lex = lex_open(filename);
if (!parser->lex) {
con_err("failed to open file \"%s\"\n", filename);
return false;
}
- return parser_compile();
+ return parser_compile(parser);
}
-bool parser_compile_string(const char *name, const char *str, size_t len)
+bool parser_compile_string(parser_t *parser, const char *name, const char *str, size_t len)
{
parser->lex = lex_open_string(str, len, name);
if (!parser->lex) {
con_err("failed to create lexer for string \"%s\"\n", name);
return false;
}
- return parser_compile();
+ return parser_compile(parser);
}
-void parser_cleanup()
+void parser_cleanup(parser_t *parser)
{
size_t i;
for (i = 0; i < vec_size(parser->accessors); ++i) {
vec_free(parser->functions);
vec_free(parser->imm_vector);
vec_free(parser->imm_string);
+ util_htdel(parser->ht_imm_string);
vec_free(parser->imm_float);
vec_free(parser->globals);
vec_free(parser->fields);
util_htdel(parser->aliases);
- intrin_intrinsics_destroy();
+ intrin_intrinsics_destroy(parser);
mem_d(parser);
}
-bool parser_finish(const char *output)
+bool parser_finish(parser_t *parser, const char *output)
{
size_t i;
ir_builder *ir;