GithubHelp home page GithubHelp logo

kanaka / mal Goto Github PK

View Code? Open in Web Editor NEW
10.0K 183.0 2.5K 12.21 MB

mal - Make a Lisp

License: Other

Makefile 6.96% Shell 4.29% C 15.62% Clojure 1.90% CoffeeScript 1.67% C++ 4.72% C# 7.44% Forth 4.50% Go 3.19% Haskell 3.11% Java 20.84% JavaScript 5.40% Julia 1.74% Lua 2.49% MATLAB 3.03% Nim 2.06% OCaml 2.41% Perl 2.47% PHP 2.70% PostScript 3.46%
mal docker lisp lisp-interpreter c java javascript python c-plus-plus c-sharp php ruby r swift bash makefile clojure scala learn-to-code webassembly

mal's Issues

Finish/improve guide (steps 9, A, keyword, vector, hash-map, atom)

The prompt has to be "user>" or "mal-user>", the guide doesn't mention that until you run the test suite and it complains about that.

"distringuishing" is not a word :P

Generally there is a lot of handwaving going on I feel. For example "Add support for the other mal types: keyword, vector, hash-map, and atom." Not even examples for syntax are given! Same with the optional step of implementing quoting for example.

I'm just at end of step 1. Might fill in more in this ticket when I continue,

Move non-TCO step5 tests to implementation specific directories

About half the implementations do not fail the non-TCO test case: some crash, some pass due to the language already having automatic TCO or automatic stack growth. It would still be useful to test the TCO case. The non-TCO case should be moved to implementation specific directories and made specific to that implementation. For the few implementations that crash on stackoverflow, there would be no implementation specific case.

Matlab code doesn't clone properly on case-insensitive file systems

Here's some fascinating collision behavior for you. On case-insensitive file systems, Git repositories that have two files differing only in case don't clone properly. The just-cloned repo thinks it already has modifications, and attempting checkout just toggles which file it believes was modified!

Since we're unlikely to change people's file systems, perhaps someone with more (any) Matlab knowledge would like to rejigger {R,r}eader.m.

src$ git clone https://github.com/kanaka/mal.git
Cloning into 'mal'...
remote: Counting objects: 4239, done.
remote: Total 4239 (delta 0), reused 0 (delta 0), pack-reused 4239
Receiving objects: 100% (4239/4239), 2.14 MiB | 1.76 MiB/s, done.
Resolving deltas: 100% (2994/2994), done.
Checking connectivity... done.
src$ cd mal
mal(master)$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   matlab/Reader.m

no changes added to commit (use "git add" and/or "git commit -a")
mal(master)$ git checkout matlab/Reader.m
mal(master)$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   matlab/reader.m

no changes added to commit (use "git add" and/or "git commit -a")
mal(master)$ git checkout matlab/reader.m
mal(master)$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   matlab/Reader.m

no changes added to commit (use "git add" and/or "git commit -a")

If macro expands to symbol it doesn't go through another EVAL

The behaviour I encounter is a bit strange, but maybe I'm missing something (not sure my issue description is correct).

Here is the problematic scenario:

user> (let* [foo 123] (or false foo))
foo

I expected 123 as the result here.

Or with a simpler macro:

Mal [clojure]
user> (defmacro! nothing (fn* [x] x))
#object[clojure.lang.AFunction$1 0x2a341864 "clojure.lang.AFunction$1@2a341864"]
user> (nothing a)
a

I expected an exception 'a' not found.

Note that if the macro expands to a list, then things seem OK:

user> (let* [foo 123] (or false (+ foo 0)))
123

I see the same behaviour in Clojure, Ruby, Python implementations.

Here is the fix status per implementation:

  • awk
  • bash
  • c
  • cpp
  • coffee
  • cs
  • clojure
  • crystal
  • d
  • elixir
  • erlang
  • es6
  • factor
  • forth
  • fsharp
  • go
  • groovy
  • guile
  • haskell
  • java
  • js
  • julia
  • kotlin
  • lua
  • make
  • mal
  • matlab
  • miniMAL
  • nim
  • ocaml
  • perl
  • php
  • ps
  • python
  • r
  • racket
  • rpython
  • ruby
  • rust
  • scala
  • swift
  • tcl
  • vb
  • vimscript

Mal [c] … user () … Segmentation fault (core dumped)

The empty list causes the C version of Mal to fall over and die a screaming horrible death.

