Skip to content

Commit

Permalink
more cleanup to the library.
Browse files Browse the repository at this point in the history
  • Loading branch information
Navaneeth committed Nov 17, 2010
1 parent a073eb7 commit f59e072
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 203 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ list (APPEND VARNAM_SOURCES
parser.c
vstgen.c
tl.c
varnam.c
)

# Build foreign functions
Expand Down
58 changes: 21 additions & 37 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

#include <string.h>
#include "util.h"
#include "module.h"
#include "sexpr/sexp.h"
#include "parser.h"
#include "trie.h"
#include "varnam.h"
#include "varnam-result-codes.h"

const char compile_usage[] =
"compile: varnam compile [--help] <scheme-file> [output_directory]\n";
Expand Down Expand Up @@ -79,37 +80,30 @@ static void handle_error(struct parser_result *pr)
varnam_error(":: %d errors. Compilation aborted.", error_count);
}

static int compile(struct compile_parameters *params)
static int compile_scheme_file(varnam *handle, const char *scheme_file, const char *output_directory)
{
char output_file_path[MAX_PATH_LENGTH], *error_message = NULL;
struct parser_result *pr = NULL;
int status;

struct path_info *pinfo = splitpath(params->scheme_file);
const char *directory = params->output_directory ? params->output_directory : pinfo->directory;
struct path_info *pinfo = splitpath(scheme_file);
const char *directory = output_directory ? output_directory : pinfo->directory;
const char *extension = ".vst";

if(pinfo->filename == 0 || strcmp(pinfo->filename, "") == 0) {
varnam_error("compile: invalid scheme file specified. aborting.");
varnam_info(compile_usage);
return VARNAM_ERROR;
}

strncpy(output_file_path, directory, MAX_PATH_LENGTH);
strcat(output_file_path, pinfo->filename);
strcat(output_file_path, extension);

varnam_info(":: Varnam Scheme File Compiler");
varnam_info(":: Copyright (C) Navaneeth.K.N");
varnam_info("::");
varnam_info(":: Compiling scheme file : %s", params->scheme_file);
varnam_info(":: Output will be written to : %s", output_file_path);
varnam_info("::");

if(parser_init(params->scheme_file) != VARNAM_OK) {
varnam_error(":: Compilation failed for '%s'", params->scheme_file);
status = parser_init(scheme_file);
if(status != VARNAM_SUCCESS) {
varnam_error(":: Compilation failed for '%s'", scheme_file);
xfree( pinfo );
return VARNAM_ERROR;
return status;
}

pr = parser_parse();
Expand All @@ -121,39 +115,29 @@ static int compile(struct compile_parameters *params)
}

status = varnam_generate_symbols(output_file_path, pr->result, &error_message);
if(status != VARNAM_OK) {
varnam_error(":: Compilation failed for %s", params->scheme_file);
if(status != VARNAM_SUCCESS) {
varnam_error(":: Compilation failed for %s", scheme_file);
xfree( pinfo );
parser_destroy( pr );
return VARNAM_ERROR;
return status;
}

varnam_info("::");
varnam_info(":: Successfully compiled '%s'", params->scheme_file);
varnam_info(":: Successfully compiled '%s'", scheme_file);
varnam_info(":: Created %s", output_file_path);

xfree( pinfo );
parser_destroy( pr );

return VARNAM_OK;
return VARNAM_SUCCESS;
}

int compile_scheme_file(int argc, char **argv)
int varnam_compile(varnam *handle, const char *scheme_file, const char *output_directory)
{
struct compile_parameters params;
if(argc == 0) {
varnam_error(":: compile: expected scheme file but found none.\n%s", compile_usage);
return 1;
}
else if(strcmp(argv[0], "--help") == 0) {
varnam_info(compile_usage);
return 1;
}

params.scheme_file = argv[0];
params.output_directory = 0;
if(argc >= 2) {
params.output_directory = argv[1];
}
return compile(&params) == VARNAM_OK ? 0 : 1;
if(handle == NULL || scheme_file == NULL)
return VARNAM_MISUSE;

return compile_scheme_file(handle, scheme_file, output_directory);
}


5 changes: 3 additions & 2 deletions lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "util.h"
#include "sexpr/sexp.h"
#include "lex.h"
#include "varnam-result-codes.h"

#define PAGE_SIZE 4096
#define READ_ONLY_MODE "r"
Expand Down Expand Up @@ -180,7 +181,7 @@ static int handle_null_expression()
return VARNAM_ERROR;
}

