From be6ca0c7447e62fdaca7ef008681a032adc12053 Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Wed, 30 Nov 2022 23:27:15 +0100 Subject: [PATCH] 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 --- dsk/lib/lisp/alias.lsp | 2 +- dsk/lib/lisp/core.lsp | 4 ++-- dsk/tmp/lisp/colors.lsp | 23 ++++++++++++++++++++ dsk/tmp/lisp/geotime.lsp | 41 ++++++++++++++++++++++++++++++++++++ src/usr/colors.rs | 30 --------------------------- src/usr/geotime.rs | 45 ---------------------------------------- src/usr/install.rs | 2 ++ src/usr/lisp/env.rs | 12 +++++++++++ src/usr/lisp/number.rs | 8 +++++++ src/usr/mod.rs | 2 -- src/usr/shell.rs | 12 +++++------ 11 files changed, 94 insertions(+), 87 deletions(-) create mode 100644 dsk/tmp/lisp/colors.lsp create mode 100644 dsk/tmp/lisp/geotime.lsp delete mode 100644 src/usr/colors.rs delete mode 100644 src/usr/geotime.rs diff --git a/dsk/lib/lisp/alias.lsp b/dsk/lib/lisp/alias.lsp index b7973e3..cfea9ff 100644 --- a/dsk/lib/lisp/alias.lsp +++ b/dsk/lib/lisp/alias.lsp @@ -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))) diff --git a/dsk/lib/lisp/core.lsp b/dsk/lib/lisp/core.lsp index 8124462..22a12ab 100644 --- a/dsk/lib/lisp/core.lsp +++ b/dsk/lib/lisp/core.lsp @@ -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))) diff --git a/dsk/tmp/lisp/colors.lsp b/dsk/tmp/lisp/colors.lsp new file mode 100644 index 0000000..e91e21d --- /dev/null +++ b/dsk/tmp/lisp/colors.lsp @@ -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)) diff --git a/dsk/tmp/lisp/geotime.lsp b/dsk/tmp/lisp/geotime.lsp new file mode 100644 index 0000000..60585ae --- /dev/null +++ b/dsk/tmp/lisp/geotime.lsp @@ -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 []"))) diff --git a/src/usr/colors.rs b/src/usr/colors.rs deleted file mode 100644 index f55b56a..0000000 --- a/src/usr/colors.rs +++ /dev/null @@ -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(()) -} diff --git a/src/usr/geotime.rs b/src/usr/geotime.rs deleted file mode 100644 index 2d8786d..0000000 --- a/src/usr/geotime.rs +++ /dev/null @@ -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: []"); - 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) -} diff --git a/src/usr/install.rs b/src/usr/install.rs index 60f6ee2..276a9b9 100644 --- a/src/usr/install.rs +++ b/src/usr/install.rs @@ -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); diff --git a/src/usr/lisp/env.rs b/src/usr/lisp/env.rs index fb04fa0..3c712d7 100644 --- a/src/usr/lisp/env.rs +++ b/src/usr/lisp/env.rs @@ -145,6 +145,10 @@ pub fn default_env() -> Rc> { ensure_length_eq!(args, 1); Ok(Exp::Num(number(&args[0])?.tan())) })); + data.insert("trunc".to_string(), Exp::Primitive(|args: &[Exp]| -> Result { + ensure_length_eq!(args, 1); + Ok(Exp::Num(number(&args[0])?.trunc())) + })); data.insert("system".to_string(), Exp::Primitive(|args: &[Exp]| -> Result { ensure_length_eq!(args, 1); let cmd = string(&args[0])?; @@ -293,6 +297,14 @@ pub fn default_env() -> Rc> { data.insert("list".to_string(), Exp::Primitive(|args: &[Exp]| -> Result { Ok(Exp::List(args.to_vec())) })); + data.insert("length".to_string(), Exp::Primitive(|args: &[Exp]| -> Result { + 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 { let mut res = vec![]; for arg in args { diff --git a/src/usr/lisp/number.rs b/src/usr/lisp/number.rs index 3898baf..7116a20 100644 --- a/src/usr/lisp/number.rs +++ b/src/usr/lisp/number.rs @@ -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)), diff --git a/src/usr/mod.rs b/src/usr/mod.rs index 7b441fd..ea33e6b 100644 --- a/src/usr/mod.rs +++ b/src/usr/mod.rs @@ -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; diff --git a/src/usr/shell.rs b/src/usr/shell.rs index 7d9eab1..c91b09c 100644 --- a/src/usr/shell.rs +++ b/src/usr/shell.rs @@ -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),