X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=test.c;h=c3a4b1467f51256bbec89e210374cb69d90a44d8;hb=d1b4e7fdab92589146e19cf4b8b402edf4d6b33d;hp=b07a08d0898a7f2816a6bb281c9f6050ac31d9bb;hpb=734fa8f9223c140a583e7f193df810d48555ac34;p=dsw-obn.git diff --git a/test.c b/test.c index b07a08d..c3a4b14 100644 --- a/test.c +++ b/test.c @@ -1,41 +1,104 @@ #include "oberon.h" #include "generator.h" + +#include #include -static const char source[] = +static char source_test[] = + "(* Main module *)" "MODULE Test;" - "TYPE" - " Rec = RECORD i : INTEGER; END;" - "VAR" - " i : INTEGER;" - " j : INTEGER;" - "" - "PROCEDURE ^ Tier(x : INTEGER);" - "" - "PROCEDURE Tier(x : INTEGER);" - "VAR a : INTEGER;" + "IMPORT Out;" "BEGIN;" - " a := 1;" - "END Tier;" - "" - "PROCEDURE ^ Tier(x : INTEGER);" - "" - "BEGIN;" - " i := 666;" - " Tier(1);" + " Out.Open;" + " Out.Int(666, 0);" + " Out.Ln;" "END Test." ; +static char source_out[] = + "MODULE Out;" + "VAR" + " Open- : PROCEDURE;" +// " Char- : PROCEDURE(ch : CHAR);" +// " String- : PROCEDURE(str : ARRAY OF CHAR)" +// " Int- : PROCEDURE(i, n : LONGINT);" + " Int- : PROCEDURE(i, n : INTEGER);" +// " Real- : PROCEDURE(x : REAL; n : INTEGER);" +// " LongReal- : PROCEDURE(x : LONGREAL; n : INTEGER);" + " Ln- : PROCEDURE;" + "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() +{ +} + +typedef void (*TOutInt)(int, int); +static TOutInt * OutIntPtr; +void ImplOutInt(int i, int n) +{ + printf("%i", i); +} + +typedef void (*TOutLn)(); +static TOutLn * OutLnPtr; +void ImplOutLn() +{ + printf("\n"); +} + +void init_system_modules() +{ + OutOpenPtr = oberon_generator_get_var(ctx, "Out_Open"); + *OutOpenPtr = ImplOutOpen; + OutIntPtr = oberon_generator_get_var(ctx, "Out_Int"); + *OutIntPtr = ImplOutInt; + OutLnPtr = oberon_generator_get_var(ctx, "Out_Ln"); + *OutLnPtr = ImplOutLn; +} + +void start_module() +{ + void (*begin)() = oberon_generator_get_procedure(ctx, "Test_BEGIN"); + begin(); +} + int main(int argc, char ** argv) { - ctx = oberon_create_context(); - mod = oberon_compile_module(ctx, source); - //mod -> begin(); + ctx = oberon_create_context(import_module); + mod = oberon_compile_module(ctx, source_test); + + oberon_generate_code(ctx); + + init_system_modules(); + oberon_generator_dump(ctx, "dump.txt"); + + start_module(); + oberon_destroy_context(ctx); return 0; }