In the Ruby version a NoMethodError: undefined method []' for nil:NilClassexception is raised because inreturn f[*el.drop(1)]at the end ofEVALthe variablefisnil` (the non-existent first element of the list.

Is Mal meant to treat the empty list as both a List and Atom. If so, how?

Typing () into the online common Lisp interpreter http://learnlispthehardway.org/try-lisp/ gives the result NIL

Pretty printing

Hi,

I just wrote some code in MAL and got back a pretty long s-expression. Has anybody looked at pretty printing in MAL? Would it be difficult to write a pprint function in MAL?

Kerry

haskell Type.hs ERROR

Env : opensuse 42.1 ghc 7.10.3
runhaskell step2,...,stepA
Types.hs:64:1:
Non type-variable argument
in the constraint: Control.Monad.Error.Class.MonadError MalError m
(Use FlexibleContexts to permit this)
When checking that ‘throwStr’ has the inferred type
throwStr :: forall (m :: * -> *) a.
Control.Monad.Error.Class.MonadError MalError m =>
String -> m a

Types.hs:65:1:
Non type-variable argument
in the constraint: Control.Monad.Error.Class.MonadError MalError m
(Use FlexibleContexts to permit this)
When checking that ‘throwMalVal’ has the inferred type
throwMalVal :: forall (m :: * -> *) a.
Control.Monad.Error.Class.MonadError MalError m =>
MalVal -> m a

Objective-C with xcodebuild doesn't integrate with top-level Makefile

Root Makefile Diff:

diff --git a/Makefile b/Makefile
index 29ac249..37b370f 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,8 @@ mal_TEST_OPTS = --start-timeout 60 --test-timeout 120

 IMPLS = awk bash c clojure coffee cpp crystal cs erlang elixir es6 factor forth fsharp go groovy \
        guile haskell java julia js kotlin lua make mal ocaml matlab miniMAL nim \
-       perl php ps python r racket rpython ruby rust scala swift vb vimscript
+       perl php ps python r racket rpython ruby rust scala swift vb vimscript \
+       objc

 step0 = step0_repl
 step1 = step1_read_print
@@ -104,6 +105,7 @@ swift_STEP_TO_PROG =   swift/$($(1))
 vb_STEP_TO_PROG =      vb/$($(1)).exe
 vimscript_STEP_TO_PROG = vimscript/$($(1)).vim
 guile_STEP_TO_PROG =   guile/$($(1)).scm
+objc_STEP_TO_PROG =    objc/DerivedData/mal/Build/Products/Debug/$($(1))

 # Needed some argument munging
 COMMA = ,
@@ -155,6 +157,7 @@ vb_RUNSTEP =      mono ../$(2) --raw $(3)
 vimscript_RUNSTEP = ./run_vimscript.sh ../$(2) $(3)
 # needs TERM=dumb to work with readline
 guile_RUNSTEP =   guile --no-auto-compile -L ../guile ../$(2) $(3)
+objc_RUNSTEP =    ../$(2) $(3)


 vimscript_TEST_OPTS = --test-timeout 30

objc/Makefile:

DerivedData/mal/Build/Products/Debug/step0_repl: step0_repl/main.m
    xcodebuild -scheme step0_repl -configuration Debug -derivedDataPath DerivedData/mal

I get the following output from make test^objc^step0

$ make test^objc^step0
/Applications/Xcode7.1.app/Contents/Developer/usr/bin/make -C objc/DerivedData/mal/Build/Products/Debug/ step0_repl
make: *** objc/DerivedData/mal/Build/Products/Debug/: No such file or directory.  Stop.
make: *** [objc/DerivedData/mal/Build/Products/Debug/step0_repl] Error 2

If I built with Xcode (which creates the same binary as make would), I then get the following output from make test^objc^step0

