Finished standalone compiler. Added strip-comments flag.

This commit is contained in:
claudiol 2016-01-11 10:29:36 -05:00
parent 0657e049d1
commit a54933ee2d
3 changed files with 314 additions and 15 deletions

View file

@ -131,7 +131,7 @@ int main(int argc, char *argv[])
needcleanup++;
outputfile=malloc(end-inputfile+10);
if(!outputfile) {
fprintf(stderr,"Memory allocation error\n");
fprintf(stderr,"error: Memory allocation error\n");
return 1;
}
memmove(outputfile,inputfile,end-inputfile);
@ -147,7 +147,7 @@ int main(int argc, char *argv[])
// READ THE INPUT FILE INTO A BUFFER
FILE *f=fopen(inputfile,"rb");
if(f==NULL) {
fprintf(stderr,"Error: File not found %s\n",inputfile);
fprintf(stderr,"error: File not found %s\n",inputfile);
if(needcleanup) free(outputfile);
return 1;
}
@ -157,37 +157,177 @@ int main(int argc, char *argv[])
mainbuffer=malloc(length);
if(!mainbuffer) {
fprintf(stderr,"Memory allocation error\n");
fprintf(stderr,"error: Memory allocation error\n");
if(needcleanup) free(outputfile);
return 1;
}
if(fread(mainbuffer,1,length,f)!=(size_t)length) {
fprintf(stderr,"Can't read from input file\n");
fprintf(stderr,"error: Can't read from input file\n");
if(needcleanup) free(outputfile);
free(mainbuffer);
return 1;
}
fclose(f);
// HERE WE HAVE THE MAIN FILE
// TODO: ADD SOME PREPROCESSING HERE
// TODO: COMPILE THE CODE AND DISPLAY ANY ERRORS
rplInit();
rplSetSystemFlag(FL_STRIPCOMMENTS);
WORDPTR newobject=rplCompile((BYTEPTR)mainbuffer,length,1);
if(Exceptions) {
compShowErrorMsg(inputfile,mainbuffer,length,stderr);
// IDENTIFY CHUNKS OF CODE
char *chunk=mainbuffer;
int numchunks=0;
char *chunkstart[65537]; // MAXIMUM NUMBER OF VARIABLES IN A SINGLE FILE
while(chunk-mainbuffer<length) {
if(!utf8ncmp(chunk,"@#name",6)) {
// FOUND START OF CHUNK
chunkstart[numchunks]=chunk;
++numchunks;
if(numchunks>65535) {
fprintf(stderr,"error: Too many chunks in same file.\n");
if(needcleanup) free(outputfile);
free(mainbuffer);
return 1;
}
}
//SKIP TO THE NEXT LINE
while( (*chunk!='\n')&&(*chunk!='\r')&&(chunk-mainbuffer<length)) ++chunk;
while( ((*chunk=='\n')||(*chunk=='\r'))&&(chunk-mainbuffer<length)) ++chunk;
}
if(numchunks==0) {
chunkstart[numchunks]=mainbuffer;
++numchunks;
}
chunkstart[numchunks]=chunk;
f=fopen(outputfile,"wb");
if(f==NULL) {
fprintf(stderr,"error: Can't open %s for writing.\n",outputfile);
if(needcleanup) free(outputfile);
free(mainbuffer);
return 1;
}
printf("Compile OK\n");
// TODO: GET READY TO OUTPUT THE FILE
if(outputtype==OUTPUT_BINARY) {
// WRITE THE BINARY FILE MARKER
WORD marker[3]= {
MKPROLOG(HEXBINT,2), // PROLOG OF A 64-BIT BINT
0x4c50526e, // STRING "nRPL"
1 // VERSION OF THE newRPL BINARY FORMAT
};
fwrite(marker,4,3,f);
}
else {
fprintf(f,"// newRPL binary version 1.0\n\n");
}
// COMPILE ALL CHUNKS
int k;
char *start,*end;
for(k=0;k<numchunks;++k) {
start=chunkstart[k];
end=chunkstart[k+1];
if(!utf8ncmp(start,"@#name",6)) {
// SKIP TO THE NEXT LINE FOR THE REAL DATA
while( (*start!='\n')&&(*start!='\r')&&(start<end)) ++start;
while( ((*start=='\n')||(*start=='\r'))&&(start<end)) ++start;
}
if(end>start) {
WORDPTR newobject=rplCompile((BYTEPTR)start,end-start,1);
if(Exceptions) {
compShowErrorMsg(inputfile,mainbuffer,length,stderr);
fclose(f);
remove(outputfile);
if(needcleanup) free(outputfile);
free(mainbuffer);
return 1;
}
// OUTPUT THE CHUNK
if(outputtype==OUTPUT_C) {
if(rplObjSize(newobject)>2) {
// OUTPUT C FORMATTED CODE
char *objname;
char *nameend;
if(!utf8ncmp(chunkstart[k],"@#name",6)) {
objname=chunkstart[k]+6;
while( (*objname==' ') || (*objname=='\t')) ++objname;
nameend=objname;
while( (*nameend!='\n') && (*nameend!='\r') && (*nameend!=' ') && (*nameend!='\t')) ++nameend;
} else { nameend=objname=NULL; }
fprintf(f,"ROMOBJECT ");
if(nameend<=objname) fprintf(f,"chunk%05d",k+1);
else fwrite(objname,1,nameend-objname,f);
fprintf(f,"[]= {\n");
WORDPTR p=newobject+1,endp=rplSkipOb(newobject)-1;
int wordcount=0;
while(p<endp) {
fprintf(f,"0x%08X",*p);
if(p!=endp-1) fprintf(f,",");
wordcount++;
if((wordcount&7)==0) fprintf(f,"\n");
++p;
}
if((wordcount&7)!=0) fprintf(f,"\n");
fprintf(f,"};\n\n\n");
}
}
else {
if(rplObjSize(newobject)>2) {
// RAW BINARY OUTPUT
fwrite(newobject+1,4,rplObjSize(newobject)-2,f);
}
}
}
// AND MOVE ON TO THE NEXT ONE
}
// CLOSE THE OUTPUT FILE
fclose(f);
return 0;

149
newrpl-comp.pro Normal file
View file

@ -0,0 +1,149 @@
#-------------------------------------------------
#
# Project created by QtCreator 2014-11-29T15:53:29
#
#-------------------------------------------------
TARGET = newrpl-comp
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
DEFINES += TARGET_PC
SOURCES += firmware/ggl/ggl/ggl_bitblt.c \
firmware/ggl/ggl/ggl_bitbltoper.c \
firmware/ggl/ggl/ggl_filter.c \
firmware/ggl/ggl/ggl_fltdarken.c \
firmware/ggl/ggl/ggl_fltlighten.c \
firmware/ggl/ggl/ggl_getnib.c \
firmware/ggl/ggl/ggl_hblt.c \
firmware/ggl/ggl/ggl_hbltfilter.c \
firmware/ggl/ggl/ggl_hbltoper.c \
firmware/ggl/ggl/ggl_hline.c \
firmware/ggl/ggl/ggl_initscr.c \
firmware/ggl/ggl/ggl_mkcolor.c \
firmware/ggl/ggl/ggl_mkcolor32.c \
firmware/ggl/ggl/ggl_opmask.c \
firmware/ggl/ggl/ggl_optransp.c \
firmware/ggl/ggl/ggl_ovlblt.c \
firmware/ggl/ggl/ggl_pltnib.c \
firmware/ggl/ggl/ggl_rect.c \
firmware/ggl/ggl/ggl_rectp.c \
firmware/ggl/ggl/ggl_revblt.c \
firmware/ggl/ggl/ggl_scrolldn.c \
firmware/ggl/ggl/ggl_scrolllf.c \
firmware/ggl/ggl/ggl_scrollrt.c \
firmware/ggl/ggl/ggl_scrollup.c \
firmware/ggl/ggl/ggl_vline.c \
firmware/hal_battery.c \
firmware/hal_keyboard.c \
firmware/hal_screen.c \
firmware/sys/graphics.c \
firmware/sys/icons.c \
firmware/sys/target_pc/non-gui-stubs.c \
firmware/sys/target_pc/battery.c \
firmware/sys/target_pc/cpu.c \
firmware/sys/target_pc/exception.c \
firmware/sys/target_pc/irq.c \
firmware/sys/target_pc/keyboard.c \
firmware/sys/target_pc/lcd.c \
firmware/sys/target_pc/stdlib.c \
firmware/sys/target_pc/timer.c \
firmware/hal_globals.c \
newrpl/compiler.c \
newrpl/datastack.c \
newrpl/directory.c \
newrpl/errors.c \
newrpl/gc.c \
newrpl/lam.c \
newrpl/lib-24-string.c \
newrpl/lib-28-dirs.c \
newrpl/lib-30-complex.c \
newrpl/lib-64-arithmetic.c \
newrpl/lib-66-transcendentals.c \
newrpl/lib-68-flags.c \
newrpl/lib-70-binary.c \
newrpl/lib-72-stack.c \
newrpl/lib-4080-localenv.c \
newrpl/lib-4090-overloaded.c \
newrpl/lib-common.c \
newrpl/lib-eight-docol.c \
newrpl/lib-nine-docol2.c \
newrpl/lib-ten-reals.c \
newrpl/lib-twelve-bint.c \
newrpl/lib-two-ident.c \
newrpl/lists.c \
newrpl/returnstack.c \
newrpl/romlibs.c \
newrpl/runstream.c \
newrpl/symbolic.c \
newrpl/sysvars.c \
newrpl/tempob.c \
firmware/sys/target_pc/mem.c \
firmware/sys/target_pc/boot.c \
firmware/ui_cmdline.c \
newrpl/utf8lib.c \
newrpl/utf8data.c \
firmware/sys/Font5C.c \
firmware/sys/Font6A.c \
firmware/sys/keybcommon.c \
firmware/sys/Font7A.c \
newrpl/matrix.c \
firmware/sys/Font8C.c \
firmware/sys/Font8D.c \
newrpl/atan_1_8_comp.c \
newrpl/atan_2_8_comp.c \
newrpl/atan_5_8_comp.c \
newrpl/atanh_1_8_comp.c \
newrpl/atanh_2_8_comp.c \
newrpl/atanh_5_8_comp.c \
newrpl/cordic_K_8_comp.c \
newrpl/cordic_Kh_8_comp.c \
newrpl/decimal.c \
newrpl/dectranscen.c \
newrpl/backup.c \
newrpl/sanity.c \
newrpl/lib-32-lam.c \
newrpl/lib-65-system.c \
firmware/hal_msgenglish.c \
newrpl/units.c \
newrpl/lib-62-lists.c \
newrpl/lib-56-symbolic.c \
newrpl/lib-52-matrix.c \
newrpl/lib-54-units.c \
newrpl/autocomplete.c \
newrpl/arithmetic.c \
newrpl/lib-20-comments.c \
firmware/sys/target_pc/flash.c \
firmware/ui_softmenu.c \
firmware/ggl/ggl/ggl_fltinvert.c \
newrpl-comp.c
HEADERS += firmware/include/ggl.h \
target_pc.h \
firmware/include/ui.h \
firmware/include/hal_api.h \
newrpl/hal.h \
newrpl/libraries.h \
newrpl/newrpl.h \
newrpl/sysvars.h \
newrpl/utf8lib.h \
newrpl/decimal.h \
newrpl/errorcodes.h \
newrpl/arithmetic.h \
newrpl/cmdcodes.h \
newrpl/common-macros.h \
newrpl/lib-header.h \
newrpl/include-all.h \
newrpl/romlibs.h
INCLUDEPATH += firmware/include newrpl /usr/local/include /usr/include
LIBS += -L/usr/local/lib
DISTFILES +=
QMAKE_CFLAGS += -Wno-duplicate-decl-specifier

View file

@ -357,6 +357,11 @@ void LIB_HANDLER()
TokenStart=(WORDPTR)ptr;
RetNum=OK_SPLITTOKEN;
} else RetNum=OK_CONTINUE;
// DROP THE COMMENT DEPENDING ON FLAGS
if( (mode!=0x40000002) && (rplTestSystemFlag(FL_STRIPCOMMENTS)==1)) CompileEnd=ScratchPointer4;
return;
}
if(count==0) temp.word=0;
@ -452,6 +457,11 @@ void LIB_HANDLER()
TokenStart=(WORDPTR)ptr;
RetNum=OK_SPLITTOKEN;
} else RetNum=OK_CONTINUE;
// DROP THE COMMENT DEPENDING ON FLAGS
if( (mode!=0x40000002) && (rplTestSystemFlag(FL_STRIPCOMMENTS)==1)) CompileEnd=ScratchPointer4;
return;
}
if(count==0) temp.word=0;