Skip to content

Commit

Permalink
Merge pull request #24 from jweinst1/develop
Browse files Browse the repository at this point in the history
Adds map, = ! symbols
  • Loading branch information
jweinst1 authored May 15, 2018
2 parents 8c043d7 + f42f5c5 commit 8fd9e7a
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ OBJ_FILES := $(patsubst src/%,lib/%,$(C_FILES:.c=.o))
LD_FLAGS :=
CC_FLAGS := -c -Wall -I$(INC_DIR)

clean:
rm -rf bin
rm -rf lib

all: bin/Wind

bin/Wind: $(OBJ_FILES) ; $(CC) $(LD_FLAGS) -o $@ $^ | mkdir -p bin
Expand Down
24 changes: 24 additions & 0 deletions include/WindBuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

// Buffer for Wind Stream

#define WindBuf_EQ_SPACE 10

// Macros that deal with size and space of buf
#define WindBuf_SPACE(wb) (wb->cap - wb->len)
#define WindBuf_FITS(wb, size) (wb->cap - wb->len) > size
Expand All @@ -21,23 +23,35 @@
//Checks if index is in the range of the buffer.
#define WindBuf_IN(wb, index) (index < wb->len && index >= 0)

// This sets the len of the buffer equal to the space that head was moved.
#define WindBuf_HEAD_LEN(wb) (wb->len = (wb->head - wb->data))
#define WindBuf_HEAD_RE(wb) (wb->head = wb->data)
// Determines if the head is at the length point.
#define WindBuf_HEAD_AT_L(wb) (wb->head == (wb->data + wb->len))

#define WindBuf_HEAD_FULL(wb) (wb->head == (wb->data + wb->cap))
#define WindBuf_HEAD_FITS(wb, size) (wb->head + size) > (wb->data + wb->cap)

// Macro handles expansion of buffer due to memory allocated in place.
#define WindBuf_EXPAND(wb, amount) do { \
wb->cap += amount; \
wb = realloc(wb, WindBuf_SIZE(wb)); \
wb->head = wb->data; \
} while (0)

// Shrinks the capacity of the buffer if neccesary
#define WindBuf_SHRINK(wb, newsize) if(newsize > wb->len) { \
wb->cap = newsize; \
wb = realloc(wb, WindBuf_SIZE(wb)); \
wb->head = wb->data; \
}

// Macro that checks if a size can fit in the buffer, and if it doesn't
// expands buffer by add
#define WindBuf_CHECK(wb, size, add) if((wb->cap - wb->len) < size) { \
wb->cap += add; \
wb = realloc(wb, WindBuf_SIZE(wb)); \
wb->head = wb->data; \
}

// Gets a typed pointer to some area of the buffer.
Expand All @@ -51,10 +65,12 @@
#define WindBuf_WRITE(wb, ptr, n) if((wb->cap - wb->len) > n) memcpy(wb->data, ptr, n)

// Buffer is an in-place allocated memory chunk that reserves additional capacity.
// head serves as a movable writer or reader for iterating across a buffer.
typedef struct
{
size_t cap;
size_t len;
unsigned char* head;
unsigned char data[0];
} WindBuf;

Expand All @@ -63,8 +79,16 @@ WindBuf* WindBuf_new(size_t size);
// Low level get function
unsigned char* WindBuf_get(WindBuf* wb, size_t index);

// appends item to end of buffer, but returns pointer to place where item
// begins in buffer after appending.
unsigned char* WindBuf_place(WindBuf* wb, void* item, size_t size);

// Counts the amount of items in the buffer.
// returns -1 if error
long WindBuf_count(WindBuf* wb);

// Makes buffer 2 the same capacity as buffer 1, if it's smaller than buffer 1
// other buffer must be double pointer due to direct allocation of WindBuf
void WindBuf_equalize(WindBuf* wb, WindBuf** other);

#endif
3 changes: 3 additions & 0 deletions include/WindExec.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
// header for executing commands on the stream.

#include "WindStream.h"
#include "WindVal.h"

int WindExec_out(WindStream* ws, BufKey bkey);

int WindExec_push(WindStream* ws);

int WindExec_clr(WindStream* ws);

int WindExec_map(WindStream* ws);

#endif
2 changes: 1 addition & 1 deletion include/WindStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void WindStream_del(WindStream* ws);
void WindStream_write_err(WindStream* ws, const char* fmt, ...);

void WindStream_print_err(WindStream* ws);

// swaps active and alt Buffer
void WindStream_swap_buf(WindStream* ws);

#endif
8 changes: 6 additions & 2 deletions include/WindType.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
typedef enum
{
WindType_None,
WindType_Bool
WindType_Bool,
WindType_Not,
WindType_Assign,
WindType_Sep
} WindType;

// An enum to track the kinds of Wind Commands.
Expand All @@ -15,7 +18,8 @@ typedef enum
WindCommand_null, // abscence of a command
WindCommand_out,
WindCommand_push,
WindCommand_clr
WindCommand_clr,
WindCommand_map
} WindCommand;

#endif
31 changes: 31 additions & 0 deletions include/WindVal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef WIND_VAL_H
#define WIND_VAL_H

#include <stdio.h>
#include <stdlib.h>
#include "WindType.h"

// Header that handles transformation of binary objects for mapping,
// filtering, reducing, copying, and more.
// Also provides utilities for moving WindType pointers
// Uses mostly pure unsigned char* buffers

//prints a single wind value from bytes
int WindVal_print(unsigned char* item);

