Skip to content

Commit

Permalink
Merge f9d8f1d into efea104
Browse files Browse the repository at this point in the history
  • Loading branch information
uucidl committed Jan 31, 2016
2 parents efea104 + f9d8f1d commit 91b9c39
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -92,12 +92,12 @@ matrix:
env: BINDINGS=cpp CC=clang
before_install:
- sudo apt-get update -qq
- sudo apt-get install lcov
- sudo apt-get install -y lcov
- gem install coveralls-lcov
- if [ "$BINDINGS" != "none" ]; then sudo apt-get install -qq swig; fi
- if [ "$BINDINGS" == "perl" ]; then sudo add-apt-repository ppa:dns/irc -y; sudo apt-get update -qq; sudo apt-get install -qq swig=2.0.8-1irc1~12.04; fi
- if [ "$BINDINGS" == "python" ]; then sudo apt-get install -qq python-dev; fi
- if [ "$BINDINGS" == "dotnet" ]; then sudo add-apt-repository ppa:directhex/monoxide -y; sudo apt-get update -qq; sudo apt-get install -qq mono-devel mono-mcs nunit nunit-console; mozroots --import --sync; fi
- if [ "$BINDINGS" == "dotnet" ]; then sudo add-apt-repository ppa:directhex/monoxide -y; sudo apt-get update -qq; sudo apt-get install -y -qq mono-devel mono-mcs nunit nunit-console; mozroots --import --sync; fi
install: true
before_script:
- if [ "$BINDINGS" == "php" ]; then phpenv config-add src/bindings/php/hammer.ini; fi
Expand Down
3 changes: 1 addition & 2 deletions appveyor.yml
Expand Up @@ -14,6 +14,5 @@ build_script:
}
- call "%VS140COMNTOOLS%\..\..\VC\vcvarsall.bat" %VCVARS_PLATFORM%
- call tools\windows\build.bat
# FIXME(windows) TODO(uucidl): reactivate examples
# - call tools\windows\build_examples.bat
- call tools\windows\build_examples.bat
- exit /b 0
7 changes: 4 additions & 3 deletions examples/grammar.c
Expand Up @@ -22,11 +22,12 @@
const char *nonterminal_name(const HCFGrammar *g, const HCFChoice *nt) {
// if user_data exists and is printable:
if(nt->user_data != NULL && *(char*)(nt->user_data) > ' ' && *(char*)(nt->user_data) < 127) {
if(*(char*)(nt->user_data) != '0') {
char* user_str = (char*)(nt->user_data);
if(*user_str != '\0') {
// user_data is a non-empty string
return nt->user_data;
return user_str;
} else {
return nt->user_data+1;
return user_str+1;
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/datastructures.c
Expand Up @@ -52,14 +52,14 @@ HSlist* h_slist_copy(HSlist *slist) {
h_slist_push(ret, head->elem);
tail = ret->head;
head = head->next;
}
while (head != NULL) {
// append head item to tail in a new node
HSlistNode *node = h_arena_malloc(slist->arena, sizeof(HSlistNode));
node->elem = head->elem;
node->next = NULL;
tail = tail->next = node;
head = head->next;
while (head != NULL) {
// append head item to tail in a new node
HSlistNode *node = h_arena_malloc(slist->arena, sizeof(HSlistNode));
node->elem = head->elem;
node->next = NULL;
tail = tail->next = node;
head = head->next;
}
}
return ret;
}
Expand Down
21 changes: 19 additions & 2 deletions src/parsers/choice.c
@@ -1,6 +1,20 @@
#include <stdarg.h>
#include "parser_internal.h"

#if defined(__STDC_VERSION__) && ( \
(__STDC_VERSION__ >= 201112L && !defined(__STDC_NO_VLA__)) || \
(__STDC_VERSION__ >= 199901L) \
)
# define STACK_VLA(type,name,size) type name[size]
#else
# if defined(_MSC_VER)
# include <malloc.h> // for _alloca
# define STACK_VLA(type,name,size) type* name = _alloca(size)
# else
# error "Missing VLA implementation for this compiler"
# endif
#endif

typedef struct {
size_t len;
HParser **p_array;
Expand Down Expand Up @@ -53,11 +67,14 @@ static void desugar_choice(HAllocator *mm__, HCFStack *stk__, void *env) {

static bool choice_ctrvm(HRVMProg *prog, void* env) {
HSequence *s = (HSequence*)env;
uint16_t gotos[s->len];
// NOTE(uucidl): stack allocation since this backend uses
// setjmp/longjmp for error handling.
STACK_VLA(uint16_t, gotos, s->len);
for (size_t i=0; i<s->len; ++i) {
uint16_t insn = h_rvm_insert_insn(prog, RVM_FORK, 0);
if (!h_compile_regex(prog, s->p_array[i]))
if (!h_compile_regex(prog, s->p_array[i])) {
return false;
}
gotos[i] = h_rvm_insert_insn(prog, RVM_GOTO, 65535);
h_rvm_patch_arg(prog, insn, h_rvm_get_ip(prog));
}
Expand Down
4 changes: 3 additions & 1 deletion src/parsers/nothing.c
@@ -1,6 +1,8 @@
#include "parser_internal.h"

static HParseResult* parse_nothing() {
static HParseResult* parse_nothing(void* x,HParseState* y) {
(void)(x);
(void)(y);
// not a mistake, this parser always fails
return NULL;
}
Expand Down
10 changes: 8 additions & 2 deletions src/registry.c
Expand Up @@ -15,10 +15,16 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <search.h>
#include <stdlib.h>
#include "hammer.h"
#include "internal.h"
#include "tsearch.h"

#if defined(_MSC_VER)
#define h_strdup _strdup
#else
#define h_strdup strdup
#endif

typedef struct Entry_ {
const char* name;
Expand Down Expand Up @@ -58,7 +64,7 @@ HTokenType h_allocate_token_type(const char* name) {
return probe->value;
} else {
// new value
probe->name = strdup(probe->name); // drop ownership of name
probe->name = h_strdup(probe->name); // drop ownership of name
probe->value = tt_next++;
if ((probe->value - TT_START) >= tt_by_id_sz) {
if (tt_by_id_sz == 0) {
Expand Down
15 changes: 15 additions & 0 deletions src/search.h
@@ -0,0 +1,15 @@
#if defined(_MSC_VER)
/* find or insert datum into search tree */
void *tsearch(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *));

/* delete node with given key */
void * tdelete(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *));

/* Walk the nodes of a tree */
void twalk(const void *vroot, void (*action)(const void *, VISIT, int));

#else
#include <search.h>
#endif
141 changes: 141 additions & 0 deletions src/tsearch.c
@@ -0,0 +1,141 @@
/* $OpenBSD: tsearch.c,v 1.9 2015/08/20 21:49:29 deraadt Exp $ */

/*
* Tree search generalized from Knuth (6.2.2) Algorithm T just like
* the AT&T man page says.
*
* The node_t structure is for internal use only
*
* Written by reading the System V Interface Definition, not the code.
*
* Totally public domain.
*/

#include <stdlib.h>
#include "tsearch.h"

typedef struct node_t {
char *key;
struct node_t *left, *right;
} node;

/* find or insert datum into search tree */
void *
tsearch(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *))
{
node *q;
char *key = (char *)vkey;
node **rootp = (node **)vrootp;

if (rootp == (struct node_t **)0)
return ((void *)0);
while (*rootp != (struct node_t *)0) { /* Knuth's T1: */
int r;

if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
return ((void *)*rootp); /* we found it! */
rootp = (r < 0) ?
&(*rootp)->left : /* T3: follow left branch */
&(*rootp)->right; /* T4: follow right branch */
}
q = malloc(sizeof(node)); /* T5: key not found */
if (q != (struct node_t *)0) { /* make new node */
*rootp = q; /* link new node to old */
q->key = key; /* initialize new node */
q->left = q->right = (struct node_t *)0;
}
return ((void *)q);
}

/* delete node with given key */
void *
tdelete(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *))
{
node **rootp = (node **)vrootp;
char *key = (char *)vkey;
node *p = (node *)1;
node *q;
node *r;
int cmp;

if (rootp == (struct node_t **)0 || *rootp == (struct node_t *)0)
return ((struct node_t *)0);
while ((cmp = (*compar)(key, (*rootp)->key)) != 0) {
p = *rootp;
rootp = (cmp < 0) ?
&(*rootp)->left : /* follow left branch */
&(*rootp)->right; /* follow right branch */
if (*rootp == (struct node_t *)0)
return ((void *)0); /* key not found */
}
r = (*rootp)->right; /* D1: */
if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */
q = r;
else if (r != (struct node_t *)0) { /* Right link is null? */
if (r->left == (struct node_t *)0) { /* D2: Find successor */
r->left = q;
q = r;
} else { /* D3: Find (struct node_t *)0 link */
for (q = r->left; q->left != (struct node_t *)0; q = r->left)
r = q;
r->left = q->right;
q->left = (*rootp)->left;
q->right = (*rootp)->right;
}
}
free((struct node_t *) *rootp); /* D4: Free node */
*rootp = q; /* link parent to new node */
return(p);
}

/* Walk the nodes of a tree */
static void
trecurse(node *root, void (*action)(const void *, VISIT, int), int level)
{
if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
(*action)(root, leaf, level);
else {
(*action)(root, preorder, level);
if (root->left != (struct node_t *)0)
trecurse(root->left, action, level + 1);
(*action)(root, postorder, level);
if (root->right != (struct node_t *)0)
trecurse(root->right, action, level + 1);
(*action)(root, endorder, level);
}
}

/* Walk the nodes of a tree */
void
twalk(const void *vroot, void (*action)(const void *, VISIT, int))
{
node *root = (node *)vroot;

if (root != (node *)0 && action != (void (*)(const void *, VISIT, int))0)
trecurse(root, action, 0);
}

/* $OpenBSD: tfind.c,v 1.6 2014/03/16 18:38:30 guenther Exp $ */

/* find a node, or return 0 */
void *
tfind(const void *vkey, void * const *vrootp,
int (*compar)(const void *, const void *))
{
char *key = (char *)vkey;
node **rootp = (node **)vrootp;

if (rootp == (struct node_t **)0)
return ((struct node_t *)0);
while (*rootp != (struct node_t *)0) { /* T1: */
int r;
if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
return (*rootp); /* key found */
rootp = (r < 0) ?
&(*rootp)->left : /* T3: follow left branch */
&(*rootp)->right; /* T4: follow right branch */
}
return (node *)0;
}
26 changes: 26 additions & 0 deletions src/tsearch.h
@@ -0,0 +1,26 @@
#ifndef HAMMER_TSEARCH__H
#define HAMMER_TSEARCH__H

#if defined(_MSC_VER)
typedef enum { preorder, postorder, endorder, leaf } VISIT;

/* find or insert datum into search tree */
void *tsearch(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *));

/* delete node with given key */
void * tdelete(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *));

/* Walk the nodes of a tree */
void twalk(const void *vroot, void (*action)(const void *, VISIT, int));

/* find a node, or return 0 */
void *tfind(const void *vkey, void * const *vrootp,
int (*compar)(const void *, const void *));

#else
#include <search.h>
#endif

#endif /* HAMMER_TSEARCH__H */
10 changes: 10 additions & 0 deletions tools/windows/clvars.bat
Expand Up @@ -7,6 +7,16 @@ set WARNINGS=-W4 -Wall -WX
REM c4464 (relative include path contains '..')
set WARNINGS=%WARNINGS% -wd4464

REM c4189 (local variable is initialized but not referenced)
set WARNINGS=%WARNINGS% -wd4189

REM c4018/c4388 (signed/unsigned mismatch)
REM basically useless. Complains about obviously correct code like:
REM uint8_t x = 60;
REM size_t i = 9;
REM i < x/8
set WARNINGS=%WARNINGS% -wd4018 -wd4388

REM c4457 (declaration shadowing function parameter)
REM FIXME(windows) TODO(uucidl): remove occurence of c4457 and reactivate
REM FIXME(windows) TODO(uucidl): remove occurence of c4456 and reactivate
Expand Down
9 changes: 8 additions & 1 deletion tools/windows/hammer_lib_src_list
Expand Up @@ -3,18 +3,24 @@ allocator.c
benchmark.c
bitreader.c
bitwriter.c
cfgrammar.c
cfgrammar.c
datastructures.c
desugar.c
glue.c
hammer.c
pprint.c
registry.c
system_allocator.c
tsearch.c
parsers/action.c
parsers/and.c
parsers/attr_bool.c
parsers/bind.c
parsers/bits.c
parsers/butnot.c
parsers/ch.c
parsers/charset.c
parsers/choice.c
parsers/difference.c
parsers/end.c
parsers/endianness.c
Expand All @@ -25,6 +31,7 @@ parsers/indirect.c
parsers/int_range.c
parsers/many.c
parsers/not.c
parsers/nothing.c
parsers/optional.c
parsers/permutation.c
parsers/sequence.c
Expand Down

0 comments on commit 91b9c39

Please sign in to comment.