Skip to content

Commit

Permalink
compile: add Source class for the c compiler, light EXEC_COMPILE refa…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
Reini Urban committed May 10, 2013
1 parent 94ef67c commit 4031053
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
4 changes: 2 additions & 2 deletions core/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1029,10 +1029,10 @@ PN potion_source_dump(Potion *P, PN cl, PN self, PN backend, PN options) {
return potion_source_dumpbc(P, cl, self, options);
if (potion_load(P, P->lobby, self, potion_strcat(P, "compile-", cb))) {
DBG_c("loaded compile-%s\n", cb);
DBG_c("source dump%s %s\n", cb, PN_IS_STR(options) ? PN_STR_PTR(options) : "");
DBG_c("Source dump%s(%s)\n", cb, PN_IS_STR(options) ? PN_STR_PTR(options) : "");
return potion_send(self, potion_strcat(P, "dump", cb), options);
} else {
DBG_c("failed loading the compile-%s module\n", cb);
fprintf(stderr, "** failed loading the compile-%s module\n", cb);
return PN_NIL;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void potion_loader_init(Potion *P) {
// relocatable path - relative to exe in argv[0]
//PN arg0 = potion_send(potion_str(P, "$^X")); // but too early for argv[0]
//if (arg0) PN_PUSH(pn_loader_path, potion_strcat(P, basename(PN_STR_PTR(arg0)), "../lib/potion"));
PN_PUSH(pn_loader_path, potion_str(P, "lib/potion"));
PN_PUSH(pn_loader_path, potion_str(P, "lib"));
PN_PUSH(pn_loader_path, potion_str(P, POTION_PREFIX"/lib/potion"));
PN_PUSH(pn_loader_path, potion_str(P, "."));

Expand Down
73 changes: 36 additions & 37 deletions front/potion.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,57 +185,56 @@ static void potion_cmd_compile(Potion *P, char *filename, char *compile) {

if (exec >= MAX_EXEC)
potion_fatal("fatal: stack overwrite (exec > MAX_EXEC)\n");
if (exec >= EXEC_COMPILE) { // needs an inputfile. TODO: -e"" -ofile
char pnbpath[255];

if (exec == EXEC_COMPILE) { // needs an inputfile. TODO: -e"" -ofile
char outpath[255];
FILE *pnb;
char *opts = NULL;
PN opts;
char *c_opts = NULL;
PN_SIZE written = 0;
if (compile) { // --compile=c[,OPTS]
if ((opts = strchr(compile,','))) {
opts[0] = '\0';
opts++;
if ((c_opts = strchr(compile,','))) {
c_opts[0] = '\0';
c_opts++;
//TODO check -o for outpath
}
}
if (exec == EXEC_COMPILE) {
if (!compile || !strcmp(compile, "bc"))
sprintf(pnbpath, "%sb", filename); // .pnb
else if (!strcmp(compile, "c"))
sprintf(pnbpath, "%s.c", filename); // .pn.c
else if (!strcmp(compile, "exe"))
sprintf(pnbpath, "%s.out", filename); // TODO: strip ext
}
pnb = fopen(pnbpath, "wb");
if (!compile || !strcmp(compile, "bc"))
sprintf(outpath, "%sb", filename); // .pnb
else if (!strcmp(compile, "c"))
sprintf(outpath, "%s.c", filename); // .pn.c
else if (!strcmp(compile, "exe"))
sprintf(outpath, "%s.out", filename); // TODO: strip ext
opts = c_opts
? pn_printf(P, potion_bytes(P,0), "%s,-o%s", c_opts, outpath)
: potion_strcat(P, "-o", outpath);

pnb = fopen(outpath, "wb");
if (!pnb) {
fprintf(stderr, "** could not open %s for writing. check permissions.\n", pnbpath);
fprintf(stderr, "** could not open %s for writing. check permissions.\n", outpath);
goto done;
}

if (exec == EXEC_COMPILE) { // compile backend. default: bc
if (!compile)
code = potion_source_dumpbc(P, PN_NIL, code, PN_NIL);
else
code = potion_source_dump(P, 0, code,
potion_str(P, compile),
opts ? potion_str(P, opts) : PN_NIL);
}

if (!compile)
code = potion_source_dumpbc(P, 0, code, PN_NIL);
else
code = potion_source_dump(P, 0, code,
potion_str(P, compile),
opts ? opts : PN_NIL);
if (code &&
(written = fwrite(PN_STR_PTR(code), 1, PN_STR_LEN(code), pnb) == PN_STR_LEN(code))) {
printf("** compiled code saved to %s\n", pnbpath);
printf("** compiled code saved to %s\n", outpath);
fclose(pnb);

if (exec == EXEC_COMPILE) {
if (!compile || !strcmp(compile, "bc"))
printf("** run it with: potion %s\n", pnbpath);
// TODO: let the compilers write its own hints (,-ooutfile)
else if (!strcmp(compile, "c"))
printf("** compile it with: %s %s %s\n", POTION_CC, POTION_CFLAGS, pnbpath);
else if (!strcmp(compile, "exe"))
printf("** run it with: ./%s\n", pnbpath);
}
if (!compile || !strcmp(compile, "bc"))
printf("** run it with: potion %s\n", outpath);
// TODO: let the compilers write its own hints (,-ooutfile)
else if (!strcmp(compile, "c"))
printf("** compile it with: %s %s %s\n", POTION_CC, POTION_CFLAGS, outpath);
else if (!strcmp(compile, "exe"))
printf("** run it with: ./%s\n", outpath);
} else {
fprintf(stderr, "** could not write all %s compiled code (%u/%u) to %s\n",
compile?compile:"bytecode", written, code?PN_STR_LEN(code):0, pnbpath);
compile?compile:"bytecode", written, code?PN_STR_LEN(code):0, outpath);
}
}

Expand Down
21 changes: 21 additions & 0 deletions lib/compile-c.pn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Source = class(source|opts=S): /source = source, /opts = opts.

# optionally transform ast
# no methods for the various ast types, as we have no ast types yet :)
# TODO: support method combinations? before, after, around
#SourceBlock compile :after = (): .

# the walker
#Source compile :after = ():
# "compilec" /opts join say
# /source.

# the emitter
# dump c code for compiled ast
Source dumpc = (source|opts=S):
/source = source
/opts = opts
"dumpc" opts join say
source string.

1

0 comments on commit 4031053

Please sign in to comment.