Replace some rust programs with lisp scripts (#445)

* Replace colors command with colors.lsp

* Replace geotime command with geotime.lsp

* Remove commands from shell

* Add comment
This commit is contained in:
Vincent Ollivier 2022-11-30 23:27:15 +01:00 committed by GitHub
parent 3b519377c7
commit be6ca0c744
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 94 additions and 87 deletions

View File

@ -13,6 +13,7 @@
(define def-fun
(macro args `(define-function ,@args)))
(define len length)
(define label
(macro args `(define ,@args)))
@ -23,6 +24,5 @@
(define progn
(macro args `(do ,@args)))
(define begin
(macro args `(do ,@args)))

View File

@ -87,7 +87,7 @@
(append (list i) (range (+ i 1) n))))
(define (string-join ls s)
(reduce (fn (x y) (string x s y)) ls))
(reduce (function (x y) (string x s y)) ls))
(define (read-line)
(bytes->string (reverse (rest (reverse (read-file-bytes "/dev/console" 256))))))
@ -107,7 +107,7 @@
(bytes->number (read-file-bytes "/dev/clk/uptime" 8) "float"))
(define (realtime)
(bytes->number (read-file-bytes "realtime" 8) "float"))
(bytes->number (read-file-bytes "/dev/clk/realtime" 8) "float"))
(define (write-file path str)
(write-file-bytes path (string->bytes str)))

23
dsk/tmp/lisp/colors.lsp Normal file
View File