$ make test^objc^step0
/Applications/Xcode7.1.app/Contents/Developer/usr/bin/make -C objc/DerivedData/mal/Build/Products/Debug/ step0_repl
make: *** objc/DerivedData/mal/Build/Products/Debug/: No such file or directory.  Stop.
make: *** [objc/DerivedData/mal/Build/Products/Debug/step0_repl] Error 2
LT-A8-120617:mal heath.borders$ make test^objc^step0
----------------------------------------------
Testing test^objc^step0, step file: objc/DerivedData/mal/Build/Products/Debug/step0_repl, test file: tests/step0_repl.mal
Running: ../runtest.py   ../tests/step0_repl.mal -- ../objc/DerivedData/mal/Build/Products/Debug/step0_repl 
Testing basic string
TEST: abcABC123 -> ['',abcABC123] -> SUCCESS
Testing string containing spaces
TEST: hello mal world -> ['',hello mal world] -> SUCCESS
Testing string containing symbols
TEST: []{}"'* ;:() -> ['',[]{}"'* ;:()] -> SUCCESS
Test long string
TEST: hello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"'* ;:() []{}"'* ;:() []{}"'*) -> ['',hello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"'* ;:() []{}"'* ;:() []{}"'*)] -> SUCCESS
TEST RESULTS (for ../tests/step0_repl.mal):
    0: soft failing tests
    0: failing tests
    4: passing tests
    4: total tests

I'm not blocked because I can build from Xcode and then run the tests from make, but I won't be able to issue a pull request until I can build entirely within the make infrastructure.

gensym

