5 #include "../include/oberon.h"
7 static char source_test
[] =
15 "PROCEDURE Factorial(n : R) : R;"
20 " RETURN n * Factorial(n - 1);"
22 " RETURN 0; (* Детектор ретурнов - дерьмо *)"
27 " Out.Int(Factorial(0), 0); Out.Ln;"
28 " Out.Int(Factorial(1), 0); Out.Ln;"
29 " Out.Int(Factorial(2), 0); Out.Ln;"
30 " Out.Int(Factorial(3), 0); Out.Ln;"
31 " Out.Int(Factorial(4), 0); Out.Ln;"
32 " Out.Int(Factorial(5), 0); Out.Ln;"
33 " Out.Int(Factorial(6), 0); Out.Ln;"
34 " Out.Int(Factorial(7), 0); Out.Ln;"
35 " Out.Int(Factorial(8), 0); Out.Ln;"
36 " Out.Int(Factorial(9), 0); Out.Ln;"
37 " Out.Int(Factorial(10), 0); Out.Ln;"
38 " Out.Int(Factorial(11), 0); Out.Ln;"
39 " Out.Int(Factorial(12), 0); Out.Ln;"
40 " Out.Int(Factorial(13), 0); Out.Ln;"
41 " Out.Int(Factorial(14), 0); Out.Ln;"
45 static char source_out
[] =
50 " PROCEDURE Char* (ch : CHAR);"
53 " PROCEDURE String* (str : ARRAY OF CHAR);"
56 " PROCEDURE Int*(i, n : LONGINT);"
59 " PROCEDURE Real*(x : REAL; n : INTEGER);"
62 " PROCEDURE LongReal*(x : LONGREAL; n : INTEGER);"
71 static oberon_context_t
* ctx
;
72 static oberon_module_t
* mod
;
75 import_module(const char * name
)
77 if(strcmp(name
, "Test") == 0)
81 else if(strcmp(name
, "Out") == 0)
91 typedef void (*TOutOpen
)();
92 static TOutOpen
* OutOpenPtr
;
97 typedef void (*TOutInt
)(int, int);
98 static TOutInt
* OutIntPtr
;
99 void ImplOutInt(int i
, int n
)
102 snprintf(number
, 22, "%d", i
);
103 int len
= strlen(number
);
104 for(int i
= 0; i
< n
- len
; i
++)
108 printf("%s", number
);
111 typedef void (*TOutReal
)(float, int);
112 static TOutReal
* OutRealPtr
;
113 void ImplOutReal(float i
, int n
)
116 snprintf(number
, 32, "%F", i
);
117 int len
= strlen(number
);
118 for(int i
= 0; i
< n
- len
; i
++)
122 printf("%s", number
);
125 typedef void (*TOutLn
)();
126 static TOutLn
* OutLnPtr
;
132 void init_system_modules()
134 OutOpenPtr
= oberon_generator_get_var(ctx
, "Out_Open");
135 *OutOpenPtr
= ImplOutOpen
;
136 OutIntPtr
= oberon_generator_get_var(ctx
, "Out_Int");
137 *OutIntPtr
= ImplOutInt
;
138 OutRealPtr
= oberon_generator_get_var(ctx
, "Out_Real");
139 *OutRealPtr
= ImplOutReal
;
140 OutLnPtr
= oberon_generator_get_var(ctx
, "Out_Ln");
141 *OutLnPtr
= ImplOutLn
;
146 void (*begin
)() = oberon_generator_get_procedure(ctx
, "Test_BEGIN");
151 main(int argc
, char ** argv
)
153 ctx
= oberon_create_context(import_module
);
154 mod
= oberon_compile_module(ctx
, source_test
);
156 oberon_generate_code(ctx
);
158 // init_system_modules();
160 // oberon_generator_dump(ctx, "dump.txt");
164 oberon_destroy_context(ctx
);