diff options
| author | Joel Martin <github@martintribe.org> | 2014-10-27 23:49:55 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-01-06 21:59:01 -0600 |
| commit | fb5c165838a658c79c9772c264a0a1673bc5081b (patch) | |
| tree | 648af704e93cad3369401d871a0e5ae0169f2860 | |
| parent | b554fd4ed035396886c673778d60659085d8b286 (diff) | |
| download | mal-fb5c165838a658c79c9772c264a0a1673bc5081b.tar.gz mal-fb5c165838a658c79c9772c264a0a1673bc5081b.zip | |
go: add time-ms. Ruby: fix step9,A content.
| -rw-r--r-- | go/Makefile | 5 | ||||
| -rw-r--r-- | go/src/core/core.go | 9 | ||||
| -rw-r--r-- | go/src/reader/reader.go | 4 | ||||
| -rw-r--r-- | ruby/step9_try.rb | 2 | ||||
| -rw-r--r-- | ruby/stepA_interop.rb | 17 |
5 files changed, 30 insertions, 7 deletions
diff --git a/go/Makefile b/go/Makefile index 4b32a0b..5c966f5 100644 --- a/go/Makefile +++ b/go/Makefile @@ -5,8 +5,9 @@ export GOPATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) SOURCES_BASE = src/types/types.go src/readline/readline.go \ src/reader/reader.go src/printer/printer.go \ src/env/env.go src/core/core.go -SOURCES_LISP = src/stepA_interop/stepA_interop.go -SOURCES = $(SOURCES_BASE) $(SOURCES_LISP) +SOURCES_LISP = src/env/env.go src/core/core.go \ + src/stepA_interop/stepA_interop.go +SOURCES = $(SOURCES_BASE) $(word $(words $(SOURCES_LISP)),${SOURCES_LISP}) ##################### diff --git a/go/src/core/core.go b/go/src/core/core.go index 57cf932..17e92f0 100644 --- a/go/src/core/core.go +++ b/go/src/core/core.go @@ -4,6 +4,7 @@ import ( "errors" "io/ioutil" "fmt" + "time" ) import ( @@ -45,6 +46,11 @@ func slurp(a []MalType) (MalType, error) { return string(b), nil } +// Number functions +func time_ms(a []MalType) (MalType, error) { + return int(time.Now().UnixNano() / int64(time.Millisecond)), nil +} + // Hash Map functions func copy_hash_map(hm HashMap) (HashMap) { @@ -87,7 +93,7 @@ func get(a []MalType) (MalType, error) { } func contains_Q(hm MalType, key MalType) (MalType, error) { - if Nil_Q(hm) { return nil, nil } + if Nil_Q(hm) { return false, nil } if !HashMap_Q(hm) { return nil, errors.New("get called on non-hash map") } if !String_Q(key) { return nil, errors.New("get called with non-string key") } _, ok := hm.(HashMap).Val[key.(string)] @@ -317,6 +323,7 @@ var NS = map[string]MalType{ return a[0].(int) * a[1].(int), nil }, "/": func(a []MalType) (MalType, error) { return a[0].(int) / a[1].(int), nil }, + "time-ms": time_ms, "list": func(a []MalType) (MalType, error) { return List{a,nil}, nil }, diff --git a/go/src/reader/reader.go b/go/src/reader/reader.go index 38f163c..6fbe311 100644 --- a/go/src/reader/reader.go +++ b/go/src/reader/reader.go @@ -79,11 +79,11 @@ func read_atom(rdr Reader) (MalType, error) { func read_list(rdr Reader, start string, end string) (MalType, error) { token := rdr.next() if token == nil { return nil, errors.New("read_list underflow") } - - ast_list := []MalType{} if *token != start { return nil, errors.New("expected '" + start + "'") } + + ast_list := []MalType{} token = rdr.peek() for ; true ; token = rdr.peek() { if token == nil { return nil, errors.New("exepected '" + end + "', got EOF") } diff --git a/ruby/step9_try.rb b/ruby/step9_try.rb index 6123293..eecba9c 100644 --- a/ruby/step9_try.rb +++ b/ruby/step9_try.rb @@ -95,8 +95,6 @@ def EVAL(ast, env) return env.set(a1, func) when :macroexpand return macroexpand(a1, env) - when :"rb*" - return eval(a1) when :"try*" begin return EVAL(a1, env) diff --git a/ruby/stepA_interop.rb b/ruby/stepA_interop.rb index 6d2cbe2..6123293 100644 --- a/ruby/stepA_interop.rb +++ b/ruby/stepA_interop.rb @@ -97,6 +97,21 @@ def EVAL(ast, env) return macroexpand(a1, env) when :"rb*" return eval(a1) + when :"try*" + begin + return EVAL(a1, env) + rescue Exception => exc + if exc.is_a? MalException + exc = exc.data + else + exc = exc.message + end + if a2 && a2[0] == :"catch*" + return EVAL(a2[2], Env.new(env, [a2[1]], [exc])) + else + raise esc + end + end when :do eval_ast(ast[1..-2], env) ast = ast.last # Continue loop (TCO) @@ -142,6 +157,7 @@ repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || [])) # core.mal: defined using the language itself +RE["(def! *host-language* \"ruby\")"] RE["(def! not (fn* (a) (if a false true)))"] RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"] RE["(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"] @@ -153,6 +169,7 @@ if ARGV.size > 0 end # repl loop +RE["(println (str \"Mal [\" *host-language* \"]\"))"] while line = _readline("user> ") begin puts REP[line] |
