type 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; proc write_i(value: Int); var digit: Int, n: Int, buffer: array 10 of Char; begin n := 9; buffer[9] := '0'; while value /= 0 do digit := value % 10; value := value / 10; buffer[n] := Char(Int('0') + digit); n := n - 1 end; while n < 10 do n := n + 1; write_c(buffer[n]) end end; proc write_u(value: Word); begin write_i(value) end; -- -- End of standard procedures. -- proc test_record(); var r: Token; begin write_s("\nTest record:\n"); r.kind := 4; r.value.intValue := 8; write_i(r.value.intValue) end; proc test_primitive(); begin write_s("\nTest primitives:\n"); write_u(25u); write_c('\n'); write_i(8); write_c('\n'); write_b(true); write_c('\n') 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 compile(); test_record(); test_primitive(); exit(0) end.