Skip to content

Commit

Permalink
On master: config
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed May 3, 2024
2 parents 8cb7034 + 6302c37 commit d8359eb
Show file tree
Hide file tree
Showing 8 changed files with 1,730 additions and 1,405 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
git push origin -f --follow-tags nightly
- name: Deploy release
uses: WebFreak001/deploy-nightly@v1.1.0
uses: WebFreak001/deploy-nightly@v3.1.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
Expand Down
31 changes: 16 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@ PRG := gwfmt
CFLAGS += -O3

# Warnings
CFLAGS += -Wall -Wextra -Wno-unused
CFLAGS += -Wall -Wextra

# Includes
CFLAGS += -I../util/include
CFLAGS += -I../ast/include
CFLAGS += -I../ast/libprettyerr/src
CFLAGS += -I../include
CFLAGS += -Iinclude

# Libraries
LDFLAGS += -L../util
LDFLAGS += -L../ast
LDFLAGS += -L../ast/libprettyerr

#CFLAGS += -flto -Ofast
#LDFLAGS += -flto

LDFLAGS += -lprettyerr

ifneq ($(shell uname), Darwin)
LDFLAGS += -static
endif
LDFLAGS += -L../

ifeq ($(shell uname), Darwin)
AR = /usr/bin/libtool
Expand All @@ -42,10 +33,10 @@ endif

all: ${PRG}

${PRG}: src/${PRG}.o src/unpy.o libgwion_fmt.a
${CC} ${CFLAGS} $^ -Iinclude -lgwion_ast -lgwion_util ${LDFLAGS} -lpthread -lm -o $@
${PRG}: src/${PRG}.o src/unpy.o src/gwion_config.o libgwion_fmt.a
${CC} ${DEPFLAGS} ${CFLAGS} $^ -Iinclude -lgwion -lgwion_ast -lgwion_util ${LDFLAGS} -ldl -lpthread -lm -o $@

libgwion_fmt.a: src/lint.o
libgwion_fmt.a: src/lint.o src/casing.o
${AR} ${AR_OPT}

src/unpy.c: src/unpy.l
Expand All @@ -63,3 +54,13 @@ uninstall:
rm ${PREFIX}/bin/${PRG}
rm ${PREFIX}/lib/lib${PRG}.a
rm ${PREFIX}/include/${PRG}.h

.c.o:
$(info compile $(<:.c=))
@${CC} $(DEPFLAGS) ${CFLAGS} ${PACKAGE_INFO} ${INSTALL_PREFIX} -c $< -o $(<:.c=.o)
@mv -f $(DEPDIR)/$(@F:.o=.Td) $(DEPDIR)/$(@F:.o=.d) && touch $@
@echo $@: config.mk >> $(DEPDIR)/$(@F:.o=.d)

DEPDIR := .d
$(shell mkdir -p $(DEPDIR) >/dev/null)
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(@F:.o=.Td)
72 changes: 64 additions & 8 deletions include/gwfmt.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
enum char_type { cht_id, cht_colon, cht_lbrack, cht_nl, cht_op, cht_delim, cht_sp };
#pragma once