(Following discussion in #103)

Here's a pure mal gensym implementation, and an implementation of the or macro (called or2 below) with gensym:

(def! incr (fn* [x] (+ 1 x)))

(def! *gensym-counter* (atom 0))

(def! gensym
  (fn* []
    (symbol (str "G__" (swap! *gensym-counter* incr)))))

;;
;; test `or` implementation with `gensym` instead of `or_FIXME`
;;
(defmacro! or2
  (fn* (& xs)
    (if (empty? xs)
      nil
      (if (= 1 (count xs))
        (first xs)
        (let* (condvar (gensym))
          `(let* (~condvar ~(first xs))
             (if ~condvar ~condvar (or2 ~@(rest xs)))))))))

(prn (macroexpand (or2 nil "yes")))
(prn (or2 nil "yes"))

(prn (macroexpand (or2 "yes" false)))
(prn (or2 "yes" false))

Output:

(let* (G__1 nil) (if G__1 G__1 (or2 "yes")))
"yes"
(let* (G__3 "yes") (if G__3 G__3 (or2 false)))
"yes"

It relies on atom, so cannot be used in step 8 where we test the or and and macros (atom is added in step A).

Do you prefer a native impl in step 8, with all the quote special operators?

Relevance of conj for self-hosting

guide.md speaks of conj being optional and not required for self-hosting. However the Mal implementation that is run in this step does contain conj in its core.mal. I cannot find any other usage of conj in its sources, so I suspect it's either been added with the intention of making use of it (and making conj mandatory to implement for self-hosting) or in error.

Make test^js failed with a float precision error

TEST: (/ (- (+ 515 (* 222 311)) 300) 41) -> ['','1689'] -> FAIL (line 15):
    Expected : '(/ (- (+ 515 (* 222 311)) 300) 41)\r\n1689'
    Got      : '(/ (- (+ 515 (* 222 311)) 300) 41)\r\n1689.1951219512196'

Pure mal equal implementation

After implementing a few of those hash-equality fixes, I have an idea: how about we implement scalar-equal? with a native implementation, and implement the generic = in mal itself (like we implement not). The mal implementation will use sequential?, hash_map?, contains?, get, first, rest (and scalar-equal?) to recursively check whether the two data structures are equal.

Of course changing it now is a bit late, because = is already implemented natively in all languages (and few still got hash-equality wrong).

Here is the "pure" mal implementation of = (called here new-equal?):

(def! scalar-equal? =)

;; need the `and` macro
(load-file "../core.mal")

(def! sequential-equal?
  (fn* [a b]
    (if (scalar-equal? (count a) (count b))
      (if (scalar-equal? 0 (count a))
        true
        (if (new-equal? (first a) (first b))
          (sequential-equal? (rest a) (rest b))
          false))
    false)))

(def! hash-vals-equal?
  (fn* [a b map-keys]
    (if (scalar-equal? 0 (count map-keys))
      true
      (let* [key (first map-keys)]
        (if (and (contains? a key) (contains? b key))
          (if (new-equal? (get a key) (get b key))
            (hash-vals-equal? a b (rest map-keys))
            false)
          false)))))

(def! hash-map-equal?
  (fn* [a b]
    (if (scalar-equal? (count (keys a)) (count (keys b)))
      (hash-vals-equal? a b (keys a))
      false)))

;; This implements = in pure mal (using only scalar-equal? as native impl)
(def! new-equal?
  (fn* [a b]
    (cond
      (and (sequential? a) (sequential? b)) (sequential-equal? a b)
      (and (map? a) (map? b)) (hash-map-equal? a b)
      true (scalar-equal? a b))))

Implement native hash-map compare across all implementations

This is to track the remaining implementations that need native hash-map equality.

Here is current list of implementations which still need it native hash-map:

  • forth
  • guile
  • kotlin
  • matlab
  • miniMAL
  • ocaml
  • perl
  • r
  • racket
  • vb

@dubek, since we both seem to be working through them, let's claim them when we start working on them so that we don't duplicate work. So to start off, I'll do miniMAL.

Javascript interop: dot form

I noticed the dot form in the online repl docs. I was looking at the source but I'm not sure how it works. Does it work on the web version? Could you provide an example?
Thanks

Mal Fails to Compile on Windows 64 bit, Go 1.4.2

# readline src\readline\readline.go:12:31: fatal error: readline/readline.h: No such file or directory #include <readline/readline.h> ^ compilation terminated. make: *** [step0_repl] Error 2

Similar test failures in multiple languages

$ ./runtest.py  q.mal -- rust/target/release/step1_read_print
TEST: (+ 1 2) -> ['','(+ 1 2)'] -> FAIL (line 2):
    Expected : '(+ 1 2)\r\n(+ 1 2)'
    Got      : '(+ 1 2)\x08\x08\x08\x08\x08\x08\x08\x1b[C\x1b[C\x1b[C\x1b[C\x1b[C\x1b[C\x1b[C\r\n(+ 1 2)'
FAILURES: 1
$ ./runtest.py  q.mal -- python python/step1_read_print.py 
TEST: (+ 1 2) -> ['','(+ 1 2)'] -> FAIL (line 2):
    Expected : '(+ 1 2)\r\n(+ 1 2)'
    Got      : '(+ 1 2)\x08\x08\x08\x08\x08\x08\x08\x1b[C\x1b[C\x1b[C\x1b[C\x1b[C\x1b[C\x1b[C\r\n(+ 1 2)'
FAILURES: 1
$ ./runtest.py  q.mal -- perl perl/step1_read_print.pl 
TEST: (+ 1 2) -> ['','(+ 1 2)'] -> FAIL (line 2):
    Expected : '(+ 1 2)\r\n(+ 1 2)'
    Got      : '(+ 1 2)\x08\x08\x08\x08\x08\x08\x08\x1b[C\x1b[C\x1b[C\x1b[C\x1b[C\x1b[C\x1b[C\r\n(+ 1 2)'
FAILURES: 1

The programs does actually output those bytes:

$ rust/target/release/step1_read_print < q.mal  | hd
00000000  75 73 65 72 3e 20 28 2b  20 31 20 32 29 08 08 08  |user> (+ 1 2)...|
00000010  08 08 08 08 1b 5b 43 1b  5b 43 1b 5b 43 1b 5b 43  |.....[C.[C.[C.[C|
00000020  1b 5b 43 1b 5b 43 1b 5b  43 0a 28 2b 20 31 20 32  |.[C.[C.[C.(+ 1 2|
00000030  29 0a 75 73 65 72 3e 20  3b 3d 3e 28 2b 20 31 20  |).user> ;=>(+ 1 |
00000040  32 29 08 08 08 08 08 08  08 1b 5b 43 1b 5b 43 1b  |2)........[C.[C.|
00000050  5b 43 1b 5b 43 1b 5b 43  1b 5b 43 1b 5b 43 0a 75  |[C.[C.[C.[C.[C.u|
00000060  73 65 72 3e 20                                    |ser> |
00000065

Why does this happen? Is it some interference from readline?

RFC: x64 Implementation Proposal

This is a slightly longer version of the proposal posted on Twitter: https://twitter.com/peterastephens/status/806710260815822849

Originally I was thinking the x64 implementation would need to emit binary opcodes, ELF headers, etc. After taking a look at @dubek's malc LLVM implementation I think a simpler approach can be taken: the compiler can be written in mal and bootstrap itself by emitting x64 assembler source code which can then be assembled using nasm and linked using ld. See this gist for a quick POC I did.

There are some challenges:
1. I'll need to implement a JIT compiler in order to execute (eval ...). Nasm could work here but binary address space manipulation would still be required to patch the running process. A more likely plan would be to create a higher level mal based AST which could be transformed to either assembly source or could be assembled in process using built-in binary support. The bootstrapping process would then be to build the compiler using the existing mal language as well as an existing mal implementation using AOT compilation. The produced binary would then implement a super set of mal capable of manipulating binary data and patching its running process.
2. GC - A garbage collector would be a nice to have but not an initial requirement. A fixed size heap could be used (1GB for instance) and then the process could be terminated when OOM was reached. This would likely pass most of the mal test suite but would likely fail with heavy recursion. Clearly a rudimentary GC would be something to look at down the road and even a stop-the-world single generational GC would be useful and educational.
3. Data structures - the heap could be tuned for the native mal data structures plus a few extras to support JIT compilation. Nice to have would be a native implementation of the persistent data structures as described by Chris Okasaki.
4. Exception handling - this would require keeping detailed metadata on how each function's opcodes were laid out and then to perform stack analysis when an exception was thrown. This will be an area of research.
5. Interop - not quite sure what form this would take. Maybe the ability to make unsafe syscalls? With GC this would require pinning, native pointers, and complexity.

This mal implementation could be written using the 10 steps and could pass the test suite when fully implemented.

Question: should this be built as part of the mal repo proper? Or as a separate repo as with @dubek's malc?

Comments welcome.

example from page 59 of TAPL not working quite as expected?

user> (def! test (fn* [l m n] l m n))

user> (def! tru (fn* [t f] t))

user> (def! fls (fn* [t f] f))

user> (test tru 3 4) should return 3?
user> (test fls 3 4) should return 4?

Instead I seem to get an unevaluated functions, either #<Function: (fn* [t f] t)> or #<Function: (fn* [t f] f)>

Thoughts?

Create a spec

Note: This has been brought up by @Benaiah on IRC.

It's wonderful that MAL has a guide, loads of tests and some example code, however some parts of the guide are less than clear. One example of this is the number type (there is no mention whether you need to support signed numbers, what range they must have, whether they may be floats, etc.), another one would be the specific semantics of hash maps (how is key equality defined, would it be possible to compare the values only instead of having to keep track of the key type?). There is no need to specify everything, simply describing the minimum requirements in great detail and marking the unspecified parts would be enough.

runtest.py needs the ability to test expected failures

I am currently implementing mal in D and I've almost reached the end of step 1.

At the moment the only types I've implemented are symbols, lists, and vectors, but despite this I am passing the "string error" test for step 1.

Actual ./mal Output:

user> [1 2
expected ], got EOF
user> "abc
abc

Observed Test Output:

TEST: (1 2 -> ["expected ')', got EOF\r\n",''] -> SUCCESS
TEST: [1 2 -> ["expected ']', got EOF\r\n",'
'] -> SUCCESS
TEST: "abc -> ['expected '"', got EOF\r\n','*'] -> SUCCESS

This issue is related to #89.

Filename typo in step 3

In step three of the process guide, when renaming the files for implementing step 3, the new file name is printed as step2_env.qx, but it should be step3_env.qx.

Keyword equals is false

Just noticed this in the CSharp version:

> (def! kw :abc)
:abc

> kw
:abc

> (= kw :abc)
false

> (= :abc :abc)
true

Add string manipulation capability

Right now there is no cross-implementation way to manipulate (index, slice, etc).

In Clojure, strings are treated as sequences of characters and so sequence functions operate on them. However, as a simple initial (or maybe final) step, simply adding a seq function that turns a string into a list of single character strings would allow manipulation using normal sequence functions (str will already concatenate a sequence of character strings into a single string).

For now I'm thinking this would be an optional stepA function.

So here is the list for adding a seq function:

  • awk
  • bash
  • c
  • d
  • clojure
  • coffee
  • cpp
  • crystal
  • cs
  • erlang
  • elixir
  • es6
  • factor
  • forth
  • fsharp
  • go
  • groovy
  • guile
  • haskell
  • haxe
  • java
  • julia
  • js
  • kotlin
  • lua
  • make
  • mal
  • ocaml
  • matlab
  • miniMAL
  • nim
  • perl
  • php
  • ps
  • python
  • r
  • racket
  • rpython
  • ruby
  • rust
  • scala
  • swift
  • swift3
  • tcl
  • vb
  • vimscript

Also:

  • Update: process/stepA_mal.txt and process/guide.md

Extending language using the host language

I'm using the javascript interpreter. What would be the best way to extend the interpreter by writing a new function in the host language (without modifying the original interpreter code)?

Fix nested equality tests and then make them mandatory

The following implementations fail the soft tests for nested equality. Sequence nested tests in step4 and hash-map nested steps in step9:

  • bash (hash-map in hash-map)
  • elixir (seq in seq)
  • erlang (seq in seq)
  • factor (seq in seq)
  • forth (no native hash-map equality, see #116)
  • guile
  • haskell (seq in hash-map)
  • julia
  • kotlin (no native hash-map equality, see #116)
  • php (seq and hash-map in hash-map)
  • scala

Once those are all fixed we should make those tests mandatory.

Missing regression testing

During the development of the Tcl implementation I found it important in some steps to verify that I haven't broken something that was working OK earlier. In my case, while implementing TCO in step 5 I broke let* by mistake, but no tests covered it. However, running the tests/step4_if_fn_do.mal test file against my step5_tco.tcl program quickly showed that let* is broken.

I wrote a small regression tester bash script (for Tcl - see code below) that runs all the test cases from previous steps (except step0 and step1) against the given step implementation. For example, if you run the script with step6 it'll concatenate the tests files for step2, step3, step4, step5 and step6 and feed the combined test file into the step6 program.

A better implementation would be in the Makefile with all the info about RUNSTEP and STEP_TO_PROG available there. We can also improve runtest.py to accept several test files and execute them one after the other (reporting total scores of success/failed tests at the end).

regtest.sh

#!/bin/bash

step=$1

if [ -z $step ] ; then
  echo "Usage: $0 step"
  echo "Example: $0 step9"
  exit 1
fi

step0_regs="step0"
step1_regs="step1"
step2_regs="step2"
step3_regs="$step2_regs step3"
step4_regs="$step3_regs step4"
step5_regs="$step4_regs step5"
step6_regs="$step5_regs step6"
step7_regs="$step6_regs step7"
step8_regs="$step7_regs step8"
step9_regs="$step8_regs step9"
stepA_regs="$step9_regs stepA"

projectdir=$(dirname $0)
cd $projectdir/tcl

testfiles=""
eval "regsteps=\$${step}_regs"
for s in $regsteps ; do
  testfile=$(echo ../tests/$s*.mal)
  testfiles="$testfiles $testfile"
done

../runtest.py <(cat $testfiles) -- tclsh ../tcl/${step}_*

Quasiquote within quasiquote appears to be broken

Here's a simplified example in Common Lisp of what I'm trying to do.

* (let ((foo 2)) (eval `(let ((foo 1)) (eval `(print ,foo)))))

1
1
* (let ((foo 2)) (eval `(let ((foo 1)) (eval `(print ,,foo)))))

2
2

The equivalent in mal:

user> (let* [foo 2] (eval `(let* [foo 1] (eval `(prn ~foo)))))
2
nil
user> (let* [foo 2] (eval `(let* [foo 1] (eval `(prn ~~foo)))))
[long traceback omitted]
Exception: 'unquote' not found

The first case gets the wrong value at the wrong time. And the second case just fails altogether. Nested quasiquotes are not unusual in macros, but I can't seem to make them work in mal. Is there a different syntax for this I'm not seeing?

I've tested this in the python implementation, but since the code seems to follow the guide, I have to assume it's broken in the other implementations too.

TCO test not general enough

step5_tco.mal

TCO means 'Tail Call Optimization'. Not 'tail recursion'. TCO means that ALL tail calls are optimized, not only calls to a recursive function.

The test only tests the simple case of a self-recursive tail call. A test for the more general cases of TCO is missing.

; recursive, simple case

(define (foo a)
   (foo ...))  ; <- tail call

; mutually recursive. Will run in an endless loop, will not run out of stack space.

(define (bar a)
   (baz a))  ; <- tail call

(define (baz a)
  (bar a))  ; <- tail call

; general

(define (example-1 a)
  (+    ;  <-  tail call
     (* a a)
     (* a a a)))

; let's say you have a fantastillion functions and each calls the next one in a tail call
; -> will not run out of stack space 

Tests do not work under Windows

I'm trying to run the unit tests for step 0 under Windows, using cygwin. But there seems to be an issue with the line endings. I'm doing what is described in step 0 - I write the prompt, read the input, write the input out and repeat.

Here's the output of the test call:

$ make test^quux^step0
----------------------------------------------
Testing test^quux^step0, step file: quux/step0_repl.exe, test file: tests/step0_repl.mal
Running: ../runtest.py  ../tests/step0_repl.mal -- ../quux/step0_repl.exe
 -> ['','hello world\r'] -> FAIL (line 2):
    Expected : ['hello world\r\r\nhello world\r', 'hello world\r\r\nhello world\r\r\nhello world\r']
    Got      : 'hello world\r\n\r\nhello world\r'
 -> ['','abcABC123\r'] -> FAIL (line 5):
    Expected : ['abcABC123\r\r\nabcABC123\r', 'abcABC123\r\r\nabcABC123\r\r\nabcABC123\r']
    Got      : 'user> abcABC123\r\n\r\nabcABC123\r'
 -> ['',';:() []{}"\'*\r'] -> FAIL (line 8):
    Expected : [';:() []{}"\'*\r\r\n;:() []{}"\'*\r', ';:() []{}"\'*\r\r\n;:() []{}"\'*\r\r\n;:() []{}"\'*\r']
    Got      : 'user> ;:() []{}"\'*\r\n\r\n;:() []{}"\'*\r'
 -> ['','hello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"\'* ;:() []{}"\'* ;:() []{}"\'*)\r'] -> FAIL (line 13):
    Expected : ['hello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"\'* ;:() []{}"\'* ;:() []{}"\'*)\r\r\nhello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"\'* ;:() []{}"\'* ;:() []{}"\'*)\r', 'hello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"\'* ;:() []{}"\'* ;:() []{}"\'*)\r\r\nhello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"\'* ;:() []{}"\'* ;:() []{}"\'*)\r\r\nhello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"\'* ;:() []{}"\'* ;:() []{}"\'*)\r']
    Got      : 'user> hello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"\'* ;:() []{}"\'* ;:() []{}"\'*)\r\n\r\nhello world abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 (;:() []{}"\'* ;:() []{}"\'* ;:() []{}"\'*)\r'
FAILURES: 4
Makefile:156: recipe for target 'test^quux^step0' failed
make: *** [test^quux^step0] Error 2

Got any idea on how to solve this?

Python: String comparsing fails when a unicode string is involved

(Discovered while working on #134)

Here it is with some debug prints I added (running python 2.6.6):

REPL implementation python, step file: python/step4_if_fn_do.py
Running: python ../python/step4_if_fn_do.py 
user> (= (str :abc) ":abc")
('DEBUG in _equal_Q: a=', u':abc', 'type(a)=', <type 'unicode'>)
('DEBUG in _equal_Q: b=', ':abc', 'type(b)=', <type 'str'>)
false

Somehow the str call creates a unicode string (probably due to the special char which is used to mark mal keywords). This fails in _equal_Q because the types are not identical.

Not sure what's the appropriate way in Python to convert str to unicode. Also I thought that unicode is only in Python 3 but it seems that it occurs here (Python 2.6) too.

Trying to write filter function

Hi,

I'm trying to write a filter function. I've tried a couple attempts and I am getting some odd results. Would appreciate some feedback as to what I am doing wrong.

(def! zero? (fn* (n) (= 0 n)))

(def! car first)

(def! cdr rest)

(def! filter
  (fn* (pred xs)
    (if (> (count xs) 0)
      (if (pred (car xs)) 
        (cons (car xs) (filter pred (cdr xs)))
        (filter pred (cdr xs)) 
      '() 
    ))))

And then in the js repl I'm getting:

> (filter zero? '(0 1 2 3))
(0 nil)

Thanks

Javascript interop

I was wondering what options are available for javascript (browser) interop in mal. Could you provide some examples of function execution and object property access?

Thanks!

Threading macro question

I was trying out the -> operator that is in core.mal.

How would you use functions like map or filter with ->? Leaving off the argument doesn't automatically create a partially applied function and return another function. I could create an anonymous function or create a new function. Is there a more general way to do partial function application on map or filter so that I can use it in ->?

Thanks,
Kerry

Why a mal function needs the function in step 5 (TCO)?

It says:

The return value from the fn* special form will now become an object/structure
with attributes that allow the default invoke case of EVAL to do TCO
on mal functions. Those attributes are:

fn: the original function value return in step 4
...

But I don't see where that original function is used, anywhere.

Is that a mistake or is there something I'm overlooking?

TCO test in Guile-2.1+ implementation

I've encountered a happy trouble when I'm writing step5 in GNU Guile.
I think most of the languages will crash when calling (sum-to 10000) in non-tail-call way. But in Guile-2.1+, it's not true. It's because Guile-2.1+ has auto-grow-stack feature. So if the stack is out, there'll be new allocated stack, depends on your physical memory.
So I can get 50005000 after calling (sum-to 10000), which means rest1 will be set to this number rather than keep nil.
Some implementation, like C could undertake (sum-to 10000), but crash on (sum-to 100000).
But in Guile-2.1+, even (sum-to 1000000) is fine.

Now, how should we fix this properly...?

Not able to run tests.

Just cloned the mal repo and I'm having problems running the tests. I use make test^ruby^step0 to run the ruby implementation, but it's the bash implementation that gets run, and it even breaks, same for other languages I tried.

Build Rust in release mode

cargo build --release

This enables compiler optimizations and will likely give it a big boost without touching the code.

Node / Js / es6 step 0

I've noticed the node-gyp rebuild fails when I npm --install in the js or es6 implementations.
Found out why here had to bump ffi to 2.2.0

That is not however the issue per se. My issue is that the tests fail (I would assume unless I use the ffi readline)

I was trying to rework it myself from scratch and started on my own implementation.

Currently I'm using the following for step 0

const readline = require('readline')
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
rl.setPrompt('user> ')

function READ(inp) {return inp}
function EVAL(inp) {return inp}
function PRINT(inp) {return inp}

function rep(inp) {
    return PRINT(EVAL(READ(inp)))
}


rl.on('line', (inp) => {
    process.stdout.write(`${inp}\n`)
    rl.prompt()
})
rl.on('close',()=>{
    process.exit()
})

rl.prompt()

And all tests fail like this:

FAILED TEST (line 3): abcABC123 -> ['',abcABC123]:
    Expected : 'abcABC123\r\nabcABC123'
    Got      : None

however when i run this myself and enter input etc it works as expected. What is it that the python test is doing which prevents this from working?

Regexp problem with unterminated strings

[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}('"`,;)]*)

The regular expression provided in the guide is really impressive, however, when the input string is "123, it will filter out the first double quote. So how can we detect the unmatched " then?

I found several implementations, which is using regular expressions to do lexical analysis, have this problem, including C, Go and OCaml.

Mal [c]
user> "123
123

Useful functions

I was wondering what useful functions people have written (in mal) to supplement the base mal language. I noticed some functions and macros in core.mal with some useful functions. I like the clojure-style threading macros. Very clever and shows what a capable language mal is. I didn't see filter in the git repository so I wrote my own the other day and was going to write 'range' next. Are there any other sources of supplemental functions/macros written in the core mal language that I am missing? Could anybody share what useful functions they have written? I'd be very interested in seeing what clever things people have written, what functions they have implemented, and how they implemented them.

@kanaka Joel, if it doesn't exist already, what do you think about a place for user contributed functions?

-Kerry

Question: Writing some macros for familiar lisp expressions

Hi,

I'd like to define some familiar lisp expressions in MAL. Lisp style defun, defmacro and creating aliases of functions/macros.

How would you write macros for lisp style defun and defmacro?

Also, I was wondering how you would write defalias in MAL.

Thanks,
Kerry

Testing commands fail in zsh

Love this project, finding it super pedagogical!

The guide mentions the shell command

make test^quux^stepX

to run tests.

This works fine in bash. It does not work in zsh, however. After an entire night and morning of thinking it was something wrong in my implementation code that prevented me from running the tests, I found out that zsh treats unquoted carets as globbing characters.

Perhaps you should change the guide to say

make "test^quux^stepX"

for sake of universality?

Or make a note that the args should be quoted, for zsh users?
Or make a note that the command should be prefixed with noglob, i.e.
noglob make test^quux^stepX, for zsh users?

.gitignore */mal?

Is there a reason you .gitignore the */mal path?

It makes it hard to add files under factor/mal without using git add -f...

Fix string escaping/unescaping in Erlang implementation

@nlfiedler I recently added some tests that do better coverage of string escaping and unescaping. In particular, this allows ANSI escape codes to be used in output to support. For example, being able to run the mal/clojurewest2014.mal presentation correctly.

I was able to fix the escaping/unescaping for all the other languages, but I'm not familiar enough with Erlang to be able to fix it easily. I think the escaping in printer.erl is okay, but the unescaping in reader.erl is missing the "\n" case. At least, that appears to be the issue. Do you think you could take a look at it?

You can reproduce the failures with:

make test^erlang^step4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.