Skip to content

Instantly share code, notes, and snippets.

@emekoi
Last active July 28, 2016 14:23
Show Gist options
  • Save emekoi/0d5b0d320477c8c34c347b16efe42b3a to your computer and use it in GitHub Desktop.
Save emekoi/0d5b0d320477c8c34c347b16efe42b3a to your computer and use it in GitHub Desktop.
/* Copyright (c) 2016 Emeka N. All Rights Reserved. */
#ifndef log_h_
#define log_h_
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_YELLOW "\x1b[33m"
#define COLOR_BLUE "\x1b[34m"
#define COLOR_MAGENTA "\x1b[35m"
#define COLOR_CYAN "\x1b[36m"
#define COLOR_RESET "\x1b[0m"
#define TRACE(...) do { \
fprintf(stderr, COLOR_BLUE"[TRACE %s]%s %s:%d: ", __TIME__, COLOR_RESET, __FILE__, __LINE__); \
printf(__VA_ARGS__); \
} while(0);
#define DEBUG(...) do { \
fprintf(stderr, COLOR_CYAN"[DEBUG %s]%s %s:%d: ", __TIME__, COLOR_RESET, __FILE__, __LINE__); \
printf(__VA_ARGS__); \
} while(0);
#define INFO(...) do { \
fprintf(stderr, COLOR_GREEN"[INFO %s]%s %s:%d: ", __TIME__, COLOR_RESET, __FILE__, __LINE__); \
printf(__VA_ARGS__); \
} while(0);
#define WARN(...) do { \
fprintf(stderr, COLOR_YELLOW"[WARN %s]%s %s:%d: ", __TIME__, COLOR_RESET, __FILE__, __LINE__); \
printf(__VA_ARGS__); \
} while(0);
#define ERROR(...) do { \
fprintf(stderr, COLOR_RED"[ERROR %s]%s %s:%d: ", __TIME__, COLOR_RESET, __FILE__, __LINE__); \
printf(__VA_ARGS__); \
} while(0);
#define FATAL(...) do { \
fprintf(stderr, COLOR_MAGENTA"[FATAL %s]%s %s:%d: ", __TIME__, COLOR_RESET, __FILE__, __LINE__); \
printf(__VA_ARGS__); \
} while(0);
#endif /* log_h_ */
#include "lux.h"
lx_state *lx_new_state(){
lx_value *t;
size_t tcap;
tcap = (cap > 0) ? cap : 256 * 256;
mb_state *s = malloc(sizeof(mb_state));
t = (mb_value *)malloc(sizeof(mb_value) * tcap);
if (t == NULL){
FATAL("insufficient memory to initiazlize new state\n");
exit(-1);
}
s->stack = t;
s->top = -1;
s->cap = tcap;
return s;
}
void lx_close_state(lx_state *s){}
bool lx_is_full(lx_state *s){}
bool lx_is_empty(lx_state *s){}
void lx_push_value(lx_state *s, lx_value v){}
lx_value lx_pop_value(lx_state *s){}
lx_value *lx_new_number(lx_state *s, double num){}
lx_value *lx_new_lstring(char *str, size_t len){}
lx_value *lx_new_string(lx_state *s, char *str){}
/* Copyright (c) 2016 Emeka N. All Rights Reserved. */
#ifndef lux_h_
#define lux_h_
/* max size for things */
#define max_size 256 * 256
/* standard c libraries*/
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
#include <time.h>
#include <limits.h>
#include "log.h"
enum {
LX_NUMBER,
LX_STRING
};
typedef struct lx_value lx_value;
struct lx_value {
int type;
union {
struct {double n; } num;
struct {char *s; size_t len; } str;
} value;
};
typedef struct lx_state lx_state;
struct lx_state {
int top;
size_t cap;
lx_value *stack;
};
lx_state *lx_new_state();
void lx_close_state(lx_state *s);
bool lx_is_full(lx_state *s);
bool lx_is_empty(lx_state *s);
void lx_push_value(lx_state *s, lx_value v);
lx_value lx_pop_value(lx_state *s);
lx_value *lx_new_number(lx_state *s, double num);
lx_value *lx_new_lstring(char *str, size_t len);
lx_value *lx_new_string(lx_state *s, char *str);
#endif /* lux_h_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment