mirror of
https://github.com/kanaka/mal.git
synced 2024-09-11 13:55:55 +03:00
R: add time-ms. Readline history. Misc cleanups.
This commit is contained in:
parent
f947d503f7
commit
9b3362e86a
1
.gitignore
vendored
1
.gitignore
vendored
@ -31,3 +31,4 @@ rust/target/
|
||||
rust/mal
|
||||
rust/Cargo.lock
|
||||
rust/.cargo
|
||||
r/lib
|
||||
|
13
README.md
13
README.md
@ -3,7 +3,7 @@
|
||||
## Description
|
||||
|
||||
Mal is an interpreter for a subset of the Clojure programming
|
||||
language. Mal is implemented from scratch in 15 different languages:
|
||||
language. Mal is implemented from scratch in 16 different languages:
|
||||
|
||||
* Bash shell
|
||||
* C
|
||||
@ -18,6 +18,7 @@ language. Mal is implemented from scratch in 15 different languages:
|
||||
* PHP
|
||||
* Postscript
|
||||
* Python
|
||||
* R
|
||||
* Ruby
|
||||
* Rust
|
||||
|
||||
@ -173,6 +174,16 @@ cd python
|
||||
python stepX_YYY.py
|
||||
```
|
||||
|
||||
### R
|
||||
|
||||
The R implementation of mal requires R (r-base-core) to run.
|
||||
|
||||
```
|
||||
cd r
|
||||
make libs
|
||||
Rscript stepX_YYY.rb
|
||||
```
|
||||
|
||||
### Ruby (1.8)
|
||||
|
||||
```
|
||||
|
19
docs/TODO
19
docs/TODO
@ -64,6 +64,10 @@ Python:
|
||||
- error: python ../python/stepA_interop.py ../mal/stepA_interop.mal ../mal/stepA_interop.mal
|
||||
- interop tests
|
||||
|
||||
R:
|
||||
- readline history
|
||||
- tracebacks in errors
|
||||
|
||||
Ruby:
|
||||
|
||||
|
||||
@ -90,6 +94,11 @@ Future Implementations:
|
||||
- http://stackoverflow.com/questions/23942627/does-rust-0-10-have-a-rl-package
|
||||
- http://blog.skylight.io/rust-means-never-having-to-close-a-socket/
|
||||
|
||||
- R
|
||||
- https://stat.ethz.ch/R-manual/R-devel/library/base/html/readline.html
|
||||
- http://dssm.unipa.it/CRAN/web/packages/rdyncall/rdyncall.pdf
|
||||
- http://www.dyncall.org/docs/FFI.pdf
|
||||
|
||||
- Redmonk languages from Jan 2014:
|
||||
http://sogrady-media.redmonk.com/sogrady/files/2014/01/lang-rank-114-wm.png
|
||||
|
||||
@ -97,24 +106,24 @@ Future Implementations:
|
||||
* JavaScript
|
||||
* Java
|
||||
* PHP
|
||||
* C#
|
||||
* Python
|
||||
* C#
|
||||
- C++
|
||||
* Ruby
|
||||
* C
|
||||
- Objective-C
|
||||
* Perl
|
||||
* Shell (Bash 4)
|
||||
* Perl
|
||||
|
||||
- Tier 2
|
||||
- R
|
||||
- Scala
|
||||
- Haskell
|
||||
* Clojure
|
||||
- CoffeeScript
|
||||
- Visual Basic
|
||||
- CoffeeScript
|
||||
* Clojure
|
||||
- Groovy
|
||||
- Go
|
||||
* Go
|
||||
- Lua
|
||||
- Erlang
|
||||
- Emacs Lisp
|
||||
|
1
r/core.r
1
r/core.r
@ -137,6 +137,7 @@ core_ns <- list(
|
||||
"-"=function(a,b) a-b,
|
||||
"*"=function(a,b) a*b,
|
||||
"/"=function(a,b) a/b,
|
||||
"time-ms"=function() round(as.numeric(Sys.time())*1000),
|
||||
|
||||
"list"=new.list,
|
||||
"list?"=function(a) .list_q(a),
|
||||
|
34
r/readline.r
34
r/readline.r
@ -1,16 +1,40 @@
|
||||
..readline.. <- TRUE
|
||||
|
||||
HISTORY_FILE = paste(path.expand("~"), "/.mal-history", sep="")
|
||||
|
||||
library(rdyncall, lib.loc="lib/")
|
||||
|
||||
#rllib <- dynfind(c("edit"))
|
||||
rllib <- dynfind(c("readline"))
|
||||
rl <- .dynsym(rllib,"readline")
|
||||
#.rllib <- dynfind(c("edit"))
|
||||
.rllib <- dynfind(c("readline"))
|
||||
.call_readline <- .dynsym(.rllib,"readline")
|
||||
.call_add_history <- .dynsym(.rllib,"add_history")
|
||||
|
||||
readline <- function(prompt) {
|
||||
res <- .dyncall(rl, "Z)p", prompt)
|
||||
.state <- new.env()
|
||||
.state$rl_history_loaded = FALSE
|
||||
|
||||
.readline <- function(prompt) {
|
||||
res <- .dyncall(.call_readline, "Z)p", prompt)
|
||||
if (is.nullptr(res)) {
|
||||
return(NULL)
|
||||
} else {
|
||||
return(ptr2str(res))
|
||||
}
|
||||
}
|
||||
|
||||
readline <- function(prompt) {
|
||||
if (!.state$rl_history_loaded) {
|
||||
.state$rl_history_loaded <- TRUE
|
||||
|
||||
lines <- scan(HISTORY_FILE, what="", sep="\n", quiet=TRUE)
|
||||
for(add_line in lines) {
|
||||
.dyncall(.call_add_history, "Z)v", add_line)
|
||||
}
|
||||
}
|
||||
|
||||
line <- .readline(prompt)
|
||||
if (is.null(line)) return(NULL)
|
||||
.dyncall(.call_add_history, "Z)v", line)
|
||||
write(line, file=HISTORY_FILE, append=TRUE)
|
||||
|
||||
line
|
||||
}
|
||||
|
@ -176,7 +176,11 @@ Env.set(repl_env, "*ARGV*", new.list())
|
||||
args <- commandArgs(trailingOnly = TRUE)
|
||||
if (length(args) > 0) {
|
||||
Env.set(repl_env, "*ARGV*", new.listl(slice(list(args),2)))
|
||||
. <- rep(concat("(load-file \"", args[[1]], "\")"))
|
||||
tryCatch({
|
||||
. <- rep(concat("(load-file \"", args[[1]], "\")"))
|
||||
}, error=function(err) {
|
||||
cat("Error: ", get_error(err),"\n", sep="")
|
||||
})
|
||||
quit(save="no", status=0)
|
||||
}
|
||||
|
||||
|
@ -176,7 +176,11 @@ Env.set(repl_env, "*ARGV*", new.list())
|
||||
args <- commandArgs(trailingOnly = TRUE)
|
||||
if (length(args) > 0) {
|
||||
Env.set(repl_env, "*ARGV*", new.listl(slice(list(args),2)))
|
||||
. <- rep(concat("(load-file \"", args[[1]], "\")"))
|
||||
tryCatch({
|
||||
. <- rep(concat("(load-file \"", args[[1]], "\")"))
|
||||
}, error=function(err) {
|
||||
cat("Error: ", get_error(err),"\n", sep="")
|
||||
})
|
||||
quit(save="no", status=0)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user