September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,12 @@
begin
string(80) line;
% allow the program to continue after reaching end-of-file %
% without this, end-of-file would cause a run-time error %
ENDFILE := EXCEPTION( false, 1, 0, false, "EOF" );
% read lines until end of file %
read( line );
while not XCPNOTED(ENDFILE) do begin
write( line );
read( line )
end
end.

View file

@ -1,11 +0,0 @@
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
SYS "GetStdHandle", STD_INPUT_HANDLE TO @hfile%(1)
SYS "GetStdHandle", STD_OUTPUT_HANDLE TO @hfile%(2)
SYS "SetConsoleMode", @hfile%(1), 0
*INPUT 13
*OUTPUT 14
REPEAT
INPUT A$
PRINT A$
UNTIL FALSE

View file

@ -1,9 +1,8 @@
#import system.
#import system'routines.
#import system'io.
#import extensions'routines.
import system'routines.
import system'io.
import extensions'routines.
#symbol program =
program =
[
ReaderEnumerator new:("file.txt" file_path textreader) run &each:printingLn.
ReaderEnumerator new(File new:"file.txt"); forEach:printingLn.
].

View file

@ -1,11 +1,10 @@
#import system.
#import system'io.
import system'io.
#symbol program =
program =
[
#var reader := "file.txt" file_path textreader.
#loop (reader available)?
var reader := File new:"file.txt"; textreader.
while (reader available)
[
console writeLine:(reader read &literal).
console writeLine(reader readLiteral).
].
].

View file

@ -0,0 +1 @@
!cat

View file

@ -0,0 +1,17 @@
// version 1.1
import java.util.*
fun main(args: Array<String>) {
println("Keep entering text or the word 'quit' to end the program:")
val sc = Scanner(System.`in`)
val words = mutableListOf<String>()
while (true) {
val input: String = sc.next()
if (input.trim().toLowerCase() == "quit") {
if (words.size > 0) println("\nYou entered the following words:\n${words.joinToString("\n")}")
return
}
words.add(input)
}
}

View file

@ -0,0 +1,5 @@
var %n = 1
while (%n <= $lines(input.txt)) {
write output.txt $read(input.txt,%n)
inc %n
}

View file

@ -0,0 +1,9 @@
readinput:=proc(filename)
local line,file;
file:="";
line:=readline(filename);
while line<>0 do
line:=readline(filename);
file:=cat(file,line);
end do;
end proc;

View file

@ -0,0 +1,10 @@
procedure process_line_by_line(integer fn)
object line
while 1 do
line = gets(fn)
if atom(line) then
exit
end if
-- process the line
end while
end procedure

View file

@ -1,6 +1,3 @@
my_file = open(filename, 'r')
try:
for line in my_file:
pass # process line, includes newline
finally:
my_file.close()
while(True):
x = input("What is your age? ")
print(x)

View file

@ -1,5 +1,6 @@
from __future__ import with_statement
with open(filename, 'r') as f:
for line in f:
my_file = open(filename, 'r')
try:
for line in my_file:
pass # process line, includes newline
finally:
my_file.close()

View file

@ -1,2 +1,5 @@
line = my_file.readline() # returns a line from the file
lines = my_file.readlines() # returns a list of the rest of the lines from the file
from __future__ import with_statement
with open(filename, 'r') as f:
for line in f:
pass # process line, includes newline

View file

@ -1,3 +1,2 @@
import fileinput
for line in fileinput.input():
pass # process line, includes newline
line = my_file.readline() # returns a line from the file
lines = my_file.readlines() # returns a list of the rest of the lines from the file

View file

@ -0,0 +1,3 @@
import fileinput
for line in fileinput.input():
pass # process line, includes newline

View file

@ -0,0 +1,19 @@
use std::io::{self, BufReader, Read, BufRead};
use std::fs::File;
fn main() {
print_by_line(io::stdin())
.expect("Could not read from stdin");
File::open("/etc/fstab")
.and_then(print_by_line)
.expect("Could not read from file");
}
fn print_by_line<T: Read>(reader: T) -> io::Result<()> {
let buffer = BufReader::new(reader);
for line in buffer.lines() {
println!("{}", line?)
}
Ok(())
}

View file

@ -1,3 +0,0 @@
scala.io.Source.fromFile(filename).getLines.foreach {
line => // do something
}

View file

@ -1,3 +0,0 @@
scala.io.Source.fromPath(filename).getLines().foreach {
line => // do something
}

View file

