X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Ftest.c;h=8029be718d08bc4823e6723657a66441b68fe651;hb=3cd41dedaffe60018890cbe66baea63691fe62e8;hp=a144f3616074e1f8891dbc30af0007420bad0ef9;hpb=51a1ab2543ec5c221d4a3a9ab89968ae7dd39981;p=dsw-obn.git diff --git a/src/test.c b/src/test.c index a144f36..8029be7 100644 --- a/src/test.c +++ b/src/test.c @@ -1,166 +1,59 @@ #include +#include #include #include #include "../include/oberon.h" -static char source_test[] = - "(* Main module *)" - "MODULE Test;" - "IMPORT Out;" - "" - "TYPE" - " R = INTEGER;" - "" - "PROCEDURE Factorial(n : R) : R;" - "BEGIN" - " IF n <= 1 THEN" - " RETURN 1;" - " ELSE" - " RETURN n * Factorial(n - 1);" - " END;" - " RETURN 0; (* Детектор ретурнов - дерьмо *)" - "END Factorial;" - "" - "BEGIN" - " Out.Open();" - " Out.Int(Factorial(0), 0); Out.Ln;" - " Out.Int(Factorial(1), 0); Out.Ln;" - " Out.Int(Factorial(2), 0); Out.Ln;" - " Out.Int(Factorial(3), 0); Out.Ln;" - " Out.Int(Factorial(4), 0); Out.Ln;" - " Out.Int(Factorial(5), 0); Out.Ln;" - " Out.Int(Factorial(6), 0); Out.Ln;" - " Out.Int(Factorial(7), 0); Out.Ln;" - " Out.Int(Factorial(8), 0); Out.Ln;" - " Out.Int(Factorial(9), 0); Out.Ln;" - " Out.Int(Factorial(10), 0); Out.Ln;" - " Out.Int(Factorial(11), 0); Out.Ln;" - " Out.Int(Factorial(12), 0); Out.Ln;" - " Out.Int(Factorial(13), 0); Out.Ln;" - " Out.Int(Factorial(14), 0); Out.Ln;" - "END Test." -; - -static char source_out[] = - "MODULE Out;" - " PROCEDURE Open*;" - " END Open;" - "" - " PROCEDURE Char* (ch : CHAR);" - " END Char;" - "" - " PROCEDURE String* (str : ARRAY OF CHAR);" - " END String;" - "" - " PROCEDURE Int*(i, n : LONGINT);" - " END Int;" - "" - " PROCEDURE Real*(x : REAL; n : INTEGER);" - " END Real;" - "" - " PROCEDURE LongReal*(x : LONGREAL; n : INTEGER);" - " END LongReal;" - "" - " PROCEDURE Ln*;" - " END Ln;" - "" - "END Out." -; - static oberon_context_t * ctx; static oberon_module_t * mod; static const char * import_module(const char * name) { - if(strcmp(name, "Test") == 0) - { - return source_test; - } - else if(strcmp(name, "Out") == 0) - { - return source_out; - } - else - { - return NULL; - } -} - -typedef void (*TOutOpen)(); -static TOutOpen * OutOpenPtr; -void ImplOutOpen() -{ -} + assert(name); -typedef void (*TOutInt)(int, int); -static TOutInt * OutIntPtr; -void ImplOutInt(int i, int n) -{ - char number[22]; - snprintf(number, 22, "%d", i); - int len = strlen(number); - for(int i = 0; i < n - len; i++) + FILE * fp; + char fname[256]; + snprintf(fname, 256, "%s.obn", name); + fp = fopen(fname, "r"); + if(fp == NULL) { - putchar(' '); + printf("can't open file %s\n", fname); + exit(1); + return NULL; } - printf("%s", number); -} -typedef void (*TOutReal)(float, int); -static TOutReal * OutRealPtr; -void ImplOutReal(float i, int n) -{ - char number[32]; - snprintf(number, 32, "%F", i); - int len = strlen(number); - for(int i = 0; i < n - len; i++) - { - putchar(' '); - } - printf("%s", number); -} + char * source; + size_t len; -typedef void (*TOutLn)(); -static TOutLn * OutLnPtr; -void ImplOutLn() -{ - putchar('\n'); -} + fseek(fp, 0, SEEK_END); + len = ftell(fp); + fseek(fp, 0, SEEK_SET); -void init_system_modules() -{ - OutOpenPtr = oberon_generator_get_var(ctx, "Out_Open"); - *OutOpenPtr = ImplOutOpen; - OutIntPtr = oberon_generator_get_var(ctx, "Out_Int"); - *OutIntPtr = ImplOutInt; - OutRealPtr = oberon_generator_get_var(ctx, "Out_Real"); - *OutRealPtr = ImplOutReal; - OutLnPtr = oberon_generator_get_var(ctx, "Out_Ln"); - *OutLnPtr = ImplOutLn; -} + source = calloc(1, len + 1); + fread(source, len, 1, fp); -void start_module() -{ - void (*begin)() = oberon_generator_get_procedure(ctx, "Test_BEGIN"); - begin(); + return source; } int main(int argc, char ** argv) { - ctx = oberon_create_context(import_module); - mod = oberon_compile_module(ctx, source_test); - - oberon_generate_code(ctx); + const char * code; -// init_system_modules(); + if(argc != 2) + { + printf("use: %s \n", argv[0]); + return 1; + } -// oberon_generator_dump(ctx, "dump.txt"); + code = import_module(argv[1]); -// start_module(); + ctx = oberon_create_context(import_module); + mod = oberon_compile_module(ctx, code); + oberon_generate_code(ctx); oberon_destroy_context(ctx); return 0; }