return VARNAM_OK;
return VARNAM_SUCCESS;
}

lex_statuscodes lex_status()
Expand Down Expand Up @@ -209,7 +210,7 @@ int lex_init(const char *filename)
change_status(LEX_ERRORED, "error opening input file");
return VARNAM_ERROR;
}
return VARNAM_OK;
return VARNAM_SUCCESS;
}

/*
Expand Down
31 changes: 16 additions & 15 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "trie.h"
#include "sexpr/sexp.h"
#include "parser.h"
#include "varnam-result-codes.h"
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -191,7 +192,7 @@ static int install_symbol(struct symbol *sym)
else {
return VARNAM_ERROR;
}
return VARNAM_OK;
return VARNAM_SUCCESS;
}

static struct symbol *make_symbol(const char *name,
Expand Down Expand Up @@ -224,7 +225,7 @@ static int argcount_ok(struct execution_context *context,
"'%s' expects %d arguments but found %d", function_name, expected, actual);
return VARNAM_ERROR;
}
return VARNAM_OK;
return VARNAM_SUCCESS;
}

static struct token *create_token(const char *pattern,
Expand Down Expand Up @@ -257,7 +258,7 @@ static int pattern_valid(struct execution_context *context,
"Empty patterns are not allowed in function '%s", function_name);
return VARNAM_ERROR;
}
return VARNAM_OK;
return VARNAM_SUCCESS;
}

static int value_valid(struct execution_context *context,
Expand All @@ -270,20 +271,20 @@ static int value_valid(struct execution_context *context,
"Empty values are not allowed in function '%s", function_name);
return VARNAM_ERROR;
}
return VARNAM_OK;
return VARNAM_SUCCESS;
}

static int symbol_length_ok(struct execution_context *context, const char *sym)
{
if(sym == NULL) return VARNAM_OK;
if(sym == NULL) return VARNAM_SUCCESS;

if(strlen(sym) >= PARSER_SYMBOL_MAX) {
snprintf(context->error_message,
PARSER_ERROR_MESSAGE_MAX,
"'%s' exceeds allowed symbol size %d", sym, PARSER_SYMBOL_MAX);
return VARNAM_ERROR;
}
return VARNAM_OK;
return VARNAM_SUCCESS;
}

static const char *builtin_set(struct execution_context *context, unsigned int argcount)
Expand Down Expand Up @@ -560,16 +561,16 @@ static int eval_exp_recursive(struct execution_context *context, sexp_t *exp)
}

result = s->handler(context, exp, s);
if(result != VARNAM_OK) {
return VARNAM_ERROR;
if(result != VARNAM_SUCCESS) {
return result;
}

/**
* that was a successful execution! removing the expression in the stack as it doesn't
* make sense to keep it there
**/
pop_exp( context );
return VARNAM_OK;
return VARNAM_SUCCESS;
}