@ -0,0 +1,149 @@
#define SYS_WRITE $1
#define SYS_OPEN $2
#define SYS_CLOSE $3
#define SYS_FSTAT $5
#define SYS_MMAP $9
#define SYS_MUNMAP $11
#define SYS_EXIT $60
// From experiments:
#define FSIZEOFF 48
#define STATSIZE 144
// From Linux source:
#define RDONLY $00
#define PROT_READ $0x1
#define MAP_PRIVATE $0x02
#define STDIN $0
#define STDOUT $1
.global _start
.text
/* Details: */
/*
Remember: %rax(%rdi, %rsi, %rdx, %r10, %r8, %r9)
- Open a file (get its fd)
- int fd = open("filename", RDONLY)
- Get its filesize:
- fstat(fd, statstruct). 0 if ok. fsize at statstruct+48
- Then memory map it.
- void* vmemptr = mmap(vmemptr, fsize, PROT_READ, MAP_PRIVATE, fd, 0)
- Scan for newlines, print line.
- Keep going until done. Details at 11.
- Unmap memory
- munmap(vmemptr, filesize). 0 if ok.
- Exit
*/
.macro ERRCHECK code
cmpq $\code, %rax
je fs_error
.endm
/* Local stack notes:
0: int fd
4: void* vmemptr
12: void* head
20: void* lookahead
28: void* end
*/
_start:
// Open:
movq RDONLY, %rsi
// Filename ptr is on stack currently as argv[1]:
cmpq $1, (%rsp) // if argc is 1, default to stdin
jnz open_file
subq $36, %rsp // local stack
movl STDIN, (%rsp)
jmp fstat
open_file:
movq 16(%rsp), %rdi // argc(8), argv0(8) => rsp+16. filename
movq SYS_OPEN, %rax
syscall
ERRCHECK -1
subq $36, %rsp // local stack
movl %eax, (%rsp) // int fd = open(argv[1], RDONLY)
// fstat to get filesize
fstat:
movq $statstruct, %rsi
movl (%rsp), %edi // fd
movq SYS_FSTAT, %rax
syscall // fstat(fd, statstruct)
ERRCHECK -1
// mmap - don't forget to munmap.
mmap:
movq $0, %r9 // offset
movq (%rsp), %r8 // fd
movq MAP_PRIVATE, %r10
movq PROT_READ, %rdx
movq filesize, %rsi
movq (%rsp), %rdi // vmemptr
movq SYS_MMAP, %rax
syscall
ERRCHECK -1
movq %rax, 4(%rsp) // void* vmemptr = mmap(vmemptr, fsize, PROT_READ, MAP_PRIVATE, fd, 0)
/* Print lines */
movq %rax, 12(%rsp) // head = vmemptr
addq filesize, %rax
decq %rax
movq %rax, 28(%rsp) // end = vmemptr+filesize-1
scan_outer:
movq 12(%rsp), %rax
cmpq 28(%rsp), %rax
jge cleanup // if head >= end, done
movq %rax, %rbx // Using rbx as lookahead
scan_inner:
cmpq 28(%rsp), %rbx
jge writeline // if lookahead >= end, write the line.
cmpb $'\n, (%rbx)
jz writeline // if '\n'==*lookahead, write the line
incq %rbx
jmp scan_inner
writeline:
// write:
incq %rbx
movq %rbx, %rdx
subq 12(%rsp), %rdx // rdx <- lookahead-head
movq 12(%rsp), %rsi
movq STDOUT, %rdi
movq SYS_WRITE, %rax
syscall // write(stdout, head, lookahead-head)
safety:
movq %rbx, 12(%rsp) // head = lookahead.
jmp scan_outer
cleanup:
// munmap
movq filesize, %rsi
movq 4(%rsp), %rdi
movq SYS_MUNMAP, %rax
syscall // munmap(vmemptr, filesize)
cmpq $-1, %rax
je fs_error
// close
movl (%rsp), %edi
movq SYS_CLOSE, %rax
syscall // close(fd)
ERRCHECK -1
exit:
movq SYS_EXIT, %rax
xorq %rdi, %rdi // The exit code.
syscall
fs_error:
movq SYS_EXIT, %rax
movq $-1, %rdi
syscall // exit(-1)
.data
statstruct: // This struct is 144 bytes. Only want size (+48)
.zero FSIZEOFF
filesize: // 8 bytes.
.quad 0
.zero STATSIZE-FSIZEOFF+8

View file

@ -0,0 +1,4 @@
foreach line in (File("foo.txt")){...}
List(1,2,3).readln() // here, a "line" is a list element
Utils.Helpers.zipWith(False, // enumerate a file
fcn(n,line){"%3d: %s".fmt(n,line).print()},[1..],File("cmp.zkl"))