@ -0,0 +1,23 @@
(load "/lib/lisp/core.lsp")
(define esc (bytes->string '(27)))
(define (ansi-color x y)
(string esc "[" x ";" y "m"))
(define (fg c)
(ansi-color c 40))
(define (bg c)
(ansi-color 30 c))
(define (color f c)
(string " " (f c) (if (< c 100) " " "") c (ansi-color 0 0)))
(define (colors fs i j)
(string-join (map (function (c) (color fs c)) (range i j)) ""))
(println (colors fg 30 38))
(println (colors fg 90 98))
(println (colors bg 40 48))
(println (colors bg 100 108))

41
dsk/tmp/lisp/geotime.lsp Normal file
View File

@ -0,0 +1,41 @@
(load "/lib/lisp/core.lsp")
(define (equation-of-time y)
(* 60.0 229.18 (+ 0.000075 (-
(* 0.001868 (cos (* 1.0 y)))
(* 0.032077 (sin (* 1.0 y)))
(* 0.014615 (cos (* 2.0 y)))
(* 0.040849 (sin (* 2.0 y)))))))
(define (days timestamp)
(trunc (% (/ timestamp 86400.0) 365.2425)))
(define (hours timestamp)
(trunc (/ (% timestamp 86400.0) 3600.0)))
(define (seconds timestamp longitude)
(+
(% timestamp 86400.0)
(/ (* longitude 86400.0) 360.0)
(equation-of-time (*
(/ (* 2 pi) 365.0)
(+ (days timestamp) (/ (- (hours timestamp) 12.0) 24.0))))))
(define (abs x)
(if (< x 0) (- x) x))
(define (pad x)
(string (if (< x 10) "0" "") x))
(define (fmt x)
(string (pad (trunc x)) ":" (pad (abs (trunc (* (- x (trunc x)) 100.0))))))
(define (geotime longitude timestamp)
(fmt (% (/ (* (seconds timestamp longitude) 100.0) 86400.0) 100.0)))
(println
(if (= (length args) 1)
(geotime (string->number (first args)) (realtime))
(if (= (length args) 2)
(geotime (string->number (first args)) (string->number (second args)))
"Usage: geotime <longitude> [<timestamp>]")))

View File

@ -1,30 +0,0 @@
use crate::api::process::ExitCode;
use alloc::format;
pub fn main(_args: &[&str]) -> Result<(), ExitCode> {
let csi_reset = "\x1b[0m";
for i in 30..38 {
let csi_color = format!("\x1b[{};40m", i);
print!(" {}{:3}{}", csi_color, i, csi_reset);
}
println!();
for i in 90..98 {
let csi_color = format!("\x1b[{};40m", i);
print!(" {}{:3}{}", csi_color, i, csi_reset);
}
println!();
for i in 40..48 {
let csi_color = format!("\x1b[30;{}m", i);
print!(" {}{:3}{}", csi_color, i, csi_reset);
}
println!();
for i in 100..108 {
let csi_color = format!("\x1b[30;{}m", i);
print!(" {}{:3}{}", csi_color, i, csi_reset);
}
println!();
Ok(())
}

View File

@ -1,45 +0,0 @@
use crate::api::clock;
use crate::api::process::ExitCode;
use alloc::format;
use core::f64::consts::PI;
pub fn main(args: &[&str]) -> Result<(), ExitCode> {
if args.len() < 2 {
eprintln!("Usage: <longitude> [<timestamp>]");
return Err(ExitCode::UsageError);
}
let longitude = args[1].parse().expect("Could not parse longitude");
let timestamp = if args.len() == 3 {
args[2].parse().expect("Could not parse timestamp")
} else {
clock::realtime()
};
let t = geotime(longitude, timestamp);
let t = libm::floor(100.0 * t) / 100.0; // Avoid rounding up 99.996 to 100.00
println!("{}", format!("{:05.2}", t).replace(".", ":"));
Ok(())
}
pub fn geotime(longitude: f64, timestamp: f64) -> f64 {
let days = libm::floor(libm::fmod(timestamp / 86400.0, 365.2425));
let hours = libm::floor(libm::fmod(timestamp, 86400.0) / 3600.0);
// Equation of time (https://www.esrl.noaa.gov/gmd/grad/solcalc/solareqns.PDF)
let y = (2.0 * PI / 365.0) * (days + (hours - 12.0) / 24.0);
let eot = 60.0 * 229.18 * (
0.000075 +
0.001868 * libm::cos(1.0 * y) -
0.032077 * libm::sin(1.0 * y) -
0.014615 * libm::cos(2.0 * y) -
0.040849 * libm::sin(2.0 * y)
);
let seconds = libm::fmod(timestamp, 86400.0) + (longitude * 86400.0 / 360.0) + eot;
libm::fmod(100.0 * seconds / 86400.0, 100.0)
}

View File

@ -54,8 +54,10 @@ pub fn copy_files(verbose: bool) {
copy_file("/tmp/machines.txt", include_bytes!("../../dsk/tmp/machines.txt"), verbose);
create_dir("/tmp/lisp", verbose);
copy_file("/tmp/lisp/colors.lsp", include_bytes!("../../dsk/tmp/lisp/colors.lsp"), verbose);
copy_file("/tmp/lisp/factorial.lsp", include_bytes!("../../dsk/tmp/lisp/factorial.lsp"), verbose);
copy_file("/tmp/lisp/fibonacci.lsp", include_bytes!("../../dsk/tmp/lisp/fibonacci.lsp"), verbose);
copy_file("/tmp/lisp/geotime.lsp", include_bytes!("../../dsk/tmp/lisp/geotime.lsp"), verbose);
copy_file("/tmp/lisp/pi.lsp", include_bytes!("../../dsk/tmp/lisp/pi.lsp"), verbose);
copy_file("/tmp/lisp/sum.lsp", include_bytes!("../../dsk/tmp/lisp/sum.lsp"), verbose);

View File

@ -145,6 +145,10 @@ pub fn default_env() -> Rc<RefCell<Env>> {
ensure_length_eq!(args, 1);
Ok(Exp::Num(number(&args[0])?.tan()))
}));
data.insert("trunc".to_string(), Exp::Primitive(|args: &[Exp]| -> Result<Exp, Err> {
ensure_length_eq!(args, 1);
Ok(Exp::Num(number(&args[0])?.trunc()))
}));
data.insert("system".to_string(), Exp::Primitive(|args: &[Exp]| -> Result<Exp, Err> {
ensure_length_eq!(args, 1);
let cmd = string(&args[0])?;
@ -293,6 +297,14 @@ pub fn default_env() -> Rc<RefCell<Env>> {
data.insert("list".to_string(), Exp::Primitive(|args: &[Exp]| -> Result<Exp, Err> {
Ok(Exp::List(args.to_vec()))
}));
data.insert("length".to_string(), Exp::Primitive(|args: &[Exp]| -> Result<Exp, Err> {
ensure_length_eq!(args, 1);
if let Exp::List(list) = &args[0] {
Ok(Exp::Num(Number::from(list.len())))
} else {
return Err(Err::Reason("Expected arg to be a list".to_string()))
}
}));
data.insert("append".to_string(), Exp::Primitive(|args: &[Exp]| -> Result<Exp, Err> {
let mut res = vec![];
for arg in args {

View File

@ -117,6 +117,14 @@ impl Number {
}
}
pub fn trunc(self) -> Number {
if let Number::Float(a) = self {
Number::Int(libm::trunc(a) as i64)
} else {
self
}
}
pub fn shl(self, other: Number) -> Number {
match (self, other) {
(Number::BigInt(a), Number::Int(b)) => Number::BigInt(a.shl(b)),

View File

@ -2,7 +2,6 @@ pub mod base64;
pub mod beep;
pub mod calc;
pub mod chess;
pub mod colors;
pub mod copy;
pub mod date;
pub mod delete;
@ -12,7 +11,6 @@ pub mod editor;
pub mod elf;
pub mod env;
pub mod find;
pub mod geotime;
pub mod help;
pub mod hex;
pub mod host;

View File

@ -13,10 +13,10 @@ use alloc::vec::Vec;
use alloc::string::{String, ToString};
// TODO: Scan /bin
const AUTOCOMPLETE_COMMANDS: [&str; 36] = [
"2048", "base64", "calc", "colors", "copy", "date", "delete", "dhcp", "disk", "edit", "env",
"geotime", "goto", "help", "hex", "host", "http", "httpd", "install", "keyboard", "life",
"lisp", "list", "memory", "move", "net", "pci", "quit", "read", "shell", "socket", "tcp",
const AUTOCOMPLETE_COMMANDS: [&str; 34] = [
"2048", "base64", "calc", "copy", "date", "delete", "dhcp", "disk", "edit", "env",
"goto", "help", "hex", "host", "http", "httpd", "install", "keyboard", "life", "lisp",
"list", "memory", "move", "net", "pci", "quit", "read", "shell", "socket", "tcp",
"time", "user", "vga", "write"
];
@ -466,7 +466,6 @@ fn exec_with_config(cmd: &str, config: &mut Config) -> Result<(), ExitCode> {
"beep" => usr::beep::main(&args),
"calc" => usr::calc::main(&args),
"chess" => usr::chess::main(&args),
"colors" => usr::colors::main(&args),
"copy" => usr::copy::main(&args),
"date" => usr::date::main(&args),
"delete" => usr::delete::main(&args),
@ -476,8 +475,7 @@ fn exec_with_config(cmd: &str, config: &mut Config) -> Result<(), ExitCode> {
"elf" => usr::elf::main(&args),
"env" => usr::env::main(&args),
"find" => usr::find::main(&args),
"geotime" => usr::geotime::main(&args),
"goto" => cmd_change_dir(&args, config),
"goto" => cmd_change_dir(&args, config), // TODO: Remove this
"help" => usr::help::main(&args),
"hex" => usr::hex::main(&args),
"host" => usr::host::main(&args),