#include #include #include #include "../include/oberon.h" /* static char source_test[] = "(* Main module *)" "MODULE Test;" "IMPORT Out;" "" "TYPE" " R = LONGINT;" "" "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;" " Out.Int(Factorial(15), 0); Out.Ln;" " Out.Int(Factorial(16), 0); Out.Ln;" " Out.Int(Factorial(17), 0); Out.Ln;" " Out.Int(Factorial(18), 0); Out.Ln;" " Out.Int(Factorial(19), 0); Out.Ln;" " Out.Int(Factorial(20), 0); Out.Ln;" "END Test." ; */ static char source_test[] = "(* Main module *)" "MODULE Test;" "IMPORT Out;" "" "TYPE" " R = LONGREAL;" "" "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.LongReal(Factorial(0), 0); Out.Ln;" " Out.LongReal(Factorial(1), 0); Out.Ln;" " Out.LongReal(Factorial(2), 0); Out.Ln;" " Out.LongReal(Factorial(3), 0); Out.Ln;" " Out.LongReal(Factorial(4), 0); Out.Ln;" " Out.LongReal(Factorial(5), 0); Out.Ln;" " Out.LongReal(Factorial(6), 0); Out.Ln;" " Out.LongReal(Factorial(7), 0); Out.Ln;" " Out.LongReal(Factorial(8), 0); Out.Ln;" " Out.LongReal(Factorial(9), 0); Out.Ln;" " Out.LongReal(Factorial(10), 0); Out.Ln;" " Out.LongReal(Factorial(11), 0); Out.Ln;" " Out.LongReal(Factorial(12), 0); Out.Ln;" " Out.LongReal(Factorial(13), 0); Out.Ln;" " Out.LongReal(Factorial(14), 0); Out.Ln;" " Out.LongReal(Factorial(15), 0); Out.Ln;" " Out.LongReal(Factorial(16), 0); Out.Ln;" " Out.LongReal(Factorial(17), 0); Out.Ln;" " Out.LongReal(Factorial(18), 0); Out.Ln;" " Out.LongReal(Factorial(19), 0); Out.Ln;" " Out.LongReal(Factorial(20), 0); Out.Ln;" " Out.LongReal(Factorial(170), 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() { } 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++) { putchar(' '); } 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); } typedef void (*TOutLn)(); static TOutLn * OutLnPtr; void ImplOutLn() { putchar('\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; OutRealPtr = oberon_generator_get_var(ctx, "Out_Real"); *OutRealPtr = ImplOutReal; 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(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; }