1 if(!exists("..readline..")) source("readline.r")
2 if(!exists("..types..")) source("types.r")
3 if(!exists("..reader..")) source("reader.r")
4 if(!exists("..printer..")) source("printer.r")
5 if(!exists("..env..")) source("env.r")
6 if(!exists("..core..")) source("core.r")
9 READ
<- function(str
) {
14 is_pair
<- function(x
) {
15 .sequential_q(x
) && length(x
) > 0
18 quasiquote
<- function(ast
) {
20 new
.list(new
.symbol("quote"),
22 } else if (.symbol_q(ast
[[1]]) && ast
[[1]] == "unquote") {
24 } else if (is_pair(ast
[[1]]) &&
25 .symbol_q(ast
[[1]][[1]]) &&
26 ast
[[1]][[1]] == "splice-unquote") {
27 new
.list(new
.symbol("concat"),
29 quasiquote(slice(ast
, 2)))
31 new
.list(new
.symbol("cons"),
33 quasiquote(slice(ast
, 2)))
37 eval_ast
<- function(ast
, env
) {
40 } else if (.list_q(ast
)) {
41 new
.listl(lapply(ast
, function(a
) EVAL(a
, env
)))
42 } else if (.vector_q(ast
)) {
43 new
.vectorl(lapply(ast
, function(a
) EVAL(a
, env
)))
44 } else if (.hash_map_q(ast
)) {
47 lst
[[length(lst
)+1]] = k
48 lst
[[length(lst
)+1]] = EVAL(ast
[[k
]], env
)
56 EVAL
<- function(ast
, env
) {
59 #cat("EVAL: ", .pr_str(ast,TRUE), "\n", sep="")
61 return(eval_ast(ast
, env
))
65 switch(paste("l",length(ast
),sep
=""),
67 l1
={ a0
<- ast
[[1]]; a1
<- NULL; a2
<- NULL },
68 l2
={ a0
<- ast
[[1]]; a1
<- ast
[[2]]; a2
<- NULL },
69 { a0
<- ast
[[1]]; a1
<- ast
[[2]]; a2
<- ast
[[3]] })
70 if (length(a0
) > 1) a0sym
<- "__<*fn*>__"
71 else a0sym
<- as
.character(a0
)
72 if (a0sym
== "def!") {
74 return(Env
.set(env
, a1
, res
))
75 } else if (a0sym
== "let*") {
76 let_env
<- new
.Env(env
)
77 for(i
in seq(1,length(a1
),2)) {
78 Env
.set(let_env
, a1
[[i
]], EVAL(a1
[[i
+1]], let_env
))
82 } else if (a0sym
== "quote") {
84 } else if (a0sym
== "quasiquote") {
86 } else if (a0sym
== "do") {
87 eval_ast(slice(ast
,2,length(ast
)-1), env
)
88 ast
<- ast
[[length(ast
)]]
89 } else if (a0sym
== "if") {
91 if (.nil_q(cond
) || identical(cond
, FALSE)) {
92 if (length(ast
) < 4) return(nil
)
97 } else if (a0sym
== "fn*") {
98 return(malfunc(EVAL
, a2
, env
, a1
))
100 el
<- eval_ast(ast
, env
)
102 if (class(f
) == "MalFunc") {
104 env
<- f$
gen_env(slice(el
,2))
106 return(do
.call(f
,slice(el
,2)))
114 PRINT
<- function(exp
) {
115 return(.pr_str(exp
, TRUE))
119 repl_env
<- new
.Env()
120 rep
<- function(str
) return(PRINT(EVAL(READ(str
), repl_env
)))
122 # core.r: defined using R
123 for(k
in names(core_ns
)) { Env
.set(repl_env
, k
, core_ns
[[k
]]) }
124 Env
.set(repl_env
, "eval", function(ast
) EVAL(ast
, repl_env
))
125 Env
.set(repl_env
, "*ARGV*", new
.list())
127 # core.mal: defined using the language itself
128 . <- rep("(def! not (fn* (a) (if a false true)))")
129 . <- rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
131 args
<- commandArgs(trailingOnly
= TRUE)
132 if (length(args
) > 0) {
133 Env
.set(repl_env
, "*ARGV*", new
.listl(slice(as
.list(args
),2)))
134 . <- rep(concat("(load-file \"", args
[[1]], "\")"))
135 quit(save
="no", status
=0)
139 line
<- readline("user> ")
140 if (is
.null(line
)) { cat("\n"); break }
142 cat(rep(line
),"\n", sep
="")
143 }, error
=function(err
) {
144 cat("Error: ", get_error(err
),"\n", sep
="")
146 # R debug/fatal with tracebacks:
147 #cat(rep(line),"\n", sep="")