elna/example.elna

197 lines
2.7 KiB
Plaintext

type
T = array 5 of Int,
R = record
x: Int;
y: Int
end,
FILE = record
x: Int
end,
U = union
a: Int;
b: Int
end;
proc test_string();
var s: String;
begin
s := "Test string.";
writei("");
writei(s)
end;
proc test_array();
var a: T, x: Int;
begin
a[0] := 2;
a[1] := 5;
writei("");
writei("Test array:");
x := 0;
while x < 2 do
writei(a[x]);
x := x + 1
end
end;
proc test_record();
var r: R;
begin
writei("");
writei("Test record:");
r.x := 4;
r.y := 8;
writei(r.y)
end;
proc test_union();
var u: U;
begin
writei("");
writei("Test union:");
u.a := 9;
writei(u.b)
end;
proc test_primitive();
var c: Char, z: Float;
begin
c := 'x';
z := 8.2;
writei("");
writei("Test primitives:");
writei(c);
writei(z)
end;
proc test_const();
const t = 5;
var x: Int;
begin
x := t;
writei("");
writei("Test const:");
writei(x)
end;
proc test_if();
var x: Bool, y: Bool;
begin
x := true;
y := false;
writei("");
if x and y then
writei("Test if: True")
else
writei("Test if: False")
end
end;
proc test_not();
var x: Bool;
begin
x := false;
writei("");
if not x then
writei("Test not true.")
else
writei("Test not false")
end
end;
proc test_param(d: Int, e: Int);
begin
writei("");
writei("Test param");
writei(d);
writei(e)
end;
proc test_const_char();
const x = 'u';
begin
writei("");
writei("Test constant character");
writei(x)
end;
proc test_return_int(): Int;
begin
writei("");
writei("Test return int:");
return 5
end;
proc exit(code: Int); extern;
proc test_add_pointer();
var x: Int, p1: pointer to Int;
begin
writei("");
writei("Test add pointer:");
x := 5;
p1 := @x;
writei(p1);
p1 := p1 + 2;
writei(p1)
end;
proc fopen(pathname: String, mode: String): pointer to FILE; extern;
proc fclose(stream: pointer to FILE): Int; extern;
proc fseek(stream: pointer to FILE, off: Int, whence: Int): Int; extern;
proc ftell(stream: pointer to FILE): Int; extern;
proc compile();
const
SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2;
var
input_file: pointer to FILE,
source_size: Int, ret: Int;
begin
input_file := fopen("example.elna", "rb");
// Bug: Functions as statements aren't called.
ret := fseek(input_file, 0, SEEK_END);
source_size := ftell(input_file);
ret := fseek(input_file, 0, SEEK_SET);
writei("");
writei("File size: ");
writei(input_file);
writei(source_size);
ret := fclose(input_file)
end;
begin
test_primitive();
test_string();
test_array();
test_record();
test_const();
test_if();
test_not();
test_param(8, 7);
test_const_char();
test_union();
writei(test_return_int());
test_add_pointer();
compile();
exit(0)
end.