Skip to content

Instantly share code, notes, and snippets.

@HernanRivasAcosta
Last active Aug 29, 2015
Embed
What would you like to do?
Erlang dummy code generaor
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef enum {NONE, WHITESPACE, BINARY, FUNCTIONS, CLAUSES} MODE;
int showHelp();
MODE getMode(const char *modeString, MODE *mode);
void writeFile(FILE *fd, MODE mode, int size);
const char *nameForMode(MODE mode);
int main(int argc, const char *argv[])
{
const char *name = NULL;
int size = 0;
MODE mode = NONE;
if (argc < 1)
{
return showHelp();
}
const char *t;
for(int i = 0; i < argc; i++)
{
t = argv[i];
if(!strcmp(t, "-h") || !strcmp(t, "--help"))
{
return showHelp();
}
else if(!strcmp(t, "-n") || !strcmp(t, "--name"))
{
if(i + 1 < argc)
{
name = argv[++i];
}
else
{
printf("ERROR: NO NAME SPECIFIED\n");
return 1;
}
}
else if(!strcmp(t, "-m") || !strcmp(t, "--mode"))
{
if(i + 1 < argc)
{
if(!getMode(argv[++i], &mode))
{
printf("ERROR: INVALID MODE '%s'\n", argv[i++]);
return 1;
}
else if(!name)
{
name = nameForMode(mode);
}
}
else
{
printf("ERROR: NO MODE SPECIFIED\n");
return 1;
}
}
else if(!strcmp(t, "-s") || !strcmp(t, "--s"))
{
if(i + 1 < argc)
{
size = atoi(argv[++i]);
}
else
{
printf("ERROR: NO SIZE SPECIFIED\n");
return 1;
}
}
}
if (size < 1)
{
printf("ERROR: NO SIZE SPECIFIED\n");
return 1;
}
if (mode == NONE)
{
printf("ERROR: NO MODE SPECIFIED\n");
return 1;
}
char fileName[strlen(name) + 5];
snprintf(fileName, sizeof fileName, "%s%s", name, ".erl");
remove(fileName);
FILE *fd = fopen(fileName, "w");
fprintf(fd, "-module(%s).\n", name);
writeFile(fd, mode, size);
return 0;
}
int showHelp()
{
printf("\nUsage:\n\n"
" -h, --help: show this help\n"
" -m, --mode: The generation mode, valid modes are: 'whitespace' (w),"
" 'binary' (b), 'functions' (f) and 'clauses (c)'\n"
" -s, --size: Different generators will interpret this value "
"differently, for example in binary mode, this is the size of the "
"binary\n"
" -n, --name: The name of the generated module (optional)\n\n");
return 1;
}
MODE getMode(const char *modeString, MODE *mode)
{
if(!strcmp(modeString, "w") || !strcmp(modeString, "whitespace"))
{
*mode = WHITESPACE;
return 1;
}
else if(!strcmp(modeString, "b") || !strcmp(modeString, "binary"))
{
*mode = BINARY;
return 1;
}
else if(!strcmp(modeString, "f") || !strcmp(modeString, "functions"))
{
*mode = FUNCTIONS;
return 1;
}
else if(!strcmp(modeString, "c") || !strcmp(modeString, "clauses"))
{
*mode = CLAUSES;
return 1;
}
return 0;
}
const char *nameForMode(MODE mode)
{
switch(mode)
{
case WHITESPACE: return "whitespace";
case FUNCTIONS: return "functions";
case CLAUSES: return "clauses";
case BINARY: return "binary";
case NONE: return ""; // This can't happen
}
}
void writeFile(FILE *fd, MODE mode, int size)
{
switch(mode)
{
case WHITESPACE:
for(int i = 0; i < size; i++)
{
fprintf(fd, " ");
}
break;
case FUNCTIONS:
fprintf(fd, "-export([");
for(int i = 1; i <= size - 1; i++)
{
fprintf(fd, "test%d/0, ", i);
}
fprintf(fd, "test%d/0", size);
fprintf(fd, "]).\n");
for(int i = 1; i <= size; i++)
{
fprintf(fd, "test%d() -> ok.\n", i);
}
break;
case CLAUSES:
fprintf(fd, "-export([case_clauses/1]).\n"
"case_clauses(Value) ->\n"
" case Value of\n");
for(int i = 1; i <= size - 1; i++)
{
fprintf(fd, " %d -> {ok, %d};\n", i, i);
}
fprintf(fd, " %d -> {ok, %d}\n", size, size);
fprintf(fd, " end.\n");
break;
case BINARY:
fprintf(fd, "-export([get_bin/0]).\n"
"get_bin() -> <<\"");
for(int i = 0; i <= size - 1; i++)
{
fprintf(fd, "H");
}
fprintf(fd, "\">>.\n");
break;
case NONE:
// This can't happen
break;
}
}
@HernanRivasAcosta

This comment has been minimized.

Copy link
Owner Author

@HernanRivasAcosta HernanRivasAcosta commented Aug 4, 2015

$ gcc build.c -o build
$ ./build -m functions -n func_test -s 20
$ cat func_test.erl 
-module(func_test).
-export([test1/0, test2/0, test3/0]).
test1() -> ok.
test2() -> ok.
test3() -> ok.
$ erl -compile func_test.erl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment