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")
7 READ
<- function(str
) {
11 eval_ast
<- function(ast
, env
) {
14 } else if (.list_q(ast
)) {
15 new
.listl(lapply(ast
, function(a
) EVAL(a
, env
)))
16 } else if (.vector_q(ast
)) {
17 new
.vectorl(lapply(ast
, function(a
) EVAL(a
, env
)))
18 } else if (.hash_map_q(ast
)) {
21 lst
[[length(lst
)+1]] = k
22 lst
[[length(lst
)+1]] = EVAL(ast
[[k
]], env
)
30 EVAL
<- function(ast
, env
) {
31 #cat("EVAL: ", .pr_str(ast,TRUE), "\n", sep="")
33 return(eval_ast(ast
, env
))
37 switch(paste("l",length(ast
),sep
=""),
39 l1
={ a0
<- ast
[[1]]; a1
<- NULL; a2
<- NULL },
40 l2
={ a0
<- ast
[[1]]; a1
<- ast
[[2]]; a2
<- NULL },
41 { a0
<- ast
[[1]]; a1
<- ast
[[2]]; a2
<- ast
[[3]] })
42 a0sym
<- as
.character(a0
)
43 if (a0sym
== "def!") {
44 res
<- EVAL(ast
[[3]], env
)
45 return(Env
.set(env
, a1
, res
))
46 } else if (a0sym
== "let*") {
47 let_env
<- new
.Env(env
)
48 for(i
in seq(1,length(a1
),2)) {
49 Env
.set(let_env
, a1
[[i
]], EVAL(a1
[[i
+1]], let_env
))
51 return(EVAL(a2
, let_env
))
53 el
<- eval_ast(ast
, env
)
55 return(do
.call(f
,slice(el
,2)))
59 PRINT
<- function(exp
) {
60 return(.pr_str(exp
, TRUE))
64 Env
.set(repl_env
, "+", function(a
,b
) a
+b
)
65 Env
.set(repl_env
, "-", function(a
,b
) a
-b
)
66 Env
.set(repl_env
, "*", function(a
,b
) a
*b
)
67 Env
.set(repl_env
, "/", function(a
,b
) a
/b
)
69 rep
<- function(str
) return(PRINT(EVAL(READ(str
), repl_env
)))
72 line
<- readline("user> ")
73 if (is
.null(line
)) { cat("\n"); break }
75 cat(rep(line
),"\n", sep
="")
76 }, error
=function(err
) {
77 cat("Error: ", get_error(err
),"\n", sep
="")
79 # R debug/fatal with tracebacks:
80 #cat(rep(line),"\n", sep="")