Compare commits

..

6 Commits

Author SHA1 Message Date
vaclavt
0869ba3f9b remove duplicated functionality 2022-02-28 19:48:45 +01:00
vaclavt
b845cf756c constexpr instead const 2022-02-28 19:48:27 +01:00
vaclavt
2b3d8ab026 fix for is-file? function 2022-02-28 19:48:05 +01:00
vaclavt
aad8724cbb unit tests added 2022-02-28 19:47:51 +01:00
vaclavt
25252e3954 fix is-pos? and is-neg? functions 2022-02-28 19:47:35 +01:00
vaclavt
3c698824ec doc updates 2022-02-28 19:47:06 +01:00
7 changed files with 45 additions and 44 deletions

View File

@@ -80,7 +80,6 @@ utils/local_install.sh
- better output of !e in repl (resp !e !ee)
#### Doc
- string-case, make-csv fix doc
- add functions from stdlib into doc
- doc for set!

View File

@@ -80,27 +80,25 @@
|`(exit code)`|Exit the program with an integer code||System|
|`(quit code)`|Same as (exit ..)||System|
|`(print ..)`|Print several values and return the last one||IO|
|`(input [prompt])`|Get user input with an optional prompt||IO|
|`(random low high)`|Get a random number between two numbers inclusively||System|
|`(include file)`|Read a file and execute its code||IO|
|`(input [prompt])`|Get user input with an optional prompt||IO|
|`(read)`|Reads in the printed representation of a Lisp object from input-stream, builds a corresponding Lisp object, and returns the object||IO|
|`(read-line)`|Reads in a line of text terminated by a newline||IO|
|`(read-file filename)`|Get the contents of a file||IO|
|`(read-file filename)`|Get the contents of a file|`>>> (read-file "/tmp/a.txt") => "test"`|IO|
|`(read-file-lines filename lambda)`|Reads file and for each line call lambda with passing the line as a parameter|`(read-file-lines "/tmp/f.txt" (lambda (ln) (print ln))`|IO|
|`(write-file filename content-str)`|Write a string to a file||IO|
|`(read-url url [headers] [body] [method])`|Reads URL|Returns list (status-code content)|IO|
|`(system-cmd command_str)`|Execute system command||System|
|`(ls-dir dir)`|List a dir|List of directory entries|
|`(is-file? filename)`|Returns true if passed filename is a file||IO|
|`(is-dir? filename)`|Returns true if passed filename is a directory||IO|
|`(write-file filename content-str)`|Write a string to a file|`>>> (write-file "/tmp/a.txt" "test") => #t`|IO|
|`(read-url url [headers] [body] [method])`|Reads URL. Returns list (status-code content)||IO|
|`(system-cmd command_str)`|Execute system command|`>>> (system-cmd "date") => (0 "Fri Feb 25 12:35:28 CET 2022\n")`|System|
|`(ls-dir dir)`|List a dir|List of directory entries|`>>> (ls-dir "/tmp") => ("." ".." "vscode-ipc-cccbe1dd-8c71-4028-a863-df975ad5887b.sock" "vscode-ipc-1163bb52-d088-41dc-80a5-81b6a7c7fa36.sock" "vscode-ipc-630f21df-26b5-43d4-8b2e-5175d53ce317.sock")`|IO|
|`(is-file? filename)`|Returns true if passed filename is a file|`>>> (is-file? "/tmp") => nil`|IO|
|`(is-dir? filename)`|Returns true if passed filename is a directory|`>>> (is-dir? "/tmp") => #t`|IO|
|`(tcp-server port handler)`|Starts listening on port and when request comes calls passed lambda and writes its returned value back to client. Lambda must return either string or two element list where first element is boolean and second string.When first element is true it closes listening socker.|(`tcp-server 7777 (lambda (str) (list #t (string-upcase str))))`|IO|
|`(tcp-client address port data)`|Opens connection to server on port, writes there data and returns response.|`(print (tcp-client "127.0.0.1" 7777 "abcd"))`|IO|
|`(is-dir? filename)`|Returns true if passed filename is a directory||IO|
|`(parse-csv string)`|Parse CSV string|`>>> (make-csv '(("A" "B")(1 1)(2 2))) => "A,B\n1,1\n2,2"`|String manipulation|
|`(parse-json json_string)`|Parse JSON string||String manipulation|
|`(make-csv ..)`|Returns list as csv string||IO|
|`(parse-csv string)`|Parse CSV string|`>>> (parse-csv "A,B\n1,1\n2,2") => ((1 1) (2 2))`|String manipulation|
|`(parse-json json_string)`|Parse JSON string|`>>> (parse-json "{\"k1\":\"v1\", \"k2\":42, \"k3\":[\"a\",123,true,false,null]}") => (("k1" "v1") ("k2" 42) ("k3" ("a" 123 #t nil nil)))`|String manipulation|
|`(make-csv ..)`|Returns list as csv string|`>>> (make-csv '(("A" "B")(1 1)(2 2))) => "A,B\n1,1\n2,2"`|String manipulation|
|`(get-universal-time)`|Get current time as secs from epoch|`>>> (get-universal-time) => 1642152733`|Date and time|
|`(get-localtime-offset)`|Offset in seconds between local time and gmt time||Date and time|
|`(get-localtime-offset)`|Offset in seconds between local time and gmt time|`>>> (get-localtime-offset) => 3600`|Date and time|
|`(date-to-str date format)`|Converts date to formated string. Format is strftime format (https://www.tutorialspoint.com/c_standard_library/c_function_strftime.htm)|`>>> (date-to-str (get-universal-time) "%Y-%m-%d %H:%M:%S") => "2021-03-13 19:53:01"`|Date and time|
|`(str-to-date string format)`|Converst string to time of secs since epoch. |`>>> (str-to-date "2021-03-13 19:53:01" "%Y-%m-%d %H:%M:%S") => 1615665181`|Date and time|
|`(date-add date amount unit)`|Add number of units to date. A unit is one of 'year', 'month', 'day', 'hour', 'minute' or 'second'|`>>> (date-to-str (date-add (str-to-date "2021-03-13 19:53:01" "%Y-%m-%d %H:%M:%S") 10 "day") "%Y-%m-%d %H:%M:%S") => "2021-03-23 20:53:01"`|Date and time|
@@ -126,10 +124,10 @@
|`(string-rpad str len char)`|Pad string from righ with char to length len|`>>> (string-rpad "0" 10 "x") => "0xxxxxxxxx"`|String manipulation|
|`(string-split str separator)`|Splits string into list by regexp|`>>> (string-split "split me by space" "\s+") => ("split" "me" "by" "space")`|String manipulation|
|`(string-rltrim str len RKRRKR)`|Removes " \n\r\t" from the begininfg or end or both ends of str||Regex|
|`(string-ltrim str)`|Removes " \n\r\t" from the begininfg of str||String manipulation|
|`(string-rtrim str)`|Removes " \n\r\t" from the end of str||String manipulation|
|`(string-trim str)`|Removes " \n\r\t" from the both strart and end of str||String manipulation|
|`(string-case str RKRRKR)`|Returns up or down cased string||String manipulation|
|`(string-ltrim str)`|Removes " \n\r\t" from the begininfg of str|`>>> (string-ltrim " *** ") => "*** "`|String manipulation|
|`(string-rtrim str)`|Removes " \n\r\t" from the end of str|`>>> (string-rtrim " *** ") => " ***"`|String manipulation|
|`(string-trim str)`|Removes " \n\r\t" from the both strart and end of str|`>>> (string-trim " *** ") => "***"`|String manipulation|
|`(string-case str UPLO)`|Returns up or down cased string based on second parameter (upper/lower)|`>>> (string-case "abc" "upper") => "ABC"`|String manipulation|
|`(string-upcase str)`|Returns up cased string|`>>> (string-upcase "abcdefghchijklmn") => "ABCDEFGHCHIJKLMN"`|String manipulation|
|`(string-downcase str)`|Returns down cased string |`>>> (string-downcase "ABCDefghchijklmn") => "abcdefghchijklmn"`|String manipulation|
|`(string-join lst sep)`|Returns string created as elements of concatenation of lst elements separated by sep|`>>> (string-join ("A" "B" "C" "D") ",") => "A,B,C,D"`|String manipulation|
@@ -147,18 +145,18 @@
|`(int? e)`|Returns true if type of e is integer|`>>> (int? "str") => nil >>> (int? 9) => #t`|Type casting|
|`(float? e)`|Returns true if type of e is float|`>>> (float? 9) => nil >>> (float? 9.0) => #t`|Type casting|
|`(nil? e)`|Returns true if type of e is nil|`>>> (nil? 9.0) => nil >>> (nil? nil) => #t >>> (nil? '()) => nil`|Type casting|
|`(true? e)`|Returns true if type of e is #t||Type casting|
|`(true? e)`|Returns true if type of e is #t|`>>> (true? 1) => nil >>> (true? #t) => #t`|Type casting|
|`(parse ..)`|Parses string to be evaluated|`>>> (eval (first (parse "(+ 1 2)"))) => 3`|Language|
|`(quick-sort-by list cmp)`|Returns list sorted by comparsion function||Language|
|`(quick-sort list)`|Return sorted list|`>>> (quick-sort '(2 4 6 1 7 3 3 9 5)) => (1 2 3 3 4 5 6 7 9)`|Language|
|`(quick-sort-reverse list)`|Return reverse sorted list|`>>> (quick-sort-reverse '(2 4 6 1 7 3 3 9 5)) => (9 7 6 5 4 3 3 2 1)`|Language|
|`(not c)`|Logical NOT of c|`>>> (not 1) => nil`|Language|
|`(neg n)`|Negates number|`>>> (neg -5) => 5`|Language|
|`(is-pos? n)`|Returns true if n is positive number||Language|
|`(is-neg? n)`|Returns true if n is negative number||Language|
|`(is-pos? n)`|Returns true if n is positive number|`>>> (is-pos? -1) => nil`|Language|
|`(is-neg? n)`|Returns true if n is negative number|`>>> (is-neg? -1) => #t`|Language|
|`(dec n)`|Return n decremented by 1|`>>> (dec 5) => 4`|Language|
|`(inc n)`|Return n incremented by 1|`>>> (inc 5) => 6`|Language|
|`(sleep time)`|Pauses execution for time interval of seconds||System|
|`(sleep time)`|Pauses execution for time interval of seconds. Calls system sleep command|`>>> (sleep 1) => (0 "")`|System|
|`(function? e)`|Returns true if type of e is lambda, macro or builtin|`>>> (function? index) => #t`|Language|
|`(unless test v)`|If test is not truthy then v is evaluated. This is macro.|`>>> (unless (> 1 2) "1 < 2") => "1 < 2"`|Language|
|`(dotimes v n body)`|Evaluates body n-times. For each eveluation v is set to value between 0 and n minus one|`(dotimes i 10 (print "i :" i))`|Language|
@@ -177,7 +175,7 @@
|`(sprintf ..)`|Writes string pointed by format to the standard output|`>>> (sprintf "%s, %d, %.2f" (list "string" 1000 3.14)) => "string, 1000, 3.14"`|String manipulation|
|`(thread-create code..)`|Creates new thread, starts evalueating code and returns immediatelly thread id||Threading|
|`(thread-under-lock lockname code)`|Acquire lock with lockname and eval code. "ilock" currently is only allowed lockname||Threading|
|`(thread-sleep milisecons)`|Sleeps thread for given amount of miliseconds||Threading|
|`(thread-sleep milisecons)`|Sleeps thread for given amount of miliseconds|`>>> (thread-sleep 100) => 100`|Threading|
|`(threads-join)`|Wait for all running threads to finish||Threading|
|`(try block catch_block [finally_block])`|Evaluates block and if an exception is thrown, evaluates catch_block.Eventually in both cases evals finally_block. Return evaluated last expression from block if no exception or last expression from catch_block, if exception is thrown from block. Variable ml-exception in catch block is available with astring describing exception||Exceptions|
|`(throw exp_desc)`|Throws an exception with exp_desc describing what happened. exp_desc will be evaluated a returned as string|`>>> (throw (+ 1 2))\n3`|Exceptions|

18
ml.cpp
View File

@@ -1165,18 +1165,6 @@ MlValue read(std::vector<MlValue> args, MlEnvironment &env) {
return run(line, env);
}
// Read line from stdin
MlValue read_line(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 0)
throw MlError(MlValue("read-line", read_line), env, TOO_MANY_ARGS);
std::string line;
std::getline(std::cin, line);
return MlValue::string(line);
}
// Get the contents of a file
MlValue read_file(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@@ -1425,7 +1413,8 @@ MlValue get_env(std::vector<MlValue> args, MlEnvironment &env) {
if (const char* env_p = std::getenv(args[0].as_string().c_str()))
return MlValue::string(env_p);
else
return MlValue::nil();
// TODO maybe better to return MlValue::nil();
return MlValue::string("");
}
// set environment variable
@@ -2208,11 +2197,10 @@ std::map <const std::string, Builtin> builtin_funcs
std::make_pair("exit", builtin::exit),
std::make_pair("quit", builtin::exit),
std::make_pair("print", builtin::print),
std::make_pair("input", builtin::input),
std::make_pair("random", builtin::random),
std::make_pair("include", builtin::include),
std::make_pair("input", builtin::input),
std::make_pair("read", builtin::read),
std::make_pair("read-line", builtin::read_line),
std::make_pair("read-file", builtin::read_file),
std::make_pair("read-file-lines", builtin::read_file_lines),
std::make_pair("write-file", builtin::write_file),

View File

@@ -33,7 +33,7 @@ std::string date_to_string(const long datetime, const std::string& format) {
}
time_t time_to_epoch ( const struct tm *ltm, int utcdiff ) {
const int mon_days [] = {0, 31, 31+28, 31+28+31,
constexpr int mon_days [] = {0, 31, 31+28, 31+28+31,
31+28+31+30, 31+28+31+30+31, 31+28+31+30+31+30,
31+28+31+30+31+30+31, 31+28+31+30+31+30+31+31, 31+28+31+30+31+30+31+31+30,
31+28+31+30+31+30+31+31+30+31, 31+28+31+30+31+30+31+31+30+31+30, 31+28+31+30+31+30+31+31+30+31+30+31};

View File

@@ -50,8 +50,13 @@ MlValue list_dir(const std::string &path) {
}
bool is_path_file(const std::string &path) {
std::ifstream ifile(path.c_str());
return (bool) ifile;
struct stat buf{};
stat(path.c_str(), &buf);
return (bool) S_ISREG(buf.st_mode) ||
(bool) S_ISBLK(buf.st_mode) ||
(bool) S_ISCHR(buf.st_mode) ||
(bool) S_ISFIFO(buf.st_mode) ||
(bool) S_ISSOCK(buf.st_mode);
}
bool is_path_dir(const std::string &path) {

View File

@@ -1,8 +1,8 @@
; not a bool
(defn not (x) (if x nil #t))
(defn is-pos? (n) (> n nil))
(defn is-neg? (n) (< n nil))
(defn is-pos? (n) (> n 0))
(defn is-neg? (n) (< n 0))
(defn neg (n) (- 0 n))
(defn dec (n) (- n 1))

View File

@@ -91,12 +91,22 @@
(ut::define-test "result of (string-substr \"ABCDEF\"4 42)" '(ut::assert-equal "EF" (string-substr "ABCDEF" 4 42)))
(ut::define-test "result of (string-substr \"ABCDEF\" -2 2)" '(ut::assert-equal "EF" (string-substr "ABCDEF" -2 2)))
(ut::define-test "result of (string-ltrim \" *** \")" '(ut::assert-equal "*** " (string-ltrim " *** ")))
(ut::define-test "result of (string-rtrim \" *** \")" '(ut::assert-equal " ***" (string-rtrim " *** ")))
(ut::define-test "result of (string-trim \" *** \")" '(ut::assert-equal "***" (string-trim " *** ")))
(ut::define-test "result of (string-find \" long long int;\" \"long\")" '(ut::assert-equal 1 (string-find " long long int;" "long")))
(ut::define-test "result of (string-find \" long long int;\" \"long\" 2)" '(ut::assert-equal 6 (string-find " long long int;" "long" 2)))
(ut::define-test "result of (string-find \" long long int;\" \" \")" '(ut::assert-equal 0 (string-find " long long int;" " ")))
(ut::define-test "result of (string-find \" long long int;\" \"o\")" '(ut::assert-equal 2 (string-find " long long int;" "o")))
(ut::define-test "result of (string-find \" long long int;\" \"float\")" '(ut::assert-nil (string-find " long long int;" "float")))
(ut::define-test "result of (is-pos? -1)" '(ut::assert-false (is-pos? -1)))
(ut::define-test "result of (is-neg? -1)" '(ut::assert-true (is-neg? -1)))
(ut::define-test "result of (neg -5)" '(ut::assert-equal 5 (neg -5)))
(ut::define-test "result of (inc 4)" '(ut::assert-equal 5 (inc 4)))
(ut::define-test "result of (dec 4)" '(ut::assert-equal 3 (dec 4)))
(ut::define-test "result of (itok 65)" '(ut::assert-equal "A" (itok 65)))
(ut::define-test "result of (itok 48)" '(ut::assert-equal "0" (itok 48)))
(ut::define-test "result of (ktoi \"A\")" '(ut::assert-equal 65 (ktoi "A")))
@@ -107,6 +117,7 @@
(ut::define-test "result of (is-file? \"/tmp/file_whichnotex_ists\")" '(ut::assert-false (is-file? "/tmp/file_whichnotex_ists")))
(ut::define-test "result of (is-dir? \"/tmp/file_whichnotex_ists\")" '(ut::assert-false (is-dir? "/tmp/file_whichnotex_ists")))
(ut::define-test "result of (is-dir? \"/tmp\"" '(ut::assert-true (is-dir? "/tmp")))
(ut::define-test "result of (is-file? \"/tmp\"" '(ut::assert-false (is-file? "/tmp")))
(ut::define-test "result of (uniq '(1 2 2 2 3 4 5 4 4 1 2 2 2 3 4 5 4 4 61 2 2 2 3 4 5 4 4 66))" '(ut::assert-equal '(1 2 3 4 5 61 66) (uniq '(1 2 2 2 3 4 5 4 4 1 2 2 2 3 4 5 4 4 61 2 2 2 3 4 5 4 4 66))))
(ut::define-test "result of (flatten '(1 2 (3 3 (4)) 5 6))" '(ut::assert-equal '(1 2 3 3 4 5 6) (flatten (1 2 (3 3 (4)) 5 6))))