static int eval_function(struct execution_context *context,
Expand Down Expand Up @@ -662,7 +663,7 @@ static int eval_function(struct execution_context *context,
**/
push_arg(context, result);
}
return VARNAM_OK;
return VARNAM_SUCCESS;
}
else {
return VARNAM_ERROR;
Expand Down Expand Up @@ -693,7 +694,7 @@ static void eval_exp(struct parser_result *res, sexp_t *exp)
context.result = res->result;

status = eval_exp_recursive(&context, exp);
if(status != VARNAM_OK) {
if(status != VARNAM_SUCCESS) {
err = (struct parser_error *) xmalloc(sizeof (struct parser_error));
assert(err);

Expand Down Expand Up @@ -747,15 +748,15 @@ struct parser_result *parser_parse()
return result;
}

unsigned int parser_init(const char *filename)
int parser_init(const char *filename)
{
struct symbol *set, *vo, *co, *cc, *nu, *sy, *ot;
int ls;

ls = lex_init(filename);
if(ls != VARNAM_OK) {
if(ls != VARNAM_SUCCESS) {
varnam_error("%s\n", lex_message());
return VARNAM_ERROR;
return ls;
}

memset(&st[0], 0, sizeof(st));
Expand Down Expand Up @@ -798,7 +799,7 @@ unsigned int parser_init(const char *filename)
ot->handler = &eval_function;
install_symbol(ot);

return VARNAM_OK;
return VARNAM_SUCCESS;
}

void parser_destroy(struct parser_result *pr)
Expand Down
2 changes: 1 addition & 1 deletion parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct parser_result {
struct trie *result;
};

unsigned int parser_init(const char *filename);
int parser_init(const char *filename);
struct parser_result *parser_parse();
void parser_destroy(struct parser_result *result);

Expand Down
57 changes: 11 additions & 46 deletions tl.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "util.h"
#include "parser.h"
#include "foreign/sqlite3.h"
#include "varnam.h"
#include "varnam-result-codes.h"
#include <string.h>

const char tl_usage[] =
Expand Down Expand Up @@ -77,7 +79,7 @@ static int can_find_solution(sqlite3 *db, struct token *last, const char *lookup
rc = sqlite3_step( stmt );
if( rc == SQLITE_ROW ) {
if( sqlite3_column_int( stmt, 0 ) > 0 ) {
result = VARNAM_OK;
result = VARNAM_SUCCESS;
}
}
}
Expand Down Expand Up @@ -129,57 +131,20 @@ static int tokenize(sqlite3 *db,
if( strlen( remaining ) > 0 )
return tokenize( db, remaining, string );

return VARNAM_OK;
return VARNAM_SUCCESS;
}

struct strbuf *transliterate(const char *scheme_file, const char *input)
int varnam_transliterate(varnam *handle, const char *input, struct strbuf *output)
{
sqlite3 *db;
int rc;
struct strbuf *string;

if(handle == NULL || input == NULL || output == NULL)
return VARNAM_MISUSE;

rc = sqlite3_open( scheme_file, &db );
if( rc ) {
varnam_error("Can't open %s: %s\n", scheme_file, sqlite3_errmsg(db));
sqlite3_close(db);
return VARNAM_ERROR;
}

string = strbuf_init( 20 );
if( tokenize( db, input, string ) != VARNAM_OK ) {
strbuf_destroy( string );
string = NULL;
}
db = handle->internal->db;
rc = tokenize( db, input, output );

sqlite3_close( db );
return string;
return rc;
}

int transliterate_input(int argc, char **argv)
{
char *scheme_file;
char *input;
struct strbuf *output;

if(argc == 0) {
varnam_info( "transliterate: invalid usage" );
varnam_info( tl_usage );
return 1;
}
else if(argc == 1) {
varnam_info( "transliterate: no input text found" );
varnam_info( tl_usage );
return 1;
}

scheme_file = argv[0];
input = argv[1];
output = transliterate( scheme_file, input );
if( output == NULL ) {
varnam_error( "transliteration failed! ");
return 1;
}
varnam_info("%s", output->buffer);
strbuf_destroy(output);
return 0;
}
11 changes: 7 additions & 4 deletions trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h>
#include "util.h"
#include "trie.h"
#include "varnam-result-codes.h"

static struct trie *trie_new(const char *label, void *value)
{
Expand Down Expand Up @@ -200,17 +201,19 @@ struct trie *trie_add_child(struct trie *parent, const char *label, void *value)

static int iterate_trie_recursive(struct trie *t, itfunction function, unsigned int depth, void *userdata)
{
int rc;
while(t != NULL)
{
if(function(t, depth, userdata) != VARNAM_OK) {
if(function(t, depth, userdata) != VARNAM_SUCCESS) {
return VARNAM_ERROR;
}
if(iterate_trie_recursive(t->child, function, depth + 1, userdata) != VARNAM_OK) {
return VARNAM_ERROR;
rc = iterate_trie_recursive(t->child, function, depth + 1, userdata);
if(rc != VARNAM_SUCCESS) {
return rc;
}
t = t->next;
}
return VARNAM_OK;
return VARNAM_SUCCESS;
}

int trie_iterate(struct trie *t, itfunction function, void *userdata)
Expand Down
Loading

0 comments on commit f59e072

Please sign in to comment.