elna/example.elna

174 lines
2.9 KiB
Plaintext

type
T = array 5 of Int,
TokenValue = union
intValue: Int;
stringValue: String
end,
Token = record
kind: Int;
value: TokenValue
end,
FILE = record
dummy: Int
end;
const
SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2;
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: Token;
begin
writei("");
writei("Test record:");
r.kind := 4;
r.value.intValue := 8;
writei(r.value.intValue)
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_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_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 fread(ptr: pointer to Char, size: Int, nmemb: Int, stream: pointer to FILE): Int; extern;
proc write(fd: Int, buf: pointer to Char, count: Int): Int; extern;
proc malloc(size: Int): pointer to Char; extern;
proc free(ptr: pointer to Char); extern;
proc calloc(nmemb: Int, size: Int): pointer to Char; extern;
proc memset(ptr: pointer to Char, c: Int, n: Int): pointer to Char; extern;
proc read_source(filename: String): pointer to Char;
var
input_file: pointer to FILE,
source_size: Int,
input: pointer to Char;
begin
input_file := fopen(filename, "rb");
fseek(input_file, 0, SEEK_END);
source_size := ftell(input_file);
fseek(input_file, 0, SEEK_SET);
input := calloc(source_size + 1, 1);
fread(input, source_size, 1, input_file);
fclose(input_file);
return input
end;
proc compile();
var
input: pointer to Char,
input_pointer: pointer to Char;
begin
input := read_source("example.elna");
input_pointer := input;
while input_pointer^ /= '\0' do
write(0, input_pointer, 1);
input_pointer := input_pointer + 1
end;
free(input)
end;
begin
test_primitive();
test_array();
test_record();
test_if();
test_param(8, 7);
test_const_char();
writei(test_return_int());
test_add_pointer();
compile();
exit(0)
end.