struct GwfmtState {
#include "gwion_util.h"
#include "gwion_ast.h"
#include "casing.h"

enum gwfmt_char_type { cht_id, cht_colon, cht_lbrack, cht_nl, cht_op, cht_delim, cht_sp };

typedef enum CaseType {
TypeCase,
VariableCase,
FunctionCase,
LastCase
} CaseType;

#ifdef LINT_IMPL
static const char* ct_name[LastCase] = {
"Type",
"Variable",
"Function",
};
#endif

typedef enum FmtColor {
StringColor,
KeywordColor,
FlowColor,
FunctionColor,
TypeColor,
VariableColor,
NumberColor,
OpColor,
ModColor,
PunctuationColor,
PPColor,
SpecialColor,
LastColor,
} FmtColor;

#define MAX_COLOR_LENGTH 8

typedef struct GwFmtMark {
uint16_t line;
char sign[16];
} GwFmtMark;

typedef struct GwfmtState {
GwText text;
struct PPArg_ *ppa;
PPArg *ppa;
Casing cases[LastCase];
char colors[LastColor][MAX_COLOR_LENGTH];
unsigned int nindent;
unsigned int mark;
MP_Vector *marks; // NOTE: make it a vector?
unsigned int base_column;
bool py;
bool unpy;
Expand All @@ -16,9 +62,14 @@ struct GwfmtState {
bool show_line;
bool header;
bool use_tabs;
};
bool error;
bool fix;
} GwfmtState;

void gwfmt_state_init(GwfmtState *ls);

typedef struct {
const char *filename;
MemPool mp;
SymTable *st; // only for spread
Map macro;
Expand All @@ -27,15 +78,15 @@ typedef struct {
unsigned int indent;
unsigned int skip_indent;
unsigned int nl;
enum char_type last;
enum gwfmt_char_type last;
unsigned int line;
unsigned int column;
// bool skip; // check_pos
bool need_space;
} Gwfmt;

ANN void gwfmt(Gwfmt *a, const m_str, ...);
ANN void gwfmt_util(Gwfmt *a, const m_str, ...);
ANN int gwfmt_util(Gwfmt *a, const char *, ...);
ANN void gwfmt(Gwfmt *a, const char*, ...);
ANN void gwfmt_indent(Gwfmt *a);
ANN void gwfmt_sc(Gwfmt *a);
ANN void gwfmt_nl(Gwfmt *a);
Expand All @@ -56,3 +107,8 @@ ANN void gwfmt_prim_def(Gwfmt *a, Prim_Def b);
ANN void gwfmt_ast(Gwfmt *a, Ast b);
ANN void gwfmt_type_decl(Gwfmt *a, const Type_Decl *b);
ANN void gwfmt_variable(Gwfmt *a, const Variable *b);


void set_color(struct GwfmtState *ls, const FmtColor b,
const char *color);
bool run_config(struct GwfmtState *ls, const char *filename);
4 changes: 4 additions & 0 deletions include/gwfmt_internal.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include <gwion_util.h>
#include <gwion_ast.h>
#include <gwfmt.h>

ANN static void gwfmt_symbol(Gwfmt *a, Symbol b);
ANN static void gwfmt_array_sub(Gwfmt *a, Array_Sub b);
ANN static void gwfmt_id_list(Gwfmt *a, ID_List b);
Expand Down
61 changes: 36 additions & 25 deletions src/gwfmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include <limits.h>
#include "gwion_util.h"
#include "gwion_ast.h"
#include "gwion_env.h"
#include "gwfmt.h"
#include "unpy.h"
#include "cmdapp.h"
#include "arg.h"

ANN static inline void gwfmt_file(Gwfmt *a, const m_str name) {
gwfmt_util(a, "{-}File:{0} {+}%s{0}\n", name);
Expand All @@ -14,7 +16,7 @@ ANN static inline void gwfmt_file(Gwfmt *a, const m_str name) {
ANN static int gwfmt_gw(struct AstGetter_ *arg, struct GwfmtState *ls) {
const Ast ast = parse(arg);
if (!ast) return 1;
Gwfmt l = {.mp = arg->st->p, .st = arg->st, .ls = ls, .line = 1, .last = cht_nl };
Gwfmt l = {.mp = arg->st->p, .st = arg->st, .filename = arg->name, .ls = ls, .line = 1, .last = cht_nl };
if (!ls->pretty) {
if (l.ls->header) {
gwfmt_util(&l, " {N}┏━━━{0} ");
Expand All @@ -28,7 +30,6 @@ ANN static int gwfmt_gw(struct AstGetter_ *arg, struct GwfmtState *ls) {
}
gwfmt_ast(&l, ast);
free_ast(l.mp, ast);
ls->mark = 0;
if (!ls->pretty) {
if(!ls->minimize)
gwfmt_util(&l, "\b\b\b\b\b\b\b");
Expand Down Expand Up @@ -80,9 +81,11 @@ enum {
UNPY,
ONLYPY,
HEADER,
MARK,
// MARK,
EXPAND,
MINIFY,
CASING,
CONFIG,
COLOR,
NOPTIONS
};
Expand All @@ -100,12 +103,16 @@ static void setup_options(cmdapp_t *app, cmdopt_t *opt) {
"bool", &opt[HEADER]);
cmdapp_set(app, 'h', "header", CMDOPT_TAKESARG, NULL, "enable or disable header mode",
"bool", &opt[ONLYPY]);
cmdapp_set(app, 'M', "mark", CMDOPT_TAKESARG, NULL, "mark a line",
"integer", &opt[MARK]);
// cmdapp_set(app, 'M', "mark", CMDOPT_TAKESARG, NULL, "mark a line",
// "integer", &opt[MARK]);
cmdapp_set(app, 'e', "expand", CMDOPT_TAKESARG, NULL, "enable or disable lint mode",
"bool", &opt[HEADER]);
cmdapp_set(app, 'm', "minify", CMDOPT_TAKESARG, NULL, "minimize input",
"bool", &opt[MINIFY]);
cmdapp_set(app, 'f', "fix", CMDOPT_TAKESARG, NULL, "fix casing",
"bool", &opt[CASING]);
cmdapp_set(app, 'C', "config", CMDOPT_TAKESARG, NULL, "config file",
"filename", &opt[CONFIG]);
cmdapp_set(app, 'c', "color", CMDOPT_TAKESARG, NULL, "enable or disable {R}c{G}o{B}l{M}o{Y}r{C}s{0}",
"{+}auto{-}/never/always", &opt[COLOR]);
}
Expand All @@ -117,7 +124,7 @@ ANN static inline bool arg2bool(const char *arg) {

typedef struct GwArg {
SymTable *st;
struct PPArg_ *ppa;
PPArg *ppa;
struct GwfmtState *ls;
} GwArg;

Expand Down Expand Up @@ -147,30 +154,36 @@ static void myproc(void *data, cmdopt_t *option, const char *arg) {
if (arg) run(gwarg, arg);
else {
switch (option->shorto) {
case 'i': // indent
case 'i':
ls->nindent = ARG2INT(option->value);
break;
case 'n': // pretty
case 'n':
ls->pretty = !ls->pretty;
break;
case 'h': // header
case 'h':
ls->header = !ls->header;
break;
case 'u': // unpy
case 'u':
ls->unpy = !ls->unpy;
break;
case 'r': // unpy
case 'r':
ls->onlypy = ls->unpy = arg2bool(option->value);
break;
case 'M': // mark
ls->mark = ARG2INT(option->value);
break;
case 'e': // expand
// case 'M':
// ls->mark = ARG2INT(option->value);
// break;
case 'e':
gwarg->ppa->fmt = !arg2bool(option->value);
break;
case 'm': // minify
case 'm':
ls->minimize = arg2bool(option->value);
break;
case 'f':
ls->fix = arg2bool(option->value);
break;
case 'C':
run_config(ls, option->value);
break;
case 'c': // color
ls->color = arg2bool(option->value);
tcol_override_color_checks(ls->color);
Expand All @@ -179,7 +192,7 @@ static void myproc(void *data, cmdopt_t *option, const char *arg) {
}
}

ANN static bool arg_parse(GwArg *a, int argc, char **argv) {
ANN static bool gwfmt_arg_parse(GwArg *a, int argc, char **argv) {
cmdapp_t app;
const cmdapp_info_t info = {
.program = "gwion",
Expand All @@ -199,27 +212,25 @@ ANN static bool arg_parse(GwArg *a, int argc, char **argv) {
cmdopt_t opt[NOPTIONS];
setup_options(&app, opt);
bool ret = cmdapp_run(&app) == EXIT_SUCCESS && cmdapp_should_exit(&app);
//if (cmdapp_run(&app) == EXIT_SUCCESS && cmdapp_should_exit(&app))
// arg->arg->quit = 1;
cmdapp_destroy(&app);
return ret;
}

int main(int argc, char **argv) {
MemPool mp = mempool_ini(sizeof(Exp));
SymTable * st = new_symbol_table(mp, 65347); // could be smaller
struct PPArg_ ppa = {.fmt = 1};
PPArg ppa = {.fmt = 1};
pparg_ini(mp, &ppa);
struct GwfmtState ls = {.color = isatty(1), .show_line = true, .header = true, .nindent = 2, .ppa = &ppa};
text_init(&ls.text, mp);
int ret = 0;
struct GwfmtState ls = {.color = isatty(1) ? COLOR_AUTO : COLOR_NEVER, .show_line = true, .header = true, .nindent = 2, .ppa = &ppa, .minimize = false };
tcol_override_color_checks(ls.color);
gwfmt_state_init(&ls);
text_init(&ls.text, mp);
GwArg arg = { .ls = &ls, .st = st, .ppa = &ppa };
argc--; argv++;
arg_parse(&arg, argc, argv);
gwfmt_arg_parse(&arg, argc, argv);
text_release(&ls.text);
pparg_end(&ppa);
free_symbols(st);
mempool_end(mp);
return ret;
return ls.error;
}
Loading

0 comments on commit d8359eb

Please sign in to comment.