flat assembler
Message board for the users of flat assembler.
Index
> Main > How to properly invoke FlatAssembler from an external progra |
Author |
|
UniverseIsASimulation 17 Apr 2020, 17:37
So, I've made a compiler for my own simplified low-level programming language, the source code is available here, and the executable files are available here. Now, the compiler, "aec.exe" or "aec.c" produces assembly code that can be compiled with FlatAssembler, however, it can't invoke FlatAssembler even if it's installed. The compiler itself is written in JavaScript, in the files "compiler.js" and "compiler.js", and "aec.c" mainly initializes Duktape (the JavaScript engine written in C). So, here is the contents of the "aec.c" file (compiled into "aec.exe" executable):
Code: #include <stdio.h> #include <string.h> #include "duktape.h" #include "tcc-patch.h" void handle_fatal(void *udata,const char *msg) { fprintf(stderr,"Fatal error in Javascript: %s\n",(msg?msg:"no message")); exit(1); } void catch(void *ctx) { fprintf(stderr,"JavaScript reports the following error:\n\"%s\"\n",duk_safe_to_string(ctx,-1)); duk_destroy_heap(ctx); exit(1); } int main(int argc, char **argv) { if (argc-2 or strlen(argv[1])<5 or (strcmp(argv[1]+strlen(argv[1])-4,".AEC") and strcmp(argv[1]+strlen(argv[1])-4,".aec"))) { fprintf(stderr,"Please invoke this program from command-line and supply an AEC source file as the only argument, for example:\n" "Windows: aec.exe euclid.aec\n" "Linux and MacOS: ./aec euclid.aec\n"); return 1; } #include "readCompiler.h" input=fopen("control.js","r"); if (!input) { fprintf(stderr,"Can't open 'control.js'?!\n"); fflush(stderr); return 1; } while (!feof(input)) if (fscanf(input,"%[^\n]\n",line)){ strcat(line,"\n"); strcat(compilerString,line); } fclose(input); fprintf(stderr,"Initializing Duktape!\n"); fflush(stderr); duk_context *ctx=duk_create_heap(0,0,0,0,handle_fatal); duk_push_c_function(ctx,getIEEE754,1); duk_put_global_string(ctx,"getIEEE754"); duk_eval_string(ctx,compilerString); free(compilerString),free(line); duk_set_top(ctx,1); fprintf(stderr,"Compilation started!\n"); fflush(stderr); input=fopen(argv[1],"r"); if (!input) { fprintf(stderr,"Can't read \'%s\'!\n",argv[1]); fflush(stderr); return 1; } int lineNumber=0; line=calloc(512,1); int hasError=0; compilerString=calloc(1024,1); if (!compilerString or !line) { fprintf(stderr,"Out of memory!?\n"); return 1; } while (!feof(input)) { lineNumber++; if (fgets(line,512,input)) { if (line[strlen(line)-1]=='\n') line[strlen(line)-1]=0; if (line[strlen(line)-1]=='\r') line[strlen(line)-1]=0; memset(compilerString,0,512); strcat(compilerString,"compileString(\""); int i; for (i=0; line[i]; i++) if (line[i]=='\\') strcat(compilerString,"\\\\"); else if (line[i]=='"') strcat(compilerString,"\\\""); else strncat(compilerString,line+i,1); strcat(compilerString,"\")"); duk_push_string(ctx,compilerString); if (duk_peval(ctx)) { const char *message=duk_safe_to_string(ctx,-1); if (strstr(message,"error")) hasError=1; fprintf(stderr,"Message on line %d:\n\"%s\"\n",lineNumber,message); } duk_pop(ctx); } } free(line); free(compilerString); fclose(input); if (!hasError) { duk_eval_string(ctx,"stack.length"); if (duk_get_int(ctx,-1)) { fprintf(stderr,"Control structures ('If', 'Else', 'While', 'EndIf' and 'EndWhile') appear not to be properly nested, compilation aborted!\n"); fflush(stderr); return 1; } char *outputFileName=calloc(265,1); strncat(outputFileName,argv[1],strlen(argv[1])-4); strcat(outputFileName,".asm"); FILE *output=fopen(outputFileName,"w"); free(outputFileName); if (!output) { fprintf(stderr,"Can't open output file!\n"); fflush(stderr); return 1; } duk_eval_string(ctx,"assembler"); fprintf(output,";Generated by Arithmetic Expression Compiler (https://flatassembler.github.io/compiler.html) run in Duktape.\n" "%s",duk_get_string(ctx,-1)); fclose(output); fprintf(stderr,"Compilation finished without errors!\n"); } else fprintf(stderr,"Compilation aborted due to the errors reported by JavaScript!\n"); duk_destroy_heap(ctx); return 0; } |
|||
17 Apr 2020, 17:37 |
|
Tomasz Grysztar 20 Apr 2020, 12:23
The required setup of INCLUDE environment variable is documented in section 1.1.1 of FASM.PDF.
|
|||
20 Apr 2020, 12:23 |
|
UniverseIsASimulation 20 Apr 2020, 15:00
Tomasz Grysztar wrote: The required setup of INCLUDE environment variable is documented in section 1.1.1 of FASM.PDF. So, how would you go about invoking FlatAssembler from an external program? Would you first check whether it's present in $path with system("fasm > nul")? If it is, then what? Somehow list all files in all the folders in $path to see in which "fasm.exe" is present, and then do "setenv" to add that folder plus "/include/" into $include, and then finally do system("fasm assembly.asm")? Or do you need to do something more? |
|||
20 Apr 2020, 15:00 |
|
DimonSoft 20 Apr 2020, 17:27
UniverseIsASimulation wrote: OK, I missed that, and it helps a little. It's not included in the documentation on the web. What about this? https://flatassembler.net/docs.php?article=manual#2.3.1 |
|||
20 Apr 2020, 17:27 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.