168 lines
2.8 KiB
Plaintext
168 lines
2.8 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;
|
|
|
|
--
|
|
-- External procedures.
|
|
--
|
|
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 strlen(ptr: pointer to Char): Word; extern;
|
|
|
|
proc exit(code: Int); extern;
|
|
|
|
--
|
|
-- Standard procedures.
|
|
--
|
|
proc write_s(value: String);
|
|
begin
|
|
write(0, value, strlen(value))
|
|
end;
|
|
|
|
proc write_b(value: Bool);
|
|
begin
|
|
if value then
|
|
write_s("true")
|
|
else
|
|
write_s("false")
|
|
end
|
|
end;
|
|
|
|
proc write_c(value: Char);
|
|
begin
|
|
write(0, @value, 1)
|
|
end;
|
|
|
|
--
|
|
-- End of standard procedures.
|
|
--
|
|
|
|
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 u: Word, z: Float;
|
|
begin
|
|
u := 25u;
|
|
z := 8.2;
|
|
|
|
writei("");
|
|
writei("Test primitives:");
|
|
writei(u);
|
|
writei(z)
|
|
end;
|
|
|
|
proc test_param(d: Int, e: Int);
|
|
begin
|
|
writei("");
|
|
writei("Test param");
|
|
writei(d);
|
|
writei(e)
|
|
end;
|
|
|
|
proc test_return_int(): Int;
|
|
begin
|
|
writei("");
|
|
writei("Test return int:");
|
|
return 5
|
|
end;
|
|
|
|
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_param(8, 7);
|
|
writei(test_return_int());
|
|
write_b(true);
|
|
write_c('\n');
|
|
|
|
compile();
|
|
|
|
exit(0)
|
|
end.
|