Skip to content

Commit

Permalink
janet: merge eval_ast into eval, update Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Oct 10, 2024
1 parent a3349f2 commit aa6f9a8
Show file tree
Hide file tree
Showing 12 changed files with 557 additions and 616 deletions.
21 changes: 10 additions & 11 deletions impls/janet/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:20.04
FROM ubuntu:24.04
MAINTAINER Joel Martin <[email protected]>

##########################################################
Expand All @@ -9,10 +9,8 @@ MAINTAINER Joel Martin <[email protected]>
RUN apt-get -y update

# Required for running tests
RUN apt-get -y install make python

# Some typical implementation and test requirements
RUN apt-get -y install wget libreadline-dev libedit-dev
RUN apt-get -y install make python3
RUN ln -fs /usr/bin/python3 /usr/local/bin/python

RUN mkdir -p /mal
WORKDIR /mal
Expand All @@ -21,9 +19,10 @@ WORKDIR /mal
# Specific implementation requirements
##########################################################

# janet
RUN cd /usr/lib/x86_64-linux-gnu/ \
&& wget https://github.com/janet-lang/janet/releases/download/v1.12.2/janet-v1.12.2-linux.tar.gz \
&& tar xvzf janet-v1.12.2-linux.tar.gz \
&& ln -sf /usr/lib/x86_64-linux-gnu/janet-v1.12.2-linux/janet /usr/bin/janet \
&& rm janet-v1.12.2-linux.tar.gz
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install \
ca-certificates wget

RUN wget -O- \
https://github.com/janet-lang/janet/releases/download/v1.36.0/janet-v1.36.0-linux-x64.tar.gz \
| tar -xzC/opt
RUN ln -sf /opt/janet-v1.36.0-linux/bin/janet /usr/local/bin/janet
15 changes: 3 additions & 12 deletions impls/janet/env.janet
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,8 @@
(put-in env [:data sym]
value))

(defn env-find
[env sym]
(if (get-in env [:data sym])
env
(when-let [outer (get env :outer)]
(env-find outer sym))))

(defn env-get
[env sym]
(if-let [goal-env (env-find env sym)]
(get-in goal-env [:data sym])
(u/throw*
(t/make-string
(string "'" (t/get-value sym) "'" " not found" )))))
(or (get-in env [:data sym])
(if-let [outer (get env :outer)]
(env-get outer sym))))
3 changes: 1 addition & 2 deletions impls/janet/run
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/bash

#!/bin/sh
exec janet $(dirname $0)/${STEP:-stepA_mal}.janet "${@}"
59 changes: 28 additions & 31 deletions impls/janet/step2_eval.janet
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,42 @@

(var EVAL nil)

(defn eval_ast
(defn EVAL
[ast env]
(cond
(t/symbol?* ast)
(if-let [val (env ast)]
val
(error (t/make-string (string "unbound symbol: " (t/get-value ast)))))
#
(t/hash-map?* ast)

# (print (string "EVAL: " (printer/pr_str ast true)))

(case (t/get-type ast)

:symbol
(or (env ast)
(error
(t/make-string
(string "'" (t/get-value ast) "'" " not found" ))))

:hash-map
(t/make-hash-map (struct ;(map |(EVAL $0 env)
(kvs (t/get-value ast)))))
#
(t/list?* ast)
(t/make-list (map |(EVAL $0 env)
(t/get-value ast)))
#
(t/vector?* ast)

:vector
(t/make-vector (map |(EVAL $0 env)
(t/get-value ast)))
#
ast))

(varfn EVAL
[ast env]
(cond
(not (t/list?* ast))
(eval_ast ast env)
#
(t/empty?* ast)
ast
#
(let [eval-list (t/get-value (eval_ast ast env))
f (first eval-list)
args (drop 1 eval-list)]
(apply f args))))
:list
(if (t/empty?* ast)
ast
(let [ast-head (in (t/get-value ast) 0)
f (EVAL ast-head env)
raw-args (drop 1 (t/get-value ast))
args (map |(EVAL $0 env) raw-args)]
(apply f args)))

# Neither a list, map, symbol or vector.
ast))

(defn PRINT
[value]
(printer/pr_str value true))
[ast]
(printer/pr_str ast true))

(defn rep
[code-str]
Expand Down
98 changes: 51 additions & 47 deletions impls/janet/step3_env.janet
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(import ./reader)
(import ./printer)
(import ./types :as t)
(import ./utils :as u)
(import ./env :as e)

(defn READ
Expand All @@ -23,61 +24,64 @@

(var EVAL nil)

(defn eval_ast
(var DEBUG-EVAL (t/make-symbol "DEBUG-EVAL"))

(varfn EVAL
[ast env]
(cond
(t/symbol?* ast)
(e/env-get env ast)
#
(t/hash-map?* ast)

(if-let [dbgeval (e/env-get env DEBUG-EVAL)]
(if (not (or (t/nil?* dbgeval)
(t/false?* dbgeval)))
(print (string "EVAL: " (printer/pr_str ast true)))))

(case (t/get-type ast)

:symbol
(or (e/env-get env ast)
(u/throw*
(t/make-string
(string "'" (t/get-value ast) "'" " not found" ))))

:hash-map
(t/make-hash-map (struct ;(map |(EVAL $0 env)
(kvs (t/get-value ast)))))
#
(t/list?* ast)
(t/make-list (map |(EVAL $0 env)
(t/get-value ast)))
#
(t/vector?* ast)

:vector
(t/make-vector (map |(EVAL $0 env)
(t/get-value ast)))
#
ast))

(varfn EVAL
[ast env]
(cond
(not (t/list?* ast))
(eval_ast ast env)
#
(t/empty?* ast)
ast
#
(let [ast-head (first (t/get-value ast))
head-name (t/get-value ast-head)]
(case head-name
"def!"
(let [def-name (in (t/get-value ast) 1)
def-val (EVAL (in (t/get-value ast) 2) env)]
(e/env-set env
def-name def-val)
def-val)
#
"let*"
(let [new-env (e/make-env env)
bindings (t/get-value (in (t/get-value ast) 1))]
(each [let-name let-val] (partition 2 bindings)
(e/env-set new-env
let-name (EVAL let-val new-env)))
(EVAL (in (t/get-value ast) 2) new-env))
#
(let [eval-list (t/get-value (eval_ast ast env))
f (first eval-list)
args (drop 1 eval-list)]
(apply f args))))))
:list
(if (t/empty?* ast)
ast
(let [ast-head (in (t/get-value ast) 0)
head-name (t/get-value ast-head)]
(case head-name
"def!"
(let [def-name (in (t/get-value ast) 1)
def-val (EVAL (in (t/get-value ast) 2) env)]
(e/env-set env
def-name def-val)
def-val)
##
"let*"
(let [new-env (e/make-env env)
bindings (t/get-value (in (t/get-value ast) 1))]
(each [let-name let-val] (partition 2 bindings)
(e/env-set new-env
let-name (EVAL let-val new-env)))
(EVAL (in (t/get-value ast) 2) new-env))
##
(let [f (EVAL ast-head env)
raw-args (drop 1 (t/get-value ast))
args (map |(EVAL $0 env) raw-args)]
(apply f args)))))

# Neither a list, map, symbol or vector.
ast))

(defn PRINT
[value]
(printer/pr_str value true))
[ast]
(printer/pr_str ast true))

(defn rep
[code-str]
Expand Down
Loading

0 comments on commit aa6f9a8

Please sign in to comment.