// Copies <amnt> wind values from src to destination.
// Assumes sufficient space.
int WindVal_copy(unsigned char** dest, unsigned char** src, int amnt);

// Applies not operation to binary item.
// Accepts a double bytes pointer so it can be moved.
int WindVal_apply_not(unsigned char* item);

// Here inst is the load buffers loaded instructions.
int WindVal_apply_assign(unsigned char* item, unsigned char* inst);

// Advances the pointer pointer some amount of times, according to the WindType code.
int WindVal_move(unsigned char** item, int amnt);


#endif
Empty file added map
Empty file.
Empty file added out
Empty file.
47 changes: 43 additions & 4 deletions src/code/WindRun.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ int WindRun_exec(WindStream* ws, const char** code)
case WindCommand_clr:
WindExec_clr(ws);
break;
case WindCommand_map:
WindExec_map(ws);
break;
}
WindStream_RESET_LOAD(ws);
ws->command = WindCommand_null;
Expand All @@ -34,10 +37,6 @@ int WindRun_load(WindStream* ws, const char** code)
case '\v':
*code += 1; //white space
break;
case '|':
// pipe sep found
*code += 1;
goto TRANS_TO_EXEC;
case '-':
if((*code)[1] == '>')
{
Expand All @@ -49,6 +48,20 @@ int WindRun_load(WindStream* ws, const char** code)
WindStream_write_err(ws, "Expected separator ->, found '-%c'", (*code)[1]);
return 0; // error
}
case '|':
*code += 1;
WindStream_put(ws, BufKey_load, WindType_Sep);
continue;
case '!':
// not :symbol
*code += 1;
WindStream_put(ws, BufKey_load, WindType_Not);
continue;
case '=':
// not :symbol
*code += 1;
WindStream_put(ws, BufKey_load, WindType_Assign);
continue;
case 'T':
if((*code)[1] == 'r' && (*code)[2] == 'u' && (*code)[3] == 'e')
{
Expand Down Expand Up @@ -150,6 +163,27 @@ int WindRun_command(WindStream* ws, const char** code)
return 0;
}
break;
case 'm':
switch((*code)[1])
{
case 'a':
switch((*code)[2])
{
case 'p':
// exec out
*code += 3;
ws->command = WindCommand_map;
goto TRANS_TO_LOAD;
default:
WindStream_write_err(ws, "Expected command symbol, found 'ma%c'", *code[2]);
return 0;
}
break;
default:
WindStream_write_err(ws, "Expected command symbol, found 'm%c'", *code[1]);
return 0;
}
break;
case 'o':
switch((*code)[1])
{
Expand Down Expand Up @@ -237,4 +271,9 @@ void WindRun_code(WindStream* ws, const char* code)
}
// Executes any lasting commands, if null char is reached first.
if(ws->command != WindCommand_null) WindRun_exec(ws, &code);
if(ws->hasErr)
{
WindStream_print_err(ws);
return;
}
}
14 changes: 11 additions & 3 deletions src/main/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ int main(int argc, char const *argv[]) {
fprintf(stderr, "%s\n", "Error, need exactly two command line arguments.");
exit(1);
}
else if(strcmp(argv[1], "-c"))
else if(!strcmp(argv[1], "-c"))
{
WindRun_code(stream, argv[2]);
}
// debug option
else if(!strcmp(argv[1], "-d"))
{
WindRun_code(stream, argv[2]);
Debug_stream(stream);
}
else
{
fprintf(stderr, "Error, option not recognized found option %s\n", argv[1]);
}
WindRun_code(stream, argv[2]);
Debug_stream(stream);

WindStream_del(stream);
return 0;
Expand Down
24 changes: 24 additions & 0 deletions src/stream/WindBuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ WindBuf* WindBuf_new(size_t size)
WindBuf* newbuf = malloc(sizeof(WindBuf) + (sizeof(unsigned char) * size));
newbuf->len = 0;
newbuf->cap = size;
newbuf->head = newbuf->data;
return newbuf;
}

unsigned char* WindBuf_get(WindBuf* wb, size_t index)
{
// needs moving to windval header
unsigned char* startPtr = wb->data;
unsigned char* endPtr = wb->data + wb->len;
while(startPtr != endPtr && index--)
Expand All @@ -20,6 +22,7 @@ unsigned char* WindBuf_get(WindBuf* wb, size_t index)
case WindType_None:
startPtr++;
break;
// needs more types handled.
default:
return NULL;
}
Expand All @@ -28,8 +31,18 @@ unsigned char* WindBuf_get(WindBuf* wb, size_t index)
else return startPtr;
}

unsigned char* WindBuf_place(WindBuf* wb, void* item, size_t size)
{
WindBuf_CHECK(wb, size, size + 5);
unsigned char* reader = item;
unsigned char* placed = wb->data + wb->len;
for(size_t i = 0; i < size; i++) wb->data[wb->len++] = reader[i];
return placed;
}

long WindBuf_count(WindBuf* wb)
{
// Needs updating
long total = 0;
unsigned char* startPtr = wb->data;
unsigned char* endPtr = wb->data + wb->len;
Expand All @@ -47,3 +60,14 @@ long WindBuf_count(WindBuf* wb)
}
return total;
}


void WindBuf_equalize(WindBuf* wb, WindBuf** other)
{
(*other)->len = wb->len;
if(wb->cap > (*other)->cap)
{
size_t newCap = wb->cap + WindBuf_EQ_SPACE;
WindBuf_EXPAND((*other), newCap);
}
}
Loading

0 comments on commit 8fd9e7a

Please sign in to comment.