Preface
Scheme is a Lisp dialect created in 1975 by MITβs Gerald J. Sussman and Guy L. Steele Jr. It is one of the two major modern Lisp dialects; the other is Common Lisp. Despite its long history, Scheme remains active and has implementations for many platforms and environments, such as Racket, Guile, MIT Scheme, and Chez Scheme. TinyScheme is a lightweight embeddable Scheme interpreter that follows the R5RS (Revised^5 Report on the Algorithmic Language Scheme) specification. This specification was released in 1998 and is now widely used. Although TinyScheme lacks extensive official documentation, its adherence to the R5RS standard enables developers to consult documentation from mainstream Scheme implementations, such as Racket, for guidance.
Install
Download:
https://sourceforge.net/projects/tinyscheme/files/tinyscheme/
$ make all
$ ./scheme
TinyScheme 1.41
ts>
Directory layout:
.
βββ BUILDING
βββ CHANGES
βββ COPYING
βββ dynload.c
βββ dynload.h
βββ hack.txt
βββ init.scm
βββ makefile
βββ Manual.txt
βββ MiniSCHEMETribute.txt
βββ opdefines.h # Defines TinyScheme reserved keywords; developers can add custom features here.
βββ scheme.c # Implements the core interpreter logic.
βββ scheme.h
βββ scheme-private.h
Read a file
# open a file
(define shadow (open-input-file "/etc/shadow"))
# read one line and print it
(read shadow)
Write a file
# open a file for output
(define ou (open-output-file "/tmp/shadow"))
# define a variable containing the string to write
(define text "root:aaddd.asdasd.:13333:0:99999:3:::")
# display the content to standard output
(display text)
# write the string representation (including quotes) to the file
(write text ou)
# write a single character 'c' to the file
(write-char #\c ou)
# write a newline character to the file
(newline ou)
# save changes and close the file
(close-output-port ou)
When using write to save a string variable to a file, the output will include double quotes. To avoid this, writing characters individually is often preferred. Additionally, attempting to write a large volume of data at once may cause failures.
Furthermore, when interacting with a remote TinyScheme shell, standard newline characters (\n) might not be processed correctly; in such cases, the (newline port) function should be used instead.
The following Python script generates Scheme code to write a file byte-by-byte, handling special characters appropriately:
#!/usr/bin/python3
import argparse
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser(
description="Write file by tinyscheme")
parser.add_argument(
"-l", "--local-file", help="directory to be read", type=str, required=True)
parser.add_argument(
"-t", "--target-file", help="path to write the file to", type=str, required=True)
args = parser.parse_args()
file_path = args.local_file
print("(define ou (open-output-file \"" + args.target_file + "\"))")
try:
with open(file_path, 'rb') as local_file:
raw = local_file.read()
for i in raw:
if i == 10:
# print("\n", end="")
print("(newline ou)")
else:
print("(write-char #\\", end="")
if i == 13:
print("\r", end="")
elif i == 9:
print("\t", end="")
elif i == 32:
print(" ", end="")
else:
print(chr(i), end="")
print(" ou)")
local_file.close()
except Exception as e:
print(e)
print("(newline ou)")
print("(close-output-port ou)")
except Exception as e:
print(e)